feat(dashbard): explude files from stdpath data/cache/state in recent files and projects

This commit is contained in:
Folke Lemaitre 2024-12-01 19:49:26 +01:00
parent 891648a483
commit b99bc64ef9
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -735,6 +735,45 @@ function M.have_plugin(name)
return package.loaded.lazy and require("lazy.core.config").spec.plugins[name] ~= nil return package.loaded.lazy and require("lazy.core.config").spec.plugins[name] ~= nil
end end
---@param opts? {filter?: table<string, boolean>}
---@return fun():string?
function M.oldfiles(opts)
opts = vim.tbl_deep_extend("force", {
filter = {
[vim.fn.stdpath("data")] = false,
[vim.fn.stdpath("cache")] = false,
[vim.fn.stdpath("state")] = false,
},
}, opts or {})
---@cast opts {filter:table<string, boolean>}
local filter = {} ---@type {path:string, want:boolean}[]
for path, want in pairs(opts.filter or {}) do
table.insert(filter, { path = vim.fs.normalize(path), want = want })
end
local done = {} ---@type table<string, boolean>
local i = 1
return function()
while vim.v.oldfiles[i] do
local file = vim.fs.normalize(vim.v.oldfiles[i], { _fast = true, expand_env = false })
local want = not done[file]
if want then
done[file] = true
for _, f in ipairs(filter) do
if (file:sub(1, #f.path) == f.path) ~= f.want then
want = false
break
end
end
end
i = i + 1
if want and uv.fs_stat(file) then
return file
end
end
end
end
M.sections = {} M.sections = {}
-- Adds a section to restore the session if any of the supported plugins are installed. -- Adds a section to restore the session if any of the supported plugins are installed.
@ -769,11 +808,7 @@ function M.sections.recent_files(opts)
local limit = opts.limit or 5 local limit = opts.limit or 5
local root = opts.cwd and vim.fs.normalize(opts.cwd == true and vim.fn.getcwd() or opts.cwd) or "" local root = opts.cwd and vim.fs.normalize(opts.cwd == true and vim.fn.getcwd() or opts.cwd) or ""
local ret = {} ---@type snacks.dashboard.Section local ret = {} ---@type snacks.dashboard.Section
local done = {} ---@type table<string, boolean> for file in M.oldfiles({ filter = { [root] = true } }) do
for _, file in ipairs(vim.v.oldfiles) do
file = vim.fs.normalize(file, { _fast = true, expand_env = false })
if not done[file] and file:sub(1, #root) == root and uv.fs_stat(file) then
done[file] = true
ret[#ret + 1] = { ret[#ret + 1] = {
file = file, file = file,
icon = "file", icon = "file",
@ -784,7 +819,6 @@ function M.sections.recent_files(opts)
break break
end end
end end
end
return ret return ret
end end
end end
@ -803,9 +837,8 @@ function M.sections.projects(opts)
dirs = vim.list_slice(dirs, 1, limit) dirs = vim.list_slice(dirs, 1, limit)
if not opts.dirs then if not opts.dirs then
for _, file in ipairs(vim.v.oldfiles) do for file in M.oldfiles() do
file = vim.fs.normalize(file, { _fast = true, expand_env = false }) local dir = Snacks.git.get_root(file)
local dir = uv.fs_stat(file) and Snacks.git.get_root(file)
if dir and not vim.tbl_contains(dirs, dir) then if dir and not vim.tbl_contains(dirs, dir) then
table.insert(dirs, dir) table.insert(dirs, dir)
if #dirs >= limit then if #dirs >= limit then