Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Merged
merged 20 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/actions/finders/find-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lua/nvim-tree/actions/fs/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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]
Expand Down
33 changes: 11 additions & 22 deletions lua/nvim-tree/actions/fs/create-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ",
Expand Down
6 changes: 5 additions & 1 deletion lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {},
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/actions/moves/item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions lua/nvim-tree/actions/moves/parent.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lua/nvim-tree/actions/tree/modifiers/collapse-all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions lua/nvim-tree/actions/tree/modifiers/expand-all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions lua/nvim-tree/class.lua
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading