From 8859bbb3b8436c118f6f69994238ddec1d0ec567 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 11 Oct 2024 14:07:48 +1100 Subject: [PATCH 01/12] add todo --- lua/nvim-tree/node/init.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index d5e78678545..06635465232 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -1,5 +1,8 @@ local git = require("nvim-tree.git") +---TODO remove all @cast +---TODO remove all references to directory fields: + ---Abstract Node class. ---Uses the abstract factory pattern to instantiate child instances. ---@class (exact) BaseNode From 98ca98cd87acf4c699607cda76edda28d1220076 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 11 Oct 2024 17:49:34 +1100 Subject: [PATCH 02/12] 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 --- lua/nvim-tree/git/init.lua | 31 +++++++--- lua/nvim-tree/log.lua | 19 ++++++ lua/nvim-tree/node/directory-link.lua | 54 ++++++++++++++++ lua/nvim-tree/node/directory.lua | 58 +++++++++++++++++ lua/nvim-tree/node/factory.lua | 31 +++++++--- lua/nvim-tree/node/file-link.lua | 50 +++++++++++++++ lua/nvim-tree/node/file.lua | 19 +++++- lua/nvim-tree/node/init.lua | 89 ++++----------------------- lua/nvim-tree/node/link.lua | 89 --------------------------- 9 files changed, 256 insertions(+), 184 deletions(-) create mode 100644 lua/nvim-tree/node/directory-link.lua create mode 100644 lua/nvim-tree/node/file-link.lua delete mode 100644 lua/nvim-tree/node/link.lua diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 9e828bd8546..b4fb55314d4 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -283,33 +283,46 @@ function M.load_project_status(path) end end +---Git file and directory status for an absolute path with optional file fallback ---@param parent_ignored boolean ---@param status table|nil ----@param absolute_path string +---@param path string +---@param path_file string? alternative file path when no other file status ---@return GitStatus|nil -function M.git_status_dir(parent_ignored, status, absolute_path) +function M.git_status_dir(parent_ignored, status, path, path_file) if parent_ignored then return { file = "!!" } end if status then return { - file = status.files and status.files[absolute_path], + file = status.files and status.files[path] or path_file and status.files[path_file], dir = status.dirs and { - direct = status.dirs.direct[absolute_path], - indirect = status.dirs.indirect[absolute_path], + direct = status.dirs.direct[path], + indirect = status.dirs.indirect[path], }, } end end +---Git file status for an absolute path with optional fallback ---@param parent_ignored boolean ---@param status table|nil ----@param absolute_path string +---@param path string +---@param path_fallback string? ---@return GitStatus -function M.git_status_file(parent_ignored, status, absolute_path) - local file_status = parent_ignored and "!!" or (status and status.files and status.files[absolute_path]) - return { file = file_status } +function M.git_status_file(parent_ignored, status, path, path_fallback) + if parent_ignored then + return { file = "!!" } + end + + if not status or not status.files then + return {} + end + + return { + file = status.files[path] or status.files[path_fallback] + } end function M.purge_state() diff --git a/lua/nvim-tree/log.lua b/lua/nvim-tree/log.lua index 8e796b9e6c8..ad8f34cf175 100644 --- a/lua/nvim-tree/log.lua +++ b/lua/nvim-tree/log.lua @@ -21,6 +21,25 @@ function M.raw(typ, fmt, ...) end end +--- Write to a new file +---@param typ string as per log.types config +---@param path string absolute path +---@param fmt string for string.format +---@param ... any arguments for string.format +function M.file(typ, path, fmt, ...) + if not M.enabled(typ) then + return + end + + local line = string.format(fmt, ...) + local file = io.open(path, "w") + if file then + io.output(file) + io.write(line) + io.close(file) + end +end + ---@class Profile ---@field start number nanos ---@field tag string diff --git a/lua/nvim-tree/node/directory-link.lua b/lua/nvim-tree/node/directory-link.lua new file mode 100644 index 00000000000..c8759541e22 --- /dev/null +++ b/lua/nvim-tree/node/directory-link.lua @@ -0,0 +1,54 @@ +local git = require("nvim-tree.git") + +local DirectoryNode = require("nvim-tree.node.directory") + +---@class (exact) DirectoryLinkNode: DirectoryNode +---@field link_to string absolute path +---@field fs_stat_target uv.fs_stat.result +local DirectoryLinkNode = DirectoryNode:new() + +---Static factory method +---@param explorer Explorer +---@param parent Node +---@param absolute_path string +---@param link_to string +---@param name string +---@param fs_stat uv.fs_stat.result? +---@param fs_stat_target uv.fs_stat.result +---@return DirectoryLinkNode? nil on vim.loop.fs_realpath failure +function DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target) + -- create DirectoryNode with the target path for the watcher + local o = DirectoryNode:create(explorer, parent, link_to, name, fs_stat) + + o = self:new(o) --[[@as DirectoryLinkNode]] + + -- reset absolute path to the link itself + o.absolute_path = absolute_path + + o.type = "link" + o.link_to = link_to + o.fs_stat_target = fs_stat_target + + return o +end + +-----Update the directory GitStatus of link target and the file status of the link itself +-----@param parent_ignored boolean +-----@param status table|nil +function DirectoryLinkNode:update_git_status(parent_ignored, status) + self.git_status = git.git_status_dir(parent_ignored, status, self.link_to, self.absolute_path) +end + +---Create a sanitized partial copy of a node, populating children recursively. +---@return DirectoryLinkNode cloned +function DirectoryLinkNode:clone() + local clone = DirectoryNode.clone(self) --[[@as DirectoryLinkNode]] + + clone.type = self.type + clone.link_to = self.link_to + clone.fs_stat_target = self.fs_stat_target + + return clone +end + +return DirectoryLinkNode diff --git a/lua/nvim-tree/node/directory.lua b/lua/nvim-tree/node/directory.lua index 050d3e35b8f..4bb216b7518 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -1,3 +1,4 @@ +local git = require("nvim-tree.git") local watch = require("nvim-tree.explorer.watch") local BaseNode = require("nvim-tree.node") @@ -58,6 +59,63 @@ function DirectoryNode:destroy() end end +---Update the GitStatus of the directory +---@param parent_ignored boolean +---@param status table|nil +function DirectoryNode:update_git_status(parent_ignored, status) + self.git_status = git.git_status_dir(parent_ignored, status, self.absolute_path, nil) +end + +---@return GitStatus|nil +function DirectoryNode:get_git_status() + if not self.git_status or not self.explorer.opts.git.show_on_dirs then + return nil + end + + local status = {} + if not self:last_group_node().open or self.explorer.opts.git.show_on_open_dirs then + -- dir is closed or we should show on open_dirs + if self.git_status.file ~= nil then + table.insert(status, self.git_status.file) + end + if self.git_status.dir ~= nil then + if self.git_status.dir.direct ~= nil then + for _, s in pairs(self.git_status.dir.direct) do + table.insert(status, s) + end + end + if self.git_status.dir.indirect ~= nil then + for _, s in pairs(self.git_status.dir.indirect) do + table.insert(status, s) + end + end + end + else + -- dir is open and we shouldn't show on open_dirs + if self.git_status.file ~= nil then + table.insert(status, self.git_status.file) + end + if self.git_status.dir ~= nil and self.git_status.dir.direct ~= nil then + local deleted = { + [" D"] = true, + ["D "] = true, + ["RD"] = true, + ["DD"] = true, + } + for _, s in pairs(self.git_status.dir.direct) do + if deleted[s] then + table.insert(status, s) + end + end + end + end + if #status == 0 then + return nil + else + return status + end +end + ---Create a sanitized partial copy of a node, populating children recursively. ---@return DirectoryNode cloned function DirectoryNode:clone() diff --git a/lua/nvim-tree/node/factory.lua b/lua/nvim-tree/node/factory.lua index a46057da111..e6a7e7b437a 100644 --- a/lua/nvim-tree/node/factory.lua +++ b/lua/nvim-tree/node/factory.lua @@ -1,5 +1,6 @@ +local DirectoryLinkNode = require("nvim-tree.node.directory-link") local DirectoryNode = require("nvim-tree.node.directory") -local LinkNode = require("nvim-tree.node.link") +local FileLinkNode = require("nvim-tree.node.file-link") local FileNode = require("nvim-tree.node.file") local Watcher = require("nvim-tree.watcher") @@ -8,21 +9,37 @@ local M = {} ---Factory function to create the appropriate Node ---@param explorer Explorer ---@param parent Node ----@param abs string +---@param absolute_path string ---@param stat uv.fs_stat.result? -- on nil stat return nil Node ---@param name string ---@return Node? -function M.create_node(explorer, parent, abs, stat, name) +function M.create_node(explorer, parent, absolute_path, stat, name) if not stat then return nil end - if stat.type == "directory" and vim.loop.fs_access(abs, "R") and Watcher.is_fs_event_capable(abs) then - return DirectoryNode:create(explorer, parent, abs, name, stat) + if stat.type == "directory" then + -- directory must be readable and enumerable + if vim.loop.fs_access(absolute_path, "R") and Watcher.is_fs_event_capable(absolute_path) then + return DirectoryNode:create(explorer, parent, absolute_path, name, stat) + end elseif stat.type == "file" then - return FileNode:create(explorer, parent, abs, name, stat) + -- any file + return FileNode:create(explorer, parent, absolute_path, name, stat) elseif stat.type == "link" then - return LinkNode:create(explorer, parent, abs, name, stat) + -- link target path and stat must resolve + local link_to = vim.loop.fs_realpath(absolute_path) + local link_to_stat = link_to and vim.loop.fs_stat(link_to) + if not link_to or not link_to_stat then + return + end + + -- choose directory or file + if link_to_stat.type == "directory" then + return DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name, stat, link_to_stat) + else + return FileLinkNode:create(explorer, parent, absolute_path, link_to, name, stat, link_to_stat) + end end return nil diff --git a/lua/nvim-tree/node/file-link.lua b/lua/nvim-tree/node/file-link.lua new file mode 100644 index 00000000000..60c50d771f9 --- /dev/null +++ b/lua/nvim-tree/node/file-link.lua @@ -0,0 +1,50 @@ +local git = require("nvim-tree.git") + +local FileNode = require("nvim-tree.node.file") + +---@class (exact) FileLinkNode: FileNode +---@field link_to string absolute path +---@field fs_stat_target uv.fs_stat.result +local FileLinkNode = FileNode:new() + +---Static factory method +---@param explorer Explorer +---@param parent Node +---@param absolute_path string +---@param link_to string +---@param name string +---@param fs_stat uv.fs_stat.result? +---@param fs_stat_target uv.fs_stat.result +---@return FileLinkNode? nil on vim.loop.fs_realpath failure +function FileLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_stat, fs_stat_target) + local o = FileNode:create(explorer, parent, absolute_path, name, fs_stat) + + o = self:new(o) --[[@as FileLinkNode]] + + o.type = "link" + o.link_to = link_to + o.fs_stat_target = fs_stat_target + + return o +end + +-----Update the GitStatus of the target otherwise the link itself +-----@param parent_ignored boolean +-----@param status table|nil +function FileLinkNode:update_git_status(parent_ignored, status) + self.git_status = git.git_status_file(parent_ignored, status, self.link_to, self.absolute_path) +end + +---Create a sanitized partial copy of a node +---@return FileLinkNode cloned +function FileLinkNode:clone() + local clone = FileNode.clone(self) --[[@as FileLinkNode]] + + clone.type = self.type + clone.link_to = self.link_to + clone.fs_stat_target = self.fs_stat_target + + return clone +end + +return FileLinkNode diff --git a/lua/nvim-tree/node/file.lua b/lua/nvim-tree/node/file.lua index f504631b7fb..cbf3f96df5a 100644 --- a/lua/nvim-tree/node/file.lua +++ b/lua/nvim-tree/node/file.lua @@ -1,3 +1,4 @@ +local git = require("nvim-tree.git") local utils = require("nvim-tree.utils") local BaseNode = require("nvim-tree.node") @@ -36,7 +37,23 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat) return o end ----Create a sanitized partial copy of a node, populating children recursively. +---Update the GitStatus of the file +---@param parent_ignored boolean +---@param status table|nil +function FileNode:update_git_status(parent_ignored, status) + self.git_status = git.git_status_file(parent_ignored, status, self.absolute_path, nil) +end + +---@return GitStatus|nil +function FileNode:get_git_status() + if not self.git_status then + return nil + end + + return self.git_status.file and { self.git_status.file } +end + +---Create a sanitized partial copy of a node ---@return FileNode cloned function FileNode:clone() local clone = BaseNode.clone(self) --[[@as FileNode]] diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index 06635465232..086177fcf85 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -21,7 +21,7 @@ local git = require("nvim-tree.git") ---@field diag_status DiagStatus? local BaseNode = {} ----@alias Node RootNode|BaseNode|DirectoryNode|FileNode|LinkNode +---@alias Node RootNode|BaseNode|DirectoryNode|FileNode|DirectoryLinkNode|FileLinkNode ---@param o BaseNode? ---@return BaseNode @@ -63,84 +63,16 @@ function BaseNode:has_one_child_folder() return #self.nodes == 1 and self.nodes[1].nodes and vim.loop.fs_access(self.nodes[1].absolute_path, "R") or false end +--luacheck: push ignore 212 +---Update the GitStatus of the node ---@param parent_ignored boolean ----@param status table|nil -function BaseNode:update_git_status(parent_ignored, status) - local get_status - if self.nodes then - get_status = git.git_status_dir - else - get_status = git.git_status_file - end - - -- status of the node's absolute path - self.git_status = get_status(parent_ignored, status, self.absolute_path) - - -- status of the link target, if the link itself is not dirty - if self.link_to and not self.git_status then - self.git_status = get_status(parent_ignored, status, self.link_to) - end +---@param status table? +function BaseNode:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local end +--luacheck: pop ----@return GitStatus|nil +---@return GitStatus? function BaseNode:get_git_status() - if not self.git_status then - -- status doesn't exist - return nil - end - - if not self.nodes then - -- file - return self.git_status.file and { self.git_status.file } - end - - -- dir - if not self.explorer.opts.git.show_on_dirs then - return nil - end - - local status = {} - if not self:last_group_node().open or self.explorer.opts.git.show_on_open_dirs then - -- dir is closed or we should show on open_dirs - if self.git_status.file ~= nil then - table.insert(status, self.git_status.file) - end - if self.git_status.dir ~= nil then - if self.git_status.dir.direct ~= nil then - for _, s in pairs(self.git_status.dir.direct) do - table.insert(status, s) - end - end - if self.git_status.dir.indirect ~= nil then - for _, s in pairs(self.git_status.dir.indirect) do - table.insert(status, s) - end - end - end - else - -- dir is open and we shouldn't show on open_dirs - if self.git_status.file ~= nil then - table.insert(status, self.git_status.file) - end - if self.git_status.dir ~= nil and self.git_status.dir.direct ~= nil then - local deleted = { - [" D"] = true, - ["D "] = true, - ["RD"] = true, - ["DD"] = true, - } - for _, s in pairs(self.git_status.dir.direct) do - if deleted[s] then - table.insert(status, s) - end - end - end - end - if #status == 0 then - return nil - else - return status - end end ---@param projects table @@ -176,7 +108,8 @@ end -- If node is grouped, return the last node in the group. Otherwise, return the given node. ---@return Node function BaseNode:last_group_node() - local node = self --[[@as BaseNode]] + local node = self + --- @cast node BaseNode while node.group_next do node = node.group_next @@ -185,8 +118,8 @@ function BaseNode:last_group_node() return node end ----@param project table|nil ----@param root string|nil +---@param project table? +---@param root string? function BaseNode:update_parent_statuses(project, root) local node = self while project and node do diff --git a/lua/nvim-tree/node/link.lua b/lua/nvim-tree/node/link.lua deleted file mode 100644 index 2df9d920092..00000000000 --- a/lua/nvim-tree/node/link.lua +++ /dev/null @@ -1,89 +0,0 @@ -local watch = require("nvim-tree.explorer.watch") - -local BaseNode = require("nvim-tree.node") - ----@class (exact) LinkNode: BaseNode ----@field has_children boolean ----@field group_next Node? -- If node is grouped, this points to the next child dir/link node ----@field link_to string absolute path ----@field nodes Node[] ----@field open boolean -local LinkNode = BaseNode:new() - ----Static factory method ----@param explorer Explorer ----@param parent Node ----@param absolute_path string ----@param name string ----@param fs_stat uv.fs_stat.result? ----@return LinkNode? nil on vim.loop.fs_realpath failure -function LinkNode:create(explorer, parent, absolute_path, name, fs_stat) - -- INFO: sometimes fs_realpath returns nil - -- I expect this be a bug in glibc, because it fails to retrieve the path for some - -- links (for instance libr2.so in /usr/lib) and thus even with a C program realpath fails - -- when it has no real reason to. Maybe there is a reason, but errno is definitely wrong. - local link_to = vim.loop.fs_realpath(absolute_path) - if not link_to then - return nil - end - - local open, nodes, has_children - local is_dir_link = (link_to ~= nil) and vim.loop.fs_stat(link_to).type == "directory" - - if is_dir_link and link_to then - local handle = vim.loop.fs_scandir(link_to) - has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil or false - open = false - nodes = {} - end - - ---@type LinkNode - local o = { - type = "link", - explorer = explorer, - absolute_path = absolute_path, - executable = false, - fs_stat = fs_stat, - hidden = false, - is_dot = false, - name = name, - parent = parent, - watcher = nil, - diag_status = nil, - - has_children = has_children, - group_next = nil, - link_to = link_to, - nodes = nodes, - open = open, - } - o = self:new(o) --[[@as LinkNode]] - - if is_dir_link then - o.watcher = watch.create_watcher(o) - end - - return o -end - ----Create a sanitized partial copy of a node, populating children recursively. ----@return LinkNode cloned -function LinkNode:clone() - local clone = BaseNode.clone(self) --[[@as LinkNode]] - - clone.has_children = self.has_children - clone.group_next = nil - clone.link_to = self.link_to - clone.nodes = {} - clone.open = self.open - - if self.nodes then - for _, child in ipairs(self.nodes) do - table.insert(clone.nodes, child:clone()) - end - end - - return clone -end - -return LinkNode From 893957a8d95706856cc531bcd31b6ea8da088f1d Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 11 Oct 2024 18:00:25 +1100 Subject: [PATCH 03/12] safer git_status_dir --- lua/nvim-tree/git/init.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index b4fb55314d4..b16cc58320f 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -296,10 +296,10 @@ function M.git_status_dir(parent_ignored, status, path, path_file) if status then return { - file = status.files and status.files[path] or path_file and status.files[path_file], + file = status.files and (status.files[path] or status.files[path_file]), dir = status.dirs and { - direct = status.dirs.direct[path], - indirect = status.dirs.indirect[path], + direct = status.dirs.direct and status.dirs.direct[path], + indirect = status.dirs.indirect and status.dirs.indirect[path], }, } end From 03f9dd29c4326723f649364714bff22755e13c76 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 14 Oct 2024 10:50:22 +1100 Subject: [PATCH 04/12] refactor(#2886): multi instance: node class refactoring: DirectoryNode:expand_or_collapse (#2957) move expand_or_collapse to DirectoryNode --- lua/nvim-tree/actions/moves/item.lua | 9 +++++++-- lua/nvim-tree/api.lua | 5 ++++- lua/nvim-tree/node/directory.lua | 29 +++++++++++++++++++++++++++ lua/nvim-tree/node/init.lua | 30 ---------------------------- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index ec73d010b5f..a327f0b0bef 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -4,6 +4,8 @@ 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") + local M = {} local MAX_DEPTH = 100 @@ -70,8 +72,10 @@ local function move(where, what, skip_gitignored) end end +---@param node Node local function expand_node(node) - if not node.open then + if node:is(DirectoryNode) and not node.open then + ---@cast node DirectoryNode -- Expand the node. -- Should never collapse since we checked open. node:expand_or_collapse() @@ -96,7 +100,8 @@ local function move_next_recursive(what, skip_gitignored) if node_init.name ~= ".." then -- root node cannot have a status valid = status_is_valid(node_init, what, skip_gitignored) end - if node_init.nodes ~= nil and valid and not node_init.open then + if node_init:is(DirectoryNode) and valid and not node_init.open then + ---@cast node_init DirectoryNode node_init:expand_or_collapse() end diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index c153c07ad60..c6b5de44b53 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -9,6 +9,8 @@ local help = require("nvim-tree.help") local keymap = require("nvim-tree.keymap") local notify = require("nvim-tree.notify") +local DirectoryNode = require("nvim-tree.node.directory") + local Api = { tree = {}, node = { @@ -213,7 +215,8 @@ local function open_or_expand_or_dir_up(mode, toggle_group) return function(node) if node.name == ".." then actions.root.change_dir.fn("..") - elseif node.nodes then + elseif node:is(DirectoryNode) then + ---@cast node DirectoryNode node:expand_or_collapse(toggle_group) elseif not toggle_group then edit(mode, node) diff --git a/lua/nvim-tree/node/directory.lua b/lua/nvim-tree/node/directory.lua index 4bb216b7518..439034afd3e 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -116,6 +116,35 @@ function DirectoryNode:get_git_status() end end +function DirectoryNode:expand_or_collapse(toggle_group) + toggle_group = toggle_group or false + if self.has_children then + self.has_children = false + end + + if #self.nodes == 0 then + self.explorer:expand(self) + end + + local head_node = self:get_parent_of_group() + if toggle_group then + head_node:toggle_group_folders() + end + + local open = self:last_group_node().open + local next_open + if toggle_group then + next_open = open + else + next_open = not open + end + for _, n in ipairs(head_node:get_all_nodes_in_group()) do + n.open = next_open + end + + self.explorer.renderer:draw() +end + ---Create a sanitized partial copy of a node, populating children recursively. ---@return DirectoryNode cloned function DirectoryNode:clone() diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index 086177fcf85..6c51c19c993 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -225,36 +225,6 @@ function BaseNode:ungroup_empty_folders() end end -function BaseNode:expand_or_collapse(toggle_group) - toggle_group = toggle_group or false - if self.has_children then - ---@cast self DirectoryNode -- TODO #2886 move this to the class - self.has_children = false - end - - if #self.nodes == 0 then - self.explorer:expand(self) - end - - local head_node = self:get_parent_of_group() - if toggle_group then - head_node:toggle_group_folders() - end - - local open = self:last_group_node().open - local next_open - if toggle_group then - next_open = open - else - next_open = not open - end - for _, n in ipairs(head_node:get_all_nodes_in_group()) do - n.open = next_open - end - - self.explorer.renderer:draw() -end - ---Create a sanitized partial copy of a node, populating children recursively. ---@return BaseNode cloned function BaseNode:clone() From 8331a24c77ae52e217dcf05ac4989c189d84d6e5 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Oct 2024 17:23:22 +1100 Subject: [PATCH 05/12] 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 --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/actions/finders/find-file.lua | 1 + lua/nvim-tree/actions/fs/clipboard.lua | 4 +- lua/nvim-tree/actions/fs/create-file.lua | 33 ++-- lua/nvim-tree/actions/fs/rename-file.lua | 6 +- lua/nvim-tree/actions/moves/item.lua | 4 +- lua/nvim-tree/actions/moves/parent.lua | 26 +-- .../actions/tree/modifiers/collapse-all.lua | 7 +- .../actions/tree/modifiers/expand-all.lua | 16 +- lua/nvim-tree/api.lua | 9 +- lua/nvim-tree/class.lua | 40 +++++ lua/nvim-tree/explorer/init.lua | 37 ++-- lua/nvim-tree/explorer/live-filter.lua | 18 +- lua/nvim-tree/explorer/watch.lua | 2 +- lua/nvim-tree/node/directory-link.lua | 2 +- lua/nvim-tree/node/directory.lua | 99 ++++++++-- lua/nvim-tree/node/factory.lua | 2 +- lua/nvim-tree/node/file-link.lua | 2 +- lua/nvim-tree/node/file.lua | 11 +- lua/nvim-tree/node/init.lua | 170 ++++-------------- lua/nvim-tree/renderer/builder.lua | 33 ++-- lua/nvim-tree/renderer/decorator/init.lua | 18 +- 22 files changed, 284 insertions(+), 258 deletions(-) create mode 100644 lua/nvim-tree/class.lua diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 16f2fa33ac3..7f9b23520fb 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -125,7 +125,7 @@ function M.place_cursor_on_node() if not node or node.name == ".." then return end - node = node:get_parent_of_group() + 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) diff --git a/lua/nvim-tree/actions/finders/find-file.lua b/lua/nvim-tree/actions/finders/find-file.lua index 174ffbdbd81..fa5eea51369 100644 --- a/lua/nvim-tree/actions/finders/find-file.lua +++ b/lua/nvim-tree/actions/finders/find-file.lua @@ -43,6 +43,7 @@ function M.fn(path) return node.absolute_path == path_real or node.link_to == path_real end) :applier(function(node) + ---@cast node DirectoryNode local incremented_line = false if not node.group_next then line = line + 1 diff --git a/lua/nvim-tree/actions/fs/clipboard.lua b/lua/nvim-tree/actions/fs/clipboard.lua index cc75be4d13f..93c16e87606 100644 --- a/lua/nvim-tree/actions/fs/clipboard.lua +++ b/lua/nvim-tree/actions/fs/clipboard.lua @@ -7,6 +7,8 @@ local notify = require("nvim-tree.notify") local find_file = require("nvim-tree.actions.finders.find-file").fn +local DirectoryNode = require("nvim-tree.node.directory") + ---@enum ACTION local ACTION = { copy = "copy", @@ -219,7 +221,7 @@ end function Clipboard:do_paste(node, action, action_fn) if node.name == ".." then node = self.explorer - else + elseif node:is(DirectoryNode) then node = node:last_group_node() end local clip = self.data[action] diff --git a/lua/nvim-tree/actions/fs/create-file.lua b/lua/nvim-tree/actions/fs/create-file.lua index 588c978fc92..455f6f564f7 100644 --- a/lua/nvim-tree/actions/fs/create-file.lua +++ b/lua/nvim-tree/actions/fs/create-file.lua @@ -5,6 +5,9 @@ local notify = require("nvim-tree.notify") local find_file = require("nvim-tree.actions.finders.find-file").fn +local FileNode = require("nvim-tree.node.file") +local DirectoryNode = require("nvim-tree.node.directory") + local M = {} ---@param file string @@ -29,35 +32,21 @@ local function get_num_nodes(iter) return i end ----@param node Node ----@return string -local function get_containing_folder(node) - if node.nodes ~= nil then - return utils.path_add_trailing(node.absolute_path) - end - local node_name_size = #(node.name or "") - return node.absolute_path:sub(0, -node_name_size - 1) -end - ---@param node Node? function M.fn(node) - local cwd = core.get_cwd() - if cwd == nil then + node = node or core.get_explorer() --[[@as Node]] + if not node then return end - if not node or node.name == ".." then - node = { - absolute_path = cwd, - name = "", - nodes = core.get_explorer().nodes, - open = true, - } - else - node = node:last_group_node() + local dir = node:is(FileNode) and node.parent or node:as(DirectoryNode) + if not dir then + return end - local containing_folder = get_containing_folder(node) + dir = dir:last_group_node() + + local containing_folder = utils.path_add_trailing(dir.absolute_path) local input_opts = { prompt = "Create file ", diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index a06fb201c56..d60cbb4969d 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -6,6 +6,8 @@ local notify = require("nvim-tree.notify") local find_file = require("nvim-tree.actions.finders.find-file").fn +local DirectoryNode = require("nvim-tree.node.directory") + local M = { config = {}, } @@ -120,7 +122,9 @@ function M.fn(default_modifier) return end - node = node:last_group_node() + if node:is(DirectoryNode) then + node = node:last_group_node() + end if node.name == ".." then return end diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index a327f0b0bef..2b081273626 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -78,7 +78,7 @@ local function expand_node(node) ---@cast node DirectoryNode -- Expand the node. -- Should never collapse since we checked open. - node:expand_or_collapse() + node:expand_or_collapse(false) end end @@ -102,7 +102,7 @@ local function move_next_recursive(what, skip_gitignored) end if node_init:is(DirectoryNode) and valid and not node_init.open then ---@cast node_init DirectoryNode - node_init:expand_or_collapse() + node_init:expand_or_collapse(false) end move("next", what, skip_gitignored) diff --git a/lua/nvim-tree/actions/moves/parent.lua b/lua/nvim-tree/actions/moves/parent.lua index 88eca47567a..9502af46f1c 100644 --- a/lua/nvim-tree/actions/moves/parent.lua +++ b/lua/nvim-tree/actions/moves/parent.lua @@ -1,6 +1,7 @@ local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") -local core = require("nvim-tree.core") + +local DirectoryNode = require("nvim-tree.node.directory") local M = {} @@ -9,33 +10,32 @@ local M = {} function M.fn(should_close) should_close = should_close or false + ---@param node Node return function(node) - local explorer = core.get_explorer() - node = node:last_group_node() - if should_close and node.open then - node.open = false - if explorer then - explorer.renderer:draw() + local dir = node:as(DirectoryNode) + if dir then + dir = dir:last_group_node() + if should_close and dir.open then + dir.open = false + dir.explorer.renderer:draw() + return end - return end - local parent = node:get_parent_of_group().parent + local parent = (node:get_parent_of_group() or node).parent if not parent or not parent.parent then return view.set_cursor({ 1, 0 }) end - local _, line = utils.find_node(core.get_explorer().nodes, function(n) + local _, line = utils.find_node(parent.explorer.nodes, function(n) return n.absolute_path == parent.absolute_path end) view.set_cursor({ line + 1, 0 }) if should_close then parent.open = false - if explorer then - explorer.renderer:draw() - end + parent.explorer.renderer:draw() end end end diff --git a/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua b/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua index 136bd357297..214d572c035 100644 --- a/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua @@ -3,6 +3,8 @@ local core = require("nvim-tree.core") local lib = require("nvim-tree.lib") local Iterator = require("nvim-tree.iterators.node-iterator") +local DirectoryNode = require("nvim-tree.node.directory") + local M = {} ---@return fun(path: string): boolean @@ -36,8 +38,9 @@ function M.fn(keep_buffers) Iterator.builder(explorer.nodes) :hidden() :applier(function(n) - if n.nodes ~= nil then - n.open = keep_buffers == true and matches(n.absolute_path) + local dir = n:as(DirectoryNode) + if dir then + dir.open = keep_buffers and matches(dir.absolute_path) end end) :recursor(function(n) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index ed898de2a30..ffede0e6d6f 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -2,6 +2,8 @@ local core = require("nvim-tree.core") local Iterator = require("nvim-tree.iterators.node-iterator") local notify = require("nvim-tree.notify") +local DirectoryNode = require("nvim-tree.node.directory") + local M = {} ---@param list string[] @@ -15,7 +17,7 @@ local function to_lookup_table(list) return table end ----@param node Node +---@param node DirectoryNode local function expand(node) node = node:last_group_node() node.open = true @@ -36,6 +38,7 @@ end local function gen_iterator() local expansion_count = 0 + ---@param parent DirectoryNode return function(parent) if parent.parent and parent.nodes and not parent.open then expansion_count = expansion_count + 1 @@ -44,12 +47,14 @@ local function gen_iterator() Iterator.builder(parent.nodes) :hidden() + ---@param node DirectoryNode :applier(function(node) if should_expand(expansion_count, node) then expansion_count = expansion_count + 1 expand(node) end end) + ---@param node DirectoryNode :recursor(function(node) return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes)) end) @@ -61,11 +66,16 @@ local function gen_iterator() end end +---Expand the directory node or the root ---@param node Node function M.fn(node) local explorer = core.get_explorer() - node = node.nodes and node or explorer - if gen_iterator()(node) then + local parent = node:as(DirectoryNode) or explorer + if not parent then + return + end + + if gen_iterator()(parent) then notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") end if explorer then diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index c6b5de44b53..7daab9a1ae2 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -10,6 +10,7 @@ local keymap = require("nvim-tree.keymap") local notify = require("nvim-tree.notify") local DirectoryNode = require("nvim-tree.node.directory") +local RootNode = require("nvim-tree.node.root") local Api = { tree = {}, @@ -137,9 +138,9 @@ Api.tree.change_root = wrap(function(...) end) Api.tree.change_root_to_node = wrap_node(function(node) - if node.name == ".." then + if node.name == ".." or node:is(RootNode) then actions.root.change_dir.fn("..") - elseif node.nodes ~= nil then + elseif node:is(DirectoryNode) then actions.root.change_dir.fn(node:last_group_node().absolute_path) end end) @@ -210,13 +211,13 @@ local function edit(mode, node) end ---@param mode string ----@return fun(node: table) +---@return fun(node: Node) local function open_or_expand_or_dir_up(mode, toggle_group) + ---@param node Node return function(node) if node.name == ".." then actions.root.change_dir.fn("..") elseif node:is(DirectoryNode) then - ---@cast node DirectoryNode node:expand_or_collapse(toggle_group) elseif not toggle_group then edit(mode, node) diff --git a/lua/nvim-tree/class.lua b/lua/nvim-tree/class.lua new file mode 100644 index 00000000000..8565e3c6ded --- /dev/null +++ b/lua/nvim-tree/class.lua @@ -0,0 +1,40 @@ +---Generic class, useful for inheritence. +---@class (exact) Class +---@field private __index? table +local Class = {} + +---@param o Class? +---@return Class +function Class:new(o) + o = o or {} + + setmetatable(o, self) + self.__index = self + + return o +end + +---Object is an instance of class +---This will start with the lowest class and loop over all the superclasses. +---@param class table +---@return boolean +function Class:is(class) + local mt = getmetatable(self) + while mt do + if mt == class then + return true + end + mt = getmetatable(mt) + end + return false +end + +---Return object if it is an instance of class, otherwise nil +---@generic T +---@param class T +---@return `T`|nil +function Class:as(class) + return self:is(class) and self or nil +end + +return Class diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 0045ba9b243..b0cbc99665a 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -5,6 +5,7 @@ local utils = require("nvim-tree.utils") local view = require("nvim-tree.view") local node_factory = require("nvim-tree.node.factory") +local DirectoryNode = require("nvim-tree.node.directory") local RootNode = require("nvim-tree.node.root") local Watcher = require("nvim-tree.watcher") @@ -72,12 +73,12 @@ function Explorer:create(path) return o end ----@param node Node +---@param node DirectoryNode function Explorer:expand(node) self:_load(node) end ----@param node Node +---@param node DirectoryNode ---@param git_status table|nil function Explorer:reload(node, git_status) local cwd = node.link_to or node.absolute_path @@ -169,11 +170,10 @@ function Explorer:reload(node, git_status) end, node.nodes) ) - local is_root = not node.parent - local child_folder_only = node:has_one_child_folder() and node.nodes[1] - if config.renderer.group_empty and not is_root and child_folder_only then - node.group_next = child_folder_only - local ns = self:reload(child_folder_only, git_status) + local single_child = node:single_child_directory() + if config.renderer.group_empty and node.parent and single_child then + node.group_next = single_child + local ns = self:reload(single_child, git_status) node.nodes = ns or {} log.profile_end(profile) return ns @@ -219,7 +219,7 @@ function Explorer:refresh_parent_nodes_for_path(path) end ---@private ----@param node Node +---@param node DirectoryNode function Explorer:_load(node) local cwd = node.link_to or node.absolute_path local git_status = git.load_project_status(cwd) @@ -243,7 +243,7 @@ end ---@private ---@param handle uv.uv_fs_t ---@param cwd string ----@param node Node +---@param node DirectoryNode ---@param git_status table ---@param parent Explorer function Explorer:populate_children(handle, cwd, node, git_status, parent) @@ -295,7 +295,7 @@ function Explorer:populate_children(handle, cwd, node, git_status, parent) end ---@private ----@param node Node +---@param node DirectoryNode ---@param status table ---@param parent Explorer ---@return Node[]|nil @@ -311,12 +311,12 @@ function Explorer:explore(node, status, parent) self:populate_children(handle, cwd, node, status, parent) local is_root = not node.parent - local child_folder_only = node:has_one_child_folder() and node.nodes[1] - if config.renderer.group_empty and not is_root and child_folder_only then - local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path + local single_child = node:single_child_directory() + if config.renderer.group_empty and not is_root and single_child then + local child_cwd = single_child.link_to or single_child.absolute_path local child_status = git.load_project_status(child_cwd) - node.group_next = child_folder_only - local ns = self:explore(child_folder_only, child_status, parent) + node.group_next = single_child + local ns = self:explore(single_child, child_status, parent) node.nodes = ns or {} log.profile_end(profile) @@ -335,9 +335,10 @@ end function Explorer:refresh_nodes(projects) Iterator.builder({ self }) :applier(function(n) - if n.nodes then - local toplevel = git.get_toplevel(n.cwd or n.link_to or n.absolute_path) - self:reload(n, projects[toplevel] or {}) + local dir = n:as(DirectoryNode) + if dir then + local toplevel = git.get_toplevel(dir.cwd or dir.link_to or dir.absolute_path) + self:reload(dir, projects[toplevel] or {}) end end) :recursor(function(n) diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 02cfda1c251..30152d63ae4 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -1,6 +1,8 @@ local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") + local Iterator = require("nvim-tree.iterators.node-iterator") +local DirectoryNode = require("nvim-tree.node.directory") ---@class LiveFilter ---@field explorer Explorer @@ -31,17 +33,19 @@ local function reset_filter(self, node_) return end - node_.hidden_stats = vim.tbl_deep_extend("force", node_.hidden_stats or {}, { - live_filter = 0, - }) + local dir_ = node_:as(DirectoryNode) + if dir_ then + dir_.hidden_stats = vim.tbl_deep_extend("force", dir_.hidden_stats or {}, { live_filter = 0, }) + end Iterator.builder(node_.nodes) :hidden() :applier(function(node) node.hidden = false - node.hidden_stats = vim.tbl_deep_extend("force", node.hidden_stats or {}, { - live_filter = 0, - }) + local dir = node:as(DirectoryNode) + if dir then + dir.hidden_stats = vim.tbl_deep_extend("force", dir.hidden_stats or {}, { live_filter = 0, }) + end end) :iterate() end @@ -85,7 +89,7 @@ local function matches(self, node) return vim.regex(self.filter):match_str(name) ~= nil end ----@param node_ Node? +---@param node_ DirectoryNode? function LiveFilter:apply_filter(node_) if not self.filter or self.filter == "" then reset_filter(self, node_) diff --git a/lua/nvim-tree/explorer/watch.lua b/lua/nvim-tree/explorer/watch.lua index 7fd13f4c3fb..a83758132ca 100644 --- a/lua/nvim-tree/explorer/watch.lua +++ b/lua/nvim-tree/explorer/watch.lua @@ -53,7 +53,7 @@ local function is_folder_ignored(path) return false end ----@param node Node +---@param node DirectoryNode ---@return Watcher|nil function M.create_watcher(node) if not M.config.filesystem_watchers.enable or type(node) ~= "table" then diff --git a/lua/nvim-tree/node/directory-link.lua b/lua/nvim-tree/node/directory-link.lua index c8759541e22..f0543825fe9 100644 --- a/lua/nvim-tree/node/directory-link.lua +++ b/lua/nvim-tree/node/directory-link.lua @@ -9,7 +9,7 @@ local DirectoryLinkNode = DirectoryNode:new() ---Static factory method ---@param explorer Explorer ----@param parent Node +---@param parent DirectoryNode ---@param absolute_path string ---@param link_to string ---@param name string diff --git a/lua/nvim-tree/node/directory.lua b/lua/nvim-tree/node/directory.lua index 439034afd3e..cf423364a74 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -1,19 +1,20 @@ local git = require("nvim-tree.git") local watch = require("nvim-tree.explorer.watch") -local BaseNode = require("nvim-tree.node") +local Node = require("nvim-tree.node") ----@class (exact) DirectoryNode: BaseNode +---@class (exact) DirectoryNode: Node ---@field has_children boolean ----@field group_next Node? -- If node is grouped, this points to the next child dir/link node +---@field group_next DirectoryNode? -- If node is grouped, this points to the next child dir/link node ---@field nodes Node[] ---@field open boolean +---@field watcher Watcher? ---@field hidden_stats table? -- Each field of this table is a key for source and value for count -local DirectoryNode = BaseNode:new() +local DirectoryNode = Node:new() ---Static factory method ---@param explorer Explorer ----@param parent Node? +---@param parent DirectoryNode? ---@param absolute_path string ---@param name string ---@param fs_stat uv.fs_stat.result|nil @@ -51,12 +52,18 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat) end function DirectoryNode:destroy() - BaseNode.destroy(self) + if self.watcher then + self.watcher:destroy() + self.watcher = nil + end + if self.nodes then for _, node in pairs(self.nodes) do node:destroy() end end + + Node.destroy(self) end ---Update the GitStatus of the directory @@ -116,6 +123,75 @@ function DirectoryNode:get_git_status() end end +---Refresh contents and git status for a single node +function DirectoryNode:refresh() + local node = self:get_parent_of_group() or self + local toplevel = git.get_toplevel(self.absolute_path) + + git.reload_project(toplevel, self.absolute_path, function() + local project = git.get_project(toplevel) or {} + + self.explorer:reload(node, project) + + node:update_parent_statuses(project, toplevel) + + self.explorer.renderer:draw() + end) +end + +-- If node is grouped, return the last node in the group. Otherwise, return the given node. +---@return DirectoryNode +function DirectoryNode:last_group_node() + return self.group_next and self.group_next:last_group_node() or self +end + +---Return the one and only one child directory +---@return DirectoryNode? +function DirectoryNode:single_child_directory() + if #self.nodes == 1 then + return self.nodes[1]:as(DirectoryNode) + end +end + +---@private +-- Toggle group empty folders +function DirectoryNode:toggle_group_folders() + local is_grouped = self.group_next ~= nil + + if is_grouped then + self:ungroup_empty_folders() + else + self:group_empty_folders() + end +end + +---Group empty folders +-- Recursively group nodes +---@private +---@return Node[] +function DirectoryNode:group_empty_folders() + local single_child = self:single_child_directory() + if self.explorer.opts.renderer.group_empty and self.parent and single_child then + self.group_next = single_child + local ns = single_child:group_empty_folders() + self.nodes = ns or {} + return ns + end + return self.nodes +end + +---Ungroup empty folders +-- If a node is grouped, ungroup it: put node.group_next to the node.nodes and set node.group_next to nil +---@private +function DirectoryNode:ungroup_empty_folders() + if self.group_next then + self.group_next:ungroup_empty_folders() + self.nodes = { self.group_next } + self.group_next = nil + end +end + +---@param toggle_group boolean function DirectoryNode:expand_or_collapse(toggle_group) toggle_group = toggle_group or false if self.has_children then @@ -126,7 +202,7 @@ function DirectoryNode:expand_or_collapse(toggle_group) self.explorer:expand(self) end - local head_node = self:get_parent_of_group() + local head_node = self:get_parent_of_group() or self if toggle_group then head_node:toggle_group_folders() end @@ -138,8 +214,11 @@ function DirectoryNode:expand_or_collapse(toggle_group) else next_open = not open end - for _, n in ipairs(head_node:get_all_nodes_in_group()) do - n.open = next_open + + local node = self + while node do + node.open = next_open + node = node.group_next end self.explorer.renderer:draw() @@ -148,7 +227,7 @@ end ---Create a sanitized partial copy of a node, populating children recursively. ---@return DirectoryNode cloned function DirectoryNode:clone() - local clone = BaseNode.clone(self) --[[@as DirectoryNode]] + local clone = Node.clone(self) --[[@as DirectoryNode]] clone.has_children = self.has_children clone.group_next = nil diff --git a/lua/nvim-tree/node/factory.lua b/lua/nvim-tree/node/factory.lua index e6a7e7b437a..ee0504fc807 100644 --- a/lua/nvim-tree/node/factory.lua +++ b/lua/nvim-tree/node/factory.lua @@ -8,7 +8,7 @@ local M = {} ---Factory function to create the appropriate Node ---@param explorer Explorer ----@param parent Node +---@param parent DirectoryNode ---@param absolute_path string ---@param stat uv.fs_stat.result? -- on nil stat return nil Node ---@param name string diff --git a/lua/nvim-tree/node/file-link.lua b/lua/nvim-tree/node/file-link.lua index 60c50d771f9..2bdd79f13f3 100644 --- a/lua/nvim-tree/node/file-link.lua +++ b/lua/nvim-tree/node/file-link.lua @@ -9,7 +9,7 @@ local FileLinkNode = FileNode:new() ---Static factory method ---@param explorer Explorer ----@param parent Node +---@param parent DirectoryNode ---@param absolute_path string ---@param link_to string ---@param name string diff --git a/lua/nvim-tree/node/file.lua b/lua/nvim-tree/node/file.lua index cbf3f96df5a..0f01347c0cf 100644 --- a/lua/nvim-tree/node/file.lua +++ b/lua/nvim-tree/node/file.lua @@ -1,15 +1,15 @@ local git = require("nvim-tree.git") local utils = require("nvim-tree.utils") -local BaseNode = require("nvim-tree.node") +local Node = require("nvim-tree.node") ----@class (exact) FileNode: BaseNode +---@class (exact) FileNode: Node ---@field extension string -local FileNode = BaseNode:new() +local FileNode = Node:new() ---Static factory method ---@param explorer Explorer ----@param parent Node +---@param parent DirectoryNode ---@param absolute_path string ---@param name string ---@param fs_stat uv.fs_stat.result? @@ -27,7 +27,6 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat) is_dot = false, name = name, parent = parent, - watcher = nil, diag_status = nil, extension = string.match(name, ".?[^.]+%.(.*)") or "", @@ -56,7 +55,7 @@ end ---Create a sanitized partial copy of a node ---@return FileNode cloned function FileNode:clone() - local clone = BaseNode.clone(self) --[[@as FileNode]] + local clone = Node.clone(self) --[[@as FileNode]] clone.extension = self.extension diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index ceca46ce308..a2af3afe529 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -1,12 +1,14 @@ local git = require("nvim-tree.git") +local Class = require("nvim-tree.class") + +---TODO #2886 ---TODO remove all @cast ---TODO remove all references to directory fields: ---Abstract Node class. ---Uses the abstract factory pattern to instantiate child instances. ----@class (exact) BaseNode ----@field private __index? table +---@class (exact) Node: Class ---@field type NODE_TYPE ---@field explorer Explorer ---@field absolute_path string @@ -15,68 +17,30 @@ local git = require("nvim-tree.git") ---@field git_status GitStatus? ---@field hidden boolean ---@field name string ----@field parent Node? ----@field watcher Watcher? +---@field parent DirectoryNode? ---@field diag_status DiagStatus? ---@field is_dot boolean cached is_dotfile -local BaseNode = {} - ----@alias Node RootNode|BaseNode|DirectoryNode|FileNode|DirectoryLinkNode|FileLinkNode - ----@param o BaseNode? ----@return BaseNode -function BaseNode:new(o) - o = o or {} - - setmetatable(o, self) - self.__index = self - - return o -end - -function BaseNode:destroy() - if self.watcher then - self.watcher:destroy() - self.watcher = nil - end -end - ----From plenary ----Checks if the object is an instance ----This will start with the lowest class and loop over all the superclasses. ----@param self BaseNode ----@param T BaseNode ----@return boolean -function BaseNode:is(T) - local mt = getmetatable(self) - while mt do - if mt == T then - return true - end - mt = getmetatable(mt) - end - return false -end +local Node = Class:new() ----@return boolean -function BaseNode:has_one_child_folder() - return #self.nodes == 1 and self.nodes[1].nodes and vim.loop.fs_access(self.nodes[1].absolute_path, "R") or false +function Node:destroy() end --luacheck: push ignore 212 ---Update the GitStatus of the node ---@param parent_ignored boolean ---@param status table? -function BaseNode:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local +function Node:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local + ---TODO find a way to declare abstract methods end + --luacheck: pop ---@return GitStatus? -function BaseNode:get_git_status() +function Node:get_git_status() end ---@param projects table -function BaseNode:reload_node_status(projects) +function Node:reload_node_status(projects) local toplevel = git.get_toplevel(self.absolute_path) local status = projects[toplevel] or {} for _, node in ipairs(self.nodes) do @@ -88,13 +52,13 @@ function BaseNode:reload_node_status(projects) end ---@return boolean -function BaseNode:is_git_ignored() +function Node:is_git_ignored() return self.git_status ~= nil and self.git_status.file == "!!" end ---Node or one of its parents begins with a dot ---@return boolean -function BaseNode:is_dotfile() +function Node:is_dotfile() if self.is_dot or (self.name and (self.name:sub(1, 1) == ".")) @@ -106,22 +70,9 @@ function BaseNode:is_dotfile() return false end --- If node is grouped, return the last node in the group. Otherwise, return the given node. ----@return Node -function BaseNode:last_group_node() - local node = self - --- @cast node BaseNode - - while node.group_next do - node = node.group_next - end - - return node -end - ---@param project table? ---@param root string? -function BaseNode:update_parent_statuses(project, root) +function Node:update_parent_statuses(project, root) local node = self while project and node do -- step up to the containing project @@ -151,88 +102,30 @@ function BaseNode:update_parent_statuses(project, root) end end ----Refresh contents and git status for a single node -function BaseNode:refresh() - local parent_node = self:get_parent_of_group() - local toplevel = git.get_toplevel(self.absolute_path) - - git.reload_project(toplevel, self.absolute_path, function() - local project = git.get_project(toplevel) or {} - - self.explorer:reload(parent_node, project) - - parent_node:update_parent_statuses(project, toplevel) - - self.explorer.renderer:draw() - end) -end - ----Get the highest parent of grouped nodes ----@return Node node or parent -function BaseNode:get_parent_of_group() - local node = self - while node and node.parent and node.parent.group_next do - node = node.parent or node +---Get the highest parent of grouped nodes, nil when not grouped +---@return DirectoryNode? +function Node:get_parent_of_group() + if not self.parent or not self.parent.group_next then + return nil end - return node -end ----@return Node[] -function BaseNode:get_all_nodes_in_group() - local next_node = self:get_parent_of_group() - local nodes = {} - while next_node do - table.insert(nodes, next_node) - next_node = next_node.group_next - end - return nodes -end - --- Toggle group empty folders -function BaseNode:toggle_group_folders() - local is_grouped = self.group_next ~= nil - - if is_grouped then - self:ungroup_empty_folders() - else - self:group_empty_folders() - end -end - ----Group empty folders --- Recursively group nodes ----@return Node[] -function BaseNode:group_empty_folders() - local is_root = not self.parent - local child_folder_only = self:has_one_child_folder() and self.nodes[1] - if self.explorer.opts.renderer.group_empty and not is_root and child_folder_only then - ---@cast self DirectoryNode -- TODO #2886 move this to the class - self.group_next = child_folder_only - local ns = child_folder_only:group_empty_folders() - self.nodes = ns or {} - return ns - end - return self.nodes -end - ----Ungroup empty folders --- If a node is grouped, ungroup it: put node.group_next to the node.nodes and set node.group_next to nil -function BaseNode:ungroup_empty_folders() - local cur = self - while cur and cur.group_next do - cur.nodes = { cur.group_next } - cur.group_next = nil - cur = cur.nodes[1] + local node = self.parent + while node do + if node.parent and node.parent.group_next then + node = node.parent + else + return node + end end end ---Create a sanitized partial copy of a node, populating children recursively. ----@return BaseNode cloned -function BaseNode:clone() +---@return Node cloned +function Node:clone() ---@type Explorer local explorer_placeholder = nil - ---@type BaseNode + ---@type Node local clone = { type = self.type, explorer = explorer_placeholder, @@ -244,11 +137,10 @@ function BaseNode:clone() is_dot = self.is_dot, name = self.name, parent = nil, - watcher = nil, diag_status = nil, } return clone end -return BaseNode +return Node diff --git a/lua/nvim-tree/renderer/builder.lua b/lua/nvim-tree/renderer/builder.lua index 2071bdabcac..9804bfcc4a0 100644 --- a/lua/nvim-tree/renderer/builder.lua +++ b/lua/nvim-tree/renderer/builder.lua @@ -2,6 +2,10 @@ local notify = require("nvim-tree.notify") local utils = require("nvim-tree.utils") local view = require("nvim-tree.view") +local DirectoryLinkNode = require("nvim-tree.node.directory-link") +local DirectoryNode = require("nvim-tree.node.directory") +local FileLinkNode = require("nvim-tree.node.file-link") + local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks") local DecoratorCopied = require("nvim-tree.renderer.decorator.copied") local DecoratorCut = require("nvim-tree.renderer.decorator.cut") @@ -341,19 +345,21 @@ function Builder:add_highlights(node) return icon_hl_group, name_hl_group end +---Insert node line into self.lines, calling Builder:build_lines for each directory ---@private +---@param node Node +---@param idx integer line number starting at 1 +---@param num_children integer of node function Builder:build_line(node, idx, num_children) -- various components local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers) local arrows = pad.get_arrows(node) -- main components - local is_folder = node.nodes ~= nil - local is_symlink = node.link_to ~= nil local icon, name - if is_folder then + if node:is(DirectoryNode) then icon, name = self:build_folder(node) - elseif is_symlink then + elseif node:is(DirectoryLinkNode) or node:is(FileLinkNode) then icon, name = self:build_symlink(node) else icon, name = self:build_file(node) @@ -369,11 +375,13 @@ function Builder:build_line(node, idx, num_children) self.index = self.index + 1 - node = node:last_group_node() - if node.open then - self.depth = self.depth + 1 - self:build_lines(node) - self.depth = self.depth - 1 + if node:is(DirectoryNode) then + node = node:last_group_node() + if node.open then + self.depth = self.depth + 1 + self:build_lines(node) + self.depth = self.depth - 1 + end end end @@ -403,8 +411,11 @@ function Builder:add_hidden_count_string(node, idx, num_children) end end +---Number of visible nodes ---@private -function Builder:get_nodes_number(nodes) +---@param nodes Node[] +---@return integer +function Builder:num_visible(nodes) if not self.explorer.live_filter.filter then return #nodes end @@ -423,7 +434,7 @@ function Builder:build_lines(node) if not node then node = self.explorer end - local num_children = self:get_nodes_number(node.nodes) + local num_children = self:num_visible(node.nodes) local idx = 1 for _, n in ipairs(node.nodes) do if not n.hidden then diff --git a/lua/nvim-tree/renderer/decorator/init.lua b/lua/nvim-tree/renderer/decorator/init.lua index a80ce615a05..44f05f8e23e 100644 --- a/lua/nvim-tree/renderer/decorator/init.lua +++ b/lua/nvim-tree/renderer/decorator/init.lua @@ -1,26 +1,16 @@ +local Class = require("nvim-tree.class") + local HL_POSITION = require("nvim-tree.enum").HL_POSITION local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT ---Abstract Decorator ---Uses the factory pattern to instantiate child instances. ----@class (exact) Decorator ----@field private __index? table +---@class (exact) Decorator: Class ---@field protected explorer Explorer ---@field protected enabled boolean ---@field protected hl_pos HL_POSITION ---@field protected icon_placement ICON_PLACEMENT -local Decorator = {} - ----@param o Decorator|nil ----@return Decorator -function Decorator:new(o) - o = o or {} - - setmetatable(o, self) - self.__index = self - - return o -end +local Decorator = Class:new() ---Maybe highlight groups ---@param node Node From 0992969dc50b486742e085c02621aa3efc238e2a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Oct 2024 10:56:45 +1100 Subject: [PATCH 06/12] move lib.get_cursor_position to Explorer --- lua/nvim-tree/actions/moves/item.lua | 9 +++++++-- lua/nvim-tree/explorer/init.lua | 13 +++++++++++++ lua/nvim-tree/lib.lua | 18 +----------------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index 2b081273626..462e2870931 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -34,11 +34,16 @@ end ---@param what string type of status ---@param skip_gitignored boolean default false local function move(where, what, skip_gitignored) + local explorer = core.get_explorer() + if not explorer then + return + end + 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 diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index b0cbc99665a..6dbbd1720c9 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -374,6 +374,19 @@ function Explorer:reload_git() event_running = false end +---Cursor position as per vim.api.nvim_win_get_cursor +---nil on no explorer or invalid view win +---@return integer[]|nil +function Explorer:get_cursor_position() + local winnr = view.get_winnr() + if not winnr or not vim.api.nvim_win_is_valid(winnr) then + return + end + + return vim.api.nvim_win_get_cursor(winnr) +end + + function Explorer:setup(opts) config = opts require("nvim-tree.explorer.watch").setup(opts) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 15ccb337aa1..2b9ead582aa 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -13,22 +13,6 @@ local M = { target_winid = nil, } ----Cursor position as per vim.api.nvim_win_get_cursor ----nil on no explorer or invalid view win ----@return integer[]|nil -function M.get_cursor_position() - if not core.get_explorer() then - return - end - - local winnr = view.get_winnr() - if not winnr or not vim.api.nvim_win_is_valid(winnr) then - return - end - - return vim.api.nvim_win_get_cursor(winnr) -end - ---@return Node|nil function M.get_node_at_cursor() local explorer = core.get_explorer() @@ -36,7 +20,7 @@ function M.get_node_at_cursor() return end - local cursor = M.get_cursor_position() + local cursor = explorer:get_cursor_position() if not cursor then return end From 7324fb1bf0df95d8fad879e432c70cf80ec34895 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Oct 2024 11:34:10 +1100 Subject: [PATCH 07/12] move lib.get_node_at_cursor to Explorer --- lua/nvim-tree.lua | 8 +++- lua/nvim-tree/actions/fs/rename-file.lua | 16 +++---- lua/nvim-tree/actions/moves/item.lua | 48 ++++++++++--------- .../actions/tree/modifiers/collapse-all.lua | 8 ++-- .../actions/tree/modifiers/toggles.lua | 7 +-- lua/nvim-tree/api.lua | 32 ++++++------- lua/nvim-tree/explorer/init.lua | 14 ++++++ lua/nvim-tree/explorer/live-filter.lua | 4 +- lua/nvim-tree/lib.lua | 20 -------- lua/nvim-tree/log.lua | 4 +- lua/nvim-tree/marks/init.lua | 4 +- 11 files changed, 83 insertions(+), 82 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 7f9b23520fb..a45c5c6c820 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -1,4 +1,3 @@ -local lib = require("nvim-tree.lib") local log = require("nvim-tree.log") local appearance = require("nvim-tree.appearance") local view = require("nvim-tree.view") @@ -121,7 +120,12 @@ function M.place_cursor_on_node() return end - local node = lib.get_node_at_cursor() + local explorer = core.get_explorer() + if not explorer then + return + end + + local node = explorer:get_node_at_cursor() if not node or node.name == ".." then return end diff --git a/lua/nvim-tree/actions/fs/rename-file.lua b/lua/nvim-tree/actions/fs/rename-file.lua index d60cbb4969d..da1ca001167 100644 --- a/lua/nvim-tree/actions/fs/rename-file.lua +++ b/lua/nvim-tree/actions/fs/rename-file.lua @@ -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") @@ -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 @@ -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)) diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index 462e2870931..9979963189a 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -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") @@ -30,15 +29,11 @@ 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 explorer = core.get_explorer() - if not explorer then - return - end - +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(explorer.nodes, first_node_line) local iter_start, iter_end, iter_step, cur, first, nex @@ -88,16 +83,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 @@ -110,9 +106,9 @@ local function move_next_recursive(what, skip_gitignored) node_init: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 @@ -128,10 +124,10 @@ local function move_next_recursive(what, skip_gitignored) while is_dir and i < MAX_DEPTH do expand_node(node_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() -- Update is_dir. if node_cur then is_dir = node_cur.nodes ~= nil @@ -158,24 +154,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 @@ -205,10 +202,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 @@ -223,6 +220,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 @@ -236,14 +238,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 diff --git a/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua b/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua index 214d572c035..049be2bbfa2 100644 --- a/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua @@ -1,6 +1,5 @@ local utils = require("nvim-tree.utils") local core = require("nvim-tree.core") -local lib = require("nvim-tree.lib") local Iterator = require("nvim-tree.iterators.node-iterator") local DirectoryNode = require("nvim-tree.node.directory") @@ -26,10 +25,13 @@ end ---@param keep_buffers boolean function M.fn(keep_buffers) - local node = lib.get_node_at_cursor() local explorer = core.get_explorer() + if not explorer then + return + end - if explorer == nil then + local node = explorer:get_node_at_cursor() + if not node then return end diff --git a/lua/nvim-tree/actions/tree/modifiers/toggles.lua b/lua/nvim-tree/actions/tree/modifiers/toggles.lua index 782ab35e1dd..8a468a959f2 100644 --- a/lua/nvim-tree/actions/tree/modifiers/toggles.lua +++ b/lua/nvim-tree/actions/tree/modifiers/toggles.lua @@ -1,13 +1,14 @@ -local lib = require("nvim-tree.lib") local utils = require("nvim-tree.utils") local core = require("nvim-tree.core") local M = {} ---@param explorer Explorer local function reload(explorer) - local node = lib.get_node_at_cursor() + local node = explorer:get_node_at_cursor() explorer:reload_explorer() - utils.focus_node_or_parent(node) + if node then + utils.focus_node_or_parent(node) + end end local function wrap_explorer(fn) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 7daab9a1ae2..50fd9f2a4fc 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -55,11 +55,24 @@ local function wrap(f) 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 + ---Inject the node as the first argument if present otherwise do nothing. ---@param fn function function to invoke local function wrap_node(fn) return function(node, ...) - node = node or lib.get_node_at_cursor() + node = node or wrap_explorer("get_node_at_cursor")() if node then return fn(node, ...) end @@ -70,24 +83,11 @@ end ---@param fn function function to invoke local function wrap_node_or_nil(fn) return function(node, ...) - node = node or lib.get_node_at_cursor() + node = node or wrap_explorer("get_node_at_cursor")() return fn(node, ...) 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 @@ -146,7 +146,7 @@ Api.tree.change_root_to_node = wrap_node(function(node) end) Api.tree.change_root_to_parent = wrap_node(actions.root.dir_up.fn) -Api.tree.get_node_under_cursor = wrap(lib.get_node_at_cursor) +Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor") Api.tree.get_nodes = wrap(lib.get_nodes) ---@class ApiTreeFindFileOpts diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 6dbbd1720c9..edad77b5d11 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -1,3 +1,4 @@ +local core = require("nvim-tree.core") local git = require("nvim-tree.git") local log = require("nvim-tree.log") local notify = require("nvim-tree.notify") @@ -386,6 +387,19 @@ function Explorer:get_cursor_position() return vim.api.nvim_win_get_cursor(winnr) end +---@return Node|nil +function Explorer:get_node_at_cursor() + local cursor = self:get_cursor_position() + if not cursor then + return + end + + if cursor[1] == 1 and view.is_root_folder_visible(core.get_cwd()) then + return self + end + + return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]] +end function Explorer:setup(opts) config = opts diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 30152d63ae4..6ba4b553d6b 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -196,7 +196,7 @@ local function create_overlay(self) end function LiveFilter:start_filtering() - view.View.live_filter.prev_focused_node = require("nvim-tree.lib").get_node_at_cursor() + view.View.live_filter.prev_focused_node = self.explorer:get_node_at_cursor() self.filter = self.filter or "" self.explorer.renderer:draw() @@ -210,7 +210,7 @@ function LiveFilter:start_filtering() end function LiveFilter:clear_filter() - local node = require("nvim-tree.lib").get_node_at_cursor() + local node = self.explorer:get_node_at_cursor() local last_node = view.View.live_filter.prev_focused_node self.filter = nil diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 2b9ead582aa..1b127e27cbc 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -1,6 +1,5 @@ local view = require("nvim-tree.view") local core = require("nvim-tree.core") -local utils = require("nvim-tree.utils") local events = require("nvim-tree.events") local notify = require("nvim-tree.notify") @@ -13,25 +12,6 @@ local M = { target_winid = nil, } ----@return Node|nil -function M.get_node_at_cursor() - local explorer = core.get_explorer() - if not explorer then - return - end - - local cursor = explorer:get_cursor_position() - if not cursor then - return - end - - if cursor[1] == 1 and view.is_root_folder_visible(core.get_cwd()) then - return explorer - end - - return utils.get_nodes_by_line(explorer.nodes, core.get_nodes_starting_line())[cursor[1]] -end - ---Api.tree.get_nodes ---@return Node[]? function M.get_nodes() diff --git a/lua/nvim-tree/log.lua b/lua/nvim-tree/log.lua index ad8f34cf175..a0964b3f5ba 100644 --- a/lua/nvim-tree/log.lua +++ b/lua/nvim-tree/log.lua @@ -88,14 +88,12 @@ function M.set_inspect_opts(opts) end --- Write to log file the inspection of a node ---- defaults to the node under cursor if none is provided ---@param typ string as per log.types config ----@param node Node? node to be inspected +---@param node Node node to be inspected ---@param fmt string for string.format ---@vararg any arguments for string.format function M.node(typ, node, fmt, ...) if M.enabled(typ) then - node = node or require("nvim-tree.lib").get_node_at_cursor() M.raw(typ, string.format("[%s] [%s] %s\n%s\n", os.date("%Y-%m-%d %H:%M:%S"), typ, (fmt or "???"), vim.inspect(node, inspect_opts)), ...) end end diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index c2da9009ad8..fde1e534f3b 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -151,7 +151,7 @@ function Marks:bulk_move() return end - local node_at_cursor = lib.get_node_at_cursor() + local node_at_cursor = self.explorer:get_node_at_cursor() local default_path = core.get_cwd() if node_at_cursor and node_at_cursor:is(DirectoryNode) then @@ -190,7 +190,7 @@ end ---@private ---@param up boolean function Marks:navigate(up) - local node = lib.get_node_at_cursor() + local node = self.explorer:get_node_at_cursor() if not node then return end From 8994c1e1ef168632eacc5ffe1161737efa7ab802 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Oct 2024 11:53:07 +1100 Subject: [PATCH 08/12] move lib.get_nodes to Explorer --- lua/nvim-tree/api.lua | 3 +-- lua/nvim-tree/explorer/init.lua | 6 ++++++ lua/nvim-tree/lib.lua | 7 ------- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 50fd9f2a4fc..e99d077b9c1 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -1,4 +1,3 @@ -local lib = require("nvim-tree.lib") local core = require("nvim-tree.core") local view = require("nvim-tree.view") local utils = require("nvim-tree.utils") @@ -147,7 +146,7 @@ end) Api.tree.change_root_to_parent = wrap_node(actions.root.dir_up.fn) Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor") -Api.tree.get_nodes = wrap(lib.get_nodes) +Api.tree.get_nodes = wrap_explorer("get_nodes") ---@class ApiTreeFindFileOpts ---@field buf string|number|nil diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index edad77b5d11..7535605d43d 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -401,6 +401,12 @@ function Explorer:get_node_at_cursor() return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]] end +---Api.tree.get_nodes +---@return Node +function Explorer:get_nodes() + return self:clone() +end + function Explorer:setup(opts) config = opts require("nvim-tree.explorer.watch").setup(opts) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 1b127e27cbc..8e6902e5f81 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -12,13 +12,6 @@ local M = { target_winid = nil, } ----Api.tree.get_nodes ----@return Node[]? -function M.get_nodes() - local explorer = core.get_explorer() - return explorer and explorer:clone() -end - function M.set_target_win() local id = vim.api.nvim_get_current_win() local tree_id = view.get_winnr() From 5830585318bc16e104dbf050354dcd225fa58821 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Oct 2024 11:56:10 +1100 Subject: [PATCH 09/12] move place_cursor_on_node to Explorer --- lua/nvim-tree.lua | 31 ++++--------------------------- lua/nvim-tree/explorer/init.lua | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index a45c5c6c820..d5cbba60c4b 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -114,32 +114,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 explorer = core.get_explorer() - if not explorer then - return - end - - local node = explorer: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 @@ -270,7 +244,10 @@ local function setup_autocommands(opts) pattern = "NvimTree_*", callback = function() if utils.is_nvim_tree_buf(0) then - M.place_cursor_on_node() + local explorer = core.get_explorer() + if explorer then + explorer:place_cursor_on_node() + end end end, }) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 7535605d43d..54fbb512e04 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -401,6 +401,27 @@ function Explorer:get_node_at_cursor() return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]] end +function Explorer: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 = self: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 + ---Api.tree.get_nodes ---@return Node function Explorer:get_nodes() From e2e6b2b0957380e1437e686f98f47ab2932a2f74 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Oct 2024 13:39:14 +1100 Subject: [PATCH 10/12] resolve resource leak in purge_all_state --- lua/nvim-tree.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index d5cbba60c4b..42d53c590e3 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -786,13 +786,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 From ff85d8034c220a6b183b291f9ebf4e076b292453 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 21 Oct 2024 15:36:21 +1100 Subject: [PATCH 11/12] move many autocommands into Explorer --- lua/nvim-tree.lua | 113 +------------------------- lua/nvim-tree/explorer/init.lua | 108 +++++++++++++++++++++++- lua/nvim-tree/node/directory-link.lua | 4 + lua/nvim-tree/node/file-link.lua | 4 + lua/nvim-tree/node/file.lua | 4 + lua/nvim-tree/node/root.lua | 4 + 6 files changed, 124 insertions(+), 113 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 42d53c590e3..4390adcf35b 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -1,5 +1,4 @@ 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") @@ -151,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_*", @@ -179,79 +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 - local explorer = core.get_explorer() - if explorer then - explorer:place_cursor_on_node() - end - end - end, - }) - end if opts.sync_root_with_cwd then create_nvim_tree_autocmd("DirChanged", { callback = function() @@ -277,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_*", @@ -330,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 @@ -839,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) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 54fbb512e04..02c0152d5e5 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -1,3 +1,5 @@ +local appearance = require("nvim-tree.appearance") +local buffers = require("nvim-tree.buffers") local core = require("nvim-tree.core") local git = require("nvim-tree.git") local log = require("nvim-tree.log") @@ -25,7 +27,9 @@ local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON local config ---@class (exact) Explorer: RootNode +---@field uid_explorer number vim.uv.hrtime() at construction time ---@field opts table user options +---@field augroup_id integer ---@field renderer Renderer ---@field filters Filters ---@field live_filter LiveFilter @@ -59,6 +63,9 @@ function Explorer:create(path) o.explorer = o + o.uid_explorer = vim.uv.hrtime() + o.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. o.uid_explorer, {}) + o.open = true o.opts = config @@ -69,11 +76,111 @@ function Explorer:create(path) o.marks = Marks:new(config, o) o.clipboard = Clipboard:new(config, o) + o:create_autocmds() + o:_load(o) return o end +function Explorer:destroy() + log.line("dev", "Explorer:destroy") + + vim.api.nvim_del_augroup_by_id(self.augroup_id) + + RootNode.destroy(self) +end + +function Explorer:create_autocmds() + -- reset and draw (highlights) when colorscheme is changed + vim.api.nvim_create_autocmd("ColorScheme", { + group = self.augroup_id, + callback = function() + appearance.setup() + view.reset_winhl() + self:draw() + end, + }) + + vim.api.nvim_create_autocmd("BufWritePost", { + group = self.augroup_id, + callback = function() + if self.opts.auto_reload_on_write and not self.opts.filesystem_watchers.enable then + self:reload_explorer() + end + end, + }) + + vim.api.nvim_create_autocmd("BufReadPost", { + group = self.augroup_id, + callback = function(data) + if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then + utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function() + self:reload_explorer() + end) + end + end, + }) + + -- update opened file buffers + vim.api.nvim_create_autocmd("BufUnload", { + group = self.augroup_id, + callback = function(data) + if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then + utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function() + self:reload_explorer() + end) + end + end, + }) + + vim.api.nvim_create_autocmd("BufEnter", { + group = self.augroup_id, + pattern = "NvimTree_*", + callback = function() + if utils.is_nvim_tree_buf(0) then + if vim.fn.getcwd() ~= core.get_cwd() or (self.opts.reload_on_bufenter and not self.opts.filesystem_watchers.enable) then + self:reload_explorer() + end + end + end, + }) + + vim.api.nvim_create_autocmd("User", { + group = self.augroup_id, + pattern = { "FugitiveChanged", "NeogitStatusRefreshed" }, + callback = function() + if not self.opts.filesystem_watchers.enable and self.opts.git.enable then + self:reload_git() + end + end, + }) + + if self.opts.hijack_cursor then + vim.api.nvim_create_autocmd("CursorMoved", { + group = self.augroup_id, + pattern = "NvimTree_*", + callback = function() + if utils.is_nvim_tree_buf(0) then + self:place_cursor_on_node() + end + end, + }) + end + + if self.opts.modified.enable then + vim.api.nvim_create_autocmd({ "BufModifiedSet", "BufWritePost" }, { + group = self.augroup_id, + callback = function() + utils.debounce("Buf:modified_" .. self.uid_explorer, self.opts.view.debounce_delay, function() + buffers.reload_modified() + self:reload_explorer() + end) + end, + }) + end +end + ---@param node DirectoryNode function Explorer:expand(node) self:_load(node) @@ -430,7 +537,6 @@ end function Explorer:setup(opts) config = opts - require("nvim-tree.explorer.watch").setup(opts) end return Explorer diff --git a/lua/nvim-tree/node/directory-link.lua b/lua/nvim-tree/node/directory-link.lua index f0543825fe9..0e6fd338297 100644 --- a/lua/nvim-tree/node/directory-link.lua +++ b/lua/nvim-tree/node/directory-link.lua @@ -32,6 +32,10 @@ function DirectoryLinkNode:create(explorer, parent, absolute_path, link_to, name return o end +function DirectoryLinkNode:destroy() + DirectoryNode.destroy(self) +end + -----Update the directory GitStatus of link target and the file status of the link itself -----@param parent_ignored boolean -----@param status table|nil diff --git a/lua/nvim-tree/node/file-link.lua b/lua/nvim-tree/node/file-link.lua index 2bdd79f13f3..2d2571f01a8 100644 --- a/lua/nvim-tree/node/file-link.lua +++ b/lua/nvim-tree/node/file-link.lua @@ -28,6 +28,10 @@ function FileLinkNode:create(explorer, parent, absolute_path, link_to, name, fs_ return o end +function FileLinkNode:destroy() + FileNode.destroy(self) +end + -----Update the GitStatus of the target otherwise the link itself -----@param parent_ignored boolean -----@param status table|nil diff --git a/lua/nvim-tree/node/file.lua b/lua/nvim-tree/node/file.lua index 0f01347c0cf..398607c6c9a 100644 --- a/lua/nvim-tree/node/file.lua +++ b/lua/nvim-tree/node/file.lua @@ -36,6 +36,10 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat) return o end +function FileNode:destroy() + Node.destroy(self) +end + ---Update the GitStatus of the file ---@param parent_ignored boolean ---@param status table|nil diff --git a/lua/nvim-tree/node/root.lua b/lua/nvim-tree/node/root.lua index 1b236775e70..2fd037cecf5 100644 --- a/lua/nvim-tree/node/root.lua +++ b/lua/nvim-tree/node/root.lua @@ -23,4 +23,8 @@ function RootNode:is_dotfile() return false end +function RootNode:destroy() + DirectoryNode.destroy(self) +end + return RootNode From 4c9c8852c7034720aaf17f5b7f88501bb687a03a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 25 Oct 2024 12:39:39 +1100 Subject: [PATCH 12/12] post merge tidy --- lua/nvim-tree/actions/finders/find-file.lua | 1 - lua/nvim-tree/actions/moves/item.lua | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lua/nvim-tree/actions/finders/find-file.lua b/lua/nvim-tree/actions/finders/find-file.lua index 34a8ee90d3c..55e2f23d337 100644 --- a/lua/nvim-tree/actions/finders/find-file.lua +++ b/lua/nvim-tree/actions/finders/find-file.lua @@ -45,7 +45,6 @@ function M.fn(path) return node.absolute_path == path_real or node.link_to == path_real end) :applier(function(node) - ---@cast node DirectoryNode local incremented_line = false if not node.group_next then line = line + 1 diff --git a/lua/nvim-tree/actions/moves/item.lua b/lua/nvim-tree/actions/moves/item.lua index 1cc6a3e5e7a..4390fa47564 100644 --- a/lua/nvim-tree/actions/moves/item.lua +++ b/lua/nvim-tree/actions/moves/item.lua @@ -74,8 +74,7 @@ end ---@param node DirectoryNode local function expand_node(node) - if node:is(DirectoryNode) and not node.open then - ---@cast node DirectoryNode + if not node.open then -- Expand the node. -- Should never collapse since we checked open. node:expand_or_collapse(false)