Skip to content

Commit

Permalink
feat: sort on score and sort_text only by default, disable frecency a…
Browse files Browse the repository at this point in the history
…nd proximity on no keyword

Closes #570
  • Loading branch information
Saghen committed Dec 16, 2024
1 parent 12ffc10 commit 76230d5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
10 changes: 6 additions & 4 deletions lua/blink/cmp/config/fuzzy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions lua/blink/cmp/fuzzy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -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
19 changes: 11 additions & 8 deletions lua/blink/cmp/fuzzy/sort.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,19 +27,22 @@ 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
return false
elseif entry1_under < entry2_under then
return true
end
return a.label:lower() < b.label:lower()
return a.label < b.label
end

return sort

0 comments on commit 76230d5

Please sign in to comment.