-
-
Notifications
You must be signed in to change notification settings - Fork 629
feat: Allow to expand nodes until certain condition is met #3166
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
Open
ghostbuster91
wants to merge
2
commits into
nvim-tree:master
Choose a base branch
from
ghostbuster91:expand-until-2
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.
+102
−50
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -121,6 +121,45 @@ local function wrap_explorer_member(explorer_member, member_method) | |
end) | ||
end | ||
|
||
--- | ||
---@class NodeEditOpts | ||
---@field quit_on_open boolean|nil default false | ||
---@field focus boolean|nil default true | ||
|
||
---@param mode string | ||
---@param node Node | ||
---@param edit_opts NodeEditOpts? | ||
local function edit(mode, node, edit_opts) | ||
local file_link = node:as(FileLinkNode) | ||
local path = file_link and file_link.link_to or node.absolute_path | ||
local cur_tabpage = vim.api.nvim_get_current_tabpage() | ||
|
||
local explorer = core.get_explorer() | ||
|
||
actions.node.open_file.fn(mode, path) | ||
|
||
edit_opts = edit_opts or {} | ||
|
||
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" | ||
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then | ||
if explorer then | ||
explorer.view:close(cur_tabpage, "api.edit " .. mode) | ||
end | ||
end | ||
|
||
local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" | ||
local focus = edit_opts.focus == nil or edit_opts.focus == true | ||
if not mode_unsupported_focus and not focus then | ||
-- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab | ||
if mode == "tabnew" then | ||
vim.cmd(":tabprev") | ||
end | ||
if explorer then | ||
explorer.view:focus() | ||
end | ||
end | ||
end | ||
|
||
---@class ApiTreeOpenOpts | ||
---@field path string|nil path | ||
---@field current_window boolean|nil default false | ||
|
@@ -186,7 +225,25 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn) | |
---@field keep_buffers boolean|nil default false | ||
|
||
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all) | ||
|
||
---@class ApiTreeExpandAllOpts | ||
---@field descend_until (fun(expansion_count: integer, node: Node): boolean)|nil | ||
|
||
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) | ||
|
||
Api.tree.toggle_descend_until = wrap_node(function(node, descend_until) | ||
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. note: this is actually what I would like to achieve in the end. question: how should the end user api look like? |
||
local dir = node:as(DirectoryNode) | ||
if dir then | ||
if node.open then | ||
dir:expand_or_collapse(nil) | ||
else | ||
actions.tree.modifiers.expand.all(node, { descend_until = descend_until }) | ||
end | ||
else | ||
edit("edit", node) | ||
end | ||
end) | ||
|
||
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle") | ||
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored") | ||
Api.tree.toggle_git_clean_filter = wrap_explorer_member_args("filters", "toggle", "git_clean") | ||
|
@@ -225,44 +282,6 @@ Api.fs.copy.absolute_path = wrap_node(wrap_explorer_member("clipboard", "copy_ab | |
Api.fs.copy.filename = wrap_node(wrap_explorer_member("clipboard", "copy_filename")) | ||
Api.fs.copy.basename = wrap_node(wrap_explorer_member("clipboard", "copy_basename")) | ||
Api.fs.copy.relative_path = wrap_node(wrap_explorer_member("clipboard", "copy_path")) | ||
--- | ||
---@class NodeEditOpts | ||
---@field quit_on_open boolean|nil default false | ||
---@field focus boolean|nil default true | ||
|
||
---@param mode string | ||
---@param node Node | ||
---@param edit_opts NodeEditOpts? | ||
local function edit(mode, node, edit_opts) | ||
local file_link = node:as(FileLinkNode) | ||
local path = file_link and file_link.link_to or node.absolute_path | ||
local cur_tabpage = vim.api.nvim_get_current_tabpage() | ||
|
||
local explorer = core.get_explorer() | ||
|
||
actions.node.open_file.fn(mode, path) | ||
|
||
edit_opts = edit_opts or {} | ||
|
||
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" | ||
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then | ||
if explorer then | ||
explorer.view:close(cur_tabpage, "api.edit " .. mode) | ||
end | ||
end | ||
|
||
local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place" | ||
local focus = edit_opts.focus == nil or edit_opts.focus == true | ||
if not mode_unsupported_focus and not focus then | ||
-- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab | ||
if mode == "tabnew" then | ||
vim.cmd(":tabprev") | ||
end | ||
if explorer then | ||
explorer.view:focus() | ||
end | ||
end | ||
end | ||
|
||
---@param mode string | ||
---@param toggle_group boolean? | ||
|
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.
thought: just moved this function higher so that I can refer to it from
toggle_descend_until