Skip to content

Commit 60a9c86

Browse files
authored
feat: make it possible to collapse the tree but keep the directories open which are in used in buffers (#1057)
1 parent d5a12ac commit 60a9c86

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ nnoremap <leader>n :NvimTreeFindFile<CR>
9696
" NvimTreeFocus
9797
" NvimTreeFindFileToggle
9898
" NvimTreeResize
99+
" NvimTreeCollapse
100+
" NvimTreeCollapseKeepBuffers
99101
100102
set termguicolors " this variable must be enabled for colors to be applied properly
101103

doc/nvim-tree-lua.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ adds or removes the given value to the current window width.
6666
Example `:NvimTreeResize -20` removes the value 20 from the current width. And
6767
`:NvimTreeResize +20` adds the value 20 to the current width.
6868

69+
|:NvimTreeCollapse| *:NvimTreeCollapse*
70+
71+
Collapses the nvim-tree recursively.
72+
73+
|:NvimTreeCollapseKeepBuffers| *:NvimTreeCollapseKeepBuffers*
74+
75+
Collapses the nvim-tree recursively, but keep the directories open, which are
76+
used in an open buffer.
77+
6978

7079
==============================================================================
7180
SETUP *nvim-tree.setup*

lua/nvim-tree.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ local function setup_vim_commands()
280280
command! NvimTreeFindFile lua require'nvim-tree'.find_file(true)
281281
command! NvimTreeFindFileToggle lua require'nvim-tree'.toggle(true)
282282
command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize("<args>")
283+
command! NvimTreeCollapse lua require'nvim-tree.actions.collapse-all'.fn()
284+
command! NvimTreeCollapseKeepBuffers lua require'nvim-tree.actions.collapse-all'.fn(true)
283285
]]
284286
end
285287

lua/nvim-tree/actions/collapse-all.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
local renderer = require "nvim-tree.renderer"
2+
local utils = require "nvim-tree.utils"
23

34
local M = {}
45

5-
function M.fn()
6+
function M.fn(keep_buffers)
7+
local buffer_paths = {}
8+
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
9+
table.insert(buffer_paths, vim.api.nvim_buf_get_name(buffer))
10+
end
11+
612
local function iter(nodes)
713
for _, node in pairs(nodes) do
814
if node.open then
9-
node.open = false
15+
local new_open = false
16+
17+
if keep_buffers == true then
18+
for _, buffer_path in ipairs(buffer_paths) do
19+
local matches = utils.str_find(buffer_path, node.absolute_path)
20+
if matches then
21+
new_open = true
22+
end
23+
end
24+
end
25+
26+
node.open = new_open
1027
end
1128
if node.nodes then
1229
iter(node.nodes)

0 commit comments

Comments
 (0)