Skip to content

fix(#1785): retain focused node on filter toggle #2202

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

Merged
merged 9 commits into from
May 27, 2023
31 changes: 26 additions & 5 deletions lua/nvim-tree/actions/tree-modifiers/toggles.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils"
local filters = require "nvim-tree.explorer.filters"
local reloaders = require "nvim-tree.actions.reloaders.reloaders"

local M = {}

local function reload()
local node = lib.get_node_at_cursor()
reloaders.reload_explorer()
local visible_nodes = require("nvim-tree.core").get_explorer().nodes

while node do
local found_node, _ = utils.find_node(visible_nodes, function(node_)
return node_.absolute_path == node.absolute_path
end)

if found_node or node.parent == nil then
utils.focus_file(node.absolute_path)
break
end

node = node.parent
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

return M
5 changes: 5 additions & 0 deletions lua/nvim-tree/live-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ function M.start_filtering()
end

function M.clear_filter()
local node = require("nvim-tree.api").tree.get_node_under_cursor()
M.filter = nil
reset_filter()
redraw()

if node ~= nil then
utils.focus_file(node.absolute_path)
end
end

function M.setup(opts)
Expand Down