Skip to content

Commit 2d97059

Browse files
remvnalex-courtis
andauthored
fix: bookmark filter shows marked directory children (#2719)
* fix: bookmark filter include marked-directory's children * fix(perf): add path_type to filter functions * fix: replace undefined type * fix: correct Node.fs_stat type * fix: file info popup check fs_stat not nil * refactor: add stat to should_filter, Node constructor * perf: early return if bookmark is empty --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent e508bdc commit 2d97059

File tree

8 files changed

+80
-58
lines changed

8 files changed

+80
-58
lines changed

lua/nvim-tree/actions/finders/search-node.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ local function search(search_dir, input_path)
3636
while name do
3737
path = dir .. "/" .. name
3838

39+
---@type uv.fs_stat.result|nil
3940
stat, _ = vim.loop.fs_stat(path)
4041
if not stat then
4142
break
4243
end
4344

44-
if not filters.should_filter(path, filter_status) then
45+
if not filters.should_filter(path, stat, filter_status) then
4546
if string.find(path, "/" .. input_path .. "$") then
4647
return path
4748
end

lua/nvim-tree/actions/node/file-popup.lua

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ local M = {}
66
---@return table
77
local function get_formatted_lines(node)
88
local stats = node.fs_stat
9+
if stats == nil then
10+
return {
11+
"",
12+
" Can't retrieve file information",
13+
"",
14+
}
15+
end
16+
917
local fpath = " fullpath: " .. node.absolute_path
1018
local created_at = " created: " .. os.date("%x %X", stats.birthtime.sec)
1119
local modified_at = " modified: " .. os.date("%x %X", stats.mtime.sec)

lua/nvim-tree/explorer/explore.lua

+7-13
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ local Watcher = require "nvim-tree.watcher"
1111

1212
local M = {}
1313

14-
---@param type_ string|nil
15-
---@param cwd string
16-
---@return any
17-
local function get_type_from(type_, cwd)
18-
return type_ or (vim.loop.fs_stat(cwd) or {}).type
19-
end
20-
2114
---@param handle uv.uv_fs_t
2215
---@param cwd string
2316
---@param node Node
@@ -33,18 +26,19 @@ local function populate_children(handle, cwd, node, git_status)
3326
end
3427

3528
local abs = utils.path_join { cwd, name }
36-
3729
local profile = log.profile_start("explore populate_children %s", abs)
3830

39-
t = get_type_from(t, abs)
40-
if not filters.should_filter(abs, filter_status) and not nodes_by_path[abs] and Watcher.is_fs_event_capable(abs) then
31+
---@type uv.fs_stat.result|nil
32+
local stat = vim.loop.fs_stat(abs)
33+
34+
if not filters.should_filter(abs, stat, filter_status) and not nodes_by_path[abs] and Watcher.is_fs_event_capable(abs) then
4135
local child = nil
4236
if t == "directory" and vim.loop.fs_access(abs, "R") then
43-
child = builders.folder(node, abs, name)
37+
child = builders.folder(node, abs, name, stat)
4438
elseif t == "file" then
45-
child = builders.file(node, abs, name)
39+
child = builders.file(node, abs, name, stat)
4640
elseif t == "link" then
47-
local link = builders.link(node, abs, name)
41+
local link = builders.link(node, abs, name, stat)
4842
if link.link_to ~= nil then
4943
child = link
5044
end

lua/nvim-tree/explorer/filters.lua

+32-10
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,36 @@ local function dotfile(path)
7171
end
7272

7373
---@param path string
74-
---@param bookmarks table<string, boolean> absolute paths bookmarked
75-
local function bookmark(path, bookmarks)
74+
---@param path_type string|nil filetype of path
75+
---@param bookmarks table<string, string|nil> path, filetype table of bookmarked files
76+
local function bookmark(path, path_type, bookmarks)
7677
if not M.config.filter_no_bookmark then
7778
return false
7879
end
80+
-- if bookmark is empty, we should see a empty filetree
81+
if next(bookmarks) == nil then
82+
return true
83+
end
7984

80-
-- add trailing slash to make it match only mark's parent directory
81-
-- not it's siblings
82-
local parent = utils.path_add_trailing(path)
83-
for mark, _ in pairs(bookmarks) do
84-
if path == mark or vim.fn.stridx(mark, parent) == 0 then
85+
local mark_parent = utils.path_add_trailing(path)
86+
for mark, mark_type in pairs(bookmarks) do
87+
if path == mark then
8588
return false
8689
end
90+
91+
if path_type == "directory" then
92+
-- check if path is mark's parent
93+
if vim.fn.stridx(mark, mark_parent) == 0 then
94+
return false
95+
end
96+
end
97+
if mark_type == "directory" then
98+
-- check if mark is path's parent
99+
local path_parent = utils.path_add_trailing(mark)
100+
if vim.fn.stridx(path, path_parent) == 0 then
101+
return false
102+
end
103+
end
87104
end
88105

89106
return true
@@ -139,17 +156,18 @@ function M.prepare(git_status)
139156
end
140157

141158
for _, node in pairs(marks.get_marks()) do
142-
status.bookmarks[node.absolute_path] = true
159+
status.bookmarks[node.absolute_path] = node.type
143160
end
144161

145162
return status
146163
end
147164

148165
---Check if the given path should be filtered.
149166
---@param path string Absolute path
167+
---@param fs_stat uv.fs_stat.result|nil fs_stat of file
150168
---@param status table from prepare
151169
---@return boolean
152-
function M.should_filter(path, status)
170+
function M.should_filter(path, fs_stat, status)
153171
if not M.config.enable then
154172
return false
155173
end
@@ -159,7 +177,11 @@ function M.should_filter(path, status)
159177
return false
160178
end
161179

162-
return git(path, status.git_status) or buf(path, status.bufinfo) or dotfile(path) or custom(path) or bookmark(path, status.bookmarks)
180+
return git(path, status.git_status)
181+
or buf(path, status.bufinfo)
182+
or dotfile(path)
183+
or custom(path)
184+
or bookmark(path, fs_stat and fs_stat.type, status.bookmarks)
163185
end
164186

165187
function M.setup(opts)

lua/nvim-tree/explorer/node-builders.lua

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ local M = {}
66
---@param parent Node
77
---@param absolute_path string
88
---@param name string
9+
---@param fs_stat uv.fs_stat.result|nil
910
---@return Node
10-
function M.folder(parent, absolute_path, name)
11+
function M.folder(parent, absolute_path, name, fs_stat)
1112
local handle = vim.loop.fs_scandir(absolute_path)
1213
local has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil
1314

1415
local node = {
1516
type = "directory",
1617
absolute_path = absolute_path,
17-
fs_stat = vim.loop.fs_stat(absolute_path),
18+
fs_stat = fs_stat,
1819
group_next = nil, -- If node is grouped, this points to the next child dir/link node
1920
has_children = has_children,
2021
name = name,
@@ -43,16 +44,17 @@ end
4344
---@param parent Node
4445
---@param absolute_path string
4546
---@param name string
47+
---@param fs_stat uv.fs_stat.result|nil
4648
---@return Node
47-
function M.file(parent, absolute_path, name)
49+
function M.file(parent, absolute_path, name, fs_stat)
4850
local ext = string.match(name, ".?[^.]+%.(.*)") or ""
4951

5052
return {
5153
type = "file",
5254
absolute_path = absolute_path,
5355
executable = M.is_executable(absolute_path),
5456
extension = ext,
55-
fs_stat = vim.loop.fs_stat(absolute_path),
57+
fs_stat = fs_stat,
5658
name = name,
5759
parent = parent,
5860
}
@@ -66,8 +68,9 @@ end
6668
---@param parent Node
6769
---@param absolute_path string
6870
---@param name string
71+
---@param fs_stat uv.fs_stat.result|nil
6972
---@return Node
70-
function M.link(parent, absolute_path, name)
73+
function M.link(parent, absolute_path, name, fs_stat)
7174
--- I dont know if this is needed, because in my understanding, there isn't hard links in windows, but just to be sure i changed it.
7275
local link_to = vim.loop.fs_realpath(absolute_path)
7376
local open, nodes, has_children
@@ -84,7 +87,7 @@ function M.link(parent, absolute_path, name)
8487
local node = {
8588
type = "link",
8689
absolute_path = absolute_path,
87-
fs_stat = vim.loop.fs_stat(absolute_path),
90+
fs_stat = fs_stat,
8891
group_next = nil, -- If node is grouped, this points to the next child dir/link node
8992
has_children = has_children,
9093
link_to = link_to,

lua/nvim-tree/explorer/reload.lua

+20-27
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,23 @@ function M.reload(node, git_status)
8585
node.group_next = nil
8686
end
8787

88-
local child_names = {}
88+
local remain_childs = {}
8989

9090
local node_ignored = explorer_node.is_git_ignored(node)
91+
---@type table<string, Node>
9192
local nodes_by_path = utils.key_by(node.nodes, "absolute_path")
9293
while true do
9394
local name, t = vim.loop.fs_scandir_next(handle, cwd)
9495
if not name then
9596
break
9697
end
9798

98-
local stat
99-
local function fs_stat_cached(path)
100-
if stat ~= nil then
101-
return stat
102-
end
103-
104-
stat = vim.loop.fs_stat(path)
105-
return stat
106-
end
107-
10899
local abs = utils.path_join { cwd, name }
109-
t = t or (fs_stat_cached(abs) or {}).type
110-
if not filters.should_filter(abs, filter_status) then
111-
child_names[abs] = true
100+
---@type uv.fs_stat.result|nil
101+
local stat = vim.loop.fs_stat(abs)
102+
103+
if not filters.should_filter(abs, stat, filter_status) then
104+
remain_childs[abs] = true
112105

113106
-- Recreate node if type changes.
114107
if nodes_by_path[abs] then
@@ -122,26 +115,26 @@ function M.reload(node, git_status)
122115
end
123116

124117
if not nodes_by_path[abs] then
118+
local new_child = nil
125119
if t == "directory" and vim.loop.fs_access(abs, "R") and Watcher.is_fs_event_capable(abs) then
126-
local folder = builders.folder(node, abs, name)
127-
nodes_by_path[abs] = folder
128-
table.insert(node.nodes, folder)
120+
new_child = builders.folder(node, abs, name, stat)
129121
elseif t == "file" then
130-
local file = builders.file(node, abs, name)
131-
nodes_by_path[abs] = file
132-
table.insert(node.nodes, file)
122+
new_child = builders.file(node, abs, name, stat)
133123
elseif t == "link" then
134-
local link = builders.link(node, abs, name)
124+
local link = builders.link(node, abs, name, stat)
135125
if link.link_to ~= nil then
136-
nodes_by_path[abs] = link
137-
table.insert(node.nodes, link)
126+
new_child = link
138127
end
139128
end
129+
if new_child then
130+
table.insert(node.nodes, new_child)
131+
nodes_by_path[abs] = new_child
132+
end
140133
else
141134
local n = nodes_by_path[abs]
142135
if n then
143-
n.executable = builders.is_executable(abs)
144-
n.fs_stat = fs_stat_cached(abs)
136+
n.executable = builders.is_executable(abs) or false
137+
n.fs_stat = stat
145138
end
146139
end
147140
end
@@ -150,8 +143,8 @@ function M.reload(node, git_status)
150143
node.nodes = vim.tbl_map(
151144
update_status(nodes_by_path, node_ignored, git_status),
152145
vim.tbl_filter(function(n)
153-
if child_names[n.absolute_path] then
154-
return child_names[n.absolute_path]
146+
if remain_childs[n.absolute_path] then
147+
return remain_childs[n.absolute_path]
155148
else
156149
explorer_node.node_destroy(n)
157150
return false

lua/nvim-tree/lib.lua

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ local function clone_node(node)
5757
name = node.name,
5858
open = node.open,
5959
type = node.type,
60+
fs_stat = node.fs_stat,
6061
}
6162

6263
if type(node.nodes) == "table" then

lua/nvim-tree/node.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
---@class BaseNode
77
---@field absolute_path string
88
---@field executable boolean
9-
---@field fs_stat uv.uv_fs_t
9+
---@field fs_stat uv.fs_stat.result|nil
1010
---@field git_status GitStatus|nil
1111
---@field hidden boolean
1212
---@field name string

0 commit comments

Comments
 (0)