-
-
Notifications
You must be signed in to change notification settings - Fork 631
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 4 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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,5 @@ | ||
| local M = {} | ||
|
|
||
| M.collapse = require("nvim-tree.actions.tree.modifiers.collapse") | ||
| M.expand = require("nvim-tree.actions.tree.modifiers.expand") | ||
|
|
||
| function M.setup(opts) | ||
| M.expand.setup(opts) | ||
| end | ||
|
|
||
| return M |
| 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") | ||
|
|
||
|
|
@@ -167,7 +168,7 @@ function DirectoryNode:expand_or_collapse(toggle_group) | |
| end | ||
|
|
||
| if #self.nodes == 0 then | ||
| self.explorer:expand(self) | ||
| self.explorer:expand_dir_node(self) | ||
| end | ||
|
|
||
| local head_node = self:get_parent_of_group() or self | ||
|
|
@@ -290,4 +291,129 @@ 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 | ||
| ---@param _ integer expansion_count | ||
| ---@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 | ||
|
|
||
| ---@private | ||
| ---@param expansion_count integer | ||
| ---@param node Node | ||
| ---@param should_descend fun(expansion_count: integer, node: Node): boolean | ||
| ---@return boolean | ||
| function DirectoryNode:should_expand(expansion_count, node, should_descend) | ||
| local dir = node:as(DirectoryNode) | ||
| if not dir then | ||
| return false | ||
| end | ||
|
|
||
| if not dir.open and should_descend(expansion_count, node) then | ||
| if #node.nodes == 0 then | ||
| self.explorer:expand_dir_node(dir) -- populate node.group_next | ||
| end | ||
|
|
||
| if dir.group_next then | ||
| local expand_next = self:should_expand(expansion_count, dir.group_next, should_descend) | ||
| if expand_next then | ||
| dir.open = true | ||
| end | ||
| return expand_next | ||
| else | ||
| return true | ||
| end | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| ---@private | ||
| ---@param should_descend fun(expansion_count: integer, node: Node): boolean | ||
| ---@return fun(node): any | ||
| function DirectoryNode:gen_iterator(should_descend) | ||
| local expansion_count = 0 | ||
|
|
||
| return function(parent) | ||
| if parent.parent and parent.nodes and not parent.open then | ||
| expansion_count = expansion_count + 1 | ||
| parent:expand_dir_node() | ||
| end | ||
|
|
||
| Iterator.builder(parent.nodes) | ||
| :hidden() | ||
| :applier(function(node) | ||
| if DirectoryNode:should_expand(expansion_count, node, should_descend) then | ||
| expansion_count = expansion_count + 1 | ||
| node = node:as(DirectoryNode) | ||
| if node then | ||
| self:expand_dir_node() | ||
| end | ||
| 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() | ||
| end | ||
| end | ||
|
|
||
| ---@param expand_opts ApiTreeExpandOpts? | ||
| function DirectoryNode:expand(expand_opts) | ||
| local descend_until_empty_fn = self.descend_until_empty | ||
| local descend_until = self:limit_folder_discovery((expand_opts and expand_opts.expand_until) or descend_until_empty_fn) | ||
| self:gen_iterator(descend_until)(self) | ||
|
|
||
| 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 | ||
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)