-
-
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||||||
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: just moved this function higher so that I can refer to it from 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. Moved it up during review and the changes are indeed minimal. Could we please leave edit where it is, and move I've made that change on a temporary branch for review https://github.com/nvim-tree/nvim-tree.lua/compare/expand-until-2-alex-refactor?expand=1 Yes, the API is a mess however will be cleaned up in #3088 |
||||||
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 | ||||||
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.
Suggested change
That would match the name of the api methods: expand |
||||||
|
||||||
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? 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 will be very useful, however it needs a bit of thought. Can we please do this in a future PR? The functionality for the API expands is working and we can merge that now. What should it look like? This isn't something the user should have to do; it should be core functionality. It could go into |
||||||
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? | ||||||
|
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.
This will bring it into parity with #3133