-
-
Notifications
You must be signed in to change notification settings - Fork 616
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
feat: add option to set last active window as target for file actions #3008
Conversation
- 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`
Does The |
doc/nvim-tree-lua.txt
Outdated
@@ -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, |
There was a problem hiding this comment.
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.
lua/nvim-tree.lua
Outdated
@@ -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", { |
There was a problem hiding this comment.
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
lua/nvim-tree.lua
Outdated
create_nvim_tree_autocmd("BufLeave", { | ||
callback = function() | ||
local buf_name = vim.api.nvim_buf_get_name(0) | ||
if not string.find(buf_name, "NvimTree_") then |
There was a problem hiding this comment.
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.
lua/nvim-tree.lua
Outdated
if opts.actions.set_last_win_as_target then | ||
create_nvim_tree_autocmd("BufLeave", { | ||
callback = function() | ||
local buf_name = vim.api.nvim_buf_get_name(0) |
There was a problem hiding this comment.
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()
Looking at similar issue #2262 |
- Changed the default value of `set_last_win_as_target` back to `false` to maintain original behavior. - Moved the autocmd for `set_last_win_as_target` to `open-file.lua` for better organization.
I’ve updated the code as requested and reviewed the behavior of api.node.open.no_window_picker(). I didn’t encounter any issues. Looking at similar issue #2262, I see vim.fn.winnr("#") mentioned as a more reliable approach. I’m not sure how it applies to this pull request. Could you clarify or provide an example? Let me know if there’s anything else that needs adjustment! |
The idea is to use the actual previous window, according to nvim, rather than the one tracked via It could be something like: local function get_target_winid(mode)
local target_winid
if not M.window_picker.enable or mode == "edit_no_picker" or mode == "preview_no_picker" then
target_winnr = vim.fn.winnr("#") We would likely need to check that against Looking through other issues it seems that this is not the best approach: #2262 (comment) This whole area of functionality is a mess and needs a rewrite. Let's stick with tracking for now; it will be rewritten as part of #2255 |
if opts.actions.set_last_win_as_target then | ||
vim.api.nvim_create_autocmd("BufLeave", { | ||
callback = function() | ||
if utils.is_nvim_tree_buf then |
There was a problem hiding this comment.
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
.
I'm sorry, this is not a change we should make:
I think the best option for you is to write a |
set_last_win_as_target
toopts.actions
set_last_win_as_target
totrue