feat: support collapse a single folder under cursor
When I use api.tree.expand_all to expand a folder, sometimes I need to collapse a single folder rather than all folders(invoke by api.tree.collapse_all). This feature(api.tree.collapse) can be understood as the inverse operation of api.tree.expand_all.
There might be some imperfections in the naming, documentation, and code details. What I am presenting here is just a rough idea. If this pull request idea is accepted, I will continue to work hard to make more reasonable, reliable, and complete revisions.
When I expand a folder, sometimes I need to collapse a single folder rather than all folders. This can be understood as the inverse operation of expanding.
I think I migh not understand what you mean.
Is this api.node.navigate.parent_close not what you want?
@evertonse No, that is not the same. api.node.navigate.parent_close will close the parent folder of where cursor is. The new feature api.tree.collapse will close folder and all it subfolder recursively.
@evertonse No, that is not the same.
api.node.navigate.parent_closewill close the parent folder of where cursor is. The new featureapi.tree.collapsewill close folder and all it subfolder recursively.
Ah I see now, then there should be api.tree.collapse. Have at it
I update my commit, including the command, doc and explanation. Please review.
This is great, many thanks for pushing this one.
Please an optional node parameter to
collapse-all.fnso that it may perform both and keep buffers.
All right, I've added and option to collapse_all.fn and removed collapse. Now it's only one function. Please review.
On :NvimTreeCollapseKeepBuffers
Error executing Lua callback: ...pi/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua:29: attempt to index local 'opts' (a boolean value)
stack traceback:
...pi/lua/nvim-tree/actions/tree/modifiers/collapse-all.lua:29: in function 'collapse_all'
...site/pack/packer/start/linepi/lua/nvim-tree/commands.lua:145: in function <...site/pack/packer/start/linepi/lua/nvim-tree/commands.lua:144>
It looks like we're changing API from a boolean to an opts table. We'll need to provide backwards compatibility for legacy arguments, similar to tree.toggle. That's a massive pain in the arse and adds unnecessary complexity.
Alternative: add an API method tree.collapse, leaving tree.collapse_all unchanged. They both then call the one collapse function satisfying point 2 in https://github.com/nvim-tree/nvim-tree.lua/pull/2847#pullrequestreview-2203324011
TL;DR point 2 was asking to use a single collapse function rather than a single API.
tree.collapse_all({keep_buffers}) *nvim-tree-api.tree.collapse_all()*
Collapse the entire tree.
Parameters: ~
• {keep_buffers} (boolean) do not collapse nodes with open buffers.
tree.collapse({opts}) *nvim-tree-api.tree.collapse()*
Collapse the tree.
Parameters: ~
• {opts} (table) optional parameters
Options: ~
• {under_cursor} (boolean) only collapse the node under cursor,
default false
• {keep_buffers} (boolean) do not collapse nodes with open buffers,
default false
This preserves backward compatibility and the API can stay pure, something like:
Api.tree.collapse = wrap(actions.tree.modifiers.collapse_all.collapse)
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.collapse_all)
collapse_all.fn would be renamed to collapse_all.collapse
collapse_all.collapse_all would then look something like:
---@param keep_buffers boolean
function M.fn(keep_buffers)
M.collapse({keep_buffers = keep_buffers, under_cursor = false})
end
Finally, rename collapse-all.lua to collapse.lua
Closing as this PR is stale. Please reopen if you wish to continue this work.
picked it up in https://github.com/nvim-tree/nvim-tree.lua/pull/3140