33--- @field links OrgLinks
44--- @field private sources OrgCompletionSource[]
55--- @field private sources_by_name table<string , OrgCompletionSource>
6+ --- @field private fuzzy_match ? boolean does completeopt has fuzzy option
67--- @field menu string
78local OrgCompletion = {
89 menu = ' [Org]' ,
@@ -16,6 +17,7 @@ function OrgCompletion:new(opts)
1617 links = opts .links ,
1718 sources = {},
1819 sources_by_name = {},
20+ fuzzy_match = vim .tbl_contains (vim .opt_local .completeopt :get (), ' fuzzy' ),
1921 }, OrgCompletion )
2022 this :setup_builtin_sources ()
2123 this :register_frameworks ()
@@ -46,16 +48,7 @@ function OrgCompletion:complete(context)
4648 local results = {}
4749 context .base = context .base or ' '
4850 if not context .matcher then
49- context .matcher = function (value , pattern )
50- pattern = pattern or ' '
51- if pattern == ' ' then
52- return true
53- end
54- if context .fuzzy then
55- return # vim .fn .matchfuzzy ({ value }, pattern ) > 0
56- end
57- return value :find (' ^' .. vim .pesc (pattern )) ~= nil
58- end
51+ context .matcher = self :_build_matcher (context )
5952 end
6053 for _ , source in ipairs (self .sources ) do
6154 if source :get_start (context ) then
@@ -103,12 +96,26 @@ function OrgCompletion:omnifunc(findstart, base)
10396
10497 self ._context = self ._context or { line = self :get_line () }
10598 self ._context .base = base
106- if vim .tbl_contains (vim .opt_local .completeopt :get (), ' fuzzy' ) then
107- self ._context .fuzzy = true
108- end
99+ self ._context .fuzzy = self .fuzzy_match
109100 return self :complete (self ._context )
110101end
111102
103+ --- @private
104+ --- @param context OrgCompletionContext
105+ --- @return fun ( value : string , pattern : string ): boolean
106+ function OrgCompletion :_build_matcher (context )
107+ return function (value , pattern )
108+ pattern = pattern or ' '
109+ if pattern == ' ' then
110+ return true
111+ end
112+ if context .fuzzy then
113+ return # vim .fn .matchfuzzy ({ value }, pattern ) > 0
114+ end
115+ return value :find (' ^' .. vim .pesc (pattern )) ~= nil
116+ end
117+ end
118+
112119function OrgCompletion :get_line ()
113120 local cursor = vim .api .nvim_win_get_cursor (0 )
114121 return vim .api .nvim_get_current_line ():sub (1 , cursor [2 ])
@@ -123,4 +130,16 @@ function OrgCompletion:register_frameworks()
123130 require (' orgmode.org.autocompletion.cmp' )
124131end
125132
133+ --- @param arg_lead string
134+ --- @return string[]
135+ function OrgCompletion :complete_links_from_input (arg_lead )
136+ local context = {
137+ base = arg_lead ,
138+ fuzzy = self .fuzzy_match ,
139+ }
140+ context .matcher = self :_build_matcher (context )
141+
142+ return self .links :autocomplete (context )
143+ end
144+
126145return OrgCompletion
0 commit comments