From 8f4ab9adb55857342d6a4b4770e809886ef98bd0 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 14 Oct 2024 12:27:49 +1100 Subject: [PATCH 01/20] move last_group_node to DirectoryNode --- lua/nvim-tree/actions/fs/clipboard.lua | 4 +- lua/nvim-tree/actions/fs/create-file.lua | 4 +- lua/nvim-tree/actions/fs/rename-file.lua | 6 ++- lua/nvim-tree/actions/moves/parent.lua | 6 ++- .../actions/tree/modifiers/expand-all.lua | 6 ++- lua/nvim-tree/node/directory.lua | 14 ++++++- lua/nvim-tree/node/init.lua | 14 +++---- lua/nvim-tree/renderer/builder.lua | 39 ++++++++++++------- lua/nvim-tree/renderer/components/padding.lua | 14 +++---- 9 files changed, 71 insertions(+), 36 deletions(-) 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..f08459f964d 100644 --- a/lua/nvim-tree/actions/fs/create-file.lua +++ b/lua/nvim-tree/actions/fs/create-file.lua @@ -5,6 +5,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 = {} ---@param file string @@ -53,7 +55,7 @@ function M.fn(node) nodes = core.get_explorer().nodes, open = true, } - else + elseif node:is(DirectoryNode) then node = node:last_group_node() end 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/parent.lua b/lua/nvim-tree/actions/moves/parent.lua index 88eca47567a..e23a5dc59dc 100644 --- a/lua/nvim-tree/actions/moves/parent.lua +++ b/lua/nvim-tree/actions/moves/parent.lua @@ -2,6 +2,8 @@ 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 = {} ---@param should_close boolean|nil @@ -11,7 +13,9 @@ function M.fn(should_close) return function(node) local explorer = core.get_explorer() - node = node:last_group_node() + if node:is(DirectoryNode) then + node = node:last_group_node() + end if should_close and node.open then node.open = false if explorer then diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index ed898de2a30..aff8791f00b 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[] @@ -17,7 +19,9 @@ end ---@param node Node local function expand(node) - node = node:last_group_node() + if node:is(DirectoryNode) then + node = node:last_group_node() + end node.open = true if #node.nodes == 0 then core.get_explorer():expand(node) diff --git a/lua/nvim-tree/node/directory.lua b/lua/nvim-tree/node/directory.lua index 439034afd3e..e9078ae33ba 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -5,7 +5,7 @@ local BaseNode = require("nvim-tree.node") ---@class (exact) DirectoryNode: BaseNode ---@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 hidden_stats table? -- Each field of this table is a key for source and value for count @@ -116,6 +116,18 @@ function DirectoryNode:get_git_status() end end +-- If node is grouped, return the last node in the group. Otherwise, return the given node. +---@return Node +function DirectoryNode:last_group_node() + local node = self + + while node.group_next do + node = node.group_next or node + end + + return node +end + function DirectoryNode:expand_or_collapse(toggle_group) toggle_group = toggle_group or false if self.has_children then diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index ceca46ce308..824f6818d79 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -1,5 +1,6 @@ local git = require("nvim-tree.git") +---TODO #2886 ---TODO remove all @cast ---TODO remove all references to directory fields: @@ -106,17 +107,12 @@ function BaseNode:is_dotfile() return false end --- If node is grouped, return the last node in the group. Otherwise, return the given node. +---Return self, should only be called on a DirectoryNode +---TODO #2886 remove method or leave in place, warn if practical and non too intrusive ---@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 + error(string.format("\nBaseNode:last_group_node called for '%s'", self.absolute_path)) + return self end ---@param project table? diff --git a/lua/nvim-tree/renderer/builder.lua b/lua/nvim-tree/renderer/builder.lua index 2071bdabcac..ded373809d8 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 -function Builder:build_line(node, idx, num_children) +---@param node Node +---@param idx integer line number starting at 1 +---@param siblings integer of node +function Builder:build_line(node, idx, siblings) -- various components - local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers) + local indent_markers = pad.get_indent_markers(self.depth, idx, siblings, 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,12 +434,12 @@ function Builder:build_lines(node) if not node then node = self.explorer end - local num_children = self:get_nodes_number(node.nodes) + local siblings = self:num_visible(node.nodes) local idx = 1 for _, n in ipairs(node.nodes) do if not n.hidden then self:build_signs(n) - self:build_line(n, idx, num_children) + self:build_line(n, idx, siblings) idx = idx + 1 end end diff --git a/lua/nvim-tree/renderer/components/padding.lua b/lua/nvim-tree/renderer/components/padding.lua index 8ca25e8a6cf..021a91d368d 100644 --- a/lua/nvim-tree/renderer/components/padding.lua +++ b/lua/nvim-tree/renderer/components/padding.lua @@ -19,17 +19,17 @@ local function check_siblings_for_folder(node, with_arrows) return false end -local function get_padding_indent_markers(depth, idx, nodes_number, markers, with_arrows, inline_arrows, node, early_stop) +local function get_padding_indent_markers(depth, idx, siblings, markers, with_arrows, inline_arrows, node, early_stop) local base_padding = with_arrows and (not node.nodes or depth > 0) and " " or "" local padding = (inline_arrows or depth == 0) and base_padding or "" if depth > 0 then local has_folder_sibling = check_siblings_for_folder(node, with_arrows) local indent = string.rep(" ", M.config.indent_width - 1) - markers[depth] = idx ~= nodes_number + markers[depth] = idx ~= siblings for i = 1, depth - early_stop do local glyph - if idx == nodes_number and i == depth then + if idx == siblings and i == depth then local bottom_width = M.config.indent_width - 2 + (with_arrows and not inline_arrows and has_folder_sibling and 2 or 0) glyph = M.config.indent_markers.icons.corner .. string.rep(M.config.indent_markers.icons.bottom, bottom_width) @@ -46,7 +46,7 @@ local function get_padding_indent_markers(depth, idx, nodes_number, markers, wit padding = padding .. glyph elseif inline_arrows then padding = padding - elseif idx ~= nodes_number and depth == i and not node.nodes and has_folder_sibling then + elseif idx ~= siblings and depth == i and not node.nodes and has_folder_sibling then padding = padding .. base_padding .. glyph .. base_padding else padding = padding .. base_padding .. glyph @@ -58,11 +58,11 @@ end ---@param depth integer ---@param idx integer ----@param nodes_number integer +---@param siblings integer ---@param node Node ---@param markers table ---@return HighlightedString[] -function M.get_indent_markers(depth, idx, nodes_number, node, markers, early_stop) +function M.get_indent_markers(depth, idx, siblings, node, markers, early_stop) local str = "" local show_arrows = M.config.icons.show.folder_arrow @@ -71,7 +71,7 @@ function M.get_indent_markers(depth, idx, nodes_number, node, markers, early_sto local indent_width = M.config.indent_width if show_markers then - str = str .. get_padding_indent_markers(depth, idx, nodes_number, markers, show_arrows, inline_arrows, node, early_stop or 0) + str = str .. get_padding_indent_markers(depth, idx, siblings, markers, show_arrows, inline_arrows, node, early_stop or 0) else str = str .. string.rep(" ", depth * indent_width) end From c32ee3e47b3073baa3afb190a304e4f2c46a61f1 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 14 Oct 2024 16:37:46 +1100 Subject: [PATCH 02/20] move add BaseNode:as and more doc --- lua/nvim-tree/actions/moves/item.lua | 4 ++-- lua/nvim-tree/node/directory.lua | 1 + lua/nvim-tree/node/init.lua | 19 ++++++++++++------- 3 files changed, 15 insertions(+), 9 deletions(-) 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/node/directory.lua b/lua/nvim-tree/node/directory.lua index e9078ae33ba..d25521302af 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -128,6 +128,7 @@ function DirectoryNode:last_group_node() return node end +---@param toggle_group boolean function DirectoryNode:expand_or_collapse(toggle_group) toggle_group = toggle_group or false if self.has_children then diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index 824f6818d79..b16d6fc2e8f 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -42,16 +42,14 @@ function BaseNode:destroy() end end ----From plenary ----Checks if the object is an instance +---Object is an instance of class ---This will start with the lowest class and loop over all the superclasses. ----@param self BaseNode ----@param T BaseNode +---@param class table ---@return boolean -function BaseNode:is(T) +function BaseNode:is(class) local mt = getmetatable(self) while mt do - if mt == T then + if mt == class then return true end mt = getmetatable(mt) @@ -59,6 +57,14 @@ function BaseNode:is(T) return false end +---Return object if it is an instance of class, otherwise nil +---@generic T +---@param class T +---@return `T`|nil +function BaseNode:as(class) + return self:is(class) and self or nil +end + ---@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 @@ -202,7 +208,6 @@ 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 {} From ef062a464baf63f561de302f454602447c646c25 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 12:43:46 +1100 Subject: [PATCH 03/20] revert parameter name changes --- lua/nvim-tree/renderer/builder.lua | 10 +++++----- lua/nvim-tree/renderer/components/padding.lua | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lua/nvim-tree/renderer/builder.lua b/lua/nvim-tree/renderer/builder.lua index ded373809d8..9804bfcc4a0 100644 --- a/lua/nvim-tree/renderer/builder.lua +++ b/lua/nvim-tree/renderer/builder.lua @@ -349,10 +349,10 @@ end ---@private ---@param node Node ---@param idx integer line number starting at 1 ----@param siblings integer of node -function Builder:build_line(node, idx, siblings) +---@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, siblings, node, self.markers) + local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers) local arrows = pad.get_arrows(node) -- main components @@ -434,12 +434,12 @@ function Builder:build_lines(node) if not node then node = self.explorer end - local siblings = self:num_visible(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 self:build_signs(n) - self:build_line(n, idx, siblings) + self:build_line(n, idx, num_children) idx = idx + 1 end end diff --git a/lua/nvim-tree/renderer/components/padding.lua b/lua/nvim-tree/renderer/components/padding.lua index 021a91d368d..58d996534ad 100644 --- a/lua/nvim-tree/renderer/components/padding.lua +++ b/lua/nvim-tree/renderer/components/padding.lua @@ -19,17 +19,17 @@ local function check_siblings_for_folder(node, with_arrows) return false end -local function get_padding_indent_markers(depth, idx, siblings, markers, with_arrows, inline_arrows, node, early_stop) +local function get_padding_indent_markers(depth, idx, num_children, markers, with_arrows, inline_arrows, node, early_stop) local base_padding = with_arrows and (not node.nodes or depth > 0) and " " or "" local padding = (inline_arrows or depth == 0) and base_padding or "" if depth > 0 then local has_folder_sibling = check_siblings_for_folder(node, with_arrows) local indent = string.rep(" ", M.config.indent_width - 1) - markers[depth] = idx ~= siblings + markers[depth] = idx ~= num_children for i = 1, depth - early_stop do local glyph - if idx == siblings and i == depth then + if idx == num_children and i == depth then local bottom_width = M.config.indent_width - 2 + (with_arrows and not inline_arrows and has_folder_sibling and 2 or 0) glyph = M.config.indent_markers.icons.corner .. string.rep(M.config.indent_markers.icons.bottom, bottom_width) @@ -46,7 +46,7 @@ local function get_padding_indent_markers(depth, idx, siblings, markers, with_ar padding = padding .. glyph elseif inline_arrows then padding = padding - elseif idx ~= siblings and depth == i and not node.nodes and has_folder_sibling then + elseif idx ~= num_children and depth == i and not node.nodes and has_folder_sibling then padding = padding .. base_padding .. glyph .. base_padding else padding = padding .. base_padding .. glyph @@ -58,11 +58,11 @@ end ---@param depth integer ---@param idx integer ----@param siblings integer +---@param num_children integer ---@param node Node ---@param markers table ---@return HighlightedString[] -function M.get_indent_markers(depth, idx, siblings, node, markers, early_stop) +function M.get_indent_markers(depth, idx, num_children, node, markers, early_stop) local str = "" local show_arrows = M.config.icons.show.folder_arrow @@ -71,7 +71,7 @@ function M.get_indent_markers(depth, idx, siblings, node, markers, early_stop) local indent_width = M.config.indent_width if show_markers then - str = str .. get_padding_indent_markers(depth, idx, siblings, markers, show_arrows, inline_arrows, node, early_stop or 0) + str = str .. get_padding_indent_markers(depth, idx, num_children, markers, show_arrows, inline_arrows, node, early_stop or 0) else str = str .. string.rep(" ", depth * indent_width) end From fdddb469c2113c81cec1a72b151706e0735498cb Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 12:44:46 +1100 Subject: [PATCH 04/20] revert parameter name changes --- lua/nvim-tree/renderer/components/padding.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/renderer/components/padding.lua b/lua/nvim-tree/renderer/components/padding.lua index 58d996534ad..8ca25e8a6cf 100644 --- a/lua/nvim-tree/renderer/components/padding.lua +++ b/lua/nvim-tree/renderer/components/padding.lua @@ -19,17 +19,17 @@ local function check_siblings_for_folder(node, with_arrows) return false end -local function get_padding_indent_markers(depth, idx, num_children, markers, with_arrows, inline_arrows, node, early_stop) +local function get_padding_indent_markers(depth, idx, nodes_number, markers, with_arrows, inline_arrows, node, early_stop) local base_padding = with_arrows and (not node.nodes or depth > 0) and " " or "" local padding = (inline_arrows or depth == 0) and base_padding or "" if depth > 0 then local has_folder_sibling = check_siblings_for_folder(node, with_arrows) local indent = string.rep(" ", M.config.indent_width - 1) - markers[depth] = idx ~= num_children + markers[depth] = idx ~= nodes_number for i = 1, depth - early_stop do local glyph - if idx == num_children and i == depth then + if idx == nodes_number and i == depth then local bottom_width = M.config.indent_width - 2 + (with_arrows and not inline_arrows and has_folder_sibling and 2 or 0) glyph = M.config.indent_markers.icons.corner .. string.rep(M.config.indent_markers.icons.bottom, bottom_width) @@ -46,7 +46,7 @@ local function get_padding_indent_markers(depth, idx, num_children, markers, wit padding = padding .. glyph elseif inline_arrows then padding = padding - elseif idx ~= num_children and depth == i and not node.nodes and has_folder_sibling then + elseif idx ~= nodes_number and depth == i and not node.nodes and has_folder_sibling then padding = padding .. base_padding .. glyph .. base_padding else padding = padding .. base_padding .. glyph @@ -58,11 +58,11 @@ end ---@param depth integer ---@param idx integer ----@param num_children integer +---@param nodes_number integer ---@param node Node ---@param markers table ---@return HighlightedString[] -function M.get_indent_markers(depth, idx, num_children, node, markers, early_stop) +function M.get_indent_markers(depth, idx, nodes_number, node, markers, early_stop) local str = "" local show_arrows = M.config.icons.show.folder_arrow @@ -71,7 +71,7 @@ function M.get_indent_markers(depth, idx, num_children, node, markers, early_sto local indent_width = M.config.indent_width if show_markers then - str = str .. get_padding_indent_markers(depth, idx, num_children, markers, show_arrows, inline_arrows, node, early_stop or 0) + str = str .. get_padding_indent_markers(depth, idx, nodes_number, markers, show_arrows, inline_arrows, node, early_stop or 0) else str = str .. string.rep(" ", depth * indent_width) end From 54c59b9ef4fb7d1eb524f305dbade8cf31ce42ca Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 12:54:46 +1100 Subject: [PATCH 05/20] add Class --- lua/nvim-tree/class.lua | 40 ++++++++++++++++++++++ lua/nvim-tree/node/init.lua | 41 +++-------------------- lua/nvim-tree/renderer/decorator/init.lua | 18 +++------- 3 files changed, 48 insertions(+), 51 deletions(-) create mode 100644 lua/nvim-tree/class.lua 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/node/init.lua b/lua/nvim-tree/node/init.lua index b16d6fc2e8f..254df9edace 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -1,13 +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) BaseNode: Class ---@field type NODE_TYPE ---@field explorer Explorer ---@field absolute_path string @@ -20,21 +21,10 @@ local git = require("nvim-tree.git") ---@field watcher Watcher? ---@field diag_status DiagStatus? ---@field is_dot boolean cached is_dotfile -local BaseNode = {} +local BaseNode = Class:new() ---@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() @@ -42,29 +32,6 @@ function BaseNode:destroy() end 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 BaseNode: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 BaseNode:as(class) - return self:is(class) and self or nil -end - ---@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 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 4f5126986583d41cf1a369c0bb845741c8bede79 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 13:57:05 +1100 Subject: [PATCH 06/20] move group methods into DN --- lua/nvim-tree/api.lua | 5 ++- lua/nvim-tree/explorer/init.lua | 12 ++++++ lua/nvim-tree/node/directory.lua | 63 ++++++++++++++++++++++++++++++++ lua/nvim-tree/node/init.lua | 53 --------------------------- 4 files changed, 79 insertions(+), 54 deletions(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index c6b5de44b53..d21929f2b34 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -210,13 +210,16 @@ 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/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 0045ba9b243..2c05fac4b61 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -170,7 +170,13 @@ function Explorer:reload(node, git_status) ) local is_root = not node.parent + --- + --- @cast node DirectoryNode + --- local child_folder_only = node:has_one_child_folder() and node.nodes[1] + --- + --- @cast child_folder_only DirectoryNode + --- 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) @@ -311,7 +317,13 @@ function Explorer:explore(node, status, parent) self:populate_children(handle, cwd, node, status, parent) local is_root = not node.parent + --- + --- @cast node DirectoryNode + --- local child_folder_only = node:has_one_child_folder() and node.nodes[1] + --- + --- @cast child_folder_only DirectoryNode + --- 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 child_status = git.load_project_status(child_cwd) diff --git a/lua/nvim-tree/node/directory.lua b/lua/nvim-tree/node/directory.lua index d25521302af..1db2d364995 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -128,6 +128,66 @@ function DirectoryNode:last_group_node() return node end +---@return boolean +function DirectoryNode: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 + +---@private +---@return Node[] +function DirectoryNode: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 + +---@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 is_root = not self.parent + local child_folder_only = self:has_one_child_folder() and self.nodes[1] + --- + --- @cast child_folder_only DirectoryNode + --- + if self.explorer.opts.renderer.group_empty and not is_root and child_folder_only then + 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 +---@private +function DirectoryNode: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] + end +end + ---@param toggle_group boolean function DirectoryNode:expand_or_collapse(toggle_group) toggle_group = toggle_group or false @@ -140,6 +200,9 @@ function DirectoryNode:expand_or_collapse(toggle_group) end local head_node = self:get_parent_of_group() + --- + --- @cast head_node DirectoryNode + --- if toggle_group then head_node:toggle_group_folders() end diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index 254df9edace..1826ac2c9ac 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -32,11 +32,6 @@ function BaseNode:destroy() end end ----@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 -end - --luacheck: push ignore 212 ---Update the GitStatus of the node ---@param parent_ignored boolean @@ -146,54 +141,6 @@ function BaseNode:get_parent_of_group() 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 - 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] - end -end - ---Create a sanitized partial copy of a node, populating children recursively. ---@return BaseNode cloned function BaseNode:clone() From d5e018aac51f7a6e7d37cfa2b84e67c5f1fc7d74 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 16:15:47 +1100 Subject: [PATCH 07/20] tidy group methods --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/actions/moves/parent.lua | 2 +- lua/nvim-tree/explorer/init.lua | 47 +++++++++-------------- lua/nvim-tree/explorer/watch.lua | 2 +- lua/nvim-tree/node/directory.lua | 53 ++++++++++++++++---------- lua/nvim-tree/node/init.lua | 31 ++++----------- 6 files changed, 61 insertions(+), 76 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 16f2fa33ac3..462c25fbbad 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:group_parent_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/moves/parent.lua b/lua/nvim-tree/actions/moves/parent.lua index e23a5dc59dc..0a419fcff51 100644 --- a/lua/nvim-tree/actions/moves/parent.lua +++ b/lua/nvim-tree/actions/moves/parent.lua @@ -24,7 +24,7 @@ function M.fn(should_close) return end - local parent = node:get_parent_of_group().parent + local parent = node:group_parent_or_node().parent if not parent or not parent.parent then return view.set_cursor({ 1, 0 }) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 2c05fac4b61..af7c3ad970d 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,17 +170,10 @@ function Explorer:reload(node, git_status) end, node.nodes) ) - local is_root = not node.parent - --- - --- @cast node DirectoryNode - --- - local child_folder_only = node:has_one_child_folder() and node.nodes[1] - --- - --- @cast child_folder_only DirectoryNode - --- - 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 @@ -225,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) @@ -301,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 @@ -317,18 +311,12 @@ function Explorer:explore(node, status, parent) self:populate_children(handle, cwd, node, status, parent) local is_root = not node.parent - --- - --- @cast node DirectoryNode - --- - local child_folder_only = node:has_one_child_folder() and node.nodes[1] - --- - --- @cast child_folder_only DirectoryNode - --- - 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) @@ -347,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/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.lua b/lua/nvim-tree/node/directory.lua index 1db2d364995..ed0be05ffc5 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -116,6 +116,22 @@ function DirectoryNode:get_git_status() end end +---Refresh contents and git status for a single node +function DirectoryNode:refresh() + local node = self:group_parent_or_node() + 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 Node function DirectoryNode:last_group_node() @@ -128,15 +144,18 @@ function DirectoryNode:last_group_node() return node end ----@return boolean -function DirectoryNode: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 +---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 ---@return Node[] function DirectoryNode:get_all_nodes_in_group() - local next_node = self:get_parent_of_group() + local next_node = self:group_parent_or_node() local nodes = {} while next_node do table.insert(nodes, next_node) @@ -162,14 +181,10 @@ end ---@private ---@return Node[] function DirectoryNode:group_empty_folders() - local is_root = not self.parent - local child_folder_only = self:has_one_child_folder() and self.nodes[1] - --- - --- @cast child_folder_only DirectoryNode - --- - if self.explorer.opts.renderer.group_empty and not is_root and child_folder_only then - self.group_next = child_folder_only - local ns = child_folder_only: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 @@ -180,11 +195,10 @@ end -- 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() - local cur = self - while cur and cur.group_next do - cur.nodes = { cur.group_next } - cur.group_next = nil - cur = cur.nodes[1] + if self.group_next then + self.group_next:ungroup_empty_folders() + self.nodes = { self.group_next } + self.group_next = nil end end @@ -199,10 +213,7 @@ function DirectoryNode:expand_or_collapse(toggle_group) self.explorer:expand(self) end - local head_node = self:get_parent_of_group() - --- - --- @cast head_node DirectoryNode - --- + local head_node = self:group_parent_or_node() if toggle_group then head_node:toggle_group_folders() end diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index 1826ac2c9ac..637eca148c6 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -38,6 +38,7 @@ end ---@param status table? function BaseNode:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local end + --luacheck: pop ---@return GitStatus? @@ -115,30 +116,14 @@ 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 or the node itself +---@return Node +function BaseNode:group_parent_or_node() + if self.parent and self.parent.group_next then + return self.parent:group_parent_or_node() + else + return self end - return node end ---Create a sanitized partial copy of a node, populating children recursively. From 307ead2c5033ed240e29c50608a89ac5dee0ee9a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 16:35:02 +1100 Subject: [PATCH 08/20] tidy group methods --- lua/nvim-tree/node/directory.lua | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lua/nvim-tree/node/directory.lua b/lua/nvim-tree/node/directory.lua index ed0be05ffc5..93c5188e5b6 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -135,13 +135,7 @@ end -- If node is grouped, return the last node in the group. Otherwise, return the given node. ---@return Node function DirectoryNode:last_group_node() - local node = self - - while node.group_next do - node = node.group_next or node - end - - return node + return self.group_next and self.group_next:last_group_node() or self end ---Return the one and only one child directory From daf27a8a70cfa4d8c64d4fbd653f85ad9cedcb7b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 16:45:30 +1100 Subject: [PATCH 09/20] tidy group methods --- lua/nvim-tree/node/directory.lua | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/lua/nvim-tree/node/directory.lua b/lua/nvim-tree/node/directory.lua index 93c5188e5b6..03f9302346b 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -146,18 +146,6 @@ function DirectoryNode:single_child_directory() end end ----@private ----@return Node[] -function DirectoryNode:get_all_nodes_in_group() - local next_node = self:group_parent_or_node() - local nodes = {} - while next_node do - table.insert(nodes, next_node) - next_node = next_node.group_next - end - return nodes -end - ---@private -- Toggle group empty folders function DirectoryNode:toggle_group_folders() @@ -219,8 +207,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() From 3bc4d4591accc114712584aed4a4ac742d2a565b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 17:09:53 +1100 Subject: [PATCH 10/20] tidy group methods --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/actions/moves/parent.lua | 2 +- lua/nvim-tree/node/directory.lua | 4 ++-- lua/nvim-tree/node/init.lua | 21 ++++++++++++++------- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 462c25fbbad..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:group_parent_or_node() + 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/moves/parent.lua b/lua/nvim-tree/actions/moves/parent.lua index 0a419fcff51..cf0512787e7 100644 --- a/lua/nvim-tree/actions/moves/parent.lua +++ b/lua/nvim-tree/actions/moves/parent.lua @@ -24,7 +24,7 @@ function M.fn(should_close) return end - local parent = node:group_parent_or_node().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 }) diff --git a/lua/nvim-tree/node/directory.lua b/lua/nvim-tree/node/directory.lua index 03f9302346b..aaee37243d9 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -118,7 +118,7 @@ end ---Refresh contents and git status for a single node function DirectoryNode:refresh() - local node = self:group_parent_or_node() + 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() @@ -195,7 +195,7 @@ function DirectoryNode:expand_or_collapse(toggle_group) self.explorer:expand(self) end - local head_node = self:group_parent_or_node() + local head_node = self:get_parent_of_group() or self if toggle_group then head_node:toggle_group_folders() end diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index 637eca148c6..ff913b92872 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -116,13 +116,20 @@ function BaseNode:update_parent_statuses(project, root) end end ----Get the highest parent of grouped nodes or the node itself ----@return Node -function BaseNode:group_parent_or_node() - if self.parent and self.parent.group_next then - return self.parent:group_parent_or_node() - else - return self +---Get the highest parent of grouped nodes, nil when not grouped +---@return DirectoryNode? +function BaseNode:get_parent_of_group() + if not self.parent or not self.parent.group_next then + return nil + end + + 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 From f09c518390009c9b41ce815f5132e1628e570604 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 17:15:32 +1100 Subject: [PATCH 11/20] parent is DirectoryNode --- lua/nvim-tree/actions/finders/find-file.lua | 1 + lua/nvim-tree/explorer/init.lua | 2 +- lua/nvim-tree/node/directory-link.lua | 2 +- lua/nvim-tree/node/directory.lua | 2 +- lua/nvim-tree/node/factory.lua | 2 +- lua/nvim-tree/node/file-link.lua | 2 +- lua/nvim-tree/node/file.lua | 2 +- lua/nvim-tree/node/init.lua | 2 +- 8 files changed, 8 insertions(+), 7 deletions(-) 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/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index af7c3ad970d..b0cbc99665a 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -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) 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 aaee37243d9..172cfab32ba 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -13,7 +13,7 @@ local DirectoryNode = BaseNode: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 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..78f81e11955 100644 --- a/lua/nvim-tree/node/file.lua +++ b/lua/nvim-tree/node/file.lua @@ -9,7 +9,7 @@ local FileNode = BaseNode: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? diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index ff913b92872..1653e71dd97 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -17,7 +17,7 @@ local Class = require("nvim-tree.class") ---@field git_status GitStatus? ---@field hidden boolean ---@field name string ----@field parent Node? +---@field parent DirectoryNode? ---@field watcher Watcher? ---@field diag_status DiagStatus? ---@field is_dot boolean cached is_dotfile From 579fc7b66c4e6424d770d7651641684bd6fba6db Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 17:34:55 +1100 Subject: [PATCH 12/20] tidy expand all --- .../actions/tree/modifiers/expand-all.lua | 18 ++++++++++++------ lua/nvim-tree/node/directory.lua | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index aff8791f00b..55b4ea1a52f 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -17,11 +17,9 @@ local function to_lookup_table(list) return table end ----@param node Node +---@param node DirectoryNode local function expand(node) - if node:is(DirectoryNode) then - node = node:last_group_node() - end + node = node:last_group_node() node.open = true if #node.nodes == 0 then core.get_explorer():expand(node) @@ -40,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 @@ -48,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) @@ -65,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/node/directory.lua b/lua/nvim-tree/node/directory.lua index 172cfab32ba..d8649fc82e9 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -133,7 +133,7 @@ function DirectoryNode:refresh() end -- If node is grouped, return the last node in the group. Otherwise, return the given node. ----@return Node +---@return DirectoryNode function DirectoryNode:last_group_node() return self.group_next and self.group_next:last_group_node() or self end From 8b313ab455be66bc7229a4dd351d01a3005178d7 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 17:42:20 +1100 Subject: [PATCH 13/20] BaseNode -> Node --- lua/nvim-tree/node/directory.lua | 10 +++++----- lua/nvim-tree/node/file.lua | 8 ++++---- lua/nvim-tree/node/init.lua | 34 +++++++++++++++----------------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/lua/nvim-tree/node/directory.lua b/lua/nvim-tree/node/directory.lua index d8649fc82e9..bef30cc26ed 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -1,15 +1,15 @@ 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 DirectoryNode? -- If node is grouped, this points to the next child dir/link node ---@field nodes Node[] ---@field open boolean ---@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 @@ -51,7 +51,7 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat) end function DirectoryNode:destroy() - BaseNode.destroy(self) + Node.destroy(self) if self.nodes then for _, node in pairs(self.nodes) do node:destroy() @@ -220,7 +220,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/file.lua b/lua/nvim-tree/node/file.lua index 78f81e11955..dceeedf63a9 100644 --- a/lua/nvim-tree/node/file.lua +++ b/lua/nvim-tree/node/file.lua @@ -1,11 +1,11 @@ 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 @@ -56,7 +56,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 1653e71dd97..901ff048922 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -8,7 +8,7 @@ local Class = require("nvim-tree.class") ---Abstract Node class. ---Uses the abstract factory pattern to instantiate child instances. ----@class (exact) BaseNode: Class +---@class (exact) Node: Class ---@field type NODE_TYPE ---@field explorer Explorer ---@field absolute_path string @@ -21,11 +21,9 @@ local Class = require("nvim-tree.class") ---@field watcher Watcher? ---@field diag_status DiagStatus? ---@field is_dot boolean cached is_dotfile -local BaseNode = Class:new() +local Node = Class:new() ----@alias Node RootNode|BaseNode|DirectoryNode|FileNode|DirectoryLinkNode|FileLinkNode - -function BaseNode:destroy() +function Node:destroy() if self.watcher then self.watcher:destroy() self.watcher = nil @@ -36,17 +34,17 @@ end ---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 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 @@ -58,13 +56,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) == ".")) @@ -79,14 +77,14 @@ end ---Return self, should only be called on a DirectoryNode ---TODO #2886 remove method or leave in place, warn if practical and non too intrusive ---@return Node -function BaseNode:last_group_node() - error(string.format("\nBaseNode:last_group_node called for '%s'", self.absolute_path)) +function Node:last_group_node() + error(string.format("\nNode:last_group_node called for '%s'", self.absolute_path)) return self 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 @@ -118,7 +116,7 @@ end ---Get the highest parent of grouped nodes, nil when not grouped ---@return DirectoryNode? -function BaseNode:get_parent_of_group() +function Node:get_parent_of_group() if not self.parent or not self.parent.group_next then return nil end @@ -134,12 +132,12 @@ function BaseNode:get_parent_of_group() 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, @@ -158,4 +156,4 @@ function BaseNode:clone() return clone end -return BaseNode +return Node From dcaf85280732765e44d95a8064bf1b37cf98ac0d Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 19 Oct 2024 17:52:01 +1100 Subject: [PATCH 14/20] move watcher to DirectoryNode --- lua/nvim-tree/api.lua | 3 --- lua/nvim-tree/node/directory.lua | 9 ++++++++- lua/nvim-tree/node/file.lua | 1 - lua/nvim-tree/node/init.lua | 7 +------ 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index d21929f2b34..bacdccd5279 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -217,9 +217,6 @@ local function open_or_expand_or_dir_up(mode, toggle_group) 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/node/directory.lua b/lua/nvim-tree/node/directory.lua index bef30cc26ed..cf423364a74 100644 --- a/lua/nvim-tree/node/directory.lua +++ b/lua/nvim-tree/node/directory.lua @@ -8,6 +8,7 @@ local Node = require("nvim-tree.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 = Node:new() @@ -51,12 +52,18 @@ function DirectoryNode:create(explorer, parent, absolute_path, name, fs_stat) end function DirectoryNode:destroy() - Node.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 diff --git a/lua/nvim-tree/node/file.lua b/lua/nvim-tree/node/file.lua index dceeedf63a9..0f01347c0cf 100644 --- a/lua/nvim-tree/node/file.lua +++ b/lua/nvim-tree/node/file.lua @@ -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 "", diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index 901ff048922..1906c20d5b8 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -18,16 +18,11 @@ local Class = require("nvim-tree.class") ---@field hidden boolean ---@field name string ---@field parent DirectoryNode? ----@field watcher Watcher? ---@field diag_status DiagStatus? ---@field is_dot boolean cached is_dotfile local Node = Class:new() function Node:destroy() - if self.watcher then - self.watcher:destroy() - self.watcher = nil - end end --luacheck: push ignore 212 @@ -35,6 +30,7 @@ end ---@param parent_ignored boolean ---@param status table? function Node:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local + ---TODO find a way to declare abstract methods end --luacheck: pop @@ -149,7 +145,6 @@ function Node:clone() is_dot = self.is_dot, name = self.name, parent = nil, - watcher = nil, diag_status = nil, } From cd89c7541b29f4c2d69a89268b8519c33cafb103 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Oct 2024 15:19:37 +1100 Subject: [PATCH 15/20] last_group_node is DirectoryNode only --- lua/nvim-tree/api.lua | 5 +++-- lua/nvim-tree/node/init.lua | 8 -------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index bacdccd5279..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) diff --git a/lua/nvim-tree/node/init.lua b/lua/nvim-tree/node/init.lua index 1906c20d5b8..a2af3afe529 100644 --- a/lua/nvim-tree/node/init.lua +++ b/lua/nvim-tree/node/init.lua @@ -70,14 +70,6 @@ function Node:is_dotfile() return false end ----Return self, should only be called on a DirectoryNode ----TODO #2886 remove method or leave in place, warn if practical and non too intrusive ----@return Node -function Node:last_group_node() - error(string.format("\nNode:last_group_node called for '%s'", self.absolute_path)) - return self -end - ---@param project table? ---@param root string? function Node:update_parent_statuses(project, root) From 5e5cdaa6590274e253906c63f057e233091bda74 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Oct 2024 16:24:25 +1100 Subject: [PATCH 16/20] simplify create-file --- lua/nvim-tree/actions/fs/create-file.lua | 31 +++++++----------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/lua/nvim-tree/actions/fs/create-file.lua b/lua/nvim-tree/actions/fs/create-file.lua index f08459f964d..455f6f564f7 100644 --- a/lua/nvim-tree/actions/fs/create-file.lua +++ b/lua/nvim-tree/actions/fs/create-file.lua @@ -5,6 +5,7 @@ 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 = {} @@ -31,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, - } - elseif node:is(DirectoryNode) then - 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 ", From 735381a442689704c40d6c83c7d3c01fd1454503 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Oct 2024 16:54:03 +1100 Subject: [PATCH 17/20] simplify parent --- lua/nvim-tree/actions/moves/parent.lua | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lua/nvim-tree/actions/moves/parent.lua b/lua/nvim-tree/actions/moves/parent.lua index cf0512787e7..9502af46f1c 100644 --- a/lua/nvim-tree/actions/moves/parent.lua +++ b/lua/nvim-tree/actions/moves/parent.lua @@ -1,6 +1,5 @@ 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") @@ -11,17 +10,16 @@ local M = {} function M.fn(should_close) should_close = should_close or false + ---@param node Node return function(node) - local explorer = core.get_explorer() - if node:is(DirectoryNode) then - node = node:last_group_node() - end - 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() or node).parent @@ -30,16 +28,14 @@ function M.fn(should_close) 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 From 01f66abd09b90ba3dbd95f7a3cd27d7ea96d1185 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Oct 2024 17:04:53 +1100 Subject: [PATCH 18/20] simplify collapse-all --- lua/nvim-tree/actions/tree/modifiers/collapse-all.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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) From dec0568f68616a3bcd3a4bda65a75946955140b0 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Oct 2024 17:14:42 +1100 Subject: [PATCH 19/20] simplify live-filter --- lua/nvim-tree/explorer/live-filter.lua | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) 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_) From 7582f6714fa48fd39ab759a8221d52f7ceaec99d Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 20 Oct 2024 17:22:38 +1100 Subject: [PATCH 20/20] style --- lua/nvim-tree/actions/tree/modifiers/expand-all.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua index 55b4ea1a52f..ffede0e6d6f 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand-all.lua @@ -47,14 +47,14 @@ local function gen_iterator() Iterator.builder(parent.nodes) :hidden() - ---@param node DirectoryNode + ---@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 + ---@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)