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
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -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 = true,
Copy link
Member

Choose a reason for hiding this comment

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

This would be a change to existing behaviour; we'll need to default to false.

change_dir = {
enable = true,
global = false,
Expand Down Expand Up @@ -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: `true`

*nvim-tree.actions.change_dir*
vim |current-directory| behaviour.

Expand Down Expand Up @@ -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|
Expand Down
15 changes: 13 additions & 2 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ local function setup_autocommands(opts)
end,
})
end

if opts.actions.set_last_win_as_target then
create_nvim_tree_autocmd("BufLeave", {
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid autocommands; this should be done in open-file.lua

callback = function()
local buf_name = vim.api.nvim_buf_get_name(0)
Copy link
Member

Choose a reason for hiding this comment

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

bufnr / bufname should come from the ev argument passed. See :help nvim_create_autocmd()

if not string.find(buf_name, "NvimTree_") then
Copy link
Member

Choose a reason for hiding this comment

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

utils.is_nvim_tree_buf should be used.

require("nvim-tree.lib").set_target_win()
end
end,
})
end
end

local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
Expand Down Expand Up @@ -429,6 +440,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
},
actions = {
use_system_clipboard = true,
set_last_win_as_target = true,
change_dir = {
enable = true,
global = false,
Expand Down Expand Up @@ -490,8 +502,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
default_yes = false,
},
},
experimental = {
},
experimental = {},
log = {
enable = false,
truncate = false,
Expand Down
Loading