diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 81a3edd4209..b9b2225f159 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -500,8 +500,11 @@ 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 = false, }, system_open = { cmd = "", @@ -1105,14 +1108,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: `false` ============================================================================== 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.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| diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 0c2d8752dc2..b2103be3c23 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -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 @@ -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 @@ -247,7 +247,11 @@ local function setup_autocommands(opts) end if opts.update_focused_file.enable then 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() actions.tree.find_file.fn() end) @@ -462,8 +466,11 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS }, update_focused_file = { enable = false, - update_root = false, - ignore_list = {}, + update_root = { + enable = false, + ignore_list = {}, + }, + exclude = false, }, system_open = { cmd = "", @@ -624,6 +631,9 @@ local ACCEPTED_TYPES = { group_empty = { "boolean", "function" }, root_folder_label = { "function", "string", "boolean" }, }, + update_focused_file = { + exclude = { "function" }, + }, filters = { custom = { "function" }, }, diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua index 3678276d654..610c534989a 100644 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ b/lua/nvim-tree/actions/tree/find-file.lua @@ -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 diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index 43fcb3c5998..9be8fb12ffa 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -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)