feat(fzf-lua): replace oldfiles with unified MRU recent_files

This commit is contained in:
2026-04-30 11:51:38 +02:00
parent d95de4bc1d
commit b1a567ae2f
+84 -2
View File
@@ -3,13 +3,88 @@ local fzf = require("fzf-lua")
local wide = { local wide = {
width = 160, width = 160,
height = 30, height = 30,
preview = { horizontal = "right:50%" }, preview = {
horizontal = "right:50%",
},
} }
local function valid_file(file)
local stat = vim.uv.fs_stat(file)
return stat ~= nil
and not fzf.utils.path_is_directory(file, stat)
-- FIFO blocks `fs_open` indefinitely (#908)
and not fzf.utils.file_is_fifo(file, stat)
and fzf.utils.file_is_readable(file)
end
local function recent_files()
local cwd = vim.fn.getcwd()
local current = vim.api.nvim_buf_get_name(0)
local has_current = current ~= ""
local oldfiles_count = #vim.v.oldfiles
local scores = {}
local function under_cwd(path)
local rel = vim.fs.relpath(cwd, path)
return rel ~= nil and not vim.startswith(rel, "..")
end
local function is_uri(path)
return path:match("^%w[%w+%-%.]*://") ~= nil
end
for i, f in ipairs(vim.v.oldfiles) do
if f ~= current and not is_uri(f) and under_cwd(f) then
scores[f] = oldfiles_count - i + 1
end
end
for _, b in ipairs(vim.api.nvim_list_bufs()) do
if vim.bo[b].buftype == "" then
local name = vim.api.nvim_buf_get_name(b)
if
name ~= ""
and not is_uri(name)
and name ~= current
and under_cwd(name)
and (scores[name] or valid_file(name))
then
local info = vim.fn.getbufinfo(b)[1]
---@cast info -nil
scores[name] = math.max(info.lastused, scores[name] or 0)
end
end
end
local list = {}
for f in pairs(scores) do
table.insert(list, f)
end
table.sort(list, function(a, b)
return scores[a] > scores[b]
end)
for i, f in ipairs(list) do
list[i] = vim.fs.relpath(cwd, f) or f
end
if has_current then
table.insert(list, 1, vim.fs.relpath(cwd, current) or current)
end
fzf.fzf_exec(list, {
prompt = "> ",
previewer = false,
winopts = { title = " Recent " },
fzf_opts = has_current and { ["--header-lines"] = 1 } or nil,
actions = require("fzf-lua.config").globals.actions.files,
})
end
fzf.setup({ fzf.setup({
defaults = { defaults = {
lopen = "lopen", lopen = "lopen",
copen = "copen", copen = "copen",
git_icons = false,
file_icons = false,
}, },
winopts = { winopts = {
width = 80, width = 80,
@@ -17,6 +92,7 @@ fzf.setup({
row = 0.5, row = 0.5,
col = 0.5, col = 0.5,
backdrop = 100, backdrop = 100,
treesitter = false,
}, },
keymap = { keymap = {
builtin = { builtin = {
@@ -79,10 +155,16 @@ fzf.setup({
winopts = wide, winopts = wide,
multiline = 1, multiline = 1,
}, },
manpages = {
winopts = wide,
},
helptags = {
winopts = wide,
},
}) })
vim.keymap.set("n", "<leader>ff", fzf.files) vim.keymap.set("n", "<leader>ff", fzf.files)
vim.keymap.set("n", "<leader>fr", fzf.oldfiles) vim.keymap.set("n", "<leader>fr", recent_files)
vim.keymap.set("n", "<leader>fg", fzf.live_grep) vim.keymap.set("n", "<leader>fg", fzf.live_grep)
vim.keymap.set("n", "<leader>fG", function() vim.keymap.set("n", "<leader>fG", function()
fzf.live_grep({ cwd = vim.fn.expand("%:p:h") }) fzf.live_grep({ cwd = vim.fn.expand("%:p:h") })