doc(recipes): improve git integrated hidden files recipe (#470)

This commit is contained in:
Micah Halter 2024-08-29 12:26:19 -04:00 committed by GitHub
parent 30e0438ff0
commit 85637c1e63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -30,47 +30,72 @@ require("oil").setup({
}) })
``` ```
## Hide gitignored files ## Hide gitignored files and show git tracked hidden files
```lua ```lua
local git_ignored = setmetatable({}, { -- helper function to parse output
__index = function(self, key) local function parse_output(proc)
local proc = vim.system( local result = proc:wait()
{ "git", "ls-files", "--ignored", "--exclude-standard", "--others", "--directory" }, local ret = {}
{ if result.code == 0 then
for line in vim.gsplit(result.stdout, "\n", { plain = true, trimempty = true }) do
-- Remove trailing slash
line = line:gsub("/$", "")
ret[line] = true
end
end
return ret
end
-- build git status cache
local function new_git_status()
return setmetatable({}, {
__index = function(self, key)
local ignore_proc = vim.system(
{ "git", "ls-files", "--ignored", "--exclude-standard", "--others", "--directory" },
{
cwd = key,
text = true,
}
)
local tracked_proc = vim.system({ "git", "ls-tree", "HEAD", "--name-only" }, {
cwd = key, cwd = key,
text = true, text = true,
})
local ret = {
ignored = parse_output(ignore_proc),
tracked = parse_output(tracked_proc),
} }
)
local result = proc:wait()
local ret = {}
if result.code == 0 then
for line in vim.gsplit(result.stdout, "\n", { plain = true, trimempty = true }) do
-- Remove trailing slash
line = line:gsub("/$", "")
table.insert(ret, line)
end
end
rawset(self, key, ret) rawset(self, key, ret)
return ret return ret
end, end,
}) })
end
local git_status = new_git_status()
-- Clear git status cache on refresh
local refresh = require("oil.actions").refresh
local orig_refresh = refresh.callback
refresh.callback = function(...)
git_status = new_git_status()
orig_refresh(...)
end
require("oil").setup({ require("oil").setup({
view_options = { view_options = {
is_hidden_file = function(name, _) is_hidden_file = function(name, _)
-- dotfiles are always considered hidden
if vim.startswith(name, ".") then
return true
end
local dir = require("oil").get_current_dir() local dir = require("oil").get_current_dir()
-- if no local directory (e.g. for ssh connections), always show local is_dotfile = vim.startswith(name, ".") and name ~= ".."
if not dir then -- if no local directory (e.g. for ssh connections), just hide dotfiles
return false if not dir then return is_dotfile end
-- dotfiles are considered hidden unless tracked
if is_dotfile then
return not git_status[dir].tracked[name]
else
-- Check if file is gitignored
return git_status[dir].ignored[name]
end end
-- Check if file is gitignored
return vim.list_contains(git_ignored[dir], name)
end, end,
}, },
}) })