Skip to content

Commit a6c1d45

Browse files
committed
chore: refacto populate filtering
move `nvim_tree_ignore` and `nvim_tree_hide_dotfiles` to setup
1 parent f92b7e7 commit a6c1d45

File tree

6 files changed

+80
-87
lines changed

6 files changed

+80
-87
lines changed

README.md

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,20 @@ Note that options under the `g:` command should be set **BEFORE** running the se
3636

3737
```lua
3838
-- following options are the default
39+
-- each of these are documented in `:help nvim-tree.OPTION_NAME`
3940
require'nvim-tree'.setup {
40-
-- disables netrw completely
4141
disable_netrw = true,
42-
-- hijack netrw window on startup
4342
hijack_netrw = true,
44-
-- open the tree when running this setup function
4543
open_on_setup = false,
46-
-- will not open on setup if the filetype is in this list
4744
ignore_ft_on_setup = {},
48-
-- closes neovim automatically when the tree is the last **WINDOW** in the view
4945
auto_close = false,
50-
-- opens the tree when changing/opening a new tab if the tree wasn't previously opened
5146
open_on_tab = false,
52-
-- hijacks new directory buffers when they are opened.
47+
hijack_cursor = false,
48+
update_cwd = false,
5349
update_to_buf_dir = {
54-
-- enable the feature
5550
enable = true,
56-
-- allow to open the tree if it was previously closed
5751
auto_open = true,
5852
},
59-
-- hijack the cursor in the tree to put it at the start of the filename
60-
hijack_cursor = false,
61-
-- updates the root directory of the tree on `DirChanged` (when you run `:cd` usually)
62-
update_cwd = false,
63-
-- show lsp diagnostics in the signcolumn
6453
diagnostics = {
6554
enable = false,
6655
icons = {
@@ -70,53 +59,39 @@ require'nvim-tree'.setup {
7059
error = "",
7160
}
7261
},
73-
-- update the focused file on `BufEnter`, un-collapses the folders recursively until it finds the file
7462
update_focused_file = {
75-
-- enables the feature
7663
enable = false,
77-
-- update the root directory of the tree to the one of the folder containing the file if the file is not under the current root directory
78-
-- only relevant when `update_focused_file.enable` is true
7964
update_cwd = false,
80-
-- list of buffer names / filetypes that will not update the cwd if the file isn't found under the current root directory
81-
-- only relevant when `update_focused_file.update_cwd` is true and `update_focused_file.enable` is true
8265
ignore_list = {}
8366
},
84-
-- configuration options for the system open command (`s` in the tree by default)
8567
system_open = {
86-
-- the command to run this, leaving nil should work in most cases
8768
cmd = nil,
88-
-- the command arguments as a list
8969
args = {}
9070
},
91-
71+
filters = {
72+
dotfiles = false,
73+
custom = {}
74+
},
9275
view = {
93-
-- width of the window, can be either a number (columns) or a string in `%`, for left or right side placement
9476
width = 30,
95-
-- height of the window, can be either a number (columns) or a string in `%`, for top or bottom side placement
9677
height = 30,
97-
-- Hide the root path of the current folder on top of the tree
9878
hide_root_folder = false,
99-
-- side of the tree, can be one of 'left' | 'right' | 'top' | 'bottom'
10079
side = 'left',
101-
-- if true the tree will resize itself after opening a file
10280
auto_resize = false,
10381
mappings = {
104-
-- custom only false will merge the list with the default mappings
105-
-- if true, it will only use your list to set the mappings
10682
custom_only = false,
107-
-- list of mappings to set on the tree manually
10883
list = {}
10984
}
11085
}
11186
}
11287
```
11388

89+
These additional options must be set **BEFORE** calling `require'nvim-tree'` or calling setup.
90+
They are being migrated to the setup function bit by bit, check [this issue](https://github.com/kyazdani42/nvim-tree.lua/issues/674) if you encounter any problems related to configs not working after update.
11491
```vim
115-
let g:nvim_tree_ignore = [ '.git', 'node_modules', '.cache' ] "empty by default
11692
let g:nvim_tree_gitignore = 1 "0 by default
11793
let g:nvim_tree_quit_on_open = 1 "0 by default, closes the tree when you open a file
11894
let g:nvim_tree_indent_markers = 1 "0 by default, this option shows indent markers when folders are open
119-
let g:nvim_tree_hide_dotfiles = 1 "0 by default, this option hides files and folders starting with a dot `.`
12095
let g:nvim_tree_git_hl = 1 "0 by default, will enable file highlight for git attributes (can be used without the icons).
12196
let g:nvim_tree_highlight_opened_files = 1 "0 by default, will enable folder and file icon highlight for opened files/directories.
12297
let g:nvim_tree_root_folder_modifier = ':~' "This is the default. See :help filename-modifiers for more options
@@ -219,7 +194,7 @@ highlight NvimTreeFolderIcon guibg=blue
219194
- `<C-x>` will open the file in a horizontal split
220195
- `<C-t>` will open the file in a new tab
221196
- `<Tab>` will open the file as a preview (keeps the cursor in the tree)
222-
- `I` will toggle visibility of folders hidden via |g:nvim_tree_ignore|
197+
- `I` will toggle visibility of hidden folders / files
223198
- `H` will toggle visibility of dotfiles (files/folders starting with a `.`)
224199
- `R` will refresh the tree
225200
- Double left click acts like `<CR>`

doc/nvim-tree-lua.txt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ function.
110110
custom_only = false,
111111
list = {}
112112
}
113+
},
114+
filters = {
115+
dotfiles = false,
116+
custom = {}
113117
}
114118
}
115119
<
@@ -261,10 +265,22 @@ Here is a list of the options available in the setup call:
261265
type: `boolean`
262266
default: false
263267

264-
- |view,mappings.list|: a list of keymaps that will extend or override the default keymaps
268+
- |view.mappings.list|: a list of keymaps that will extend or override the default keymaps
265269
type: list of `{ key: table of strings or string, mode: string (vim-mode), cb: callback function as a string }`
266270
default: {}
267271

272+
*nvim-tree.filters*
273+
|filters|: filtering options
274+
275+
- |filters.dotfiles|: do not show `dotfiles` (files starting with a `.`)
276+
type: `boolean`
277+
default: `false`
278+
279+
- |filters.custom|: custom list of string that will not be shown.
280+
type: `{string}`
281+
default: `{}`
282+
283+
268284
==============================================================================
269285
OPTIONS *nvim-tree-options*
270286

@@ -280,13 +296,6 @@ width of the window, can be *width_in_columns* or *'width_in_percent%'*
280296
where the window will open (default to 'left')
281297
- 'left' or 'right'
282298

283-
|g:nvim_tree_ignore| *g:nvim_tree_ignore*
284-
285-
An array of strings that the tree won't load and display.
286-
useful to hide large data/cache folders.
287-
>
288-
example: let g:nvim_tree_ignore = [ '.git', 'node_modules' ]
289-
290299
|g:nvim_tree_gitignore| *g:nvim_tree_gitignore*
291300

292301
Determines whether to include in g:nvim_tree_ignore
@@ -384,12 +393,6 @@ Default is 0
384393
Can be `0` or `1`. When `1`, will display indent markers when folders are open
385394
Default is 0
386395

387-
|g:nvim_tree_hide_dotfiles| *g:nvim_tree_hide_dotfiles*
388-
389-
Can be `0` or `1`. When `1`, will hide dotfiles, files or folders which start
390-
with the `.` character.
391-
Default is 0
392-
393396
|g:nvim_tree_root_folder_modifier| *g:nvim_tree_root_folder_modifier*
394397

395398
In what format to show root folder. See `:help filename-modifiers` for

lua/nvim-tree.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ local DEFAULT_OPTS = {
436436
error = "",
437437
}
438438
},
439+
filters = {
440+
dotfiles = false,
441+
custom_filter = {}
442+
}
439443
}
440444

441445
function M.setup(conf)
@@ -464,6 +468,7 @@ function M.setup(conf)
464468
require'nvim-tree.colors'.setup()
465469
require'nvim-tree.view'.setup(opts.view or {})
466470
require'nvim-tree.diagnostics'.setup(opts)
471+
require'nvim-tree.populate'.setup(opts)
467472

468473
setup_autocommands(opts)
469474
setup_vim_commands()

lua/nvim-tree/lib.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,12 @@ function M.parent_node(node, should_close)
561561
end
562562

563563
function M.toggle_ignored()
564-
pops.show_ignored = not pops.show_ignored
564+
pops.config.filter_ignored = not pops.config.filter_ignored
565565
return M.refresh_tree()
566566
end
567567

568568
function M.toggle_dotfiles()
569-
pops.show_dotfiles = not pops.show_dotfiles
569+
pops.config.filter_dotfiles = not pops.config.filter_dotfiles
570570
return M.refresh_tree()
571571
end
572572

lua/nvim-tree/populate.lua

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
local config = require'nvim-tree.config'
22
local git = require'nvim-tree.git'
3-
local icon_config = config.get_icon_state()
43

54
local api = vim.api
65
local luv = vim.loop
76

87
local M = {
9-
show_ignored = false,
10-
show_dotfiles = vim.g.nvim_tree_hide_dotfiles ~= 1,
8+
ignore_list = {}
119
}
1210

1311
local utils = require'nvim-tree.utils'
@@ -116,47 +114,42 @@ local function node_comparator(a, b)
116114
return a.name:lower() <= b.name:lower()
117115
end
118116

119-
local function gen_ignore_check(cwd)
120-
if not cwd then cwd = luv.cwd() end
121-
local ignore_list = {}
117+
---Check if the given path should be ignored.
118+
---@param path string Absolute path
119+
---@return boolean
120+
local function should_ignore(path)
121+
local basename = utils.path_basename(path)
122122

123-
if vim.g.nvim_tree_ignore and #vim.g.nvim_tree_ignore > 0 then
124-
for _, entry in pairs(vim.g.nvim_tree_ignore) do
125-
ignore_list[entry] = true
123+
if M.config.filter_dotfiles then
124+
if basename:sub(1, 1) == '.' then
125+
return true
126126
end
127127
end
128128

129-
---Check if the given path should be ignored.
130-
---@param path string Absolute path
131-
---@return boolean
132-
return function(path)
133-
local basename = utils.path_basename(path)
134-
135-
if not M.show_ignored then
136-
if vim.g.nvim_tree_gitignore == 1 then
137-
if git.should_gitignore(path) then return true end
138-
end
139-
140-
local relpath = utils.path_relative(path, cwd)
141-
if ignore_list[relpath] == true or ignore_list[basename] == true then
142-
return true
143-
end
129+
if not M.config.filter_ignored then
130+
return false
131+
end
144132

145-
local idx = path:match(".+()%.[^.]+$")
146-
if idx then
147-
if ignore_list['*'..string.sub(path, idx)] == true then return true end
148-
end
133+
if vim.g.nvim_tree_gitignore == 1 then
134+
if git.should_gitignore(path) then
135+
return true
149136
end
137+
end
150138

151-
if not M.show_dotfiles then
152-
if basename:sub(1, 1) == '.' then return true end
153-
end
139+
local relpath = utils.path_relative(path, vim.loop.cwd())
140+
if M.ignore_list[relpath] == true or M.ignore_list[basename] == true then
141+
return true
142+
end
154143

155-
return false
144+
local idx = path:match(".+()%.[^.]+$")
145+
if idx then
146+
if M.ignore_list['*'..string.sub(path, idx)] == true then
147+
return true
148+
end
156149
end
157-
end
158150

159-
local should_ignore = gen_ignore_check()
151+
return false
152+
end
160153

161154
function M.refresh_entries(entries, cwd, parent_node)
162155
local handle = luv.fs_scandir(cwd)
@@ -351,6 +344,7 @@ function M.populate(entries, cwd, parent_node)
351344

352345
utils.merge_sort(entries, node_comparator)
353346

347+
local icon_config = config.get_icon_state()
354348
if (not icon_config.show_git_icon) and vim.g.nvim_tree_git_hl ~= 1 then
355349
return
356350
end
@@ -360,4 +354,18 @@ function M.populate(entries, cwd, parent_node)
360354
end
361355
end
362356

357+
function M.setup(opts)
358+
M.config = {
359+
filter_ignored = true,
360+
filter_dotfiles = opts.filters.dotfiles,
361+
}
362+
363+
local custom_filter = opts.filters.custom
364+
if custom_filter and #custom_filter > 0 then
365+
for _, entry in pairs(custom_filter) do
366+
M.ignore_list[entry] = true
367+
end
368+
end
369+
end
370+
363371
return M

plugin/nvim-tree-startup.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ local out_config = {
2323
"nvim_tree_bindings",
2424
"nvim_tree_disable_keybindings",
2525
"nvim_tree_disable_default_keybindings",
26+
"nvim_tree_hide_dotfiles",
27+
"nvim_tree_ignore"
2628
}
2729

2830
local x = vim.tbl_filter(function(v)
2931
return vim.fn.exists('g:'..v) ~= 0
3032
end, out_config)
3133

3234
if #x > 0 then
33-
local msg = "following options are now set in the setup (:help nvim-tree.setup): "
34-
require'nvim-tree.utils'.echo_warning(msg..table.concat(x, " | "))
35+
local msg = "Following options were moved to setup (:help nvim-tree.setup): "
36+
require'nvim-tree.utils'.echo_warning(msg..table.concat(x, ", "))
3537
end

0 commit comments

Comments
 (0)