Skip to content

Commit 7fcfde7

Browse files
committed
Use keyword_pattern for is_symbol check
1 parent d818fd0 commit 7fcfde7

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

lua/cmp/entry.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ end
365365
---@param input string
366366
---@param matching_config cmp.MatchingConfig
367367
---@return { score: integer, matches: table[] }
368-
entry.match = function(self, input, matching_config)
368+
entry.match = function(self, input, matching_config, keyword_pattern)
369369
return self.match_cache:ensure(input .. ':' .. (self.resolved_completion_item and '1' or '0' .. ':') .. (matching_config.disallow_fuzzy_matching and '1' or '0') .. ':' .. (matching_config.disallow_partial_fuzzy_matching and '1' or '0') .. ':' .. (matching_config.disallow_partial_matching and '1' or '0') .. ':' .. (matching_config.disallow_prefix_unmatching and '1' or '0') .. ':' .. (matching_config.disallow_symbol_nonprefix_matching and '1' or '0'), function()
370370
local option = {
371371
disallow_fuzzy_matching = matching_config.disallow_fuzzy_matching,
@@ -377,6 +377,7 @@ entry.match = function(self, input, matching_config)
377377
self:get_word(),
378378
self:get_completion_item().label,
379379
},
380+
keyword_pattern = keyword_pattern,
380381
}
381382

382383
local score, matches, filter_text, _

lua/cmp/matcher.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local char = require('cmp.utils.char')
2+
local pattern = require('cmp.utils.pattern')
23

34
local matcher = {}
45

@@ -81,7 +82,7 @@ end
8182
---Match entry
8283
---@param input string
8384
---@param word string
84-
---@param option { synonyms: string[], disallow_fullfuzzy_matching: boolean, disallow_fuzzy_matching: boolean, disallow_partial_fuzzy_matching: boolean, disallow_partial_matching: boolean, disallow_prefix_unmatching: boolean, disallow_symbol_nonprefix_matching: boolean }
85+
---@param option { synonyms: string[], disallow_fullfuzzy_matching: boolean, disallow_fuzzy_matching: boolean, disallow_partial_fuzzy_matching: boolean, disallow_partial_matching: boolean, disallow_prefix_unmatching: boolean, disallow_symbol_nonprefix_matching: boolean, keyword_pattern: string|nil }
8586
---@return integer, table
8687
matcher.match = function(input, word, option)
8788
option = option or {}
@@ -103,6 +104,15 @@ matcher.match = function(input, word, option)
103104
end
104105
end
105106

107+
local is_symbol
108+
if option.keyword_pattern ~= nil then
109+
is_symbol = function(byte)
110+
return pattern.matchstr(option.keyword_pattern, string.char(byte)) == nil
111+
end
112+
else
113+
is_symbol = char.is_symbol
114+
end
115+
106116
-- Gather matched regions
107117
local matches = {}
108118
local input_start_index = 1
@@ -111,7 +121,7 @@ matcher.match = function(input, word, option)
111121
local word_bound_index = 1
112122
local no_symbol_match = false
113123
while input_end_index <= #input and word_index <= #word do
114-
local m = matcher.find_match_region(input, input_start_index, input_end_index, word, word_index)
124+
local m = matcher.find_match_region(input, input_start_index, input_end_index, word, word_index, is_symbol)
115125
if m and input_end_index <= m.input_match_end then
116126
m.index = word_bound_index
117127
input_start_index = m.input_match_start + 1
@@ -277,7 +287,7 @@ matcher.fuzzy = function(input, word, matches, option)
277287
end
278288

279289
--- find_match_region
280-
matcher.find_match_region = function(input, input_start_index, input_end_index, word, word_index)
290+
matcher.find_match_region = function(input, input_start_index, input_end_index, word, word_index, is_symbol)
281291
-- determine input position ( woroff -> word_offset )
282292
while input_start_index < input_end_index do
283293
if char.match(string.byte(input, input_end_index), string.byte(word, word_index)) then
@@ -309,7 +319,7 @@ matcher.find_match_region = function(input, input_start_index, input_end_index,
309319
strict_count = strict_count + (c1 == c2 and 1 or 0)
310320
match_count = match_count + 1
311321
word_offset = word_offset + 1
312-
no_symbol_match = no_symbol_match or char.is_symbol(c1)
322+
no_symbol_match = no_symbol_match or is_symbol(c1)
313323
else
314324
-- Match end (partial region)
315325
if input_match_start ~= -1 then

lua/cmp/matcher_spec.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ describe('matcher', function()
2222
assert.is.truthy(matcher.match('fmodify', 'fnamemodify', config.matching) >= 1)
2323
assert.is.truthy(matcher.match('candlesingle', 'candle#accept#single', config.matching) >= 1)
2424

25+
local options = {
26+
keyword_pattern = [[ \w\+ ]],
27+
}
28+
assert.is.truthy(matcher.match('ab', 'a_b_c', options) > matcher.match('ac', 'a_b_c', options))
29+
assert.is.truthy(matcher.match('a_b', 'a_b_c', options) > matcher.match('ab', 'a_b_c', options))
30+
assert.is.truthy(matcher.match('a_b/c', 'a_b/c', options) > matcher.match('a/c', 'a_b/c', options))
31+
32+
assert.is.truthy(matcher.match('bora', 'border-radius') >= 1)
33+
assert.is.truthy(matcher.match('woroff', 'word_offset') >= 1)
34+
assert.is.truthy(matcher.match('call', 'call') > matcher.match('call', 'condition_all'))
35+
assert.is.truthy(matcher.match('Buffer', 'Buffer') > matcher.match('Buffer', 'buffer'))
36+
assert.is.truthy(matcher.match('luacon', 'lua_context') > matcher.match('luacon', 'LuaContext'))
37+
assert.is.truthy(matcher.match('fmodify', 'fnamemodify') >= 1)
38+
assert.is.truthy(matcher.match('candlesingle', 'candle#accept#single') >= 1)
39+
2540
assert.is.truthy(matcher.match('vi', 'void#', config.matching) >= 1)
2641
assert.is.truthy(matcher.match('vo', 'void#', config.matching) >= 1)
2742
assert.is.truthy(matcher.match('var_', 'var_dump', config.matching) >= 1)

lua/cmp/source.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ source.get_entries = function(self, ctx)
113113
inputs[o] = string.sub(ctx.cursor_before_line, o)
114114
end
115115

116-
local match = e:match(inputs[o], matching_config)
116+
local match = e:match(inputs[o], matching_config, self:get_keyword_pattern())
117117
e.score = match.score
118118
e.exact = false
119119
if e.score >= 1 then

0 commit comments

Comments
 (0)