Skip to content

feat: add update_focused_file.exclude #2673

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 11 commits into from
Mar 25, 2024
34 changes: 24 additions & 10 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,13 @@ Following is the default configuration. See |nvim-tree-opts| for details.
},
update_focused_file = {
enable = false,
update_root = false,
ignore_list = {},
update_root = {
enable = false,
ignore_list = {},
},
exclude = function()
return false
end,
},
system_open = {
cmd = "",
Expand Down Expand Up @@ -1105,14 +1110,21 @@ Update the root directory of the tree if the file is not under current
root directory. It prefers vim's cwd and `root_dirs`.
Otherwise it falls back to the folder containing the file.
Only relevant when `update_focused_file.enable` is `true`
Type: `boolean`, Default: `false`

*nvim-tree.update_focused_file.ignore_list*
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.
Only relevant when `update_focused_file.update_root` and
`update_focused_file.enable` are `true`.
Type: {string}, Default: `{}`
*nvim-tree.update_focused_file.update_root.enable*
Type: `boolean`, Default: `false`

*nvim-tree.update_focused_file.update_root.ignore_list*
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.
Only relevant when `update_focused_file.update_root.enable` and
`update_focused_file.enable` are `true`.
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: `function() return false end`

==============================================================================
5.6 OPTS: SYSTEM OPEN *nvim-tree-opts-system-open*
Expand Down Expand Up @@ -2869,8 +2881,10 @@ highlight group is not, hard linking as follows: >
|nvim-tree.ui.confirm.remove|
|nvim-tree.ui.confirm.trash|
|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.enable|
|nvim-tree.update_focused_file.update_root.ignore_list|
|nvim-tree.view.centralize_selection|
|nvim-tree.view.cursorline|
|nvim-tree.view.debounce_delay|
Expand Down
18 changes: 13 additions & 5 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function M.change_root(path, bufnr)
-- skip if current file is in ignore_list
if type(bufnr) == "number" then
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
return
end
Expand Down Expand Up @@ -149,7 +149,7 @@ function M.change_dir(name)
actions.root.change_dir.fn(name)
end

if _config.update_focused_file.enable then
if _config.update_focused_file.update_root.enable then
actions.tree.find_file.fn()
end
end
Expand Down Expand Up @@ -247,7 +247,10 @@ local function setup_autocommands(opts)
end
if opts.update_focused_file.enable then
create_nvim_tree_autocmd("BufEnter", {
callback = function()
callback = function(event)
if opts.update_focused_file.exclude(event) then
return
end
utils.debounce("BufEnter:find_file", opts.view.debounce_delay, function()
actions.tree.find_file.fn()
end)
Expand Down Expand Up @@ -462,8 +465,13 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
},
update_focused_file = {
enable = false,
update_root = false,
ignore_list = {},
update_root = {
enable = false,
ignore_list = {},
},
exclude = function()
Copy link
Member

Choose a reason for hiding this comment

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

Please use false here, rather than a function, following the convention of similar. This keeps the config small.

Please put a new "function" definiton in ACCEPTED_TYPES so that validation may occur.

You'll need to update help as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

return false
end,
},
system_open = {
cmd = "",
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/actions/tree/find-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function M.fn(opts)
end

-- 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)
end

Expand Down
8 changes: 8 additions & 0 deletions lua/nvim-tree/legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ local function refactored(opts)
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"
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

local function deprecated(opts)
Expand Down