-
-
Notifications
You must be signed in to change notification settings - Fork 610
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
feat: Allow to expand until certain condition is met #2790
Open
ghostbuster91
wants to merge
16
commits into
nvim-tree:master
Choose a base branch
from
ghostbuster91:expand_until
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−11
Open
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cd96cf9
Add new parameter to expand function
ghostbuster91 9f0a224
feat: Add more controll to expand-all function
ghostbuster91 f691687
Working better
ghostbuster91 7cf4cf8
Remove print statements
ghostbuster91 e8c4ce4
Pass function to populate_node to userland
ghostbuster91 4510332
format code
ghostbuster91 a3f1588
Call populate_node in case it wasn't called by the user
ghostbuster91 e0e12a4
Ignore unused variable
ghostbuster91 96ffae1
Remove iterator from should_expand
ghostbuster91 7bda685
Cleanup code from redundant stuff
ghostbuster91 06aad16
Add documentation
ghostbuster91 57af3c4
Try to minimize the changes
ghostbuster91 38d9e07
Minimize amount of changes further
ghostbuster91 b506e05
Remove redundant condition
ghostbuster91 b67209a
luals types for expand_until, rename variable for consistency
alex-courtis 3c093d4
doc format and example clarification
alex-courtis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,25 +18,37 @@ local function to_lookup_table(list) | |
end | ||
|
||
---@param node Node | ||
local function expand(node) | ||
node = lib.get_last_group_node(node) | ||
node.open = true | ||
local function populate_node(node) | ||
-- noop if it is a file | ||
if node.nodes == nil then | ||
return | ||
end | ||
if #node.nodes == 0 then | ||
local cwd = node.link_to or node.absolute_path | ||
local handle = vim.loop.fs_scandir(cwd) | ||
if not handle then | ||
return | ||
end | ||
core.get_explorer():expand(node) | ||
end | ||
end | ||
|
||
---@param expansion_count integer | ||
---@param node Node | ||
---@return boolean | ||
local function should_expand(expansion_count, node) | ||
local function expand_until_max_or_empty(expansion_count, node) | ||
local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY | ||
local should_exclude = M.EXCLUDE[node.name] | ||
return not should_halt and node.nodes and not node.open and not should_exclude | ||
end | ||
|
||
local function gen_iterator() | ||
local function gen_iterator(should_expand) | ||
local expansion_count = 0 | ||
local function expand(node) | ||
populate_node(node) | ||
node = lib.get_last_group_node(node) | ||
node.open = true | ||
end | ||
|
||
return function(parent) | ||
if parent.parent and parent.nodes and not parent.open then | ||
|
@@ -47,13 +59,14 @@ local function gen_iterator() | |
Iterator.builder(parent.nodes) | ||
:hidden() | ||
:applier(function(node) | ||
if should_expand(expansion_count, node) then | ||
if should_expand(expansion_count, node, populate_node) then | ||
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. |
||
expansion_count = expansion_count + 1 | ||
expand(node) | ||
end | ||
end) | ||
:recursor(function(node) | ||
return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes)) | ||
local should_recurse = should_expand(expansion_count - 1, node, populate_node) | ||
return expansion_count < M.MAX_FOLDER_DISCOVERY and should_recurse and node.nodes | ||
end) | ||
:iterate() | ||
|
||
|
@@ -64,9 +77,11 @@ local function gen_iterator() | |
end | ||
|
||
---@param base_node table | ||
function M.fn(base_node) | ||
---@param expand_opts ApiTreeExpandAllOpts|nil | ||
function M.fn(base_node, expand_opts) | ||
local expand_until = (expand_opts and expand_opts.expand_until) or expand_until_max_or_empty | ||
local node = base_node.nodes and base_node or core.get_explorer() | ||
if gen_iterator()(node) then | ||
if gen_iterator(expand_until)(node) then | ||
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") | ||
end | ||
renderer.draw() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ end | |
---@param node Node | ||
---@return boolean | ||
function M.has_one_child_folder(node) | ||
return #node.nodes == 1 and node.nodes[1].nodes and vim.loop.fs_access(node.nodes[1].absolute_path, "R") or false | ||
return node.nodes ~= nil and #node.nodes == 1 and node.nodes[1].nodes and vim.loop.fs_access(node.nodes[1].absolute_path, "R") or false | ||
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. thought: guarding against calling this function with a file node |
||
end | ||
|
||
---@param node Node | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I needed to wrap this in a function:
or
We've hit two edges in our documentation here:
{opts}
- this is the first one.That's OK - we can add those in a follow up PR to keep this one clear: #2899
Pushed minimal doc update.