-
-
Notifications
You must be signed in to change notification settings - Fork 633
refactor(#3225): multi instance expand #3234
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
Changes from 12 commits
dd901f7
7933869
7481c19
a192e88
b0ac951
2b8623e
2f18543
9e19b5e
7822481
eb52ca7
2f851d5
8f3fd20
34c6f07
afb40e3
456f817
ad9e520
721e3cd
01d45ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| local git_utils = require("nvim-tree.git.utils") | ||
| local icons = require("nvim-tree.renderer.components.devicons") | ||
| local notify = require("nvim-tree.notify") | ||
| local Iterator = require("nvim-tree.iterators.node-iterator") | ||
|
|
||
| local Node = require("nvim-tree.node") | ||
|
|
||
|
|
@@ -290,4 +291,92 @@ function DirectoryNode:clone(api_nodes) | |
| return clone | ||
| end | ||
|
|
||
| ---@private | ||
| ---@param should_descend fun(expansion_count: integer, node: Node): boolean | ||
| ---@return fun(expansion_count: integer, node: Node): boolean | ||
| function DirectoryNode:limit_folder_discovery(should_descend) | ||
| local MAX_FOLDER_DISCOVERY = self.explorer.opts.actions.expand_all.max_folder_discovery | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice one. Reading it here will allow us to dynamically change |
||
| return function(expansion_count) | ||
| local should_halt = expansion_count >= MAX_FOLDER_DISCOVERY | ||
| if should_halt then | ||
| notify.warn("expansion iteration was halted after " .. MAX_FOLDER_DISCOVERY .. " discovered folders") | ||
| return false | ||
| end | ||
|
|
||
| return should_descend(expansion_count, self) | ||
| end | ||
| end | ||
|
|
||
| ---@private | ||
| ---@param list string[] | ||
| ---@return table | ||
| local function to_lookup_table(list) | ||
| local table = {} | ||
| for _, element in ipairs(list) do | ||
| table[element] = true | ||
| end | ||
|
|
||
| return table | ||
| end | ||
|
|
||
| ---@private | ||
| ---@return boolean | ||
| function DirectoryNode:descend_until_empty() | ||
| local EXCLUDE = to_lookup_table(self.explorer.opts.actions.expand_all.exclude) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again nice, allows dynamic changes to |
||
| local should_exclude = EXCLUDE[self.name] | ||
| return not should_exclude | ||
| end | ||
|
|
||
| ---@param expand_opts ApiTreeExpandOpts? | ||
| function DirectoryNode:expand(expand_opts) | ||
| local expansion_count = 0 | ||
|
|
||
| local function safe_descend_until_empty(_, node) | ||
| return self.descend_until_empty(node) -- calling with dot so that node is attached as self | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I understand. That's a pragmatic choice that avoids rewriting most of the descend functionality. |
||
| end | ||
|
|
||
| local should_descend = self:limit_folder_discovery((expand_opts and expand_opts.expand_until) or safe_descend_until_empty) | ||
|
|
||
|
|
||
| if self.parent and self.nodes and not self.open then | ||
| expansion_count = expansion_count + 1 | ||
| self:expand_dir_node() | ||
| end | ||
|
|
||
| Iterator.builder(self.nodes) | ||
| :hidden() | ||
| :applier(function(node) | ||
| if node:should_expand(expansion_count, should_descend) then | ||
| expansion_count = expansion_count + 1 | ||
| node:expand_dir_node() | ||
| end | ||
| end) | ||
| :recursor(function(node) | ||
| if not should_descend(expansion_count, node) then | ||
| return nil | ||
| end | ||
|
|
||
| if node.group_next then | ||
| return { node.group_next } | ||
| end | ||
|
|
||
| if node.open and node.nodes then | ||
| return node.nodes | ||
| end | ||
|
|
||
| return nil | ||
| end) | ||
| :iterate() | ||
|
|
||
| self.explorer.renderer:draw() | ||
| end | ||
|
|
||
| function DirectoryNode:expand_dir_node() | ||
| local node = self:last_group_node() | ||
| node.open = true | ||
| if #node.nodes == 0 then | ||
| self.explorer:expand_dir_node(node) | ||
| end | ||
| end | ||
|
|
||
| return DirectoryNode | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -144,4 +144,40 @@ function Node:clone(api_nodes) | |
| return clone | ||
| end | ||
|
|
||
| ---@param expansion_count integer | ||
| ---@param should_descend fun(expansion_count: integer, node: Node): boolean | ||
| ---@return boolean | ||
| function Node:should_expand(expansion_count, should_descend) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks reasonable, however a parent class should never be aware of child classes. The fact that we have a circular dependency highlights this. We should be using dynamic dispatch to decide what action to take, something like: ---@param expansion_count integer
---@param should_descend fun(expansion_count: integer, node: Node): boolean
---@return boolean
function Node:should_expand(expansion_count, should_descend)
return false
end
---@param expansion_count integer
---@param should_descend fun(expansion_count: integer, node: Node): boolean
---@return boolean
function DirectoryNode:should_expand(expansion_count, should_descend)
if not self.open and should_descend(expansion_count, self) then
if #self.nodes == 0 then
self.explorer:expand_dir_node(self) -- populate node.group_next
end |
||
| local DirectoryNode = require("nvim-tree.node.directory") | ||
|
|
||
| local dir = self:as(DirectoryNode) | ||
| if not dir then | ||
| return false | ||
| end | ||
|
|
||
| if not dir.open and should_descend(expansion_count, self) then | ||
| if #self.nodes == 0 then | ||
| self.explorer:expand_dir_node(dir) -- populate node.group_next | ||
| end | ||
|
|
||
| if dir.group_next then | ||
| local expand_next = dir.group_next:should_expand(expansion_count, should_descend) | ||
| if expand_next then | ||
| dir.open = true | ||
| end | ||
| return expand_next | ||
| else | ||
| return true | ||
| end | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| ---@param expand_opts ApiTreeExpandOpts? | ||
| function Node:expand(expand_opts) | ||
| if self.parent then | ||
| self.parent:expand(expand_opts) | ||
| end | ||
| end | ||
|
|
||
| return Node | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we can remove this file completely.
The
collapserequire does nothing.collapsehas no setup to call: it is completely stateless and is called on demand e.g.Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all)