feat: add update_focused_file.exclude (#2673)

* Add update_focused_file.exclude and move update_focused_file.ignore_list to update_focused_file.update_root.ignore_list

* Pass ci checks

* Add config migration for update_root and ignore_list

* Missed one mention of update root in find_file.lua

* Update migration code

Co-authored-by: Alexander Courtis <alex@courtis.org>

* make docs consistent

* match on filename instead of entire path

* exclude as a function

* fix docs

* default exclude value

---------

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Ava Harris 2024-03-24 23:41:05 -05:00 committed by GitHub
parent 85c502e907
commit e20966ae55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 16 deletions

View File

@ -500,9 +500,12 @@ Following is the default configuration. See |nvim-tree-opts| for details.
}, },
update_focused_file = { update_focused_file = {
enable = false, enable = false,
update_root = false, update_root = {
enable = false,
ignore_list = {}, ignore_list = {},
}, },
exclude = false,
},
system_open = { system_open = {
cmd = "", cmd = "",
args = {}, args = {},
@ -1105,15 +1108,22 @@ Update the root directory of the tree if the file is not under current
root directory. It prefers vim's cwd and `root_dirs`. root directory. It prefers vim's cwd and `root_dirs`.
Otherwise it falls back to the folder containing the file. Otherwise it falls back to the folder containing the file.
Only relevant when `update_focused_file.enable` is `true` Only relevant when `update_focused_file.enable` is `true`
*nvim-tree.update_focused_file.update_root.enable*
Type: `boolean`, Default: `false` Type: `boolean`, Default: `false`
*nvim-tree.update_focused_file.ignore_list* *nvim-tree.update_focused_file.update_root.ignore_list*
List of buffer names and filetypes that will not update the root dir List of buffer names and filetypes that will not update the root dir
of the tree if the file isn't found under the current root directory. of the tree if the file isn't found under the current root directory.
Only relevant when `update_focused_file.update_root` and Only relevant when `update_focused_file.update_root.enable` and
`update_focused_file.enable` are `true`. `update_focused_file.enable` are `true`.
Type: {string}, Default: `{}` Type: {string}, Default: `{}`
*nvim-tree.update_focused_file.exclude*
A function that returns true if the file should not be focused when opening.
Takes the `BufEnter` event as an argument. see |autocmd-events|
Type: {function}, Default: `false`
============================================================================== ==============================================================================
5.6 OPTS: SYSTEM OPEN *nvim-tree-opts-system-open* 5.6 OPTS: SYSTEM OPEN *nvim-tree-opts-system-open*
@ -2869,8 +2879,10 @@ highlight group is not, hard linking as follows: >
|nvim-tree.ui.confirm.remove| |nvim-tree.ui.confirm.remove|
|nvim-tree.ui.confirm.trash| |nvim-tree.ui.confirm.trash|
|nvim-tree.update_focused_file.enable| |nvim-tree.update_focused_file.enable|
|nvim-tree.update_focused_file.ignore_list| |nvim-tree.update_focused_file.exclude|
|nvim-tree.update_focused_file.update_root| |nvim-tree.update_focused_file.update_root|
|nvim-tree.update_focused_file.update_root.enable|
|nvim-tree.update_focused_file.update_root.ignore_list|
|nvim-tree.view.centralize_selection| |nvim-tree.view.centralize_selection|
|nvim-tree.view.cursorline| |nvim-tree.view.cursorline|
|nvim-tree.view.debounce_delay| |nvim-tree.view.debounce_delay|

View File

@ -27,7 +27,7 @@ function M.change_root(path, bufnr)
-- skip if current file is in ignore_list -- skip if current file is in ignore_list
if type(bufnr) == "number" then if type(bufnr) == "number" then
local ft = vim.api.nvim_buf_get_option(bufnr, "filetype") or "" local ft = vim.api.nvim_buf_get_option(bufnr, "filetype") or ""
for _, value in pairs(_config.update_focused_file.ignore_list) do for _, value in pairs(_config.update_focused_file.update_root.ignore_list) do
if utils.str_find(path, value) or utils.str_find(ft, value) then if utils.str_find(path, value) or utils.str_find(ft, value) then
return return
end end
@ -149,7 +149,7 @@ function M.change_dir(name)
actions.root.change_dir.fn(name) actions.root.change_dir.fn(name)
end end
if _config.update_focused_file.enable then if _config.update_focused_file.update_root.enable then
actions.tree.find_file.fn() actions.tree.find_file.fn()
end end
end end
@ -247,7 +247,11 @@ local function setup_autocommands(opts)
end end
if opts.update_focused_file.enable then if opts.update_focused_file.enable then
create_nvim_tree_autocmd("BufEnter", { create_nvim_tree_autocmd("BufEnter", {
callback = function() callback = function(event)
local exclude = opts.update_focused_file.exclude
if type(exclude) == "function" and exclude(event) then
return
end
utils.debounce("BufEnter:find_file", opts.view.debounce_delay, function() utils.debounce("BufEnter:find_file", opts.view.debounce_delay, function()
actions.tree.find_file.fn() actions.tree.find_file.fn()
end) end)
@ -462,9 +466,12 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
}, },
update_focused_file = { update_focused_file = {
enable = false, enable = false,
update_root = false, update_root = {
enable = false,
ignore_list = {}, ignore_list = {},
}, },
exclude = false,
},
system_open = { system_open = {
cmd = "", cmd = "",
args = {}, args = {},
@ -624,6 +631,9 @@ local ACCEPTED_TYPES = {
group_empty = { "boolean", "function" }, group_empty = { "boolean", "function" },
root_folder_label = { "function", "string", "boolean" }, root_folder_label = { "function", "string", "boolean" },
}, },
update_focused_file = {
exclude = { "function" },
},
filters = { filters = {
custom = { "function" }, custom = { "function" },
}, },

View File

@ -55,7 +55,7 @@ function M.fn(opts)
end end
-- update root -- update root
if opts.update_root or M.config.update_focused_file.update_root then if opts.update_root or M.config.update_focused_file.update_root.enable then
require("nvim-tree").change_root(path, bufnr) require("nvim-tree").change_root(path, bufnr)
end end

View File

@ -52,6 +52,14 @@ local function refactored(opts)
if type(opts.renderer) == "table" and type(opts.renderer.highlight_git) == "boolean" then if type(opts.renderer) == "table" and type(opts.renderer.highlight_git) == "boolean" then
opts.renderer.highlight_git = opts.renderer.highlight_git and "name" or "none" opts.renderer.highlight_git = opts.renderer.highlight_git and "name" or "none"
end end
-- 2024/02/15
if type(opts.update_focused_file) == "table" then
if type(opts.update_focused_file.update_root) ~= "table" then
opts.update_focused_file.update_root = { enable = opts.update_focused_file.update_root }
end
end
utils.move_missing_val(opts, "update_focused_file", "ignore_list", opts, "update_focused_file.update_root", "ignore_list", true)
end end
local function deprecated(opts) local function deprecated(opts)