Skip to content
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"
Comment thread
Akmadan23 marked this conversation as resolved.
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
10 changes: 10 additions & 0 deletions lua/nvim-tree/live-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ local function create_overlay()
end

function M.start_filtering()
view.View.live_filter.prev_focused_node = require("nvim-tree.lib").get_node_at_cursor()
M.filter = M.filter or ""

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

function M.clear_filter()
local node = require("nvim-tree.lib").get_node_at_cursor()
local last_node = view.View.live_filter.prev_focused_node

M.filter = nil
reset_filter()
redraw()

if node then
utils.focus_file(node.absolute_path)
elseif last_node then
Copy link
Copy Markdown
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
Copy Markdown
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
3 changes: 3 additions & 0 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ M.View = {
tabpages = {},
cursors = {},
hide_root_folder = false,
live_filter = {
prev_focused_node = nil,
},
winopts = {
relativenumber = false,
number = false,
Expand Down