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
36 changes: 31 additions & 5 deletions lua/nvim-tree/actions/tree-modifiers/toggles.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,57 @@
local lib = require "nvim-tree.lib"
local core = require "nvim-tree.core"
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 explorer = core.get_explorer()

if explorer == nil then
return
end

while node do
local found_node, _ = utils.find_node(explorer.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
11 changes: 11 additions & 0 deletions lua/nvim-tree/live-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local Iterator = require "nvim-tree.iterators.node-iterator"

-- Keeps memory of focused node when entering filter prompt
local last_node
Copy link
Member

Choose a reason for hiding this comment

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

This will be a future problem when we finally sort out multiple trees / tabs.

Can you please store this at view.View?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, that makes total sense.


local M = {
filter = nil,
}
Expand Down Expand Up @@ -134,6 +137,7 @@ local function create_overlay()
end

function M.start_filtering()
last_node = require("nvim-tree.lib").get_node_at_cursor()
M.filter = M.filter or ""

redraw()
Expand All @@ -145,9 +149,16 @@ function M.start_filtering()
end

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

if node then
utils.focus_file(node.absolute_path)
elseif last_node then
Copy link
Member

Choose a reason for hiding this comment

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

This applies only to live filter, not the other toggle filters.

Do we need to apply it there as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think it would be useful because when toggling any other filter there will always be a focused node, unless the really rare case when a filter gets toggled when the live filter is already applied and the prompt is focused.

utils.focus_file(last_node.absolute_path)
end
end

function M.setup(opts)
Expand Down