Skip to content

Commit 0b304b8

Browse files
committed
refactor(#2875): multi instance renderer
1 parent be8f0eb commit 0b304b8

File tree

17 files changed

+128
-66
lines changed

17 files changed

+128
-66
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

@@ -214,7 +221,7 @@ local function setup_autocommands(opts)
214221
return
215222
end
216223
if
217-
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
224+
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
218225
then
219226
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
220227
actions.reloaders.reload_explorer()
@@ -231,7 +238,7 @@ local function setup_autocommands(opts)
231238
return
232239
end
233240
if
234-
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
241+
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
235242
then
236243
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
237244
actions.reloaders.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
local reloaders = require "nvim-tree.actions.reloaders"
98

109
local find_file = require("nvim-tree.actions.finders.find-file").fn
@@ -194,23 +193,23 @@ function Clipboard:clear_clipboard()
194193
self.data[ACTION.copy] = {}
195194
self.data[ACTION.cut] = {}
196195
notify.info "Clipboard has been emptied."
197-
renderer.draw()
196+
self.explorer.renderer:draw()
198197
end
199198

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

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

216215
---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

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local git = require "nvim-tree.git"
22
local view = require "nvim-tree.view"
3-
local renderer = require "nvim-tree.renderer"
43
local core = require "nvim-tree.core"
54
local explorer_node = require "nvim-tree.explorer.node"
65
local Iterator = require "nvim-tree.iterators.node-iterator"
@@ -44,28 +43,30 @@ end
4443

4544
local event_running = false
4645
function M.reload_explorer()
47-
if event_running or not core.get_explorer() or vim.v.exiting ~= vim.NIL then
46+
local explorer = core.get_explorer()
47+
if event_running or not explorer or vim.v.exiting ~= vim.NIL then
4848
return
4949
end
5050
event_running = true
5151

5252
local projects = git.reload()
53-
refresh_nodes(core.get_explorer(), projects)
53+
refresh_nodes(explorer, projects)
5454
if view.is_visible() then
55-
renderer.draw()
55+
explorer.renderer:draw()
5656
end
5757
event_running = false
5858
end
5959

6060
function M.reload_git()
61-
if not core.get_explorer() or not git.config.git.enable or event_running then
61+
local explorer = core.get_explorer()
62+
if not explorer or not git.config.git.enable or event_running then
6263
return
6364
end
6465
event_running = true
6566

6667
local projects = git.reload()
67-
M.reload_node_status(core.get_explorer(), projects)
68-
renderer.draw()
68+
M.reload_node_status(explorer, projects)
69+
explorer.renderer:draw()
6970
event_running = false
7071
end
7172

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,26 @@ local NodeIterator = require "nvim-tree.iterators.node-iterator"
1010
local Watcher = require "nvim-tree.watcher"
1111

1212
local Filters = require "nvim-tree.explorer.filters"
13-
local Marks = {} -- circular dependencies
13+
---@type Marks
14+
local Marks -- circular dependencies
1415
local LiveFilter = require "nvim-tree.explorer.live-filter"
1516
local Sorters = require "nvim-tree.explorer.sorters"
16-
local Clipboard = {} -- circular dependencies
17+
---@type Clipboard
18+
local Clipboard -- circular dependencies
19+
---@type Renderer
20+
local Renderer -- circular dependencies
1721

1822
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
1923

2024
local config
2125

2226
---@class Explorer
27+
---@field opts table user options
2328
---@field absolute_path string
2429
---@field nodes Node[]
2530
---@field open boolean
31+
---@field watcher Watcher|nil
32+
---@field renderer Renderer
2633
---@field filters Filters
2734
---@field live_filter LiveFilter
2835
---@field sorters Sorter
@@ -47,17 +54,18 @@ function Explorer:new(path)
4754
return
4855
end
4956

50-
---@class Explorer
57+
---@type Explorer
5158
local o = setmetatable({
59+
opts = config,
5260
absolute_path = path,
5361
nodes = {},
5462
open = true,
5563
sorters = Sorters:new(config),
5664
}, Explorer)
57-
setmetatable(o, self)
58-
self.__index = self
65+
setmetatable(o, { __index = self })
5966

6067
o.watcher = watch.create_watcher(o)
68+
o.renderer = Renderer:new(o.opts, o)
6169
o.filters = Filters:new(config, o)
6270
o.live_filter = LiveFilter:new(config, o)
6371
o.marks = Marks:new(config, o)
@@ -275,6 +283,7 @@ function Explorer.setup(opts)
275283

276284
Marks = require "nvim-tree.marks"
277285
Clipboard = require "nvim-tree.actions.fs.clipboard"
286+
Renderer = require "nvim-tree.renderer"
278287
end
279288

280289
---@private

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)