Skip to content

refactor(#2828): multi instance nvim-tree.explorer.filters #2841

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 5 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 14 additions & 3 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local actions = require "nvim-tree.actions"
local legacy = require "nvim-tree.legacy"
local core = require "nvim-tree.core"
local git = require "nvim-tree.git"
local filters = require "nvim-tree.explorer.filters"
local buffers = require "nvim-tree.buffers"
local notify = require "nvim-tree.notify"

Expand Down Expand Up @@ -210,7 +209,13 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufReadPost", {
callback = function(data)
-- update opened file buffers
if (filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
local explorer = core.get_explorer()
if not explorer then
return
end
if
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
then
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
actions.reloaders.reload_explorer()
end)
Expand All @@ -221,7 +226,13 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufUnload", {
callback = function(data)
-- update opened file buffers
if (filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
local explorer = core.get_explorer()
if not explorer then
return
end
if
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
then
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
actions.reloaders.reload_explorer()
end)
Expand Down
10 changes: 7 additions & 3 deletions lua/nvim-tree/actions/finders/search-node.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local core = require "nvim-tree.core"
local filters = require "nvim-tree.explorer.filters"
local find_file = require("nvim-tree.actions.finders.find-file").fn

local M = {}
Expand All @@ -9,6 +8,11 @@ local M = {}
---@return string|nil
local function search(search_dir, input_path)
local realpaths_searched = {}
local explorer = core.get_explorer()

if not explorer then
return
end

if not search_dir then
return
Expand All @@ -19,7 +23,7 @@ local function search(search_dir, input_path)
local function iter(dir)
local realpath, path, name, stat, handle, _

local filter_status = filters.prepare()
local filter_status = explorer.filters:prepare()

handle, _ = vim.loop.fs_scandir(dir)
if not handle then
Expand All @@ -42,7 +46,7 @@ local function search(search_dir, input_path)
break
end

if not filters.should_filter(path, stat, filter_status) then
if not explorer.filters:should_filter(path, stat, filter_status) then
if string.find(path, "/" .. input_path .. "$") then
return path
end
Expand Down
48 changes: 32 additions & 16 deletions lua/nvim-tree/actions/tree/modifiers/toggles.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
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"

local core = require "nvim-tree.core"
local M = {}

local function reload()
Expand All @@ -11,39 +10,56 @@ local function reload()
utils.focus_node_or_parent(node)
end

function M.custom()
filters.config.filter_custom = not filters.config.filter_custom
local function wrap_explorer(fn)
return function(...)
local explorer = core.get_explorer()
if explorer then
return fn(explorer, ...)
end
end
end

local function custom(explorer)
explorer.filters.config.filter_custom = not explorer.filters.config.filter_custom
reload()
end

function M.git_ignored()
filters.config.filter_git_ignored = not filters.config.filter_git_ignored
local function git_ignored(explorer)
explorer.filters.config.filter_git_ignored = not explorer.filters.config.filter_git_ignored
reload()
end

function M.git_clean()
filters.config.filter_git_clean = not filters.config.filter_git_clean
local function git_clean(explorer)
explorer.filters.config.filter_git_clean = not explorer.filters.config.filter_git_clean
reload()
end

function M.no_buffer()
filters.config.filter_no_buffer = not filters.config.filter_no_buffer
local function no_buffer(explorer)
explorer.filters.config.filter_no_buffer = not explorer.filters.config.filter_no_buffer
reload()
end

function M.no_bookmark()
filters.config.filter_no_bookmark = not filters.config.filter_no_bookmark
local function no_bookmark(explorer)
explorer.filters.config.filter_no_bookmark = not explorer.filters.config.filter_no_bookmark
reload()
end

function M.dotfiles()
filters.config.filter_dotfiles = not filters.config.filter_dotfiles
local function dotfiles(explorer)
explorer.filters.config.filter_dotfiles = not explorer.filters.config.filter_dotfiles
reload()
end

function M.enable()
filters.config.enable = not filters.config.enable
local function enable(explorer)
explorer.filters.config.enable = not explorer.filters.config.enable
reload()
end

M.custom = wrap_explorer(custom)
M.git_ignored = wrap_explorer(git_ignored)
M.git_clean = wrap_explorer(git_clean)
M.no_buffer = wrap_explorer(no_buffer)
M.no_bookmark = wrap_explorer(no_bookmark)
M.dotfiles = wrap_explorer(dotfiles)
M.enable = wrap_explorer(enable)

return M
15 changes: 8 additions & 7 deletions lua/nvim-tree/explorer/explore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local builders = require "nvim-tree.explorer.node-builders"
local explorer_node = require "nvim-tree.explorer.node"
local git = require "nvim-tree.git"
local sorters = require "nvim-tree.explorer.sorters"
local filters = require "nvim-tree.explorer.filters"
local live_filter = require "nvim-tree.live-filter"
local log = require "nvim-tree.log"

Expand All @@ -15,10 +14,11 @@ local M = {}
---@param cwd string
---@param node Node
---@param git_status table
local function populate_children(handle, cwd, node, git_status)
---@param parent Explorer
local function populate_children(handle, cwd, node, git_status, parent)
local node_ignored = explorer_node.is_git_ignored(node)
local nodes_by_path = utils.bool_record(node.nodes, "absolute_path")
local filter_status = filters.prepare(git_status)
local filter_status = parent.filters:prepare(git_status)
while true do
local name, t = vim.loop.fs_scandir_next(handle)
if not name then
Expand All @@ -31,7 +31,7 @@ local function populate_children(handle, cwd, node, git_status)
---@type uv.fs_stat.result|nil
local stat = vim.loop.fs_stat(abs)

if not filters.should_filter(abs, stat, filter_status) and not nodes_by_path[abs] and Watcher.is_fs_event_capable(abs) then
if not parent.filters:should_filter(abs, stat, filter_status) and not nodes_by_path[abs] and Watcher.is_fs_event_capable(abs) then
local child = nil
if t == "directory" and vim.loop.fs_access(abs, "R") then
child = builders.folder(node, abs, name, stat)
Expand All @@ -56,8 +56,9 @@ end

---@param node Node
---@param status table
---@param parent Explorer
---@return Node[]|nil
function M.explore(node, status)
function M.explore(node, status, parent)
local cwd = node.link_to or node.absolute_path
local handle = vim.loop.fs_scandir(cwd)
if not handle then
Expand All @@ -66,15 +67,15 @@ function M.explore(node, status)

local profile = log.profile_start("explore init %s", node.absolute_path)

populate_children(handle, cwd, node, status)
populate_children(handle, cwd, node, status, parent)

local is_root = not node.parent
local child_folder_only = explorer_node.has_one_child_folder(node) and node.nodes[1]
if M.config.group_empty and not is_root and child_folder_only then
local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path
local child_status = git.load_project_status(child_cwd)
node.group_next = child_folder_only
local ns = M.explore(child_folder_only, child_status)
local ns = M.explore(child_folder_only, child_status, parent)
node.nodes = ns or {}

log.profile_end(profile)
Expand Down
Loading
Loading