Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A sorter matching fuzzy without reordering of the source #2251

Open
kyoh86 opened this issue Nov 28, 2022 · 2 comments
Open

A sorter matching fuzzy without reordering of the source #2251

kyoh86 opened this issue Nov 28, 2022 · 2 comments
Labels
enhancement Enhancement to performance, inner workings or existent features

Comments

@kyoh86
Copy link
Contributor

kyoh86 commented Nov 28, 2022

Sometimes I want a sorter without reordering of the list from the finder.
For example, when I'm finding files written recently.
In the case, I want to select them with matching fuzzy, but don't want to change the order of them.

Now, I use a workaround (described below).

It's complex a little, and it supports only static list (with finders.new_table).
Would you consider adding a sorter to the builtin that allows fuzzy matching without reordering?

My workaround for example, list from the most recently written/used files (with lambdalisue/mr.vim):
https://github.com/kyoh86/dotfiles/tree/main/nvim/lua/local/with/telescope_mru.lua#L1-L54

The key is a function named recent_file_sorter.

local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local make_entry = require("telescope.make_entry")
local conf = require("telescope.config").values

local M = {}

local function recent_file_sorter(opts, list)
    local indices = {}
    for i, line in ipairs(list) do
        indices[line] = i
    end
    local file_sorter = conf.file_sorter(opts)
    local base_scorer = file_sorter.scoring_function
    file_sorter.scoring_function = function(self, prompt, line)
        local score = base_scorer(self, prompt, line)
        if score <= 0 then
            return -1
        else
            return indices[line]
        end
    end
    return file_sorter
end

M.recent_written_files = function(opts)
    local safe_opts = opts or {}
    local list = vim.fn["mr#mrw#list"]()
    pickers.new(safe_opts, {
        prompt_title = "Recent written files",
        finder = finders.new_table({
            results = list,
            entry_maker = make_entry.gen_from_file(safe_opts),
        }),
        previewer = conf.file_previewer(safe_opts),
        sorter = recent_file_sorter(safe_opts, list),
    }):find()
end

M.recent_used_files = function(opts)
    local safe_opts = opts or {}
    local list = vim.fn["mr#mru#list"]()
    pickers.new(safe_opts, {
        prompt_title = "Recent used files",
        finder = finders.new_table({
            results = list,
            entry_maker = make_entry.gen_from_file(safe_opts),
        }),
        previewer = conf.file_previewer(safe_opts),
        sorter = recent_file_sorter(safe_opts, list),
    }):find()
end

return M
@kyoh86 kyoh86 added the enhancement Enhancement to performance, inner workings or existent features label Nov 28, 2022
@Icelk
Copy link

Icelk commented Jul 5, 2023

See this comment in the ripgrep repo for info on how to sort the results correctly: BurntSushi/ripgrep#2243 (comment)

@Icelk
Copy link

Icelk commented Jul 6, 2023

Using the sorter function defined here, you get priority for recent files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to performance, inner workings or existent features
Projects
None yet
Development

No branches or pull requests

2 participants