Skip to content

Commit d871619

Browse files
committed
feat: add option to set last active window as target for file actions
- Add new option `set_last_win_as_target` to `opts.actions` - Update documentation to reflect new option - Set default value of `set_last_win_as_target` to `true`
1 parent f7c65e1 commit d871619

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

doc/nvim-tree-lua.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
570570
},
571571
actions = {
572572
use_system_clipboard = true,
573+
set_last_win_as_target = true,
573574
change_dir = {
574575
enable = true,
575576
global = false,
@@ -1434,6 +1435,14 @@ function are invoked. When enabled, copied text will be stored in registers
14341435
'+' (system), otherwise, it will be stored in '1' and '"'.
14351436
Type: `boolean`, Default: `true`
14361437

1438+
*nvim-tree.actions.set_last_win_as_target*
1439+
A boolean value that toggles the behavior of setting the last active window as
1440+
the target window for `nvim-tree`. When enabled, the last active window will be
1441+
set as the target for file actions (like opening files), ensuring better window
1442+
management. When disabled, the default behavior applies.
1443+
1444+
Type: `boolean`, Default: `true`
1445+
14371446
*nvim-tree.actions.change_dir*
14381447
vim |current-directory| behaviour.
14391448

@@ -2887,6 +2896,7 @@ highlight group is not, hard linking as follows: >
28872896
|nvim-tree.actions.open_file.window_picker.picker|
28882897
|nvim-tree.actions.remove_file.close_window|
28892898
|nvim-tree.actions.use_system_clipboard|
2899+
|nvim-tree.actions.set_last_win_as_target|
28902900
|nvim-tree.auto_reload_on_write|
28912901
|nvim-tree.diagnostics.debounce_delay|
28922902
|nvim-tree.diagnostics.enable|

lua/nvim-tree.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ local function setup_autocommands(opts)
232232
end,
233233
})
234234
end
235+
236+
if opts.actions.set_last_win_as_target then
237+
create_nvim_tree_autocmd("BufLeave", {
238+
callback = function()
239+
local buf_name = vim.api.nvim_buf_get_name(0)
240+
if not string.find(buf_name, "NvimTree_") then
241+
require("nvim-tree.lib").set_target_win()
242+
end
243+
end,
244+
})
245+
end
235246
end
236247

237248
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
@@ -429,6 +440,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
429440
},
430441
actions = {
431442
use_system_clipboard = true,
443+
set_last_win_as_target = true,
432444
change_dir = {
433445
enable = true,
434446
global = false,
@@ -490,8 +502,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
490502
default_yes = false,
491503
},
492504
},
493-
experimental = {
494-
},
505+
experimental = {},
495506
log = {
496507
enable = false,
497508
truncate = false,

0 commit comments

Comments
 (0)