diff --git a/README.md b/README.md index 8185a293..f29bcce3 100644 --- a/README.md +++ b/README.md @@ -554,7 +554,7 @@ MiniDeps.add({ max_items = 200, -- controls which sorts to use and in which order, falling back to the next sort if the first one returns nil -- you may pass a function instead of a string to customize the sorting - sorts = { 'score', 'kind', 'label' }, + sorts = { 'score', 'sort_text' }, prebuilt_binaries = { -- Whether or not to automatically download a prebuilt binary from github. If this is set to `false` diff --git a/lua/blink/cmp/config/fuzzy.lua b/lua/blink/cmp/config/fuzzy.lua index 43f77ae9..bf19fa62 100644 --- a/lua/blink/cmp/config/fuzzy.lua +++ b/lua/blink/cmp/config/fuzzy.lua @@ -2,7 +2,7 @@ --- @field use_typo_resistance boolean When enabled, allows for a number of typos relative to the length of the query. Disabling this matches the behavior of fzf --- @field use_frecency boolean Tracks the most recently/frequently used items and boosts the score of the item --- @field use_proximity boolean Boosts the score of items matching nearby words ---- @field sorts ("label" | "kind" | "score" | blink.cmp.SortFunction)[] Controls which sorts to use and in which order, these three are currently the only allowed options +--- @field sorts ("label" | "sort_text" | "kind" | "score" | blink.cmp.SortFunction)[] Controls which sorts to use and in which order, these three are currently the only allowed options --- @field prebuilt_binaries blink.cmp.PrebuiltBinariesConfig --- @class (exact) blink.cmp.PrebuiltBinariesConfig @@ -20,7 +20,7 @@ local fuzzy = { use_typo_resistance = true, use_frecency = true, use_proximity = true, - sorts = { 'score', 'kind', 'label' }, + sorts = { 'score', 'sort_text' }, prebuilt_binaries = { download = true, force_version = nil, @@ -39,11 +39,13 @@ function fuzzy.validate(config) config.sorts, function(sorts) for _, sort in ipairs(sorts) do - if not vim.tbl_contains({ 'label', 'kind', 'score' }, sort) and type(sort) ~= 'function' then return false end + if not vim.tbl_contains({ 'label', 'sort_text', 'kind', 'score' }, sort) and type(sort) ~= 'function' then + return false + end end return true end, - 'one of: "label", "kind", "score" or a function', + 'one of: "label", "sort_text", "kind", "score" or a function', }, prebuilt_binaries = { config.prebuilt_binaries, 'table' }, }, config) diff --git a/lua/blink/cmp/fuzzy/init.lua b/lua/blink/cmp/fuzzy/init.lua index 40c662e8..b4a3e753 100644 --- a/lua/blink/cmp/fuzzy/init.lua +++ b/lua/blink/cmp/fuzzy/init.lua @@ -63,8 +63,8 @@ function fuzzy.fuzzy(needle, haystacks_by_provider) -- TODO: make this configurable min_score = config.fuzzy.use_typo_resistance and (6 * needle:len()) or 0, use_typo_resistance = config.fuzzy.use_typo_resistance, - use_frecency = config.fuzzy.use_frecency, - use_proximity = config.fuzzy.use_proximity, + use_frecency = config.fuzzy.use_frecency and #needle > 0, + use_proximity = config.fuzzy.use_proximity and #needle > 0, sorts = config.fuzzy.sorts, nearby_words = nearby_words, }) @@ -76,7 +76,7 @@ function fuzzy.fuzzy(needle, haystacks_by_provider) end end - return require('blink.cmp.fuzzy.sort').sort(filtered_items) + return require('blink.cmp.fuzzy.sort').sort(filtered_items, config.fuzzy.sorts) end return fuzzy diff --git a/lua/blink/cmp/fuzzy/sort.lua b/lua/blink/cmp/fuzzy/sort.lua index 27f9f842..2bfafcb2 100644 --- a/lua/blink/cmp/fuzzy/sort.lua +++ b/lua/blink/cmp/fuzzy/sort.lua @@ -1,12 +1,12 @@ local sort = {} --- @param list blink.cmp.CompletionItem[] +--- @param funcs ("label" | "sort_text" | "kind" | "score" | blink.cmp.SortFunction)[] --- @return blink.cmp.CompletionItem[] -function sort.sort(list) - local config = require('blink.cmp.config').fuzzy.sorts +function sort.sort(list, funcs) local sorting_funcs = vim.tbl_map( function(name_or_func) return type(name_or_func) == 'string' and sort[name_or_func] or name_or_func end, - config + funcs ) table.sort(list, function(a, b) for _, sorting_func in ipairs(sorting_funcs) do @@ -27,11 +27,14 @@ function sort.kind(a, b) return a.kind < b.kind end +function sort.sort_text(a, b) + if a.sortText == b.sortText then return end + return a.sortText < b.sortText +end + function sort.label(a, b) - local label_a = a.sortText or a.label - local label_b = b.sortText or b.label - local _, entry1_under = label_a:find('^_+') - local _, entry2_under = label_b:find('^_+') + local _, entry1_under = a.label:find('^_+') + local _, entry2_under = b.label:find('^_+') entry1_under = entry1_under or 0 entry2_under = entry2_under or 0 if entry1_under > entry2_under then @@ -39,7 +42,7 @@ function sort.label(a, b) elseif entry1_under < entry2_under then return true end - return a.label:lower() < b.label:lower() + return a.label < b.label end return sort