3
3
--- @field links OrgLinks
4
4
--- @field private sources OrgCompletionSource[]
5
5
--- @field private sources_by_name table<string , OrgCompletionSource>
6
+ --- @field private fuzzy_match ? boolean does completeopt has fuzzy option
6
7
--- @field menu string
7
8
local OrgCompletion = {
8
9
menu = ' [Org]' ,
@@ -16,6 +17,7 @@ function OrgCompletion:new(opts)
16
17
links = opts .links ,
17
18
sources = {},
18
19
sources_by_name = {},
20
+ fuzzy_match = vim .tbl_contains (vim .opt_local .completeopt :get (), ' fuzzy' ),
19
21
}, OrgCompletion )
20
22
this :setup_builtin_sources ()
21
23
this :register_frameworks ()
@@ -46,16 +48,7 @@ function OrgCompletion:complete(context)
46
48
local results = {}
47
49
context .base = context .base or ' '
48
50
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 )
59
52
end
60
53
for _ , source in ipairs (self .sources ) do
61
54
if source :get_start (context ) then
@@ -103,12 +96,26 @@ function OrgCompletion:omnifunc(findstart, base)
103
96
104
97
self ._context = self ._context or { line = self :get_line () }
105
98
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
109
100
return self :complete (self ._context )
110
101
end
111
102
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
+
112
119
function OrgCompletion :get_line ()
113
120
local cursor = vim .api .nvim_win_get_cursor (0 )
114
121
return vim .api .nvim_get_current_line ():sub (1 , cursor [2 ])
@@ -123,4 +130,16 @@ function OrgCompletion:register_frameworks()
123
130
require (' orgmode.org.autocompletion.cmp' )
124
131
end
125
132
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
+
126
145
return OrgCompletion
0 commit comments