From 421a830d79f819c9779ed233098ce9c2b3c9f8ab Mon Sep 17 00:00:00 2001 From: Andreas Bissinger Date: Tue, 8 Mar 2022 23:12:53 +0100 Subject: [PATCH 1/3] feat: add possibility to check if buffer is open when collapsing tree --- lua/nvim-tree/actions/collapse-all.lua | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/actions/collapse-all.lua b/lua/nvim-tree/actions/collapse-all.lua index 2596498da98..3ae3d1dd812 100644 --- a/lua/nvim-tree/actions/collapse-all.lua +++ b/lua/nvim-tree/actions/collapse-all.lua @@ -1,12 +1,29 @@ local renderer = require "nvim-tree.renderer" +local utils = require "nvim-tree.utils" local M = {} -function M.fn() +function M.fn(keep_buffers) + local buffer_paths = {} + for _, buffer in ipairs(vim.api.nvim_list_bufs()) do + table.insert(buffer_paths, vim.api.nvim_buf_get_name(buffer)) + end + local function iter(nodes) for _, node in pairs(nodes) do if node.open then - node.open = false + local new_open = false + + if keep_buffers == true then + for _, buffer_path in ipairs(buffer_paths) do + local matches = utils.str_find(buffer_path, node.absolute_path) + if matches then + new_open = true + end + end + end + + node.open = new_open end if node.nodes then iter(node.nodes) From 08ba361fcc64f0215021ed0a4d741fdd8258c267 Mon Sep 17 00:00:00 2001 From: Andreas Bissinger Date: Tue, 8 Mar 2022 23:13:40 +0100 Subject: [PATCH 2/3] feat: add apis to collapse the tree --- lua/nvim-tree.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index cefc4a87a41..d36a2838870 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -280,6 +280,8 @@ local function setup_vim_commands() command! NvimTreeFindFile lua require'nvim-tree'.find_file(true) command! NvimTreeFindFileToggle lua require'nvim-tree'.toggle(true) command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize("") + command! NvimTreeCollapse lua require'nvim-tree.actions.collapse-all'.fn() + command! NvimTreeCollapseKeepBuffers lua require'nvim-tree.actions.collapse-all'.fn(true) ]] end From c60d9502fe615c22b0139eabb7d45031fc7b079f Mon Sep 17 00:00:00 2001 From: Andreas Bissinger Date: Tue, 8 Mar 2022 23:17:59 +0100 Subject: [PATCH 3/3] feat: add documentation for tree collapse commands --- README.md | 2 ++ doc/nvim-tree-lua.txt | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 47106ec69bd..b0c0a8efeed 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,8 @@ nnoremap n :NvimTreeFindFile " NvimTreeFocus " NvimTreeFindFileToggle " NvimTreeResize +" NvimTreeCollapse +" NvimTreeCollapseKeepBuffers set termguicolors " this variable must be enabled for colors to be applied properly diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 5555eb3b813..9c452729e98 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -66,6 +66,15 @@ adds or removes the given value to the current window width. Example `:NvimTreeResize -20` removes the value 20 from the current width. And `:NvimTreeResize +20` adds the value 20 to the current width. +|:NvimTreeCollapse| *:NvimTreeCollapse* + +Collapses the nvim-tree recursively. + +|:NvimTreeCollapseKeepBuffers| *:NvimTreeCollapseKeepBuffers* + +Collapses the nvim-tree recursively, but keep the directories open, which are +used in an open buffer. + ============================================================================== SETUP *nvim-tree.setup*