Skip to content

Commit e20966a

Browse files
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 <[email protected]> * make docs consistent * match on filename instead of entire path * exclude as a function * fix docs * default exclude value --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent 85c502e commit e20966a

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

Diff for: doc/nvim-tree-lua.txt

+22-10
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,11 @@ Following is the default configuration. See |nvim-tree-opts| for details.
500500
},
501501
update_focused_file = {
502502
enable = false,
503-
update_root = false,
504-
ignore_list = {},
503+
update_root = {
504+
enable = false,
505+
ignore_list = {},
506+
},
507+
exclude = false,
505508
},
506509
system_open = {
507510
cmd = "",
@@ -1105,14 +1108,21 @@ Update the root directory of the tree if the file is not under current
11051108
root directory. It prefers vim's cwd and `root_dirs`.
11061109
Otherwise it falls back to the folder containing the file.
11071110
Only relevant when `update_focused_file.enable` is `true`
1108-
Type: `boolean`, Default: `false`
11091111

1110-
*nvim-tree.update_focused_file.ignore_list*
1111-
List of buffer names and filetypes that will not update the root dir
1112-
of the tree if the file isn't found under the current root directory.
1113-
Only relevant when `update_focused_file.update_root` and
1114-
`update_focused_file.enable` are `true`.
1115-
Type: {string}, Default: `{}`
1112+
*nvim-tree.update_focused_file.update_root.enable*
1113+
Type: `boolean`, Default: `false`
1114+
1115+
*nvim-tree.update_focused_file.update_root.ignore_list*
1116+
List of buffer names and filetypes that will not update the root dir
1117+
of the tree if the file isn't found under the current root directory.
1118+
Only relevant when `update_focused_file.update_root.enable` and
1119+
`update_focused_file.enable` are `true`.
1120+
Type: {string}, Default: `{}`
1121+
1122+
*nvim-tree.update_focused_file.exclude*
1123+
A function that returns true if the file should not be focused when opening.
1124+
Takes the `BufEnter` event as an argument. see |autocmd-events|
1125+
Type: {function}, Default: `false`
11161126

11171127
==============================================================================
11181128
5.6 OPTS: SYSTEM OPEN *nvim-tree-opts-system-open*
@@ -2869,8 +2879,10 @@ highlight group is not, hard linking as follows: >
28692879
|nvim-tree.ui.confirm.remove|
28702880
|nvim-tree.ui.confirm.trash|
28712881
|nvim-tree.update_focused_file.enable|
2872-
|nvim-tree.update_focused_file.ignore_list|
2882+
|nvim-tree.update_focused_file.exclude|
28732883
|nvim-tree.update_focused_file.update_root|
2884+
|nvim-tree.update_focused_file.update_root.enable|
2885+
|nvim-tree.update_focused_file.update_root.ignore_list|
28742886
|nvim-tree.view.centralize_selection|
28752887
|nvim-tree.view.cursorline|
28762888
|nvim-tree.view.debounce_delay|

Diff for: lua/nvim-tree.lua

+15-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function M.change_root(path, bufnr)
2727
-- skip if current file is in ignore_list
2828
if type(bufnr) == "number" then
2929
local ft = vim.api.nvim_buf_get_option(bufnr, "filetype") or ""
30-
for _, value in pairs(_config.update_focused_file.ignore_list) do
30+
for _, value in pairs(_config.update_focused_file.update_root.ignore_list) do
3131
if utils.str_find(path, value) or utils.str_find(ft, value) then
3232
return
3333
end
@@ -149,7 +149,7 @@ function M.change_dir(name)
149149
actions.root.change_dir.fn(name)
150150
end
151151

152-
if _config.update_focused_file.enable then
152+
if _config.update_focused_file.update_root.enable then
153153
actions.tree.find_file.fn()
154154
end
155155
end
@@ -247,7 +247,11 @@ local function setup_autocommands(opts)
247247
end
248248
if opts.update_focused_file.enable then
249249
create_nvim_tree_autocmd("BufEnter", {
250-
callback = function()
250+
callback = function(event)
251+
local exclude = opts.update_focused_file.exclude
252+
if type(exclude) == "function" and exclude(event) then
253+
return
254+
end
251255
utils.debounce("BufEnter:find_file", opts.view.debounce_delay, function()
252256
actions.tree.find_file.fn()
253257
end)
@@ -462,8 +466,11 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
462466
},
463467
update_focused_file = {
464468
enable = false,
465-
update_root = false,
466-
ignore_list = {},
469+
update_root = {
470+
enable = false,
471+
ignore_list = {},
472+
},
473+
exclude = false,
467474
},
468475
system_open = {
469476
cmd = "",
@@ -624,6 +631,9 @@ local ACCEPTED_TYPES = {
624631
group_empty = { "boolean", "function" },
625632
root_folder_label = { "function", "string", "boolean" },
626633
},
634+
update_focused_file = {
635+
exclude = { "function" },
636+
},
627637
filters = {
628638
custom = { "function" },
629639
},

Diff for: lua/nvim-tree/actions/tree/find-file.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function M.fn(opts)
5555
end
5656

5757
-- update root
58-
if opts.update_root or M.config.update_focused_file.update_root then
58+
if opts.update_root or M.config.update_focused_file.update_root.enable then
5959
require("nvim-tree").change_root(path, bufnr)
6060
end
6161

Diff for: lua/nvim-tree/legacy.lua

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ local function refactored(opts)
5252
if type(opts.renderer) == "table" and type(opts.renderer.highlight_git) == "boolean" then
5353
opts.renderer.highlight_git = opts.renderer.highlight_git and "name" or "none"
5454
end
55+
56+
-- 2024/02/15
57+
if type(opts.update_focused_file) == "table" then
58+
if type(opts.update_focused_file.update_root) ~= "table" then
59+
opts.update_focused_file.update_root = { enable = opts.update_focused_file.update_root }
60+
end
61+
end
62+
utils.move_missing_val(opts, "update_focused_file", "ignore_list", opts, "update_focused_file.update_root", "ignore_list", true)
5563
end
5664

5765
local function deprecated(opts)

0 commit comments

Comments
 (0)