Skip to content

feat: add option to set last active window as target for file actions #3008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
@@ -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|
4 changes: 2 additions & 2 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
@@ -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,
9 changes: 9 additions & 0 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

@alex-courtis alex-courtis Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This always evaluates to true, as it's not actually calling the function.

This will always set target_winid on leaving a buffer, and will set it to special buffers such as help. That will violate existing selection criteria like actions.open_file.window_picker.exclude.

lib.set_target_win()
end
end,
})
end
end

return M