Skip to content

Commit 6bbc2ec

Browse files
committed
feat(hidden_display): Rename opts function name for the feature
1 parent 31ba53a commit 6bbc2ec

File tree

7 files changed

+70
-40
lines changed

7 files changed

+70
-40
lines changed

doc/nvim-tree-lua.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ Following is the default configuration. See |nvim-tree-opts| for details.
423423
root_folder_label = ":~:s?$?/..?",
424424
indent_width = 2,
425425
special_files = { "Cargo.toml", "Makefile", "README.md", "readme.md" },
426-
hidden_display_function = nil,
426+
hidden_display = "none",
427427
symlink_destination = true,
428428
highlight_git = "none",
429429
highlight_diagnostics = "none",
@@ -879,7 +879,7 @@ Number of spaces for an each tree nesting level. Minimum 1.
879879
A list of filenames that gets highlighted with `NvimTreeSpecialFile`.
880880
Type: `table`, Default: `{ "Cargo.toml", "Makefile", "README.md", "readme.md", }`
881881

882-
*nvim-tree.renderer.hidden_display_function
882+
*nvim-tree.renderer.hidden_display
883883
Determines the rendering of hidden files in a folder.
884884
Type: function | string, Default: "none"
885885
Possible string values are:

lua/nvim-tree.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
398398
root_folder_label = ":~:s?$?/..?",
399399
indent_width = 2,
400400
special_files = { "Cargo.toml", "Makefile", "README.md", "readme.md" },
401-
hidden_display_function = "none",
401+
hidden_display = "none",
402402
symlink_destination = true,
403403
highlight_git = "none",
404404
highlight_diagnostics = "none",
@@ -648,7 +648,7 @@ local ACCEPTED_TYPES = {
648648
},
649649
},
650650
renderer = {
651-
hidden_display_function = { "function", "string" },
651+
hidden_display = { "function", "string" },
652652
group_empty = { "boolean", "function" },
653653
root_folder_label = { "function", "string", "boolean" },
654654
},
@@ -682,7 +682,7 @@ local ACCEPTED_STRINGS = {
682682
signcolumn = { "yes", "no", "auto" },
683683
},
684684
renderer = {
685-
hidden_display_function = { "none", "simple", "all" },
685+
hidden_display = { "none", "simple", "all" },
686686
highlight_git = { "none", "icon", "name", "all" },
687687
highlight_opened_files = { "none", "icon", "name", "all" },
688688
highlight_modified = { "none", "icon", "name", "all" },

lua/nvim-tree/enum.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@ M.ICON_PLACEMENT = {
1919
right_align = 4,
2020
}
2121

22+
---Reason for filter in filter.lua
23+
---@enum FILTER_REASON
24+
M.FILTER_REASON = {
25+
none = 0, -- It's not filtered
26+
git = 1,
27+
buf = 2,
28+
dotfile = 4,
29+
custom = 8,
30+
bookmark = 16,
31+
}
32+
2233
return M

lua/nvim-tree/explorer/explore.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local live_filter = require "nvim-tree.live-filter"
66
local log = require "nvim-tree.log"
77
-- local explorer_module = require "nvim-tree.explorer"
88

9-
local FILTER_REASON = filters.FILTER_REASON
9+
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
1010
local Watcher = require "nvim-tree.watcher"
1111

1212
local M = {}
@@ -16,11 +16,11 @@ local M = {}
1616
---@param node Node
1717
---@param git_status table
1818
---@return integer filtered_count
19-
local function populate_children(handle, cwd, node, git_status)
19+
local function populate_children(handle, cwd, node, git_status, parent)
2020
local node_ignored = explorer_node.is_git_ignored(node)
2121
local nodes_by_path = utils.bool_record(node.nodes, "absolute_path")
2222

23-
local filter_status = filters.prepare(git_status)
23+
local filter_status = parent.filters:prepare(git_status)
2424

2525
node.hidden_count = vim.tbl_deep_extend("force", node.hidden_count or {}, {
2626
git = 0,
@@ -42,7 +42,7 @@ local function populate_children(handle, cwd, node, git_status)
4242

4343
---@type uv.fs_stat.result|nil
4444
local stat = vim.loop.fs_stat(abs)
45-
local filter_reason = filters.should_filter_as_reason(abs, stat, filter_status)
45+
local filter_reason = parent.filters:should_filter_as_reason(abs, stat, filter_status)
4646
if filter_reason == FILTER_REASON.none and not nodes_by_path[abs] and Watcher.is_fs_event_capable(abs) then
4747
local child = nil
4848
if is_dir and vim.loop.fs_access(abs, "R") then
@@ -101,13 +101,11 @@ function M.explore(node, status, parent)
101101
log.profile_end(profile)
102102
return ns
103103
end
104-
local old_num = #node.nodes
105-
sorters.sort(node.nodes)
104+
105+
parent.sorters:sort(node.nodes)
106106
live_filter.apply_filter(node)
107107

108108
log.profile_end(profile)
109-
local new_num = #node.nodes
110-
assert(old_num == new_num, vim.inspect { old_num = old_num, new_num = new_num })
111109
return node.nodes
112110
end
113111

lua/nvim-tree/explorer/filters.lua

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local utils = require "nvim-tree.utils"
2+
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
23

34
---@class Filters to handle all opts.filters and related API
45
---@field config table hydrated user opts.filters
@@ -43,16 +44,6 @@ function Filters:new(opts, explorer)
4344
return o
4445
end
4546

46-
---@enum FILTER_REASON
47-
M.FILTER_REASON = {
48-
none = 0, -- It's not filtered
49-
git = 1,
50-
buf = 2,
51-
dotfile = 4,
52-
custom = 8,
53-
bookmark = 16,
54-
}
55-
5647
---@param path string
5748
---@return boolean
5849
local function is_excluded(self, path)
@@ -221,7 +212,6 @@ function Filters:should_filter(path, fs_stat, status)
221212
return false
222213
end
223214

224-
-- exclusions override all filters
225215
if is_excluded(self, path) then
226216
return false
227217
end
@@ -233,4 +223,33 @@ function Filters:should_filter(path, fs_stat, status)
233223
or bookmark(self, path, fs_stat and fs_stat.type, status.bookmarks)
234224
end
235225

226+
--- Check if the given path should be filtered, and provide the reason why it was
227+
---@param path string Absolute path
228+
---@param fs_stat uv.fs_stat.result|nil fs_stat of file
229+
---@param status table from prepare
230+
---@return FILTER_REASON
231+
function Filters:should_filter_as_reason(path, fs_stat, status)
232+
if not self.config.enable then
233+
return FILTER_REASON.none
234+
end
235+
236+
if is_excluded(self, path) then
237+
return FILTER_REASON.none
238+
end
239+
240+
if git(self, path, status.git_status) then
241+
return FILTER_REASON.git
242+
elseif buf(self, path, status.bufinfo) then
243+
return FILTER_REASON.buf
244+
elseif dotfile(self, path) then
245+
return FILTER_REASON.dotfile
246+
elseif custom(self, path) then
247+
return FILTER_REASON.custom
248+
elseif bookmark(self, path, fs_stat and fs_stat.type, status.bookmarks) then
249+
return FILTER_REASON.bookmark
250+
else
251+
return FILTER_REASON.none
252+
end
253+
end
254+
236255
return Filters

lua/nvim-tree/explorer/reload.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local live_filter = require "nvim-tree.live-filter"
55
local git = require "nvim-tree.git"
66
local log = require "nvim-tree.log"
77

8-
local FILTER_REASON = filters.FILTER_REASON
8+
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
99
local NodeIterator = require "nvim-tree.iterators.node-iterator"
1010
local Watcher = require "nvim-tree.watcher"
1111

@@ -81,8 +81,7 @@ function M.reload(node, git_status)
8181

8282
local profile = log.profile_start("reload %s", node.absolute_path)
8383

84-
local filter_status = filters.prepare(git_status)
85-
local is_dir = node.type == "directory"
84+
local filter_status = explorer.filters:prepare(git_status)
8685

8786
if node.group_next then
8887
node.nodes = { node.group_next }
@@ -114,7 +113,7 @@ function M.reload(node, git_status)
114113
---@type uv.fs_stat.result|nil
115114
local stat = vim.loop.fs_stat(abs)
116115

117-
local filter_reason = filters.should_filter_as_reason(abs, stat, filter_status)
116+
local filter_reason = explorer.filters:should_filter_as_reason(abs, stat, filter_status)
118117
if filter_reason == FILTER_REASON.none then
119118
remain_childs[abs] = true
120119

lua/nvim-tree/renderer/builder.lua

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,18 @@ end
369369

370370
---@private
371371
function Builder:add_hidden_count_string(node, idx, num_children)
372-
local hidden_count_string = M.opts.renderer.hidden_display_function(node.hidden_count)
372+
local hidden_count_string = M.opts.renderer.hidden_display(node.hidden_count)
373373
if hidden_count_string and hidden_count_string ~= "" then
374374
local indent_markers = pad.get_indent_markers(math.max(self.depth, 0), idx or 0, num_children or 0, node, self.markers)
375375
local indent_width = M.opts.renderer.indent_width
376376
local indent_string = string.rep(" ", indent_width) .. (indent_markers.str or "")
377-
table.insert(
378-
self.virtual_lines,
379-
{ indent_string = indent_string, depth = self.depth, line_nr = #self.lines - 1, text = hidden_count_string }
380-
)
377+
table.insert(self.virtual_lines, {
378+
indent_string = indent_string,
379+
depth = self.depth,
380+
line_nr = #self.lines - 1,
381+
-- Remove padding if we're in root
382+
text = (node.parent == nil and "" or string.rep(" ", indent_width)) .. hidden_count_string,
383+
})
381384
end
382385
end
383386

@@ -480,19 +483,19 @@ end
480483

481484
---@param opts table
482485
local setup_hidden_display_function = function(opts)
483-
local hidden_display = opts.renderer.hidden_display_function
484-
-- options are already validated, so ´hidden_display_function´ can ONLY be `string` or `function` if type(hidden_display) == "string" then
486+
local hidden_display = opts.renderer.hidden_display
487+
-- options are already validated, so ´hidden_display´ can ONLY be `string` or `function` if type(hidden_display) == "string" then
485488
if type(hidden_display) == "string" then
486489
if hidden_display == "none" then
487-
opts.renderer.hidden_display_function = function()
490+
opts.renderer.hidden_display = function()
488491
return nil
489492
end
490493
elseif hidden_display == "simple" then
491-
opts.renderer.hidden_display_function = function(hidden_count)
494+
opts.renderer.hidden_display = function(hidden_count)
492495
return utils.default_format_hidden_count(hidden_count, true)
493496
end
494497
elseif hidden_display == "all" then
495-
opts.renderer.hidden_display_function = function(hidden_count)
498+
opts.renderer.hidden_display = function(hidden_count)
496499
return utils.default_format_hidden_count(hidden_count, false)
497500
end
498501
end
@@ -510,13 +513,13 @@ local setup_hidden_display_function = function(opts)
510513

511514
local ok, result = pcall(hidden_display, hidden_count)
512515
if not ok then
513-
notify.warn "Problem occurred in ``opts.renderer.hidden_display_function`` see "
516+
notify.warn "Problem occurred in ``opts.renderer.hidden_display_function`` see nvim-tree.renderer.hidden_display on :h nvim-tree"
514517
return nil
515518
end
516519
return result
517520
end
518521

519-
opts.renderer.hidden_display_function = safe_render
522+
opts.renderer.hidden_display = safe_render
520523
end
521524
end
522525

0 commit comments

Comments
 (0)