Skip to content

fix(#1785): retain focused node following filter toggle #1872

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 1 commit 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
22 changes: 17 additions & 5 deletions lua/nvim-tree/actions/tree-modifiers/toggles.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
local lib = require "nvim-tree.lib"
local view = require "nvim-tree.view"
local filters = require "nvim-tree.explorer.filters"
local renderer = require "nvim-tree.renderer"
local reloaders = require "nvim-tree.actions.reloaders.reloaders"

local M = {}

--- Reload and attempt to move the cursor to the previously focused node.
local function reload()
local node = lib.get_node_at_cursor()

reloaders.reload_explorer()

if node then
lib.place_cursor_at_node(node)
end
end

function M.custom()
filters.config.filter_custom = not filters.config.filter_custom
return reloaders.reload_explorer()
reload()
end

function M.git_ignored()
filters.config.filter_git_ignored = not filters.config.filter_git_ignored
return reloaders.reload_explorer()
reload()
end

function M.git_clean()
filters.config.filter_git_clean = not filters.config.filter_git_clean
return reloaders.reload_explorer()
reload()
end

function M.no_buffer()
filters.config.filter_no_buffer = not filters.config.filter_no_buffer
return reloaders.reload_explorer()
reload()
end

function M.dotfiles()
filters.config.filter_dotfiles = not filters.config.filter_dotfiles
return reloaders.reload_explorer()
reload()
end

function M.help()
Expand Down
9 changes: 9 additions & 0 deletions lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ function M.get_node_at_cursor()
return utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[line]
end

--- Place the cursor at the node if it is visible or at the top.
--- @param node table
function M.place_cursor_at_node(node)
local _, l = utils.find_node(core.get_explorer().nodes, function(n)
return n == node
end)
view.set_cursor { l + 1, 1 }
end

---Create a sanitized partial copy of a node, populating children recursively.
---@param node table
---@return table|nil cloned node
Expand Down