Skip to content

Commit

Permalink
refactor(#2941): move lib methods to explorer (#2964)
Browse files Browse the repository at this point in the history
* add todo

* refactor(#2886): multi instance: node class refactoring: extract links, *_git_status (#2944)

* extract DirectoryLinkNode and FileLinkNode, move Node methods to children

* temporarily move DirectoryNode methods into BaseNode for easier reviewing

* move mostly unchanged DirectoryNode methods back to BaseNode

* tidy

* git.git_status_file takes an array

* update git status of links

* luacheck hack

* safer git_status_dir

* refactor(#2886): multi instance: node class refactoring: DirectoryNode:expand_or_collapse (#2957)

move expand_or_collapse to DirectoryNode

* refactor(#2886): multi instance: node group functions refactoring (#2959)

* move last_group_node to DirectoryNode

* move add BaseNode:as and more doc

* revert parameter name changes

* revert parameter name changes

* add Class

* move group methods into DN

* tidy group methods

* tidy group methods

* tidy group methods

* tidy group methods

* parent is DirectoryNode

* tidy expand all

* BaseNode -> Node

* move watcher to DirectoryNode

* last_group_node is DirectoryNode only

* simplify create-file

* simplify parent

* simplify collapse-all

* simplify live-filter

* style

* move lib.get_cursor_position to Explorer

* move lib.get_node_at_cursor to Explorer

* move lib.get_nodes to Explorer

* move place_cursor_on_node to Explorer

* resolve resource leak in purge_all_state

* move many autocommands into Explorer

* post merge tidy
  • Loading branch information
alex-courtis authored Oct 26, 2024
1 parent 8760d76 commit f3efc25
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 237 deletions.
139 changes: 6 additions & 133 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
local lib = require("nvim-tree.lib")
local log = require("nvim-tree.log")
local appearance = require("nvim-tree.appearance")
local view = require("nvim-tree.view")
local utils = require("nvim-tree.utils")
local actions = require("nvim-tree.actions")
Expand Down Expand Up @@ -115,27 +113,6 @@ function M.open_on_directory()
actions.root.change_dir.force_dirchange(bufname, true)
end

function M.place_cursor_on_node()
local ok, search = pcall(vim.fn.searchcount)
if ok and search and search.exact_match == 1 then
return
end

local node = lib.get_node_at_cursor()
if not node or node.name == ".." then
return
end
node = node:get_parent_of_group() or node

local line = vim.api.nvim_get_current_line()
local cursor = vim.api.nvim_win_get_cursor(0)
local idx = vim.fn.stridx(line, node.name)

if idx >= 0 then
vim.api.nvim_win_set_cursor(0, { cursor[1], idx })
end
end

---@return table
function M.get_config()
return M.config
Expand Down Expand Up @@ -173,19 +150,6 @@ local function setup_autocommands(opts)
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
end

-- reset and draw (highlights) when colorscheme is changed
create_nvim_tree_autocmd("ColorScheme", {
callback = function()
appearance.setup()
view.reset_winhl()

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

-- prevent new opened file from opening in the same window as nvim-tree
create_nvim_tree_autocmd("BufWipeout", {
pattern = "NvimTree_*",
Expand All @@ -201,76 +165,9 @@ local function setup_autocommands(opts)
end,
})

create_nvim_tree_autocmd("BufWritePost", {
callback = function()
if opts.auto_reload_on_write and not opts.filesystem_watchers.enable then
local explorer = core.get_explorer()
if explorer then
explorer:reload_explorer()
end
end
end,
})

create_nvim_tree_autocmd("BufReadPost", {
callback = function(data)
-- update opened file buffers
local explorer = core.get_explorer()
if not explorer then
return
end
if
(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()
end)
end
end,
})

create_nvim_tree_autocmd("BufUnload", {
callback = function(data)
-- update opened file buffers
local explorer = core.get_explorer()
if not explorer then
return
end
if
(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()
end)
end
end,
})

create_nvim_tree_autocmd("User", {
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
callback = function()
if not opts.filesystem_watchers.enable and opts.git.enable then
local explorer = core.get_explorer()
if explorer then
explorer:reload_git()
end
end
end,
})

if opts.tab.sync.open then
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_enter) })
end
if opts.hijack_cursor then
create_nvim_tree_autocmd("CursorMoved", {
pattern = "NvimTree_*",
callback = function()
if utils.is_nvim_tree_buf(0) then
M.place_cursor_on_node()
end
end,
})
end
if opts.sync_root_with_cwd then
create_nvim_tree_autocmd("DirChanged", {
callback = function()
Expand All @@ -296,20 +193,6 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory })
end

create_nvim_tree_autocmd("BufEnter", {
pattern = "NvimTree_*",
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
local explorer = core.get_explorer()
if explorer then
explorer:reload_explorer()
end
end
end
end,
})

if opts.view.centralize_selection then
create_nvim_tree_autocmd("BufEnter", {
pattern = "NvimTree_*",
Expand Down Expand Up @@ -349,20 +232,6 @@ local function setup_autocommands(opts)
end,
})
end

if opts.modified.enable then
create_nvim_tree_autocmd({ "BufModifiedSet", "BufWritePost" }, {
callback = function()
utils.debounce("Buf:modified", opts.view.debounce_delay, function()
require("nvim-tree.buffers").reload_modified()
local explorer = core.get_explorer()
if explorer then
explorer:reload_explorer()
end
end)
end,
})
end
end

local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
Expand Down Expand Up @@ -805,13 +674,16 @@ local function localise_default_opts()
end

function M.purge_all_state()
require("nvim-tree.watcher").purge_watchers()
view.close_all_tabs()
view.abandon_all_windows()
if core.get_explorer() ~= nil then
local explorer = core.get_explorer()
if explorer then
require("nvim-tree.git").purge_state()
explorer:destroy()
core.reset_explorer()
end
-- purge orphaned that were not destroyed by their nodes
require("nvim-tree.watcher").purge_watchers()
end

---@param conf table|nil
Expand Down Expand Up @@ -855,6 +727,7 @@ function M.setup(conf)
require("nvim-tree.appearance").setup()
require("nvim-tree.diagnostics").setup(opts)
require("nvim-tree.explorer"):setup(opts)
require("nvim-tree.explorer.watch").setup(opts)
require("nvim-tree.git").setup(opts)
require("nvim-tree.git.utils").setup(opts)
require("nvim-tree.view").setup(opts)
Expand Down
16 changes: 8 additions & 8 deletions lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
@@ -1,5 +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")
local notify = require("nvim-tree.notify")
Expand Down Expand Up @@ -104,11 +103,15 @@ function M.fn(default_modifier)
default_modifier = default_modifier or ":t"

return function(node, modifier)
if type(node) ~= "table" then
node = lib.get_node_at_cursor()
local explorer = core.get_explorer()
if not explorer then
return
end

if node == nil then
if type(node) ~= "table" then
node = explorer:get_node_at_cursor()
end
if not node then
return
end

Expand Down Expand Up @@ -160,10 +163,7 @@ function M.fn(default_modifier)

M.rename(node, prepend .. new_file_path .. append)
if not M.config.filesystem_watchers.enable then
local explorer = core.get_explorer()
if explorer then
explorer:reload_explorer()
end
explorer:reload_explorer()
end

find_file(utils.path_remove_trailing(new_file_path))
Expand Down
47 changes: 27 additions & 20 deletions lua/nvim-tree/actions/moves/item.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")
local core = require("nvim-tree.core")
local lib = require("nvim-tree.lib")
local diagnostics = require("nvim-tree.diagnostics")

local DirectoryNode = require("nvim-tree.node.directory")
Expand Down Expand Up @@ -30,15 +29,16 @@ local function status_is_valid(node, what, skip_gitignored)
end

---Move to the next node that has a valid status. If none found, don't move.
---@param explorer Explorer
---@param where string where to move (forwards or backwards)
---@param what string type of status
---@param skip_gitignored boolean default false
local function move(where, what, skip_gitignored)
local function move(explorer, where, what, skip_gitignored)
local first_node_line = core.get_nodes_starting_line()
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, first_node_line)
local nodes_by_line = utils.get_nodes_by_line(explorer.nodes, first_node_line)
local iter_start, iter_end, iter_step, cur, first, nex

local cursor = lib.get_cursor_position()
local cursor = explorer:get_cursor_position()
if cursor and cursor[1] < first_node_line then
cur = cursor[1]
end
Expand Down Expand Up @@ -82,16 +82,17 @@ local function expand_node(node)
end

--- Move to the next node recursively.
---@param explorer Explorer
---@param what string type of status
---@param skip_gitignored boolean default false
local function move_next_recursive(what, skip_gitignored)
local function move_next_recursive(explorer, what, skip_gitignored)
-- If the current node:
-- * is a directory
-- * and is not the root node
-- * and has a git/diag status
-- * and is not opened
-- expand it.
local node_init = lib.get_node_at_cursor()
local node_init = explorer:get_node_at_cursor()
if not node_init then
return
end
Expand All @@ -104,9 +105,9 @@ local function move_next_recursive(what, skip_gitignored)
node_dir:expand_or_collapse(false)
end

move("next", what, skip_gitignored)
move(explorer, "next", what, skip_gitignored)

local node_cur = lib.get_node_at_cursor()
local node_cur = explorer:get_node_at_cursor()
if not node_cur then
return
end
Expand All @@ -122,10 +123,10 @@ local function move_next_recursive(what, skip_gitignored)
while dir_cur and i < MAX_DEPTH do
expand_node(dir_cur)

move("next", what, skip_gitignored)
move(explorer, "next", what, skip_gitignored)

-- Save current node.
node_cur = lib.get_node_at_cursor()
node_cur = explorer:get_node_at_cursor()
dir_cur = node_cur and node_cur:as(DirectoryNode)

i = i + 1
Expand All @@ -147,24 +148,25 @@ end
--- 4.4) Call a non-recursive prev.
--- 4.5) Save the current node and start back from 4.1.
---
---@param explorer Explorer
---@param what string type of status
---@param skip_gitignored boolean default false
local function move_prev_recursive(what, skip_gitignored)
local function move_prev_recursive(explorer, what, skip_gitignored)
local node_init, node_cur

-- 1)
node_init = lib.get_node_at_cursor()
node_init = explorer:get_node_at_cursor()
if node_init == nil then
return
end

-- 2)
move("prev", what, skip_gitignored)
move(explorer, "prev", what, skip_gitignored)

node_cur = lib.get_node_at_cursor()
node_cur = explorer:get_node_at_cursor()
if node_cur == node_init.parent then
-- 3)
move_prev_recursive(what, skip_gitignored)
move_prev_recursive(explorer, what, skip_gitignored)
else
-- i is used to limit iterations.
local i = 0
Expand Down Expand Up @@ -196,10 +198,10 @@ local function move_prev_recursive(what, skip_gitignored)
end

-- 4.4)
move("prev", what, skip_gitignored)
move(explorer, "prev", what, skip_gitignored)

-- 4.5)
node_cur = lib.get_node_at_cursor()
node_cur = explorer:get_node_at_cursor()

i = i + 1
end
Expand All @@ -214,6 +216,11 @@ end
---@return fun()
function M.fn(opts)
return function()
local explorer = core.get_explorer()
if not explorer then
return
end

local recurse = false
local skip_gitignored = false

Expand All @@ -227,14 +234,14 @@ function M.fn(opts)
end

if not recurse then
move(opts.where, opts.what, skip_gitignored)
move(explorer, opts.where, opts.what, skip_gitignored)
return
end

if opts.where == "next" then
move_next_recursive(opts.what, skip_gitignored)
move_next_recursive(explorer, opts.what, skip_gitignored)
elseif opts.where == "prev" then
move_prev_recursive(opts.what, skip_gitignored)
move_prev_recursive(explorer, opts.what, skip_gitignored)
end
end
end
Expand Down
Loading

0 comments on commit f3efc25

Please sign in to comment.