Skip to content

Commit ffc93c2

Browse files
committed
feat(view): add filters.git_clean
1 parent b17358f commit ffc93c2

File tree

9 files changed

+48
-11
lines changed

9 files changed

+48
-11
lines changed

doc/nvim-tree-lua.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ Subsequent calls to setup will replace the previous configuration.
293293
debounce_delay = 50,
294294
severity = {
295295
min = vim.diagnostic.severity.HINT,
296-
max = vim.diagnostic.severity.ERROR
296+
max = vim.diagnostic.severity.ERROR,
297297
},
298298
icons = {
299299
hint = "",
@@ -306,6 +306,7 @@ Subsequent calls to setup will replace the previous configuration.
306306
dotfiles = false,
307307
custom = {},
308308
exclude = {},
309+
git_clean = false,
309310
},
310311
filesystem_watchers = {
311312
enable = true,
@@ -889,6 +890,11 @@ Filtering options.
889890
Toggle via the `toggle_dotfiles` action, default mapping `H`.
890891
Type: `boolean`, Default: `false`
891892

893+
*nvim-tree.filters.git_clean*
894+
Do not show files with no git status. This will show ignored files when
895+
|nvim-tree.git.ignore| is set, as they are effectively dirty.
896+
Type: `boolean`, Default: `false`
897+
892898
*nvim-tree.filters.custom*
893899
Custom list of vim regex for file/directory names that will not be shown.
894900
Backslashes must be escaped e.g. "^\\.git". See |string-match|.
@@ -1305,6 +1311,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
13051311
`<Tab>` preview open the file as a preview (keeps the cursor in the tree)
13061312
`K` first_sibling navigate to the first sibling of current file/directory
13071313
`J` last_sibling navigate to the last sibling of current file/directory
1314+
`C` toggle_git_clean toggle visibility of git clean via |filters.git_clean| option
13081315
`I` toggle_git_ignored toggle visibility of files/folders hidden via |git.ignore| option
13091316
`H` toggle_dotfiles toggle visibility of dotfiles via |filters.dotfiles| option
13101317
`U` toggle_custom toggle visibility of files/folders hidden via |filters.custom| option
@@ -1354,6 +1361,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
13541361
{ key = "<Tab>", action = "preview" },
13551362
{ key = "K", action = "first_sibling" },
13561363
{ key = "J", action = "last_sibling" },
1364+
{ key = "C", action = "toggle_git_clean" },
13571365
{ key = "I", action = "toggle_git_ignored" },
13581366
{ key = "H", action = "toggle_dotfiles" },
13591367
{ key = "U", action = "toggle_custom" },

lua/nvim-tree.lua

+1
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
584584
dotfiles = false,
585585
custom = {},
586586
exclude = {},
587+
git_clean = false,
587588
},
588589
filesystem_watchers = {
589590
enable = true,

lua/nvim-tree/actions/dispatch.lua

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local Actions = {
1212
toggle_dotfiles = require("nvim-tree.actions.tree-modifiers.toggles").dotfiles,
1313
toggle_custom = require("nvim-tree.actions.tree-modifiers.toggles").custom,
1414
toggle_git_ignored = require("nvim-tree.actions.tree-modifiers.toggles").git_ignored,
15+
toggle_git_clean = require("nvim-tree.actions.tree-modifiers.toggles").git_clean,
1516

1617
-- Filesystem operations
1718
copy_absolute_path = require("nvim-tree.actions.fs.copy-paste").copy_absolute_path,

lua/nvim-tree/actions/init.lua

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ local DEFAULT_MAPPINGS = {
7676
action = "last_sibling",
7777
desc = "navigate to the last sibling of current file/directory",
7878
},
79+
{
80+
key = "C",
81+
action = "toggle_git_clean",
82+
desc = "toggle visibility of git clean via |filters.git_clean| option",
83+
},
7984
{
8085
key = "I",
8186
action = "toggle_git_ignored",

lua/nvim-tree/actions/tree-modifiers/toggles.lua

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ function M.git_ignored()
1515
return reloaders.reload_explorer()
1616
end
1717

18+
function M.git_clean()
19+
filters.config.filter_git_clean = not filters.config.filter_git_clean
20+
return reloaders.reload_explorer()
21+
end
22+
1823
function M.dotfiles()
1924
filters.config.filter_dotfiles = not filters.config.filter_dotfiles
2025
return reloaders.reload_explorer()

lua/nvim-tree/explorer/explore.lua

+1-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ local function populate_children(handle, cwd, node, status)
2323

2424
local abs = utils.path_join { cwd, name }
2525
t = get_type_from(t, abs)
26-
if
27-
not filters.should_ignore(abs)
28-
and not filters.should_ignore_git(abs, status.files)
29-
and not nodes_by_path[abs]
30-
then
26+
if not filters.should_ignore(abs) and not filters.should_ignore_git(abs, status) and not nodes_by_path[abs] then
3127
local child = nil
3228
if t == "directory" and vim.loop.fs_access(abs, "R") then
3329
child = builders.folder(node, abs, name)

lua/nvim-tree/explorer/filters.lua

+24-3
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,37 @@ function M.should_ignore(path)
5252
end
5353

5454
function M.should_ignore_git(path, status)
55-
return M.config.filter_git_ignored
56-
and (M.config.filter_git_ignored and status and status[path] == "!!")
57-
and not is_excluded(path)
55+
if type(status) ~= "table" or type(status.files) ~= "table" or type(status.dirs) ~= "table" then
56+
return false
57+
end
58+
59+
-- exclusions override all
60+
if is_excluded(path) then
61+
return false
62+
end
63+
64+
-- default to clean
65+
local st = status.files[path] or status.dirs[path] or " "
66+
67+
-- ignored overrides clean as they are effectively dirty
68+
if M.config.filter_git_ignored and st == "!!" then
69+
return true
70+
end
71+
72+
-- clean last
73+
if M.config.filter_git_clean and st == " " then
74+
return true
75+
end
76+
77+
return false
5878
end
5979

6080
function M.setup(opts)
6181
M.config = {
6282
filter_custom = true,
6383
filter_dotfiles = opts.filters.dotfiles,
6484
filter_git_ignored = opts.git.ignore,
85+
filter_git_clean = opts.filters.git_clean,
6586
}
6687

6788
M.ignore_list = {}

lua/nvim-tree/explorer/reload.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function M.reload(node, status)
7171

7272
local abs = utils.path_join { cwd, name }
7373
t = t or (fs_stat_cached(abs) or {}).type
74-
if not filters.should_ignore(abs) and not filters.should_ignore_git(abs, status.files) then
74+
if not filters.should_ignore(abs) and not filters.should_ignore_git(abs, status) then
7575
child_names[abs] = true
7676

7777
-- Recreate node if type changes.

lua/nvim-tree/renderer/components/icons.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ end
8585
function M.setup(opts)
8686
M.config = opts.renderer.icons
8787

88-
M.devicons = pcall(require, "nvim-web-devicons") and require "nvim-web-devicons"
88+
M.devicons = pcall(require, "nvim-web-devicons") and require "nvim-web-devicons" or nil
8989
end
9090

9191
return M

0 commit comments

Comments
 (0)