Skip to content

Commit fe1974c

Browse files
committed
refactor(#2875): multi instance renderer
1 parent 8f6dcc0 commit fe1974c

File tree

17 files changed

+192
-59
lines changed

17 files changed

+192
-59
lines changed

Diff for: lua/nvim-tree.lua

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local lib = require "nvim-tree.lib"
22
local log = require "nvim-tree.log"
33
local appearance = require "nvim-tree.appearance"
4-
local renderer = require "nvim-tree.renderer"
54
local view = require "nvim-tree.view"
65
local commands = require "nvim-tree.commands"
76
local utils = require "nvim-tree.utils"
@@ -97,7 +96,11 @@ function M.tab_enter()
9796
end
9897
end
9998
view.open { focus_tree = false }
100-
renderer.draw()
99+
100+
local explorer = core.get_explorer()
101+
if explorer then
102+
explorer.renderer:draw()
103+
end
101104
end
102105
end
103106

@@ -179,7 +182,11 @@ local function setup_autocommands(opts)
179182
callback = function()
180183
appearance.setup()
181184
view.reset_winhl()
182-
renderer.draw()
185+
186+
local explorer = core.get_explorer()
187+
if explorer then
188+
explorer.renderer:draw()
189+
end
183190
end,
184191
})
185192

@@ -217,7 +224,7 @@ local function setup_autocommands(opts)
217224
return
218225
end
219226
if
220-
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
227+
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
221228
then
222229
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
223230
explorer:reload_explorer()
@@ -234,7 +241,7 @@ local function setup_autocommands(opts)
234241
return
235242
end
236243
if
237-
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
244+
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
238245
then
239246
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
240247
explorer:reload_explorer()

Diff for: lua/nvim-tree/actions/finders/find-file.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local log = require "nvim-tree.log"
22
local view = require "nvim-tree.view"
33
local utils = require "nvim-tree.utils"
4-
local renderer = require "nvim-tree.renderer"
54
local core = require "nvim-tree.core"
65
local Iterator = require "nvim-tree.iterators.node-iterator"
76

@@ -76,7 +75,7 @@ function M.fn(path)
7675
:iterate()
7776

7877
if found and view.is_visible() then
79-
renderer.draw()
78+
explorer.renderer:draw()
8079
view.set_cursor { line, 0 }
8180
end
8281

Diff for: lua/nvim-tree/actions/fs/clipboard.lua

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ local utils = require "nvim-tree.utils"
44
local core = require "nvim-tree.core"
55
local events = require "nvim-tree.events"
66
local notify = require "nvim-tree.notify"
7-
local renderer = require "nvim-tree.renderer"
87

98
local find_file = require("nvim-tree.actions.finders.find-file").fn
109

@@ -193,23 +192,23 @@ function Clipboard:clear_clipboard()
193192
self.data[ACTION.copy] = {}
194193
self.data[ACTION.cut] = {}
195194
notify.info "Clipboard has been emptied."
196-
renderer.draw()
195+
self.explorer.renderer:draw()
197196
end
198197

199198
---Copy one node
200199
---@param node Node
201200
function Clipboard:copy(node)
202201
utils.array_remove(self.data[ACTION.cut], node)
203202
toggle(node, self.data[ACTION.copy])
204-
renderer.draw()
203+
self.explorer.renderer:draw()
205204
end
206205

207206
---Cut one node
208207
---@param node Node
209208
function Clipboard:cut(node)
210209
utils.array_remove(self.data[ACTION.copy], node)
211210
toggle(node, self.data[ACTION.cut])
212-
renderer.draw()
211+
self.explorer.renderer:draw()
213212
end
214213

215214
---Paste cut or cop

Diff for: lua/nvim-tree/actions/moves/parent.lua

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local renderer = require "nvim-tree.renderer"
21
local view = require "nvim-tree.view"
32
local utils = require "nvim-tree.utils"
43
local core = require "nvim-tree.core"
@@ -11,11 +10,16 @@ local M = {}
1110
function M.fn(should_close)
1211
should_close = should_close or false
1312

13+
local explorer = core.get_explorer()
14+
1415
return function(node)
1516
node = lib.get_last_group_node(node)
1617
if should_close and node.open then
1718
node.open = false
18-
return renderer.draw()
19+
if explorer then
20+
explorer.renderer:draw()
21+
end
22+
return
1923
end
2024

2125
local parent = utils.get_parent_of_group(node).parent
@@ -31,7 +35,9 @@ function M.fn(should_close)
3135
view.set_cursor { line + 1, 0 }
3236
if should_close then
3337
parent.open = false
34-
renderer.draw()
38+
if explorer then
39+
explorer.renderer:draw()
40+
end
3541
end
3642
end
3743
end

Diff for: lua/nvim-tree/actions/reloaders.lua

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
local git = require "nvim-tree.git"
2+
local view = require "nvim-tree.view"
3+
local core = require "nvim-tree.core"
4+
local explorer_node = require "nvim-tree.explorer.node"
5+
local Iterator = require "nvim-tree.iterators.node-iterator"
6+
7+
local M = {}
8+
9+
---@param explorer Explorer|nil
10+
---@param projects table
11+
local function refresh_nodes(explorer, projects)
12+
Iterator.builder({ explorer })
13+
:applier(function(n)
14+
if n.nodes then
15+
local toplevel = git.get_toplevel(n.cwd or n.link_to or n.absolute_path)
16+
if explorer then
17+
explorer:reload(n, projects[toplevel] or {})
18+
end
19+
end
20+
end)
21+
:recursor(function(n)
22+
return n.group_next and { n.group_next } or (n.open and n.nodes)
23+
end)
24+
:iterate()
25+
end
26+
27+
---@param parent_node Node|nil
28+
---@param projects table
29+
function M.reload_node_status(parent_node, projects)
30+
if parent_node == nil then
31+
return
32+
end
33+
34+
local toplevel = git.get_toplevel(parent_node.absolute_path)
35+
local status = projects[toplevel] or {}
36+
for _, node in ipairs(parent_node.nodes) do
37+
explorer_node.update_git_status(node, explorer_node.is_git_ignored(parent_node), status)
38+
if node.nodes and #node.nodes > 0 then
39+
M.reload_node_status(node, projects)
40+
end
41+
end
42+
end
43+
44+
local event_running = false
45+
function M.reload_explorer()
46+
local explorer = core.get_explorer()
47+
if event_running or not explorer or vim.v.exiting ~= vim.NIL then
48+
return
49+
end
50+
event_running = true
51+
52+
local projects = git.reload()
53+
refresh_nodes(explorer, projects)
54+
if view.is_visible() then
55+
explorer.renderer:draw()
56+
end
57+
event_running = false
58+
end
59+
60+
function M.reload_git()
61+
local explorer = core.get_explorer()
62+
if not explorer or not git.config.git.enable or event_running then
63+
return
64+
end
65+
event_running = true
66+
67+
local projects = git.reload()
68+
M.reload_node_status(explorer, projects)
69+
explorer.renderer:draw()
70+
event_running = false
71+
end
72+
73+
return M

Diff for: lua/nvim-tree/actions/root/change-dir.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ M.force_dirchange = add_profiling_to(function(foldername, should_open_view)
9191
if should_open_view then
9292
require("nvim-tree.lib").open()
9393
else
94-
require("nvim-tree.renderer").draw()
94+
local explorer = core.get_explorer()
95+
if explorer then
96+
explorer.renderer:draw()
97+
end
9598
end
9699
end)
97100

Diff for: lua/nvim-tree/actions/tree/modifiers/collapse-all.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local renderer = require "nvim-tree.renderer"
21
local utils = require "nvim-tree.utils"
32
local core = require "nvim-tree.core"
43
local lib = require "nvim-tree.lib"
@@ -46,7 +45,7 @@ function M.fn(keep_buffers)
4645
end)
4746
:iterate()
4847

49-
renderer.draw()
48+
explorer.renderer:draw()
5049
utils.focus_node_or_parent(node)
5150
end
5251

Diff for: lua/nvim-tree/actions/tree/modifiers/expand-all.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local core = require "nvim-tree.core"
2-
local renderer = require "nvim-tree.renderer"
32
local Iterator = require "nvim-tree.iterators.node-iterator"
43
local notify = require "nvim-tree.notify"
54
local lib = require "nvim-tree.lib"
@@ -65,11 +64,14 @@ end
6564

6665
---@param base_node table
6766
function M.fn(base_node)
68-
local node = base_node.nodes and base_node or core.get_explorer()
67+
local explorer = core.get_explorer()
68+
local node = base_node.nodes and base_node or explorer
6969
if gen_iterator()(node) then
7070
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
7171
end
72-
renderer.draw()
72+
if explorer then
73+
explorer.renderer:draw()
74+
end
7375
end
7476

7577
function M.setup(opts)

Diff for: lua/nvim-tree/diagnostics.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local core = require "nvim-tree.core"
12
local utils = require "nvim-tree.utils"
23
local view = require "nvim-tree.view"
34
local log = require "nvim-tree.log"
@@ -165,7 +166,10 @@ function M.update()
165166
end
166167
log.profile_end(profile)
167168
if view.is_buf_valid(view.get_bufnr()) then
168-
require("nvim-tree.renderer").draw()
169+
local explorer = core.get_explorer()
170+
if explorer then
171+
explorer.renderer:draw()
172+
end
169173
end
170174
end)
171175
end

Diff for: lua/nvim-tree/explorer/init.lua

+13-5
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,26 @@ local NodeIterator = require "nvim-tree.iterators.node-iterator"
1313
local Watcher = require "nvim-tree.watcher"
1414

1515
local Filters = require "nvim-tree.explorer.filters"
16-
local Marks = {} -- circular dependencies
16+
---@type Marks
17+
local Marks -- circular dependencies
1718
local LiveFilter = require "nvim-tree.explorer.live-filter"
1819
local Sorters = require "nvim-tree.explorer.sorters"
19-
local Clipboard = {} -- circular dependencies
20+
---@type Clipboard
21+
local Clipboard -- circular dependencies
22+
---@type Renderer
23+
local Renderer -- circular dependencies
2024

2125
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
2226

2327
local config
2428

2529
---@class Explorer
30+
---@field opts table user options
2631
---@field absolute_path string
2732
---@field nodes Node[]
2833
---@field open boolean
34+
---@field watcher Watcher|nil
35+
---@field renderer Renderer
2936
---@field filters Filters
3037
---@field live_filter LiveFilter
3138
---@field sorters Sorter
@@ -48,17 +55,18 @@ function Explorer:new(path)
4855
return
4956
end
5057

51-
---@class Explorer
58+
---@type Explorer
5259
local o = setmetatable({
60+
opts = config,
5361
absolute_path = path,
5462
nodes = {},
5563
open = true,
5664
sorters = Sorters:new(config),
5765
}, Explorer)
58-
setmetatable(o, self)
59-
self.__index = self
66+
setmetatable(o, { __index = self })
6067

6168
o.watcher = watch.create_watcher(o)
69+
o.renderer = Renderer:new(o.opts, o)
6270
o.filters = Filters:new(config, o)
6371
o.live_filter = LiveFilter:new(config, o)
6472
o.marks = Marks:new(config, o)

Diff for: lua/nvim-tree/explorer/live-filter.lua

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ function LiveFilter:new(opts, explorer)
2323
return o
2424
end
2525

26-
local function redraw()
27-
require("nvim-tree.renderer").draw()
28-
end
29-
3026
---@param node_ Node|nil
3127
local function reset_filter(self, node_)
3228
node_ = node_ or self.explorer
@@ -129,7 +125,7 @@ local function record_char(self)
129125
vim.schedule(function()
130126
self.filter = vim.api.nvim_buf_get_lines(overlay_bufnr, 0, -1, false)[1]
131127
self:apply_filter()
132-
redraw()
128+
self.explorer.renderer:draw()
133129
end)
134130
end
135131

@@ -199,7 +195,7 @@ function LiveFilter:start_filtering()
199195
view.View.live_filter.prev_focused_node = require("nvim-tree.lib").get_node_at_cursor()
200196
self.filter = self.filter or ""
201197

202-
redraw()
198+
self.explorer.renderer:draw()
203199
local row = require("nvim-tree.core").get_nodes_starting_line() - 1
204200
local col = #self.prefix > 0 and #self.prefix - 1 or 1
205201
view.set_cursor { row, col }
@@ -215,7 +211,7 @@ function LiveFilter:clear_filter()
215211

216212
self.filter = nil
217213
reset_filter(self)
218-
redraw()
214+
self.explorer.renderer:draw()
219215

220216
if node then
221217
utils.focus_file(node.absolute_path)

Diff for: lua/nvim-tree/explorer/watch.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function M.create_watcher(node)
7979
local explorer = require("nvim-tree.core").get_explorer()
8080
if explorer then
8181
explorer:refresh_node(node, function()
82-
require("nvim-tree.renderer").draw()
82+
explorer.renderer:draw()
8383
end)
8484
end
8585
end)

Diff for: lua/nvim-tree/git/init.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ local function reload_tree_at(toplevel)
216216
end)
217217
:iterate()
218218

219-
require("nvim-tree.renderer").draw()
219+
local explorer = require("nvim-tree.core").get_explorer()
220+
if explorer then
221+
explorer.renderer:draw()
222+
end
220223
end)
221224
end
222225

0 commit comments

Comments
 (0)