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

@@ -451,7 +451,7 @@ confirmation.get_commit_characters~
You can append or exclude commitCharacters via this configuration option
function. The commitCharacters are defined by the LSP spec.
*cmp-config.formatting.expandable_indicator*
*cmp-config.formatting.expandable_indicator*
formatting.expandable_indicator~
`cmp.expandable_indicator`
Boolean to show the `~` expandable indicator in cmp's floating window.
@@ -474,6 +474,10 @@ matching.disallow_fuzzy_matching~
`boolean`
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*
matching.disallow_partial_matching~
`boolean`

View File

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

View File

@@ -365,11 +365,13 @@ entry.match = function(self, input, matching_config)
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',
}, function()
local option = {
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_prefix_unmatching = matching_config.disallow_prefix_unmatching,
synonyms = {

View File

@@ -78,7 +78,7 @@ end
---Match entry
---@param input 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
matcher.match = function(input, word, option)
option = option or {}
@@ -179,8 +179,10 @@ matcher.match = function(input, word, option)
-- Check remaining input as fuzzy
if matches[#matches].input_match_end < #input then
if not option.disallow_fuzzy_matching then
if prefix and matcher.fuzzy(input, word, matches) then
return score, matches
if not option.disallow_partial_fuzzy_matching or prefix then
if matcher.fuzzy(input, word, matches) then
return score, matches
end
end
end
return 0, {}

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('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)
it('disallow_fuzzy_matching', function()

View File

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