Skip to content

feat: support collapse a single folder under cursor #3140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .hooks/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
#!/usr/bin/env sh

make
23 changes: 21 additions & 2 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,17 @@ See |nvim-tree-highlight| for details.

Collapses the nvim-tree recursively.

See |nvim-tree-api.tree.collapse_all()|
See |nvim-tree-api.tree.collapse()|

Calls: api.tree.collapse({ under_cursor = false, keep_buffers = false, })

*:NvimTreeCollapseFolder*

Calls: `api.tree.collapse_all(false)`
Collapses the folder under cursor

See |nvim-tree-api.tree.collapse()|

Calls: api.tree.collapse({ under_cursor = true, keep_buffers = false, })

*:NvimTreeCollapseKeepBuffers*

Expand Down Expand Up @@ -1829,6 +1837,16 @@ tree.find_file({opts}) *nvim-tree-api.tree.find_file()*
tree.search_node() *nvim-tree-api.tree.search_node()*
Open the search dialogue as per the search_node action.

tree.collapse({opts}) *nvim-tree-api.tree.collapse()*
Collapse the tree.

Parameters: ~
• {opts} (table) parameters

Parameters: ~
• {under_cursor} (boolean) only collapse the node under cursor
• {keep_buffers} (boolean) do not collapse nodes with open buffers.

tree.collapse_all({keep_buffers}) *nvim-tree-api.tree.collapse_all()*
Collapse the tree.

Expand Down Expand Up @@ -3340,6 +3358,7 @@ highlight group is not, hard linking as follows: >
|nvim-tree-api.tree.close_in_all_tabs()|
|nvim-tree-api.tree.close_in_this_tab()|
|nvim-tree-api.tree.collapse_all()|
|nvim-tree-api.tree.collapse()|
|nvim-tree-api.tree.expand_all()|
|nvim-tree-api.tree.find_file()|
|nvim-tree-api.tree.focus()|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ local function buf_match()
end
end

---@param keep_buffers boolean
function M.fn(keep_buffers)
---@param opts ApiTreeCollapseOpts | nil
function M.collapse(opts)
opts = opts or {
keep_buffers = false,
under_cursor = true
}
local explorer = core.get_explorer()
if not explorer then
return
Expand All @@ -37,12 +41,24 @@ function M.fn(keep_buffers)

local matches = buf_match()

Iterator.builder(explorer.nodes)
local selected_nodes
if opts.under_cursor then
local dir = node:as(DirectoryNode)
if not node or not node.nodes then
return
end
selected_nodes = node.nodes
dir.open = false
else
selected_nodes = explorer.nodes
end

Iterator.builder(selected_nodes)
:hidden()
:applier(function(n)
local dir = n:as(DirectoryNode)
if dir then
dir.open = keep_buffers and matches(dir.absolute_path)
dir.open = opts.keep_buffers and matches(dir.absolute_path)
end
end)
:recursor(function(n)
Expand All @@ -54,4 +70,10 @@ function M.fn(keep_buffers)
utils.focus_node_or_parent(node)
end

---@param keep_buffers boolean | nil
function M.collapse_all(keep_buffers)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this one be deprecated even?

keep_buffers = keep_buffers or false
M.collapse({ keep_buffers = keep_buffers, under_cursor = false })
end

return M
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/tree/modifiers/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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")

function M.setup(opts)
Expand Down
8 changes: 7 additions & 1 deletion lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,13 @@ Api.tree.get_nodes = wrap_explorer("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)

---@class ApiTreeCollapseOpts
---@field under_cursor boolean
---@field keep_buffers boolean

Api.tree.collapse = wrap(actions.tree.modifiers.collapse.collapse)
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.collapse_all)
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")
Expand Down
14 changes: 12 additions & 2 deletions lua/nvim-tree/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ local CMDS = {
bar = true,
},
command = function()
api.tree.collapse_all(false)
api.tree.collapse({ under_cursor = false, keep_buffers = false })
end,
},
{
name = "NvimTreeCollapseFolder",
opts = {
desc = "nvim-tree: collapse the folder under cursor",
bar = true,
},
command = function()
api.tree.collapse({ under_cursor = true, keep_buffers = false })
end,
},
{
Expand All @@ -131,7 +141,7 @@ local CMDS = {
bar = true,
},
command = function()
api.tree.collapse_all(true)
api.tree.collapse({ under_cursor = false, keep_buffers = true })
end,
},
{
Expand Down
2 changes: 1 addition & 1 deletion scripts/doc-comments.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

out=$(grep -nr "^--- @" lua)

Expand Down
2 changes: 1 addition & 1 deletion scripts/help-update.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env sh

# run after changing nvim-tree.lua DEFAULT_OPTS or keymap.lua M.default_on_attach
# scrapes and updates nvim-tree-lua.txt
Expand Down
2 changes: 1 addition & 1 deletion scripts/luals-check.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env sh

# Performs a lua-language-server check on all files.
# luals-out/check.json will be produced on any issues, returning 1.
Expand Down