diff --git a/lua/cmp/matcher.lua b/lua/cmp/matcher.lua index a49673d..5ac00bc 100644 --- a/lua/cmp/matcher.lua +++ b/lua/cmp/matcher.lua @@ -128,17 +128,12 @@ matcher.match = function(input, word, option) end if #matches == 0 then - matches = { - { - index = 1, - input_match_start = 1, - input_match_end = 1, - word_match_start = 1, - word_match_end = 1, - strict_ratio = 0, - fuzzy = false, - } - } + if not option.disallow_fuzzy_matching and not option.disallow_prefix_unmatching and not option.disallow_partial_fuzzy_matching then + if matcher.fuzzy(input, word, matches) then + return 1, matches + end + end + return 0, {} end matcher.debug(word, matches) @@ -190,7 +185,7 @@ matcher.match = function(input, word, option) if matches[#matches].input_match_end < #input then if not option.disallow_fuzzy_matching then if not option.disallow_partial_fuzzy_matching or prefix then - if matcher.fuzzy(input, word, matches) then + if matcher.fuzzy(input, word, matches) then return score, matches end end @@ -203,10 +198,9 @@ end --- fuzzy matcher.fuzzy = function(input, word, matches) - local last_match = matches[#matches] + local input_index = matches[1] and (matches[1].input_match_end + 1) or 1 -- Lately specified middle of text. - local input_index = last_match.input_match_end + 1 for i = 1, #matches - 1 do local curr_match = matches[i] local next_match = matches[i + 1] @@ -227,7 +221,7 @@ matcher.fuzzy = function(input, word, matches) local last_input_index = input_index local matched = false local word_offset = 0 - local word_index = last_match.word_match_end + 1 + local word_index = matches[1] and (matches[1].word_match_end + 1) or 1 local input_match_start = -1 local input_match_end = -1 local word_match_start = -1 diff --git a/lua/cmp/matcher_spec.lua b/lua/cmp/matcher_spec.lua index 9105939..b3a8628 100644 --- a/lua/cmp/matcher_spec.lua +++ b/lua/cmp/matcher_spec.lua @@ -37,12 +37,23 @@ describe('matcher', function() assert.is.truthy(matcher.match('Unit', 'net.UnixListener', { disallow_partial_fuzzy_matching = true }) == 0) assert.is.truthy(matcher.match('Unit', 'net.UnixListener', { disallow_partial_fuzzy_matching = false }) >= 1) - assert.is.truthy(matcher.match('tail', 'HCDetails', { + local score, matches + score, matches = matcher.match('tail', 'HCDetails', { disallow_fuzzy_matching = false, disallow_partial_matching = false, disallow_prefix_unmatching = false, disallow_partial_fuzzy_matching = false, - }) >= 1) + }) + assert.is.truthy(score >= 1) + assert.equals(matches[1].word_match_start, 5) + + score, matches = matcher.match('tail', 'HCDetails', { + disallow_fuzzy_matching = false, + disallow_partial_matching = false, + disallow_prefix_unmatching = false, + disallow_partial_fuzzy_matching = true, + }) + assert.is.truthy(score == 0) end) it('disallow_fuzzy_matching', function()