From aae0c3e4e778ca4be6fabc52e388cbd5b844b7a5 Mon Sep 17 00:00:00 2001 From: hrsh7th <629908+hrsh7th@users.noreply.github.com> Date: Sat, 11 Feb 2023 22:52:28 +0900 Subject: [PATCH] Implement middle fuzzy matching Fixes #1422 --- doc/cmp.txt | 6 +++++- lua/cmp/config/default.lua | 1 + lua/cmp/entry.lua | 2 ++ lua/cmp/matcher.lua | 8 +++++--- lua/cmp/matcher_spec.lua | 3 +++ lua/cmp/types/cmp.lua | 1 + 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/doc/cmp.txt b/doc/cmp.txt index ef78c27..aa974e8 100644 --- a/doc/cmp.txt +++ b/doc/cmp.txt @@ -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` diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index ccac643..fc488e5 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -50,6 +50,7 @@ return function() matching = { disallow_fuzzy_matching = false, + disallow_partial_fuzzy_matching = true, disallow_partial_matching = false, disallow_prefix_unmatching = false, }, diff --git a/lua/cmp/entry.lua b/lua/cmp/entry.lua index a71d2ca..45b3618 100644 --- a/lua/cmp/entry.lua +++ b/lua/cmp/entry.lua @@ -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 = { diff --git a/lua/cmp/matcher.lua b/lua/cmp/matcher.lua index 8a3396e..96f9c84 100644 --- a/lua/cmp/matcher.lua +++ b/lua/cmp/matcher.lua @@ -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, {} diff --git a/lua/cmp/matcher_spec.lua b/lua/cmp/matcher_spec.lua index c95dfc3..ad24003 100644 --- a/lua/cmp/matcher_spec.lua +++ b/lua/cmp/matcher_spec.lua @@ -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() diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index 5ff3bb6..8826746 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -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