diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 89ac4e722ca..916d55530ab 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -570,6 +570,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua }, actions = { use_system_clipboard = true, + set_last_win_as_target = false, change_dir = { enable = true, global = false, @@ -1434,6 +1435,14 @@ function are invoked. When enabled, copied text will be stored in registers '+' (system), otherwise, it will be stored in '1' and '"'. Type: `boolean`, Default: `true` +*nvim-tree.actions.set_last_win_as_target* +A boolean value that toggles the behavior of setting the last active window as +the target window for `nvim-tree`. When enabled, the last active window will be +set as the target for file actions (like opening files), ensuring better window +management. When disabled, the default behavior applies. + + Type: `boolean`, Default: `false` + *nvim-tree.actions.change_dir* vim |current-directory| behaviour. @@ -2887,6 +2896,7 @@ highlight group is not, hard linking as follows: > |nvim-tree.actions.open_file.window_picker.picker| |nvim-tree.actions.remove_file.close_window| |nvim-tree.actions.use_system_clipboard| +|nvim-tree.actions.set_last_win_as_target| |nvim-tree.auto_reload_on_write| |nvim-tree.diagnostics.debounce_delay| |nvim-tree.diagnostics.enable| diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 4d72d1859df..cf9e87bbfa8 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -429,6 +429,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS }, actions = { use_system_clipboard = true, + set_last_win_as_target = false, change_dir = { enable = true, global = false, @@ -490,8 +491,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS default_yes = false, }, }, - experimental = { - }, + experimental = {}, log = { enable = false, truncate = false, diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 1d1fc2ecc12..2aafc9cc172 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -426,6 +426,15 @@ function M.setup(opts) opts.actions.open_file.window_picker.chars = tostring(opts.actions.open_file.window_picker.chars):upper() end M.window_picker = opts.actions.open_file.window_picker + if opts.actions.set_last_win_as_target then + vim.api.nvim_create_autocmd("BufLeave", { + callback = function() + if utils.is_nvim_tree_buf then + lib.set_target_win() + end + end, + }) + end end return M