Implement middle fuzzy matching

Fixes #1422
This commit is contained in:
hrsh7th
2023-02-11 22:52:28 +09:00
parent 53f49c5145
commit aae0c3e4e7
6 changed files with 17 additions and 4 deletions

View File

@@ -474,6 +474,10 @@ matching.disallow_fuzzy_matching~
`boolean` `boolean`
Whether to allow fuzzy matching. Whether to allow fuzzy matching.
*cmp-config.matching.disallow_partial_fuzzy_matching*
matching.disallow_partial_fuzzy_matching~
`boolean`
Whether to allow fuzzy matching without prefix matching.
*cmp-config.matching.disallow_partial_matching* *cmp-config.matching.disallow_partial_matching*
matching.disallow_partial_matching~ matching.disallow_partial_matching~
`boolean` `boolean`

View File

@@ -50,6 +50,7 @@ return function()
matching = { matching = {
disallow_fuzzy_matching = false, disallow_fuzzy_matching = false,
disallow_partial_fuzzy_matching = true,
disallow_partial_matching = false, disallow_partial_matching = false,
disallow_prefix_unmatching = false, disallow_prefix_unmatching = false,
}, },

View File

@@ -365,11 +365,13 @@ entry.match = function(self, input, matching_config)
input, input,
self.resolved_completion_item and '1' or '0', self.resolved_completion_item and '1' or '0',
matching_config.disallow_fuzzy_matching 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_partial_matching and '1' or '0',
matching_config.disallow_prefix_unmatching and '1' or '0', matching_config.disallow_prefix_unmatching and '1' or '0',
}, function() }, function()
local option = { local option = {
disallow_fuzzy_matching = matching_config.disallow_fuzzy_matching, disallow_fuzzy_matching = matching_config.disallow_fuzzy_matching,
disallow_partial_fuzzy_matching = matching_config.disallow_partial_fuzzy_matching,
disallow_partial_matching = matching_config.disallow_partial_matching, disallow_partial_matching = matching_config.disallow_partial_matching,
disallow_prefix_unmatching = matching_config.disallow_prefix_unmatching, disallow_prefix_unmatching = matching_config.disallow_prefix_unmatching,
synonyms = { synonyms = {

View File

@@ -78,7 +78,7 @@ end
---Match entry ---Match entry
---@param input string ---@param input string
---@param word string ---@param word string
---@param option { synonyms: string[], disallow_fuzzy_matching: boolean, disallow_partial_matching: boolean, disallow_prefix_unmatching: boolean } ---@param option { synonyms: string[], disallow_fuzzy_matching: boolean, disallow_partial_fuzzy_matching: boolean, disallow_partial_matching: boolean, disallow_prefix_unmatching: boolean }
---@return integer ---@return integer
matcher.match = function(input, word, option) matcher.match = function(input, word, option)
option = option or {} option = option or {}
@@ -179,10 +179,12 @@ matcher.match = function(input, word, option)
-- Check remaining input as fuzzy -- Check remaining input as fuzzy
if matches[#matches].input_match_end < #input then if matches[#matches].input_match_end < #input then
if not option.disallow_fuzzy_matching then if not option.disallow_fuzzy_matching then
if prefix and matcher.fuzzy(input, word, matches) then if not option.disallow_partial_fuzzy_matching or prefix then
if matcher.fuzzy(input, word, matches) then
return score, matches return score, matches
end end
end end
end
return 0, {} return 0, {}
end end

View File

@@ -33,6 +33,9 @@ describe('matcher', function()
assert.is.truthy(matcher.match('true', 'v:true', { synonyms = { 'true' } }) == matcher.match('true', 'true')) assert.is.truthy(matcher.match('true', 'v:true', { synonyms = { 'true' } }) == matcher.match('true', 'true'))
assert.is.truthy(matcher.match('g', 'get', { synonyms = { 'get' } }) > matcher.match('g', 'dein#get', { 'dein#get' })) assert.is.truthy(matcher.match('g', 'get', { synonyms = { 'get' } }) > matcher.match('g', 'dein#get', { 'dein#get' }))
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)
end) end)
it('disallow_fuzzy_matching', function() it('disallow_fuzzy_matching', function()

View File

@@ -125,6 +125,7 @@ cmp.ItemField = {
---@class cmp.MatchingConfig ---@class cmp.MatchingConfig
---@field public disallow_fuzzy_matching boolean ---@field public disallow_fuzzy_matching boolean
---@field public disallow_partial_fuzzy_matching boolean
---@field public disallow_partial_matching boolean ---@field public disallow_partial_matching boolean
---@field public disallow_prefix_unmatching boolean ---@field public disallow_prefix_unmatching boolean