Files
2026-04-30 17:37:34 +02:00

180 lines
4.8 KiB
Lua

local fzf = require("fzf-lua")
local wide = {
width = 160,
height = 30,
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({
defaults = {
lopen = "lopen",
copen = "copen",
git_icons = false,
file_icons = false,
},
winopts = {
width = 80,
height = 30,
row = 0.5,
col = 0.5,
backdrop = 100,
treesitter = false,
},
keymap = {
builtin = {
false,
["<C-o>"] = "toggle-fullscreen",
["<C-d>"] = "preview-half-page-down",
["<C-u>"] = "preview-half-page-up",
["<C-f>"] = "preview-down",
["<C-b>"] = "preview-up",
},
fzf = {
false,
["ctrl-y"] = "accept",
},
},
actions = {
files = {
false,
["enter"] = fzf.actions.file_edit,
["ctrl-s"] = fzf.actions.file_split,
["ctrl-v"] = fzf.actions.file_vsplit,
["ctrl-t"] = fzf.actions.file_tabedit,
["ctrl-q"] = {
fn = fzf.actions.file_sel_to_ll,
prefix = "select-all",
},
["alt-q"] = {
fn = fzf.actions.file_sel_to_qf,
prefix = "select-all",
},
},
},
files = {
hidden = true,
no_ignore = true,
previewer = false,
},
grep = {
hidden = true,
rg_opts = "--iglob=!.venv --iglob=!vendor --no-ignore "
.. require("fzf-lua.defaults").defaults.grep.rg_opts,
winopts = wide,
},
oldfiles = {
cwd_only = true,
previewer = false,
include_current_session = true,
},
buffers = {
sort_lastused = true,
previewer = false,
},
highlights = {
winopts = wide,
},
lsp = {
winopts = wide,
},
diagnostics = {
winopts = wide,
multiline = 1,
},
manpages = {
winopts = wide,
},
helptags = {
winopts = wide,
},
})
vim.keymap.set("n", "<leader>ff", fzf.files)
vim.keymap.set("n", "<leader>fr", recent_files)
vim.keymap.set("n", "<leader>fg", fzf.live_grep)
vim.keymap.set("n", "<leader>fG", function()
fzf.live_grep({ cwd = vim.fn.expand("%:p:h") })
end)
vim.keymap.set("n", "<leader>fb", fzf.buffers)
vim.keymap.set("n", "<leader>fh", fzf.highlights)
vim.keymap.set("n", "gd", fzf.lsp_definitions)
vim.keymap.set("n", "grr", fzf.lsp_references)
vim.keymap.set("n", "gri", fzf.lsp_implementations)
vim.keymap.set("n", "grt", fzf.lsp_typedefs)
vim.keymap.set("n", "<leader>fd", fzf.diagnostics_document)
vim.keymap.set("n", "<leader>fD", fzf.diagnostics_workspace)