-
-
Notifications
You must be signed in to change notification settings - Fork 635
feat(#2789): add optional function expand_until to api.tree.expand_all and api.node.expand #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
Changes from 3 commits
c1948cb
641f125
114fde0
72404b9
06575bc
8936460
e3c3105
71271e3
03e3a15
4ab16dd
3dac0be
160ae4f
37b3516
587068c
eabd08c
669a79f
8425ef0
c4777ab
cc25680
ba2d11f
8ecfcd0
8c42c02
3c666c4
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 |
|---|---|---|
|
|
@@ -30,17 +30,35 @@ end | |
| ---@param expansion_count integer | ||
| ---@param node Node | ||
| ---@return boolean | ||
| local function should_expand(expansion_count, node) | ||
| local function descend_until_max_or_empty(expansion_count, node) | ||
| local dir = node:as(DirectoryNode) | ||
| if not dir then | ||
| return false | ||
| end | ||
|
|
||
| local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY | ||
| local should_exclude = M.EXCLUDE[dir.name] | ||
| return not should_halt and not dir.open and not should_exclude | ||
|
|
||
| return not should_halt and not should_exclude | ||
| end | ||
|
|
||
| ---@param expansion_count integer | ||
| ---@param node Node | ||
| ---@param should_descend fun(expansion_count: integer, node: Node): boolean | ||
| ---@return boolean | ||
| local function should_expand(expansion_count, node, should_descend) | ||
| local dir = node:as(DirectoryNode) | ||
| if not dir then | ||
| return false | ||
| end | ||
|
|
||
| return not dir.open and should_descend(expansion_count, node) | ||
| end | ||
|
|
||
| local function gen_iterator() | ||
|
|
||
| ---@param should_descend fun(expansion_count: integer, node: Node): boolean | ||
| ---@return fun(node): any | ||
| local function gen_iterator(should_descend) | ||
| local expansion_count = 0 | ||
|
|
||
| return function(parent) | ||
|
|
@@ -52,7 +70,7 @@ 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, should_descend) then | ||
| expansion_count = expansion_count + 1 | ||
| node = node:as(DirectoryNode) | ||
| if node then | ||
|
|
@@ -61,7 +79,19 @@ local function gen_iterator() | |
| 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)) | ||
| 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() | ||
|
|
||
|
|
@@ -72,12 +102,13 @@ local function gen_iterator() | |
| end | ||
|
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. The max folder discovery test above does not appear to be necessary as Commenting this out results in the correct number of folders being expanded, just no message. Having the same test twice in two locations is fragile and unclear. Proposal: |
||
|
|
||
| ---@param node Node? | ||
| local function expand_node(node) | ||
| ---@param expand_opts ApiTreeExpandAllOpts? | ||
|
ghostbuster91 marked this conversation as resolved.
Outdated
|
||
| local function expand_node(node, expand_opts) | ||
| if not node then | ||
| return | ||
| end | ||
|
|
||
| if gen_iterator()(node) then | ||
| local descend_until = (expand_opts and expand_opts.descend_until) or descend_until_max_or_empty | ||
| if gen_iterator(descend_until)(node) then | ||
| notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") | ||
| end | ||
|
|
||
|
|
@@ -89,18 +120,20 @@ end | |
|
|
||
| ---Expand the directory node or the root | ||
| ---@param node Node | ||
| function M.all(node) | ||
| expand_node(node and node:as(DirectoryNode) or core.get_explorer()) | ||
| ---@param expand_opts ApiTreeExpandAllOpts? | ||
| function M.all(node, expand_opts) | ||
| expand_node(node and node:as(DirectoryNode) or core.get_explorer(), expand_opts) | ||
| end | ||
|
|
||
| ---Expand the directory node or parent node | ||
| ---@param node Node | ||
| function M.node(node) | ||
| ---@param expand_opts ApiTreeExpandAllOpts? | ||
| function M.node(node, expand_opts) | ||
| if not node then | ||
| return | ||
| end | ||
|
|
||
| expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode)) | ||
| expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode), expand_opts) | ||
| end | ||
|
|
||
| function M.setup(opts) | ||
|
|
||
| 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) | ||
|
ghostbuster91 marked this conversation as resolved.
Outdated
|
||
| 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 | ||
|
ghostbuster91 marked this conversation as resolved.
Outdated
|
||
|
|
||
| Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) | ||
|
|
||
| Api.tree.toggle_descend_until = wrap_node(function(node, descend_until) | ||
|
Contributor
Author
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?
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 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
Contributor
Author
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. Doing it in a separate PR makes perfect sense 👍 |
||
| 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? | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.