Merge branch 'master' into live-filter-multiinstace

This commit is contained in:
Mateusz Russak 2024-07-27 13:26:26 +02:00
commit c6ae2431bc
10 changed files with 225 additions and 183 deletions

View File

@ -2691,10 +2691,6 @@ configurations for different types of prompts.
============================================================================== ==============================================================================
11. OS SPECIFIC RESTRICTIONS *nvim-tree-os-specific* 11. OS SPECIFIC RESTRICTIONS *nvim-tree-os-specific*
macOS
- Rename to different case is not possible when using a case insensitive file
system.
Windows WSL and PowerShell Windows WSL and PowerShell
- Trash is synchronized - Trash is synchronized
- Executable file detection is disabled as this is non-performant and can - Executable file detection is disabled as this is non-performant and can

View File

@ -9,7 +9,6 @@ local actions = require "nvim-tree.actions"
local legacy = require "nvim-tree.legacy" local legacy = require "nvim-tree.legacy"
local core = require "nvim-tree.core" local core = require "nvim-tree.core"
local git = require "nvim-tree.git" local git = require "nvim-tree.git"
local filters = require "nvim-tree.explorer.filters"
local buffers = require "nvim-tree.buffers" local buffers = require "nvim-tree.buffers"
local notify = require "nvim-tree.notify" local notify = require "nvim-tree.notify"
@ -210,7 +209,13 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufReadPost", { create_nvim_tree_autocmd("BufReadPost", {
callback = function(data) callback = function(data)
-- update opened file buffers -- 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() utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
actions.reloaders.reload_explorer() actions.reloaders.reload_explorer()
end) end)
@ -221,7 +226,13 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufUnload", { create_nvim_tree_autocmd("BufUnload", {
callback = function(data) callback = function(data)
-- update opened file buffers -- 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() utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
actions.reloaders.reload_explorer() actions.reloaders.reload_explorer()
end) end)

View File

@ -1,5 +1,4 @@
local core = require "nvim-tree.core" 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 find_file = require("nvim-tree.actions.finders.find-file").fn
local M = {} local M = {}
@ -9,6 +8,11 @@ local M = {}
---@return string|nil ---@return string|nil
local function search(search_dir, input_path) local function search(search_dir, input_path)
local realpaths_searched = {} local realpaths_searched = {}
local explorer = core.get_explorer()
if not explorer then
return
end
if not search_dir then if not search_dir then
return return
@ -19,7 +23,7 @@ local function search(search_dir, input_path)
local function iter(dir) local function iter(dir)
local realpath, path, name, stat, handle, _ local realpath, path, name, stat, handle, _
local filter_status = filters.prepare() local filter_status = explorer.filters:prepare()
handle, _ = vim.loop.fs_scandir(dir) handle, _ = vim.loop.fs_scandir(dir)
if not handle then if not handle then
@ -42,7 +46,7 @@ local function search(search_dir, input_path)
break break
end 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 if string.find(path, "/" .. input_path .. "$") then
return path return path
end end

View File

@ -1,8 +1,7 @@
local lib = require "nvim-tree.lib" local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local filters = require "nvim-tree.explorer.filters"
local reloaders = require "nvim-tree.actions.reloaders" local reloaders = require "nvim-tree.actions.reloaders"
local core = require "nvim-tree.core"
local M = {} local M = {}
local function reload() local function reload()
@ -11,39 +10,56 @@ local function reload()
utils.focus_node_or_parent(node) utils.focus_node_or_parent(node)
end end
function M.custom() local function wrap_explorer(fn)
filters.config.filter_custom = not filters.config.filter_custom 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() reload()
end end
function M.git_ignored() local function git_ignored(explorer)
filters.config.filter_git_ignored = not filters.config.filter_git_ignored explorer.filters.config.filter_git_ignored = not explorer.filters.config.filter_git_ignored
reload() reload()
end end
function M.git_clean() local function git_clean(explorer)
filters.config.filter_git_clean = not filters.config.filter_git_clean explorer.filters.config.filter_git_clean = not explorer.filters.config.filter_git_clean
reload() reload()
end end
function M.no_buffer() local function no_buffer(explorer)
filters.config.filter_no_buffer = not filters.config.filter_no_buffer explorer.filters.config.filter_no_buffer = not explorer.filters.config.filter_no_buffer
reload() reload()
end end
function M.no_bookmark() local function no_bookmark(explorer)
filters.config.filter_no_bookmark = not filters.config.filter_no_bookmark explorer.filters.config.filter_no_bookmark = not explorer.filters.config.filter_no_bookmark
reload() reload()
end end
function M.dotfiles() local function dotfiles(explorer)
filters.config.filter_dotfiles = not filters.config.filter_dotfiles explorer.filters.config.filter_dotfiles = not explorer.filters.config.filter_dotfiles
reload() reload()
end end
function M.enable() local function enable(explorer)
filters.config.enable = not filters.config.enable explorer.filters.config.enable = not explorer.filters.config.enable
reload() reload()
end 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 return M

View File

@ -2,8 +2,6 @@ local utils = require "nvim-tree.utils"
local builders = require "nvim-tree.explorer.node-builders" local builders = require "nvim-tree.explorer.node-builders"
local explorer_node = require "nvim-tree.explorer.node" local explorer_node = require "nvim-tree.explorer.node"
local git = require "nvim-tree.git" 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 live_filter = require "nvim-tree.live-filter"
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
@ -15,10 +13,11 @@ local M = {}
---@param cwd string ---@param cwd string
---@param node Node ---@param node Node
---@param git_status table ---@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 node_ignored = explorer_node.is_git_ignored(node)
local nodes_by_path = utils.bool_record(node.nodes, "absolute_path") 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 while true do
local name, t = vim.loop.fs_scandir_next(handle) local name, t = vim.loop.fs_scandir_next(handle)
if not name then if not name then
@ -31,7 +30,7 @@ local function populate_children(handle, cwd, node, git_status)
---@type uv.fs_stat.result|nil ---@type uv.fs_stat.result|nil
local stat = vim.loop.fs_stat(abs) 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 local child = nil
if t == "directory" and vim.loop.fs_access(abs, "R") then if t == "directory" and vim.loop.fs_access(abs, "R") then
child = builders.folder(node, abs, name, stat) child = builders.folder(node, abs, name, stat)
@ -56,8 +55,9 @@ end
---@param node Node ---@param node Node
---@param status table ---@param status table
---@param parent Explorer
---@return Node[]|nil ---@return Node[]|nil
function M.explore(node, status) function M.explore(node, status, parent)
local cwd = node.link_to or node.absolute_path local cwd = node.link_to or node.absolute_path
local handle = vim.loop.fs_scandir(cwd) local handle = vim.loop.fs_scandir(cwd)
if not handle then if not handle then
@ -66,7 +66,7 @@ function M.explore(node, status)
local profile = log.profile_start("explore init %s", node.absolute_path) 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 is_root = not node.parent
local child_folder_only = explorer_node.has_one_child_folder(node) and node.nodes[1] local child_folder_only = explorer_node.has_one_child_folder(node) and node.nodes[1]
@ -74,14 +74,14 @@ function M.explore(node, status)
local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path
local child_status = git.load_project_status(child_cwd) local child_status = git.load_project_status(child_cwd)
node.group_next = child_folder_only 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 {} node.nodes = ns or {}
log.profile_end(profile) log.profile_end(profile)
return ns return ns
end end
sorters.sort(node.nodes) parent.sorters:sort(node.nodes)
live_filter.apply_filter(node) live_filter.apply_filter(node)
log.profile_end(profile) log.profile_end(profile)

View File

@ -1,15 +1,52 @@
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local M = { ---@class Filters to handle all opts.filters and related API
ignore_list = {}, ---@field config table hydrated user opts.filters
exclude_list = {}, ---@field private explorer Explorer
custom_function = nil, ---@field private exclude_list string[] filters.exclude
} ---@field private ignore_list string[] filters.custom string table
---@field private custom_function (fun(absolute_path: string): boolean)|nil filters.custom function
local Filters = {}
---@param opts table user options
---@param explorer Explorer
---@return Filters
function Filters:new(opts, explorer)
local o = {
explorer = explorer,
ignore_list = {},
exclude_list = opts.filters.exclude,
custom_function = nil,
config = {
enable = opts.filters.enable,
filter_custom = true,
filter_dotfiles = opts.filters.dotfiles,
filter_git_ignored = opts.filters.git_ignored,
filter_git_clean = opts.filters.git_clean,
filter_no_buffer = opts.filters.no_buffer,
filter_no_bookmark = opts.filters.no_bookmark,
},
}
local custom_filter = opts.filters.custom
if type(custom_filter) == "function" then
o.custom_function = custom_filter
else
if custom_filter and #custom_filter > 0 then
for _, filter_name in pairs(custom_filter) do
o.ignore_list[filter_name] = true
end
end
end
setmetatable(o, self)
self.__index = self
return o
end
---@param path string ---@param path string
---@return boolean ---@return boolean
local function is_excluded(path) local function is_excluded(self, path)
for _, node in ipairs(M.exclude_list) do for _, node in ipairs(self.exclude_list) do
if path:match(node) then if path:match(node) then
return true return true
end end
@ -21,7 +58,7 @@ end
---@param path string Absolute path ---@param path string Absolute path
---@param git_status table from prepare ---@param git_status table from prepare
---@return boolean ---@return boolean
local function git(path, git_status) local function git(self, path, git_status)
if type(git_status) ~= "table" or type(git_status.files) ~= "table" or type(git_status.dirs) ~= "table" then if type(git_status) ~= "table" or type(git_status.files) ~= "table" or type(git_status.dirs) ~= "table" then
return false return false
end end
@ -32,12 +69,12 @@ local function git(path, git_status)
status = status or git_status.dirs.indirect[path] and git_status.dirs.indirect[path][1] status = status or git_status.dirs.indirect[path] and git_status.dirs.indirect[path][1]
-- filter ignored; overrides clean as they are effectively dirty -- filter ignored; overrides clean as they are effectively dirty
if M.config.filter_git_ignored and status == "!!" then if self.config.filter_git_ignored and status == "!!" then
return true return true
end end
-- filter clean -- filter clean
if M.config.filter_git_clean and not status then if self.config.filter_git_clean and not status then
return true return true
end end
@ -48,8 +85,8 @@ end
---@param path string Absolute path ---@param path string Absolute path
---@param bufinfo table vim.fn.getbufinfo { buflisted = 1 } ---@param bufinfo table vim.fn.getbufinfo { buflisted = 1 }
---@return boolean ---@return boolean
local function buf(path, bufinfo) local function buf(self, path, bufinfo)
if not M.config.filter_no_buffer or type(bufinfo) ~= "table" then if not self.config.filter_no_buffer or type(bufinfo) ~= "table" then
return false return false
end end
@ -65,15 +102,15 @@ end
---@param path string ---@param path string
---@return boolean ---@return boolean
local function dotfile(path) local function dotfile(self, path)
return M.config.filter_dotfiles and utils.path_basename(path):sub(1, 1) == "." return self.config.filter_dotfiles and utils.path_basename(path):sub(1, 1) == "."
end end
---@param path string ---@param path string
---@param path_type string|nil filetype of path ---@param path_type string|nil filetype of path
---@param bookmarks table<string, string|nil> path, filetype table of bookmarked files ---@param bookmarks table<string, string|nil> path, filetype table of bookmarked files
local function bookmark(path, path_type, bookmarks) local function bookmark(self, path, path_type, bookmarks)
if not M.config.filter_no_bookmark then if not self.config.filter_no_bookmark then
return false return false
end end
-- if bookmark is empty, we should see a empty filetree -- if bookmark is empty, we should see a empty filetree
@ -107,21 +144,21 @@ end
---@param path string ---@param path string
---@return boolean ---@return boolean
local function custom(path) local function custom(self, path)
if not M.config.filter_custom then if not self.config.filter_custom then
return false return false
end end
local basename = utils.path_basename(path) local basename = utils.path_basename(path)
-- filter user's custom function -- filter user's custom function
if M.custom_function and M.custom_function(path) then if self.custom_function and self.custom_function(path) then
return true return true
end end
-- filter custom regexes -- filter custom regexes
local relpath = utils.path_relative(path, vim.loop.cwd()) local relpath = utils.path_relative(path, vim.loop.cwd())
for pat, _ in pairs(M.ignore_list) do for pat, _ in pairs(self.ignore_list) do
if vim.fn.match(relpath, pat) ~= -1 or vim.fn.match(basename, pat) ~= -1 then if vim.fn.match(relpath, pat) ~= -1 or vim.fn.match(basename, pat) ~= -1 then
return true return true
end end
@ -129,7 +166,7 @@ local function custom(path)
local idx = path:match ".+()%.[^.]+$" local idx = path:match ".+()%.[^.]+$"
if idx then if idx then
if M.ignore_list["*" .. string.sub(path, idx)] == true then if self.ignore_list["*" .. string.sub(path, idx)] == true then
return true return true
end end
end end
@ -143,14 +180,14 @@ end
--- git_status: reference --- git_status: reference
--- bufinfo: empty unless no_buffer set: vim.fn.getbufinfo { buflisted = 1 } --- bufinfo: empty unless no_buffer set: vim.fn.getbufinfo { buflisted = 1 }
--- bookmarks: absolute paths to boolean --- bookmarks: absolute paths to boolean
function M.prepare(git_status) function Filters:prepare(git_status)
local status = { local status = {
git_status = git_status or {}, git_status = git_status or {},
bufinfo = {}, bufinfo = {},
bookmarks = {}, bookmarks = {},
} }
if M.config.filter_no_buffer then if self.config.filter_no_buffer then
status.bufinfo = vim.fn.getbufinfo { buflisted = 1 } status.bufinfo = vim.fn.getbufinfo { buflisted = 1 }
end end
@ -169,47 +206,21 @@ end
---@param fs_stat uv.fs_stat.result|nil fs_stat of file ---@param fs_stat uv.fs_stat.result|nil fs_stat of file
---@param status table from prepare ---@param status table from prepare
---@return boolean ---@return boolean
function M.should_filter(path, fs_stat, status) function Filters:should_filter(path, fs_stat, status)
if not M.config.enable then if not self.config.enable then
return false return false
end end
-- exclusions override all filters -- exclusions override all filters
if is_excluded(path) then if is_excluded(self, path) then
return false return false
end end
return git(path, status.git_status) return git(self, path, status.git_status)
or buf(path, status.bufinfo) or buf(self, path, status.bufinfo)
or dotfile(path) or dotfile(self, path)
or custom(path) or custom(self, path)
or bookmark(path, fs_stat and fs_stat.type, status.bookmarks) or bookmark(self, path, fs_stat and fs_stat.type, status.bookmarks)
end end
function M.setup(opts) return Filters
M.config = {
enable = opts.filters.enable,
filter_custom = true,
filter_dotfiles = opts.filters.dotfiles,
filter_git_ignored = opts.filters.git_ignored,
filter_git_clean = opts.filters.git_clean,
filter_no_buffer = opts.filters.no_buffer,
filter_no_bookmark = opts.filters.no_bookmark,
}
M.ignore_list = {}
M.exclude_list = opts.filters.exclude
local custom_filter = opts.filters.custom
if type(custom_filter) == "function" then
M.custom_function = custom_filter
else
if custom_filter and #custom_filter > 0 then
for _, filter_name in pairs(custom_filter) do
M.ignore_list[filter_name] = true
end
end
end
end
return M

View File

@ -2,8 +2,10 @@ local git = require "nvim-tree.git"
local notify = require "nvim-tree.notify" local notify = require "nvim-tree.notify"
local watch = require "nvim-tree.explorer.watch" local watch = require "nvim-tree.explorer.watch"
local explorer_node = require "nvim-tree.explorer.node" local explorer_node = require "nvim-tree.explorer.node"
local Filters = require "nvim-tree.explorer.filters"
local Marks = require "nvim-tree.marks" local Marks = require "nvim-tree.marks"
local LiveFilter = require "nvim-tree.live-filter" local LiveFilter = require "nvim-tree.live-filter"
local Sorters = require "nvim-tree.explorer.sorters"
local M = {} local M = {}
@ -39,10 +41,12 @@ function Explorer.new(path)
absolute_path = path, absolute_path = path,
nodes = {}, nodes = {},
open = true, open = true,
live_filter = LiveFilter:new(M.config),
marks = Marks:new(), marks = Marks:new(),
sorters = Sorters:new(M.config),
}, Explorer) }, Explorer)
explorer.watcher = watch.create_watcher(explorer) explorer.watcher = watch.create_watcher(explorer)
explorer.filters = Filters:new(M.config, explorer)
explorer.live_filter = LiveFilter:new(M.config, explorer)
explorer:_load(explorer) explorer:_load(explorer)
return explorer return explorer
end end
@ -52,7 +56,7 @@ end
function Explorer:_load(node) function Explorer:_load(node)
local cwd = node.link_to or node.absolute_path local cwd = node.link_to or node.absolute_path
local git_status = git.load_project_status(cwd) local git_status = git.load_project_status(cwd)
M.explore(node, git_status) M.explore(node, git_status, self)
end end
---@param node Node ---@param node Node
@ -73,10 +77,9 @@ function Explorer:destroy()
end end
function M.setup(opts) function M.setup(opts)
M.config = opts
require("nvim-tree.explorer.node").setup(opts) require("nvim-tree.explorer.node").setup(opts)
require("nvim-tree.explorer.explore").setup(opts) require("nvim-tree.explorer.explore").setup(opts)
require("nvim-tree.explorer.filters").setup(opts)
require("nvim-tree.explorer.sorters").setup(opts)
require("nvim-tree.explorer.reload").setup(opts) require("nvim-tree.explorer.reload").setup(opts)
require("nvim-tree.explorer.watch").setup(opts) require("nvim-tree.explorer.watch").setup(opts)
end end

View File

@ -1,16 +1,17 @@
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local Iterator = require "nvim-tree.iterators.node-iterator" local Iterator = require "nvim-tree.iterators.node-iterator"
local filters = require "nvim-tree.explorer.filters"
local LiveFilter = {} local LiveFilter = {}
function LiveFilter:new(opts) function LiveFilter:new(opts, explorer)
local o = {} local o = {
explorer = explorer,
config = vim.deepcopy(opts.live_filter),
filter = nil,
}
setmetatable(o, self) setmetatable(o, self)
self.__index = self self.__index = self
o.config = vim.deepcopy(opts.live_filter)
o.filter = nil
return o return o
end end
@ -19,8 +20,8 @@ local function redraw()
end end
---@param node_ Node|nil ---@param node_ Node|nil
local function reset_filter(node_) local function reset_filter(self, node_)
node_ = node_ or require("nvim-tree.core").get_explorer() node_ = node_ or self.explorer
if node_ == nil then if node_ == nil then
return return
@ -37,7 +38,7 @@ end
local overlay_bufnr = 0 local overlay_bufnr = 0
local overlay_winnr = 0 local overlay_winnr = 0
local function remove_overlay() local function remove_overlay(self)
if view.View.float.enable and view.View.float.quit_on_focus_loss then if view.View.float.enable and view.View.float.quit_on_focus_loss then
-- return to normal nvim-tree float behaviour when filter window is closed -- return to normal nvim-tree float behaviour when filter window is closed
vim.api.nvim_create_autocmd("WinLeave", { vim.api.nvim_create_autocmd("WinLeave", {
@ -56,16 +57,15 @@ local function remove_overlay()
overlay_bufnr = 0 overlay_bufnr = 0
overlay_winnr = 0 overlay_winnr = 0
local explorer = require("nvim-tree.core").get_explorer() if self.explorer.live_filter.filter == "" then
if explorer and explorer.live_filter.filter == "" then self.explorer.live_filter.clear_filter()
explorer.live_filter.clear_filter()
end end
end end
---@param node Node ---@param node Node
---@return boolean ---@return boolean
local function matches(self, node) local function matches(self, node)
if not filters.config.enable then if not self.explorer.filters.config.enable then
return true return true
end end
@ -77,7 +77,7 @@ end
---@param node_ Node|nil ---@param node_ Node|nil
function LiveFilter:apply_filter(node_) function LiveFilter:apply_filter(node_)
if not self.filter or self.filter == "" then if not self.filter or self.filter == "" then
reset_filter(node_) reset_filter(self, node_)
return return
end end
@ -97,33 +97,30 @@ function LiveFilter:apply_filter(node_)
end end
local has_nodes = nodes and (self.config.always_show_folders or #nodes > filtered_nodes) local has_nodes = nodes and (self.config.always_show_folders or #nodes > filtered_nodes)
local ok, is_match = pcall(matches, node) local ok, is_match = pcall(matches, self, node)
node.hidden = not (has_nodes or (ok and is_match)) node.hidden = not (has_nodes or (ok and is_match))
end end
iterate(node_ or require("nvim-tree.core").get_explorer()) iterate(node_ or self.explorer)
end end
local function record_char() local function record_char(self)
vim.schedule(function() vim.schedule(function()
local explorer = require("nvim-tree.core").get_explorer() self.explorer.live_filter.filter = vim.api.nvim_buf_get_lines(overlay_bufnr, 0, -1, false)[1]
if explorer then self.explorer.live_filter.apply_filter()
explorer.live_filter.filter = vim.api.nvim_buf_get_lines(overlay_bufnr, 0, -1, false)[1] redraw()
explorer.live_filter.apply_filter()
redraw()
end
end) end)
end end
local function configure_buffer_overlay() local function configure_buffer_overlay(self)
overlay_bufnr = vim.api.nvim_create_buf(false, true) overlay_bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_attach(overlay_bufnr, true, { vim.api.nvim_buf_attach(overlay_bufnr, true, {
on_lines = record_char, on_lines = function() return record_char(self) end,
}) })
vim.api.nvim_create_autocmd("InsertLeave", { vim.api.nvim_create_autocmd("InsertLeave", {
callback = remove_overlay, callback = function() return remove_overlay(self) end,
once = true, once = true,
}) })
@ -131,21 +128,17 @@ local function configure_buffer_overlay()
end end
---@return integer ---@return integer
local function calculate_overlay_win_width(explorer) local function calculate_overlay_win_width(self)
local wininfo = vim.fn.getwininfo(view.get_winnr())[1] local wininfo = vim.fn.getwininfo(view.get_winnr())[1]
if wininfo then if wininfo then
return wininfo.width - wininfo.textoff - #explorer.live_filter.prefix return wininfo.width - wininfo.textoff - #self.explorer.live_filter.prefix
end end
return 20 return 20
end end
local function create_overlay() local function create_overlay(self)
local explorer = require("nvim-tree.core").get_explorer()
if not explorer then
return
end
if view.View.float.enable then if view.View.float.enable then
-- don't close nvim-tree float when focus is changed to filter window -- don't close nvim-tree float when focus is changed to filter window
vim.api.nvim_clear_autocmds { vim.api.nvim_clear_autocmds {
@ -155,12 +148,12 @@ local function create_overlay()
} }
end end
configure_buffer_overlay() configure_buffer_overlay(self)
overlay_winnr = vim.api.nvim_open_win(overlay_bufnr, true, { overlay_winnr = vim.api.nvim_open_win(overlay_bufnr, true, {
col = 1, col = 1,
row = 0, row = 0,
relative = "cursor", relative = "cursor",
width = calculate_overlay_win_width(explorer), width = calculate_overlay_win_width(self.explorer),
height = 1, height = 1,
border = "none", border = "none",
style = "minimal", style = "minimal",
@ -172,9 +165,9 @@ local function create_overlay()
vim.api.nvim_buf_set_option(overlay_bufnr, "modifiable", true) ---@diagnostic disable-line: deprecated vim.api.nvim_buf_set_option(overlay_bufnr, "modifiable", true) ---@diagnostic disable-line: deprecated
end end
vim.api.nvim_buf_set_lines(overlay_bufnr, 0, -1, false, { explorer.live_filter.filter }) vim.api.nvim_buf_set_lines(overlay_bufnr, 0, -1, false, { self.explorer.live_filter.filter })
vim.cmd "startinsert" vim.cmd "startinsert"
vim.api.nvim_win_set_cursor(overlay_winnr, { 1, #explorer.live_filter.filter + 1 }) vim.api.nvim_win_set_cursor(overlay_winnr, { 1, #self.explorer.live_filter.filter + 1 })
end end
function LiveFilter:start_filtering() function LiveFilter:start_filtering()
@ -186,7 +179,7 @@ function LiveFilter:start_filtering()
local col = #self.prefix > 0 and #self.prefix - 1 or 1 local col = #self.prefix > 0 and #self.prefix - 1 or 1
view.set_cursor { row, col } view.set_cursor { row, col }
-- needs scheduling to let the cursor move before initializing the window -- needs scheduling to let the cursor move before initializing the window
vim.schedule(create_overlay) vim.schedule(function() return create_overlay(self) end)
end end
function LiveFilter:clear_filter() function LiveFilter:clear_filter()
@ -194,7 +187,7 @@ function LiveFilter:clear_filter()
local last_node = view.View.live_filter.prev_focused_node local last_node = view.View.live_filter.prev_focused_node
self.filter = nil self.filter = nil
reset_filter() reset_filter(self)
redraw() redraw()
if node then if node then

View File

@ -1,8 +1,6 @@
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local builders = require "nvim-tree.explorer.node-builders" local builders = require "nvim-tree.explorer.node-builders"
local explorer_node = require "nvim-tree.explorer.node" local explorer_node = require "nvim-tree.explorer.node"
local filters = require "nvim-tree.explorer.filters"
local sorters = require "nvim-tree.explorer.sorters"
local live_filter = require "nvim-tree.live-filter" local live_filter = require "nvim-tree.live-filter"
local git = require "nvim-tree.git" local git = require "nvim-tree.git"
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
@ -70,6 +68,10 @@ end
---@param node Node ---@param node Node
---@param git_status table ---@param git_status table
function M.reload(node, git_status) function M.reload(node, git_status)
local explorer = require("nvim-tree.core").get_explorer()
if not explorer then
return
end
local cwd = node.link_to or node.absolute_path local cwd = node.link_to or node.absolute_path
local handle = vim.loop.fs_scandir(cwd) local handle = vim.loop.fs_scandir(cwd)
if not handle then if not handle then
@ -78,7 +80,7 @@ function M.reload(node, git_status)
local profile = log.profile_start("reload %s", node.absolute_path) local profile = log.profile_start("reload %s", node.absolute_path)
local filter_status = filters.prepare(git_status) local filter_status = explorer.filters:prepare(git_status)
if node.group_next then if node.group_next then
node.nodes = { node.group_next } node.nodes = { node.group_next }
@ -100,7 +102,7 @@ function M.reload(node, git_status)
---@type uv.fs_stat.result|nil ---@type uv.fs_stat.result|nil
local stat = vim.loop.fs_stat(abs) local stat = vim.loop.fs_stat(abs)
if not filters.should_filter(abs, stat, filter_status) then if not explorer.filters:should_filter(abs, stat, filter_status) then
remain_childs[abs] = true remain_childs[abs] = true
-- Recreate node if type changes. -- Recreate node if type changes.
@ -162,7 +164,7 @@ function M.reload(node, git_status)
return ns return ns
end end
sorters.sort(node.nodes) explorer.sorters:sort(node.nodes)
live_filter.apply_filter(node) live_filter.apply_filter(node)
log.profile_end(profile) log.profile_end(profile)
return node.nodes return node.nodes

View File

@ -1,12 +1,27 @@
local M = {}
local C = {} local C = {}
---@class Sorter
local Sorter = {}
function Sorter:new(opts)
local o = {}
setmetatable(o, self)
self.__index = self
o.config = vim.deepcopy(opts.sort)
if type(o.config.sorter) == "function" then
o.user = o.config.sorter
end
return o
end
--- Predefined comparator, defaulting to name --- Predefined comparator, defaulting to name
---@param sorter string as per options ---@param sorter string as per options
---@return function ---@return function
local function get_comparator(sorter) function Sorter:get_comparator(sorter)
return C[sorter] or C.name return function(a, b)
return (C[sorter] or C.name)(a, b, self.config)
end
end end
---Create a shallow copy of a portion of a list. ---Create a shallow copy of a portion of a list.
@ -27,17 +42,17 @@ end
---@param a Node ---@param a Node
---@param b Node ---@param b Node
---@return boolean|nil ---@return boolean|nil
local function folders_or_files_first(a, b) local function folders_or_files_first(a, b, cfg)
if not (M.config.sort.folders_first or M.config.sort.files_first) then if not (cfg.folders_first or cfg.files_first) then
return return
end end
if not a.nodes and b.nodes then if not a.nodes and b.nodes then
-- file <> folder -- file <> folder
return M.config.sort.files_first return cfg.files_first
elseif a.nodes and not b.nodes then elseif a.nodes and not b.nodes then
-- folder <> file -- folder <> file
return not M.config.sort.files_first return not cfg.files_first
end end
end end
@ -97,8 +112,8 @@ end
---Perform a merge sort using sorter option. ---Perform a merge sort using sorter option.
---@param t table nodes ---@param t table nodes
function M.sort(t) function Sorter:sort(t)
if C.user then if self.user then
local t_user = {} local t_user = {}
local origin_index = {} local origin_index = {}
@ -115,9 +130,9 @@ function M.sort(t)
table.insert(origin_index, n) table.insert(origin_index, n)
end end
local predefined = C.user(t_user) local predefined = self.user(t_user)
if predefined then if predefined then
split_merge(t, 1, #t, get_comparator(predefined)) split_merge(t, 1, #t, self:get_comparator(predefined))
return return
end end
@ -142,7 +157,7 @@ function M.sort(t)
split_merge(t, 1, #t, mini_comparator) -- sort by user order split_merge(t, 1, #t, mini_comparator) -- sort by user order
else else
split_merge(t, 1, #t, get_comparator(M.config.sort.sorter)) split_merge(t, 1, #t, self:get_comparator(self.config.sorter))
end end
end end
@ -150,12 +165,12 @@ end
---@param b Node ---@param b Node
---@param ignorecase boolean|nil ---@param ignorecase boolean|nil
---@return boolean ---@return boolean
local function node_comparator_name_ignorecase_or_not(a, b, ignorecase) local function node_comparator_name_ignorecase_or_not(a, b, ignorecase, cfg)
if not (a and b) then if not (a and b) then
return true return true
end end
local early_return = folders_or_files_first(a, b) local early_return = folders_or_files_first(a, b, cfg)
if early_return ~= nil then if early_return ~= nil then
return early_return return early_return
end end
@ -167,20 +182,20 @@ local function node_comparator_name_ignorecase_or_not(a, b, ignorecase)
end end
end end
function C.case_sensitive(a, b) function C.case_sensitive(a, b, cfg)
return node_comparator_name_ignorecase_or_not(a, b, false) return node_comparator_name_ignorecase_or_not(a, b, false, cfg)
end end
function C.name(a, b) function C.name(a, b, cfg)
return node_comparator_name_ignorecase_or_not(a, b, true) return node_comparator_name_ignorecase_or_not(a, b, true, cfg)
end end
function C.modification_time(a, b) function C.modification_time(a, b, cfg)
if not (a and b) then if not (a and b) then
return true return true
end end
local early_return = folders_or_files_first(a, b) local early_return = folders_or_files_first(a, b, cfg)
if early_return ~= nil then if early_return ~= nil then
return early_return return early_return
end end
@ -199,17 +214,17 @@ function C.modification_time(a, b)
return last_modified_b <= last_modified_a return last_modified_b <= last_modified_a
end end
function C.suffix(a, b) function C.suffix(a, b, cfg)
if not (a and b) then if not (a and b) then
return true return true
end end
-- directories go first -- directories go first
local early_return = folders_or_files_first(a, b) local early_return = folders_or_files_first(a, b, cfg)
if early_return ~= nil then if early_return ~= nil then
return early_return return early_return
elseif a.nodes and b.nodes then elseif a.nodes and b.nodes then
return C.name(a, b) return C.name(a, b, cfg)
end end
-- dotfiles go second -- dotfiles go second
@ -218,7 +233,7 @@ function C.suffix(a, b)
elseif a.name:sub(1, 1) ~= "." and b.name:sub(1, 1) == "." then elseif a.name:sub(1, 1) ~= "." and b.name:sub(1, 1) == "." then
return false return false
elseif a.name:sub(1, 1) == "." and b.name:sub(1, 1) == "." then elseif a.name:sub(1, 1) == "." and b.name:sub(1, 1) == "." then
return C.name(a, b) return C.name(a, b, cfg)
end end
-- unsuffixed go third -- unsuffixed go third
@ -230,7 +245,7 @@ function C.suffix(a, b)
elseif a_suffix_ndx and not b_suffix_ndx then elseif a_suffix_ndx and not b_suffix_ndx then
return false return false
elseif not (a_suffix_ndx and b_suffix_ndx) then elseif not (a_suffix_ndx and b_suffix_ndx) then
return C.name(a, b) return C.name(a, b, cfg)
end end
-- finally, compare by suffixes -- finally, compare by suffixes
@ -242,18 +257,18 @@ function C.suffix(a, b)
elseif not a_suffix and b_suffix then elseif not a_suffix and b_suffix then
return false return false
elseif a_suffix:lower() == b_suffix:lower() then elseif a_suffix:lower() == b_suffix:lower() then
return C.name(a, b) return C.name(a, b, cfg)
end end
return a_suffix:lower() < b_suffix:lower() return a_suffix:lower() < b_suffix:lower()
end end
function C.extension(a, b) function C.extension(a, b, cfg)
if not (a and b) then if not (a and b) then
return true return true
end end
local early_return = folders_or_files_first(a, b) local early_return = folders_or_files_first(a, b, cfg)
if early_return ~= nil then if early_return ~= nil then
return early_return return early_return
end end
@ -267,18 +282,18 @@ function C.extension(a, b)
local a_ext = (a.extension or ""):lower() local a_ext = (a.extension or ""):lower()
local b_ext = (b.extension or ""):lower() local b_ext = (b.extension or ""):lower()
if a_ext == b_ext then if a_ext == b_ext then
return C.name(a, b) return C.name(a, b, cfg)
end end
return a_ext < b_ext return a_ext < b_ext
end end
function C.filetype(a, b) function C.filetype(a, b, cfg)
local a_ft = vim.filetype.match { filename = a.name } local a_ft = vim.filetype.match { filename = a.name }
local b_ft = vim.filetype.match { filename = b.name } local b_ft = vim.filetype.match { filename = b.name }
-- directories first -- directories first
local early_return = folders_or_files_first(a, b) local early_return = folders_or_files_first(a, b, cfg)
if early_return ~= nil then if early_return ~= nil then
return early_return return early_return
end end
@ -292,19 +307,10 @@ function C.filetype(a, b)
-- same filetype or both nil, sort by name -- same filetype or both nil, sort by name
if a_ft == b_ft then if a_ft == b_ft then
return C.name(a, b) return C.name(a, b, cfg)
end end
return a_ft < b_ft return a_ft < b_ft
end end
function M.setup(opts) return Sorter
M.config = {}
M.config.sort = opts.sort
if type(M.config.sort.sorter) == "function" then
C.user = M.config.sort.sorter
end
end
return M