Skip to content

refactor(#2875): multi instance renderer #2900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
local lib = require "nvim-tree.lib"
local log = require "nvim-tree.log"
local appearance = require "nvim-tree.appearance"
local renderer = require "nvim-tree.renderer"
local view = require "nvim-tree.view"
local commands = require "nvim-tree.commands"
local utils = require "nvim-tree.utils"
local actions = require "nvim-tree.actions"
local legacy = require "nvim-tree.legacy"
local core = require "nvim-tree.core"
local git = require "nvim-tree.git"
local buffers = require "nvim-tree.buffers"
local notify = require "nvim-tree.notify"

local _config = {}
Expand Down Expand Up @@ -97,7 +92,11 @@ function M.tab_enter()
end
end
view.open { focus_tree = false }
renderer.draw()

local explorer = core.get_explorer()
if explorer then
explorer.renderer:draw()
end
end
end

Expand Down Expand Up @@ -179,7 +178,11 @@ local function setup_autocommands(opts)
callback = function()
appearance.setup()
view.reset_winhl()
renderer.draw()

local explorer = core.get_explorer()
if explorer then
explorer.renderer:draw()
end
end,
})

Expand Down Expand Up @@ -217,7 +220,7 @@ local function setup_autocommands(opts)
return
end
if
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
then
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
explorer:reload_explorer()
Expand All @@ -234,7 +237,7 @@ local function setup_autocommands(opts)
return
end
if
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
(explorer.filters.config.filter_no_buffer or explorer.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
then
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
explorer:reload_explorer()
Expand Down Expand Up @@ -351,7 +354,7 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd({ "BufModifiedSet", "BufWritePost" }, {
callback = function()
utils.debounce("Buf:modified", opts.view.debounce_delay, function()
buffers.reload_modified()
require("nvim-tree.buffers").reload_modified()
local explorer = core.get_explorer()
if explorer then
explorer:reload_explorer()
Expand Down Expand Up @@ -801,7 +804,7 @@ function M.purge_all_state()
view.close_all_tabs()
view.abandon_all_windows()
if core.get_explorer() ~= nil then
git.purge_state()
require("nvim-tree.git").purge_state()
core.reset_explorer()
end
end
Expand All @@ -817,7 +820,7 @@ function M.setup(conf)

localise_default_opts()

legacy.migrate_legacy_options(conf or {})
require("nvim-tree.legacy").migrate_legacy_options(conf or {})

validate_options(conf)

Expand Down Expand Up @@ -851,7 +854,7 @@ function M.setup(conf)
require("nvim-tree.git.utils").setup(opts)
require("nvim-tree.view").setup(opts)
require("nvim-tree.lib").setup(opts)
require("nvim-tree.renderer").setup(opts)
require("nvim-tree.renderer.components").setup(opts)
require("nvim-tree.buffers").setup(opts)
require("nvim-tree.help").setup(opts)
require("nvim-tree.watcher").setup(opts)
Expand All @@ -863,7 +866,7 @@ function M.setup(conf)

if vim.g.NvimTreeSetup ~= 1 then
-- first call to setup
commands.setup()
require("nvim-tree.commands").setup()
else
-- subsequent calls to setup
M.purge_all_state()
Expand Down
3 changes: 1 addition & 2 deletions lua/nvim-tree/actions/finders/find-file.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local log = require "nvim-tree.log"
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local renderer = require "nvim-tree.renderer"
local core = require "nvim-tree.core"
local Iterator = require "nvim-tree.iterators.node-iterator"

Expand Down Expand Up @@ -76,7 +75,7 @@ function M.fn(path)
:iterate()

if found and view.is_visible() then
renderer.draw()
explorer.renderer:draw()
view.set_cursor { line, 0 }
end

Expand Down
7 changes: 3 additions & 4 deletions lua/nvim-tree/actions/fs/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local events = require "nvim-tree.events"
local notify = require "nvim-tree.notify"
local renderer = require "nvim-tree.renderer"

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

Expand Down Expand Up @@ -193,23 +192,23 @@ function Clipboard:clear_clipboard()
self.data[ACTION.copy] = {}
self.data[ACTION.cut] = {}
notify.info "Clipboard has been emptied."
renderer.draw()
self.explorer.renderer:draw()
end

---Copy one node
---@param node Node
function Clipboard:copy(node)
utils.array_remove(self.data[ACTION.cut], node)
toggle(node, self.data[ACTION.copy])
renderer.draw()
self.explorer.renderer:draw()
end

---Cut one node
---@param node Node
function Clipboard:cut(node)
utils.array_remove(self.data[ACTION.copy], node)
toggle(node, self.data[ACTION.cut])
renderer.draw()
self.explorer.renderer:draw()
end

---Paste cut or cop
Expand Down
12 changes: 9 additions & 3 deletions lua/nvim-tree/actions/moves/parent.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local renderer = require "nvim-tree.renderer"
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
Expand All @@ -11,11 +10,16 @@ local M = {}
function M.fn(should_close)
should_close = should_close or false

local explorer = core.get_explorer()

return function(node)
node = lib.get_last_group_node(node)
if should_close and node.open then
node.open = false
return renderer.draw()
if explorer then
explorer.renderer:draw()
end
return
end

local parent = utils.get_parent_of_group(node).parent
Expand All @@ -31,7 +35,9 @@ function M.fn(should_close)
view.set_cursor { line + 1, 0 }
if should_close then
parent.open = false
renderer.draw()
if explorer then
explorer.renderer:draw()
end
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lua/nvim-tree/actions/root/change-dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ M.force_dirchange = add_profiling_to(function(foldername, should_open_view)
if should_open_view then
require("nvim-tree.lib").open()
else
require("nvim-tree.renderer").draw()
local explorer = core.get_explorer()
if explorer then
explorer.renderer:draw()
end
end
end)

Expand Down
3 changes: 1 addition & 2 deletions lua/nvim-tree/actions/tree/modifiers/collapse-all.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local renderer = require "nvim-tree.renderer"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local lib = require "nvim-tree.lib"
Expand Down Expand Up @@ -46,7 +45,7 @@ function M.fn(keep_buffers)
end)
:iterate()

renderer.draw()
explorer.renderer:draw()
utils.focus_node_or_parent(node)
end

Expand Down
8 changes: 5 additions & 3 deletions lua/nvim-tree/actions/tree/modifiers/expand-all.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local core = require "nvim-tree.core"
local renderer = require "nvim-tree.renderer"
local Iterator = require "nvim-tree.iterators.node-iterator"
local notify = require "nvim-tree.notify"
local lib = require "nvim-tree.lib"
Expand Down Expand Up @@ -65,11 +64,14 @@ end

---@param base_node table
function M.fn(base_node)
local node = base_node.nodes and base_node or core.get_explorer()
local explorer = core.get_explorer()
local node = base_node.nodes and base_node or explorer
if gen_iterator()(node) then
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
end
renderer.draw()
if explorer then
explorer.renderer:draw()
end
end

function M.setup(opts)
Expand Down
3 changes: 1 addition & 2 deletions lua/nvim-tree/core.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local events = require "nvim-tree.events"
local explorer = require "nvim-tree.explorer"
local view = require "nvim-tree.view"
local log = require "nvim-tree.log"

Expand All @@ -16,7 +15,7 @@ function M.init(foldername)
if TreeExplorer then
TreeExplorer:destroy()
end
TreeExplorer = explorer:new(foldername)
TreeExplorer = require("nvim-tree.explorer"):new(foldername)
if not first_init_done then
events._dispatch_ready()
first_init_done = true
Expand Down
6 changes: 5 additions & 1 deletion lua/nvim-tree/diagnostics.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local core = require "nvim-tree.core"
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local log = require "nvim-tree.log"
Expand Down Expand Up @@ -165,7 +166,10 @@ function M.update()
end
log.profile_end(profile)
if view.is_buf_valid(view.get_bufnr()) then
require("nvim-tree.renderer").draw()
local explorer = core.get_explorer()
if explorer then
explorer.renderer:draw()
end
end
end)
end
Expand Down
26 changes: 13 additions & 13 deletions lua/nvim-tree/explorer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local builders = require "nvim-tree.explorer.node-builders"
local git = require "nvim-tree.git"
local log = require "nvim-tree.log"
local notify = require "nvim-tree.notify"
local renderer = {} -- circular dependency, will become a member
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local watch = require "nvim-tree.explorer.watch"
Expand All @@ -13,19 +12,23 @@ local NodeIterator = require "nvim-tree.iterators.node-iterator"
local Watcher = require "nvim-tree.watcher"

local Filters = require "nvim-tree.explorer.filters"
local Marks = {} -- circular dependencies
local Marks = require "nvim-tree.marks"
local LiveFilter = require "nvim-tree.explorer.live-filter"
local Sorters = require "nvim-tree.explorer.sorters"
local Clipboard = {} -- circular dependencies
local Clipboard = require "nvim-tree.actions.fs.clipboard"
local Renderer = require "nvim-tree.renderer"

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

local config

---@class Explorer
---@field opts table user options
---@field absolute_path string
---@field nodes Node[]
---@field open boolean
---@field watcher Watcher|nil
---@field renderer Renderer
---@field filters Filters
---@field live_filter LiveFilter
---@field sorters Sorter
Expand All @@ -48,17 +51,19 @@ function Explorer:new(path)
return
end

---@class Explorer
local o = setmetatable({
local o = {
opts = config,
absolute_path = path,
nodes = {},
open = true,
sorters = Sorters:new(config),
}, Explorer)
}

setmetatable(o, self)
self.__index = self

o.watcher = watch.create_watcher(o)
o.renderer = Renderer:new(config, o)
o.filters = Filters:new(config, o)
o.live_filter = LiveFilter:new(config, o)
o.marks = Marks:new(config, o)
Expand Down Expand Up @@ -454,7 +459,7 @@ function Explorer:reload_explorer()
local projects = git.reload()
self:refresh_nodes(projects)
if view.is_visible() then
renderer.draw()
self.renderer:draw()
end
event_running = false
end
Expand All @@ -467,19 +472,14 @@ function Explorer:reload_git()

local projects = git.reload()
explorer_node.reload_node_status(self, projects)
renderer.draw()
self.renderer:draw()
event_running = false
end

function Explorer.setup(opts)
config = opts
require("nvim-tree.explorer.node").setup(opts)
require("nvim-tree.explorer.watch").setup(opts)

renderer = require "nvim-tree.renderer"

Marks = require "nvim-tree.marks"
Clipboard = require "nvim-tree.actions.fs.clipboard"
end

return Explorer
10 changes: 3 additions & 7 deletions lua/nvim-tree/explorer/live-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ function LiveFilter:new(opts, explorer)
return o
end

local function redraw()
require("nvim-tree.renderer").draw()
end

---@param node_ Node|nil
local function reset_filter(self, node_)
node_ = node_ or self.explorer
Expand Down Expand Up @@ -129,7 +125,7 @@ local function record_char(self)
vim.schedule(function()
self.filter = vim.api.nvim_buf_get_lines(overlay_bufnr, 0, -1, false)[1]
self:apply_filter()
redraw()
self.explorer.renderer:draw()
end)
end

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

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

self.filter = nil
reset_filter(self)
redraw()
self.explorer.renderer:draw()

if node then
utils.focus_file(node.absolute_path)
Expand Down
Loading
Loading