Skip to content

refactor(#2882, #2883): multi instance explore, reloaders #2897

New issue

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

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

Already on GitHub? # to your account

Merged
merged 3 commits into from
Sep 14, 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
24 changes: 18 additions & 6 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufWritePost", {
callback = function()
if opts.auto_reload_on_write and not opts.filesystem_watchers.enable then
actions.reloaders.reload_explorer()
local explorer = core.get_explorer()
if explorer then
explorer:reload_explorer()
end
end
end,
})
Expand All @@ -217,7 +220,7 @@ local function setup_autocommands(opts)
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
then
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
actions.reloaders.reload_explorer()
explorer:reload_explorer()
end)
end
end,
Expand All @@ -234,7 +237,7 @@ local function setup_autocommands(opts)
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
then
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
actions.reloaders.reload_explorer()
explorer:reload_explorer()
end)
end
end,
Expand All @@ -244,7 +247,10 @@ local function setup_autocommands(opts)
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
callback = function()
if not opts.filesystem_watchers.enable and opts.git.enable then
actions.reloaders.reload_git()
local explorer = core.get_explorer()
if explorer then
explorer:reload_git()
end
end
end,
})
Expand Down Expand Up @@ -292,7 +298,10 @@ local function setup_autocommands(opts)
callback = function()
if utils.is_nvim_tree_buf(0) then
if vim.fn.getcwd() ~= core.get_cwd() or (opts.reload_on_bufenter and not opts.filesystem_watchers.enable) then
actions.reloaders.reload_explorer()
local explorer = core.get_explorer()
if explorer then
explorer:reload_explorer()
end
end
end
end,
Expand Down Expand Up @@ -343,7 +352,10 @@ local function setup_autocommands(opts)
callback = function()
utils.debounce("Buf:modified", opts.view.debounce_delay, function()
buffers.reload_modified()
actions.reloaders.reload_explorer()
local explorer = core.get_explorer()
if explorer then
explorer:reload_explorer()
end
end)
end,
})
Expand Down
3 changes: 1 addition & 2 deletions lua/nvim-tree/actions/fs/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 reloaders = require "nvim-tree.actions.reloaders"

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

Expand Down Expand Up @@ -248,7 +247,7 @@ function Clipboard:do_paste(node, action, action_fn)

self.data[action] = {}
if not self.config.filesystem_watchers.enable then
reloaders.reload_explorer()
self.explorer:reload_explorer()
end
end

Expand Down
6 changes: 4 additions & 2 deletions lua/nvim-tree/actions/fs/remove-file.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 events = require "nvim-tree.events"
local view = require "nvim-tree.view"
Expand Down Expand Up @@ -116,8 +117,9 @@ function M.fn(node)

local function do_remove()
M.remove(node)
if not M.config.filesystem_watchers.enable then
require("nvim-tree.actions.reloaders").reload_explorer()
local explorer = core.get_explorer()
if not M.config.filesystem_watchers.enable and explorer then
explorer:reload_explorer()
end
end

Expand Down
6 changes: 5 additions & 1 deletion lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local core = require "nvim-tree.core"
local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
Expand Down Expand Up @@ -155,7 +156,10 @@ function M.fn(default_modifier)

M.rename(node, prepend .. new_file_path .. append)
if not M.config.filesystem_watchers.enable then
require("nvim-tree.actions.reloaders").reload_explorer()
local explorer = core.get_explorer()
if explorer then
explorer:reload_explorer()
end
end

find_file(utils.path_remove_trailing(new_file_path))
Expand Down
12 changes: 7 additions & 5 deletions lua/nvim-tree/actions/fs/trash.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local core = require "nvim-tree.core"
local lib = require "nvim-tree.lib"
local notify = require "nvim-tree.notify"
local reloaders = require "nvim-tree.actions.reloaders"

local M = {
config = {},
Expand Down Expand Up @@ -52,15 +52,17 @@ function M.remove(node)
end
end

local explorer = core.get_explorer()

if node.nodes ~= nil and not node.link_to then
trash_path(function(_, rc)
if rc ~= 0 then
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
return
end
events._dispatch_folder_removed(node.absolute_path)
if not M.config.filesystem_watchers.enable then
reloaders.reload_explorer()
if not M.config.filesystem_watchers.enable and explorer then
explorer:reload_explorer()
end
end)
else
Expand All @@ -72,8 +74,8 @@ function M.remove(node)
end
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
if not M.config.filesystem_watchers.enable then
reloaders.reload_explorer()
if not M.config.filesystem_watchers.enable and explorer then
explorer:reload_explorer()
end
end)
end
Expand Down
1 change: 0 additions & 1 deletion lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ M.finders = require "nvim-tree.actions.finders"
M.fs = require "nvim-tree.actions.fs"
M.moves = require "nvim-tree.actions.moves"
M.node = require "nvim-tree.actions.node"
M.reloaders = require "nvim-tree.actions.reloaders"
M.root = require "nvim-tree.actions.root"
M.tree = require "nvim-tree.actions.tree"

Expand Down
72 changes: 0 additions & 72 deletions lua/nvim-tree/actions/reloaders.lua

This file was deleted.

27 changes: 17 additions & 10 deletions lua/nvim-tree/actions/tree/modifiers/toggles.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils"
local reloaders = require "nvim-tree.actions.reloaders"
local core = require "nvim-tree.core"
local M = {}

local function reload()
---@param explorer Explorer
local function reload(explorer)
local node = lib.get_node_at_cursor()
reloaders.reload_explorer()
explorer:reload_explorer()
utils.focus_node_or_parent(node)
end

Expand All @@ -19,39 +19,46 @@ local function wrap_explorer(fn)
end
end

---@param explorer Explorer
local function custom(explorer)
explorer.filters.config.filter_custom = not explorer.filters.config.filter_custom
reload()
reload(explorer)
end

---@param explorer Explorer
local function git_ignored(explorer)
explorer.filters.config.filter_git_ignored = not explorer.filters.config.filter_git_ignored
reload()
reload(explorer)
end

---@param explorer Explorer
local function git_clean(explorer)
explorer.filters.config.filter_git_clean = not explorer.filters.config.filter_git_clean
reload()
reload(explorer)
end

---@param explorer Explorer
local function no_buffer(explorer)
explorer.filters.config.filter_no_buffer = not explorer.filters.config.filter_no_buffer
reload()
reload(explorer)
end

---@param explorer Explorer
local function no_bookmark(explorer)
explorer.filters.config.filter_no_bookmark = not explorer.filters.config.filter_no_bookmark
reload()
reload(explorer)
end

---@param explorer Explorer
local function dotfiles(explorer)
explorer.filters.config.filter_dotfiles = not explorer.filters.config.filter_dotfiles
reload()
reload(explorer)
end

---@param explorer Explorer
local function enable(explorer)
explorer.filters.config.enable = not explorer.filters.config.enable
reload()
reload(explorer)
end

M.custom = wrap_explorer(custom)
Expand Down
17 changes: 15 additions & 2 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ local function wrap_node_or_nil(fn)
end
end

---Invoke a method on the singleton explorer.
---Print error when setup not called.
---@param explorer_method string explorer method name
---@return fun(...) : any
local function wrap_explorer(explorer_method)
return wrap(function(...)
local explorer = core.get_explorer()
if explorer then
return explorer[explorer_method](explorer, ...)
end
end)
end

---Invoke a member's method on the singleton explorer.
---Print error when setup not called.
---@param explorer_member string explorer member name
Expand Down Expand Up @@ -108,7 +121,7 @@ Api.tree.toggle = wrap(actions.tree.toggle.fn)
Api.tree.close = wrap(view.close)
Api.tree.close_in_this_tab = wrap(view.close_this_tab_only)
Api.tree.close_in_all_tabs = wrap(view.close_all_tabs)
Api.tree.reload = wrap(actions.reloaders.reload_explorer)
Api.tree.reload = wrap_explorer "reload_explorer"

---@class ApiTreeResizeOpts
---@field width string|function|number|table|nil
Expand Down Expand Up @@ -243,7 +256,7 @@ Api.node.navigate.diagnostics.prev_recursive = wrap_node(actions.moves.item.fn {
Api.node.navigate.opened.next = wrap_node(actions.moves.item.fn { where = "next", what = "opened" })
Api.node.navigate.opened.prev = wrap_node(actions.moves.item.fn { where = "prev", what = "opened" })

Api.git.reload = wrap(actions.reloaders.reload_git)
Api.git.reload = wrap_explorer "reload_git"

Api.events.subscribe = events.subscribe
Api.events.Event = events.Event
Expand Down
Loading
Loading