nvim-tree.lua icon indicating copy to clipboard operation
nvim-tree.lua copied to clipboard

feat: support collapse a single folder under cursor

Open linepi opened this issue 1 year ago • 9 comments

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.

linepi avatar Jul 22 '24 11:07 linepi

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.

linepi avatar Jul 22 '24 11:07 linepi

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 avatar Jul 22 '24 16:07 evertonse

@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.

linepi avatar Jul 24 '24 06:07 linepi

@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.

Ah I see now, then there should be api.tree.collapse. Have at it

evertonse avatar Jul 24 '24 14:07 evertonse

I update my commit, including the command, doc and explanation. Please review.

linepi avatar Jul 26 '24 14:07 linepi

This is great, many thanks for pushing this one.

alex-courtis avatar Jul 28 '24 04:07 alex-courtis

Please an optional node parameter to collapse-all.fn so 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.

linepi avatar Jul 28 '24 06:07 linepi

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.

alex-courtis avatar Aug 03 '24 06:08 alex-courtis

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

alex-courtis avatar Aug 03 '24 06:08 alex-courtis

Closing as this PR is stale. Please reopen if you wish to continue this work.

alex-courtis avatar Jan 27 '25 00:01 alex-courtis

picked it up in https://github.com/nvim-tree/nvim-tree.lua/pull/3140

lorentzlasson avatar May 31 '25 17:05 lorentzlasson