feat(picker.matcher): moved length tiebreak to sorter instead

This commit is contained in:
Folke Lemaitre 2025-01-17 09:41:36 +01:00
parent 6ae87d9f62
commit d5ccb301c1
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 10 additions and 4 deletions

View file

@ -101,6 +101,10 @@ local defaults = {
return vim.o.columns >= 120 and "default" or "vertical"
end,
},
sort = {
-- default sort is by score, text length and index
fields = { "score:desc", "#text", "idx" },
},
ui_select = true, -- replace `vim.ui.select` with the snacks picker
---@class snacks.picker.formatters.Config
formatters = {

View file

@ -7,6 +7,7 @@
---@field prev_class number
---@field in_gap boolean
---@field str string
---@field opts snacks.picker.matcher.Config
local M = {}
M.__index = M
@ -14,7 +15,6 @@ M.__index = M
local SCORE_MATCH = 16
local SCORE_GAP_START = -3
local SCORE_GAP_EXTENSION = -1
local SCORE_LEN = -0.01
local BONUS_BOUNDARY = SCORE_MATCH / 2 -- 8
local BONUS_NONWORD = SCORE_MATCH / 2 -- 8
@ -95,8 +95,10 @@ for prev = 0, 6 do
end
end
function M.new()
---@param opts? snacks.picker.matcher.Config
function M.new(opts)
local self = setmetatable({}, M)
self.opts = opts or {}
self.score = 0
self.consecutive = 0
self.prev_class = CHAR_WHITE
@ -121,14 +123,14 @@ end
---@param first number
function M:init(str, first)
self.str = str
self.score = #str * SCORE_LEN -- tiebreak by length
self.score = 0
self.consecutive = 0
self.prev_class = CHAR_WHITE
self.prev = nil
if first > 1 then
self.prev_class = CHAR_CLASS[str:byte(first - 1)] or CHAR_NONWORD
end
if not str:find(PATH_SEP, first + 1, true) then
if self.opts.filename_bonus and not str:find(PATH_SEP, first + 1, true) then
self.score = self.score + BONUS_NO_PATH_SEP
end
self.in_gap = false