Skip to content

Commit 4e396b2

Browse files
authored
refactor(#2830): multi instance nvim-tree.marks (#2838)
refactor(#2380): multi instance nvim-tree.marks
1 parent 48a9290 commit 4e396b2

File tree

9 files changed

+135
-66
lines changed

9 files changed

+135
-66
lines changed

lua/nvim-tree/api.lua

+39-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
local lib = require "nvim-tree.lib"
2+
local core = require "nvim-tree.core"
23
local view = require "nvim-tree.view"
34
local utils = require "nvim-tree.utils"
45
local actions = require "nvim-tree.actions"
56
local appearance_diagnostics = require "nvim-tree.appearance.diagnostics"
67
local events = require "nvim-tree.events"
78
local help = require "nvim-tree.help"
89
local live_filter = require "nvim-tree.live-filter"
9-
local marks = require "nvim-tree.marks"
1010
local marks_navigation = require "nvim-tree.marks.navigation"
1111
local marks_bulk_delete = require "nvim-tree.marks.bulk-delete"
1212
local marks_bulk_trash = require "nvim-tree.marks.bulk-trash"
@@ -43,9 +43,10 @@ local Api = {
4343
diagnostics = {},
4444
}
4545

46-
--- Do nothing when setup not called.
46+
--- Print error when setup not called.
4747
--- f function to invoke
4848
---@param f function
49+
---@return fun(...) : any
4950
local function wrap(f)
5051
return function(...)
5152
if vim.g.NvimTreeSetup == 1 then
@@ -56,13 +57,13 @@ local function wrap(f)
5657
end
5758
end
5859

59-
---Inject the node as the first argument if absent.
60+
---Inject the node as the first argument if present otherwise do nothing.
6061
---@param fn function function to invoke
6162
local function wrap_node(fn)
6263
return function(node, ...)
6364
node = node or lib.get_node_at_cursor()
6465
if node then
65-
fn(node, ...)
66+
return fn(node, ...)
6667
end
6768
end
6869
end
@@ -72,10 +73,36 @@ end
7273
local function wrap_node_or_nil(fn)
7374
return function(node, ...)
7475
node = node or lib.get_node_at_cursor()
75-
fn(node, ...)
76+
return fn(node, ...)
7677
end
7778
end
7879

80+
---Inject the explorer as the first argument if present otherwise do nothing.
81+
---@param fn function function to invoke
82+
---@return fun(...) : any
83+
local function wrap_explorer(fn)
84+
return function(...)
85+
local explorer = core.get_explorer()
86+
if explorer then
87+
return fn(explorer, ...)
88+
end
89+
end
90+
end
91+
92+
---Invoke a member's method on the singleton explorer.
93+
---Print error when setup not called.
94+
---@param explorer_member string explorer member name
95+
---@param member_method string method name to invoke on member
96+
---@return fun(...) : any
97+
local function wrap_explorer_member(explorer_member, member_method)
98+
return wrap(function(...)
99+
local explorer = core.get_explorer()
100+
if explorer then
101+
return explorer[explorer_member][member_method](explorer[explorer_member], ...)
102+
end
103+
end)
104+
end
105+
79106
---@class ApiTreeOpenOpts
80107
---@field path string|nil path
81108
---@field current_window boolean|nil default false
@@ -241,13 +268,13 @@ Api.events.Event = events.Event
241268
Api.live_filter.start = wrap(live_filter.start_filtering)
242269
Api.live_filter.clear = wrap(live_filter.clear_filter)
243270

244-
Api.marks.get = wrap_node(marks.get_mark)
245-
Api.marks.list = wrap(marks.get_marks)
246-
Api.marks.toggle = wrap_node(marks.toggle_mark)
247-
Api.marks.clear = wrap(marks.clear_marks)
248-
Api.marks.bulk.delete = wrap(marks_bulk_delete.bulk_delete)
249-
Api.marks.bulk.trash = wrap(marks_bulk_trash.bulk_trash)
250-
Api.marks.bulk.move = wrap(marks_bulk_move.bulk_move)
271+
Api.marks.get = wrap_node(wrap_explorer_member("marks", "get_mark"))
272+
Api.marks.list = wrap_explorer_member("marks", "get_marks")
273+
Api.marks.toggle = wrap_node(wrap_explorer_member("marks", "toggle_mark"))
274+
Api.marks.clear = wrap_explorer_member("marks", "clear_marks")
275+
Api.marks.bulk.delete = wrap_explorer(marks_bulk_delete.bulk_delete)
276+
Api.marks.bulk.trash = wrap_explorer(marks_bulk_trash.bulk_trash)
277+
Api.marks.bulk.move = wrap_explorer(marks_bulk_move.bulk_move)
251278
Api.marks.navigate.next = wrap(marks_navigation.next)
252279
Api.marks.navigate.prev = wrap(marks_navigation.prev)
253280
Api.marks.navigate.select = wrap(marks_navigation.select)

lua/nvim-tree/explorer/filters.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local utils = require "nvim-tree.utils"
2-
local marks = require "nvim-tree.marks"
32

43
local M = {
54
ignore_list = {},
@@ -155,8 +154,11 @@ function M.prepare(git_status)
155154
status.bufinfo = vim.fn.getbufinfo { buflisted = 1 }
156155
end
157156

158-
for _, node in pairs(marks.get_marks()) do
159-
status.bookmarks[node.absolute_path] = node.type
157+
local explorer = require("nvim-tree.core").get_explorer()
158+
if explorer then
159+
for _, node in pairs(explorer.marks:get_marks()) do
160+
status.bookmarks[node.absolute_path] = node.type
161+
end
160162
end
161163

162164
return status

lua/nvim-tree/explorer/init.lua

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local git = require "nvim-tree.git"
22
local notify = require "nvim-tree.notify"
33
local watch = require "nvim-tree.explorer.watch"
44
local explorer_node = require "nvim-tree.explorer.node"
5+
local Marks = require "nvim-tree.marks"
56

67
local M = {}
78

@@ -12,6 +13,7 @@ M.reload = require("nvim-tree.explorer.reload").reload
1213
---@field absolute_path string
1314
---@field nodes Node[]
1415
---@field open boolean
16+
---@field marks Marks
1517

1618
local Explorer = {}
1719
Explorer.__index = Explorer
@@ -36,6 +38,7 @@ function Explorer.new(path)
3638
absolute_path = path,
3739
nodes = {},
3840
open = true,
41+
marks = Marks:new(),
3942
}, Explorer)
4043
explorer.watcher = watch.create_watcher(explorer)
4144
explorer:_load(explorer)

lua/nvim-tree/marks/bulk-delete.lua

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local marks = require "nvim-tree.marks"
21
local utils = require "nvim-tree.utils"
32
local remove_file = require "nvim-tree.actions.fs.remove-file"
43
local notify = require "nvim-tree.notify"
@@ -10,21 +9,29 @@ local M = {
109

1110
--- Delete nodes; each removal will be optionally notified
1211
---@param nodes Node[]
13-
local function do_delete(nodes)
12+
---@param marks Marks
13+
local function do_delete(marks, nodes)
1414
for _, node in pairs(nodes) do
1515
remove_file.remove(node)
1616
end
1717

18-
marks.clear_marks()
18+
marks:clear_marks()
1919

2020
if not M.config.filesystem_watchers.enable then
2121
require("nvim-tree.actions.reloaders").reload_explorer()
2222
end
2323
end
2424

2525
--- Delete marked nodes, optionally prompting
26-
function M.bulk_delete()
27-
local nodes = marks.get_marks()
26+
---@param explorer Explorer
27+
function M.bulk_delete(explorer)
28+
if not explorer then
29+
return
30+
end
31+
32+
local marks = explorer.marks
33+
34+
local nodes = marks:get_marks()
2835
if not nodes or #nodes == 0 then
2936
notify.warn "No bookmarksed to delete."
3037
return
@@ -36,11 +43,11 @@ function M.bulk_delete()
3643
lib.prompt(prompt_input, prompt_select, { "", "y" }, { "No", "Yes" }, "nvimtree_bulk_delete", function(item_short)
3744
utils.clear_prompt()
3845
if item_short == "y" then
39-
do_delete(nodes)
46+
do_delete(marks, nodes)
4047
end
4148
end)
4249
else
43-
do_delete(nodes)
50+
do_delete(marks, nodes)
4451
end
4552
end
4653

lua/nvim-tree/marks/bulk-move.lua

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local marks = require "nvim-tree.marks"
21
local core = require "nvim-tree.core"
32
local utils = require "nvim-tree.utils"
43
local rename_file = require "nvim-tree.actions.fs.rename-file"
@@ -9,8 +8,14 @@ local M = {
98
config = {},
109
}
1110

12-
function M.bulk_move()
13-
if #marks.get_marks() == 0 then
11+
---@param explorer Explorer
12+
function M.bulk_move(explorer)
13+
if not explorer then
14+
return
15+
end
16+
local marks = explorer.marks
17+
18+
if #marks:get_marks() == 0 then
1419
notify.warn "No bookmarks to move."
1520
return
1621
end
@@ -40,14 +45,14 @@ function M.bulk_move()
4045
return
4146
end
4247

43-
local nodes = marks.get_marks()
48+
local nodes = marks:get_marks()
4449
for _, node in pairs(nodes) do
4550
local head = vim.fn.fnamemodify(node.absolute_path, ":t")
4651
local to = utils.path_join { location, head }
4752
rename_file.rename(node, to)
4853
end
4954

50-
marks.clear_marks()
55+
marks:clear_marks()
5156

5257
if not M.config.filesystem_watchers.enable then
5358
require("nvim-tree.actions.reloaders").reload_explorer()

lua/nvim-tree/marks/bulk-trash.lua

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local marks = require "nvim-tree.marks"
21
local utils = require "nvim-tree.utils"
32
local remove_file = require "nvim-tree.actions.fs.trash"
43
local notify = require "nvim-tree.notify"
@@ -14,12 +13,17 @@ local function do_trash(nodes)
1413
for _, node in pairs(nodes) do
1514
remove_file.remove(node)
1615
end
17-
18-
marks.clear_marks()
1916
end
2017

21-
function M.bulk_trash()
22-
local nodes = marks.get_marks()
18+
---@param explorer Explorer
19+
function M.bulk_trash(explorer)
20+
if not explorer then
21+
return
22+
end
23+
24+
local marks = explorer.marks
25+
26+
local nodes = marks:get_marks()
2327
if not nodes or #nodes == 0 then
2428
notify.warn "No bookmarks to trash."
2529
return
@@ -32,10 +36,12 @@ function M.bulk_trash()
3236
utils.clear_prompt()
3337
if item_short == "y" then
3438
do_trash(nodes)
39+
marks:clear_marks()
3540
end
3641
end)
3742
else
3843
do_trash(nodes)
44+
marks:clear_marks()
3945
end
4046
end
4147

lua/nvim-tree/marks/init.lua

+36-26
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,78 @@
11
local renderer = {} -- circular dependency
22

3-
local NvimTreeMarks = {}
3+
---@class Marks
4+
---@field private marks Node[]
5+
local Marks = {}
46

5-
local M = {}
7+
---@return Marks
8+
function Marks:new()
9+
local o = {}
10+
setmetatable(o, self)
11+
self.__index = self
612

7-
---@class MinimalNode
8-
---@field absolute_path string
13+
o.marks = {}
914

10-
---@param node Node|MinimalNode
11-
local function add_mark(node)
12-
NvimTreeMarks[node.absolute_path] = node
15+
return o
16+
end
17+
18+
---@private
19+
---@param node Node
20+
function Marks:add_mark(node)
21+
self.marks[node.absolute_path] = node
1322

1423
renderer.draw()
1524
end
1625

17-
---@param node Node|MinimalNode
18-
local function remove_mark(node)
19-
NvimTreeMarks[node.absolute_path] = nil
26+
---@private
27+
---@param node Node
28+
function Marks:remove_mark(node)
29+
self.marks[node.absolute_path] = nil
2030

2131
renderer.draw()
2232
end
2333

24-
---@param node Node|MinimalNode
25-
function M.toggle_mark(node)
34+
---@param node Node
35+
function Marks:toggle_mark(node)
2636
if node.absolute_path == nil then
2737
return
2838
end
2939

30-
if M.get_mark(node) then
31-
remove_mark(node)
40+
if self:get_mark(node) then
41+
self:remove_mark(node)
3242
else
33-
add_mark(node)
43+
self:add_mark(node)
3444
end
3545

3646
renderer.draw()
3747
end
3848

39-
function M.clear_marks()
40-
NvimTreeMarks = {}
49+
function Marks:clear_marks()
50+
self.marks = {}
4151

4252
renderer.draw()
4353
end
4454

45-
---@param node Node|MinimalNode
46-
---@return table|nil
47-
function M.get_mark(node)
48-
return node and NvimTreeMarks[node.absolute_path]
55+
---@param node Node
56+
---@return Node|nil
57+
function Marks:get_mark(node)
58+
return node and self.marks[node.absolute_path]
4959
end
5060

51-
---@return table
52-
function M.get_marks()
61+
---@return Node[]
62+
function Marks:get_marks()
5363
local list = {}
54-
for _, node in pairs(NvimTreeMarks) do
64+
for _, node in pairs(self.marks) do
5565
table.insert(list, node)
5666
end
5767
return list
5868
end
5969

60-
function M.setup(opts)
70+
function Marks.setup(opts)
6171
renderer = require "nvim-tree.renderer"
6272

6373
require("nvim-tree.marks.bulk-delete").setup(opts)
6474
require("nvim-tree.marks.bulk-trash").setup(opts)
6575
require("nvim-tree.marks.bulk-move").setup(opts)
6676
end
6777

68-
return M
78+
return Marks

0 commit comments

Comments
 (0)