Skip to content

Commit

Permalink
feat: support collapse a single folder under cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
linepi committed Jul 22, 2024
1 parent 4e396b2 commit fa472f2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,9 @@ tree.collapse_all({keep_buffers}) *nvim-tree-api.tree.collapse_all()*
Parameters: ~
• {keep_buffers} (boolean) do not collapse nodes with open buffers.

tree.collapse() *nvim-tree-api.tree.collapse()*
Collapse the folder under cursor.

tree.expand_all() *nvim-tree-api.tree.expand_all()*
Recursively expand all nodes in the tree.
Folder: only the nodes underneath that folder.
Expand Down
38 changes: 38 additions & 0 deletions lua/nvim-tree/actions/tree/modifiers/collapse.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local renderer = require "nvim-tree.renderer"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local lib = require "nvim-tree.lib"
local Iterator = require "nvim-tree.iterators.node-iterator"

local M = {}

function M.fn()
local node = lib.get_node_at_cursor()
local explorer = core.get_explorer()

if explorer == nil then
return
end
if node == nil then
return
end
if node.nodes == nil then
return
end

Iterator.builder(node.nodes)
:hidden()
:applier(function(n)
n.open = false
end)
:recursor(function(n)
return n.group_next and { n.group_next } or n.nodes
end)
:iterate()

node.open = false
renderer.draw()
utils.focus_node_or_parent(node)
end

return M
1 change: 1 addition & 0 deletions lua/nvim-tree/actions/tree/modifiers/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

M.collapse_all = require "nvim-tree.actions.tree.modifiers.collapse-all"
M.collapse = require "nvim-tree.actions.tree.modifiers.collapse"
M.expand_all = require "nvim-tree.actions.tree.modifiers.expand-all"
M.toggles = require "nvim-tree.actions.tree.modifiers.toggles"

Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Api.tree.get_nodes = wrap(lib.get_nodes)
Api.tree.find_file = wrap(actions.tree.find_file.fn)
Api.tree.search_node = wrap(actions.finders.search_node.fn)
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.fn)
Api.tree.collapse = wrap(actions.tree.modifiers.collapse.fn)
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
Api.tree.toggle_enable_filters = wrap(actions.tree.modifiers.toggles.enable)
Api.tree.toggle_gitignore_filter = wrap(actions.tree.modifiers.toggles.git_ignored)
Expand Down

0 comments on commit fa472f2

Please sign in to comment.