Skip to content
31 changes: 20 additions & 11 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,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 = { "gitcommit" },
},
system_open = {
cmd = "",
Expand Down Expand Up @@ -657,12 +660,12 @@ Opens in place of the unnamed buffer if it's empty.

*nvim-tree.root_dirs*
Preferred root directories.
Only relevant when `update_focused_file.update_root` is `true`
Only relevant when `update_focused_file.update_root` is `enabled`
Comment thread
ava5627 marked this conversation as resolved.
Outdated
Type: `{string}`, Default: `{}`

*nvim-tree.prefer_startup_root*
Prefer startup root directory when updating root directory of the tree.
Only relevant when `update_focused_file.update_root` is `true`
Only relevant when `update_focused_file.update_root` is `enabled`
Type: `boolean`, Default: `false`

*nvim-tree.sync_root_with_cwd* (previously `update_cwd`)
Expand Down Expand Up @@ -1091,14 +1094,20 @@ 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*
List of buffer names and filetypes that will not update the focused file.
Type: {string}, Default: `{ "gitcommit" }`

==============================================================================
5.6 OPTS: SYSTEM OPEN *nvim-tree-opts-system-open*
Expand Down
18 changes: 14 additions & 4 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 @@ -248,6 +248,13 @@ local function setup_autocommands(opts)
if opts.update_focused_file.enable then
create_nvim_tree_autocmd("BufEnter", {
callback = function()
for _, value in pairs(_config.update_focused_file.exclude) do
local ft = vim.api.nvim_buf_get_option(0, "filetype")
local path = vim.fn.expand "%:p"
if utils.str_find(path, value) or utils.str_find(ft, value) then
return
end
end
utils.debounce("BufEnter:find_file", opts.view.debounce_delay, function()
actions.tree.find_file.fn()
end)
Expand Down Expand Up @@ -462,8 +469,11 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
},
update_focused_file = {
enable = false,
update_root = false,
ignore_list = {},
update_root = {
enable = false,
ignore_list = {},
},
exclude = { "gitcommit" },
},
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
11 changes: 11 additions & 0 deletions lua/nvim-tree/legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ 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.update_root) == "boolean" then
opts.update_focused_file.update_root = {
enable = opts.update_focused_file.update_root,
}
if opts.update_focused_file.ignore_list then
opts.update_focused_file.update_root.ignore_list = opts.update_focused_file.ignore_list
opts.update_focused_file.ignore_list = nil
end
end
Comment thread
ava5627 marked this conversation as resolved.
Outdated
end

local function deprecated(opts)
Expand Down