Skip to content

Commit 3281f33

Browse files
feat(#1984): add quit_on_open and focus opts to various api.node.open functions (#3054)
* feat: add quit_on_open opt to api.node.open.edit * fix: fix missing @param annotation * feat: add focus opt to api.node.open.edit * fix: fix focus == false behaviour on api.node.open.tab command * fix: add optional tabpage integer parameter to view.close if tabpage is not nil, then the function closes the tabpage in this specific tabpage * fix: fix quit_on_open == true behaviour on api.node.open.tab command * fix: add check to not use new opts for certain edit modes * fix: add docs for new opts --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent 8052310 commit 3281f33

File tree

3 files changed

+123
-15
lines changed

3 files changed

+123
-15
lines changed

doc/nvim-tree-lua.txt

+90-10
Original file line numberDiff line numberDiff line change
@@ -2007,43 +2007,99 @@ fs.print_clipboard() *nvim-tree-api.fs.print_clipboard()*
20072007
Parameters: ~
20082008
{node} (Node|nil) file or folder
20092009

2010-
node.open.edit({node}) *nvim-tree-api.node.open.edit()*
2010+
node.open.edit({node}, {opts}) *nvim-tree-api.node.open.edit()*
20112011
File: open as per |nvim-tree.actions.open_file|
20122012
Folder: expand or collapse
20132013
Root: change directory up
20142014

2015+
Parameters: ~
2016+
{node} (Node|nil) file or folder
2017+
{opts} (table) optional parameters
2018+
2019+
Options: ~
2020+
• {quit_on_open} (boolean) quits the tree when opening the file
2021+
{focus} (boolean) keep focus in the tree when opening the file
2022+
20152023
*nvim-tree-api.node.open.replace_tree_buffer()*
20162024
node.open.replace_tree_buffer({node})
20172025
|nvim-tree-api.node.edit()|, file will be opened in place: in the
20182026
nvim-tree window.
20192027

20202028
*nvim-tree-api.node.open.no_window_picker()*
2021-
node.open.no_window_picker({node})
2029+
node.open.no_window_picker({node}, {opts})
20222030
|nvim-tree-api.node.edit()|, window picker will never be used as per
20232031
|nvim-tree.actions.open_file.window_picker.enable| `false`
20242032

2025-
node.open.vertical({node}) *nvim-tree-api.node.open.vertical()*
2033+
Parameters: ~
2034+
{node} (Node|nil) file or folder
2035+
{opts} (table) optional parameters
2036+
2037+
Options: ~
2038+
• {quit_on_open} (boolean) quits the tree when opening the file
2039+
{focus} (boolean) keep focus in the tree when opening the file
2040+
2041+
node.open.vertical({node}, {opts}) *nvim-tree-api.node.open.vertical()*
20262042
|nvim-tree-api.node.edit()|, file will be opened in a new vertical split.
20272043

2044+
Parameters: ~
2045+
{node} (Node|nil) file or folder
2046+
{opts} (table) optional parameters
2047+
2048+
Options: ~
2049+
• {quit_on_open} (boolean) quits the tree when opening the file
2050+
{focus} (boolean) keep focus in the tree when opening the file
2051+
20282052
*nvim-tree-api.node.open.vertical_no_picker()*
2029-
node.open.vertical_no_picker({node})
2053+
node.open.vertical_no_picker({node}, {opts})
20302054
|nvim-tree-api.node.vertical()|, window picker will never be used as per
20312055
|nvim-tree.actions.open_file.window_picker.enable| `false`
20322056

2033-
node.open.horizontal({node}) *nvim-tree-api.node.open.horizontal()*
2057+
Parameters: ~
2058+
{node} (Node|nil) file or folder
2059+
{opts} (table) optional parameters
2060+
2061+
Options: ~
2062+
• {quit_on_open} (boolean) quits the tree when opening the file
2063+
{focus} (boolean) keep focus in the tree when opening the file
2064+
2065+
node.open.horizontal({node}, {opts}) *nvim-tree-api.node.open.horizontal()*
20342066
|nvim-tree-api.node.edit()|, file will be opened in a new horizontal split.
20352067

2068+
Parameters: ~
2069+
{node} (Node|nil) file or folder
2070+
{opts} (table) optional parameters
2071+
2072+
Options: ~
2073+
• {quit_on_open} (boolean) quits the tree when opening the file
2074+
{focus} (boolean) keep focus in the tree when opening the file
2075+
20362076
*nvim-tree-api.node.open.horizontal_no_picker()*
2037-
node.open.horizontal_no_picker({node})
2077+
node.open.horizontal_no_picker({node}, {opts})
20382078
|nvim-tree-api.node.horizontal()|, window picker will never be used as per
20392079
|nvim-tree.actions.open_file.window_picker.enable| `false`
20402080

2081+
Parameters: ~
2082+
{node} (Node|nil) file or folder
2083+
{opts} (table) optional parameters
2084+
2085+
Options: ~
2086+
• {quit_on_open} (boolean) quits the tree when opening the file
2087+
{focus} (boolean) keep focus in the tree when opening the file
2088+
20412089
*nvim-tree-api.node.open.toggle_group_empty()*
2042-
node.open.toggle_group_empty({node})
2090+
node.open.toggle_group_empty({node}, {opts})
20432091
Toggle |nvim-tree.renderer.group_empty| for a specific folder.
20442092
Does nothing on files.
20452093
Needs |nvim-tree.renderer.group_empty| set.
20462094

2095+
Parameters: ~
2096+
{node} (Node|nil) file or folder
2097+
{opts} (table) optional parameters
2098+
2099+
Options: ~
2100+
• {quit_on_open} (boolean) quits the tree when opening the file
2101+
{focus} (boolean) keep focus in the tree when opening the file
2102+
20472103
node.open.drop({node}) *nvim-tree-api.node.open.drop()*
20482104
Switch to window with selected file if it exists.
20492105
Open file otherwise.
@@ -2053,9 +2109,17 @@ node.open.drop({node}) *nvim-tree-api.node.open.drop()*
20532109
Folder: expand or collapse
20542110
Root: change directory up
20552111

2056-
node.open.tab({node}) *nvim-tree-api.node.open.tab()*
2112+
node.open.tab({node}, {opts}) *nvim-tree-api.node.open.tab()*
20572113
|nvim-tree-api.node.edit()|, file will be opened in a new tab.
20582114

2115+
Parameters: ~
2116+
{node} (Node|nil) file or folder
2117+
{opts} (table) optional parameters
2118+
2119+
Options: ~
2120+
• {quit_on_open} (boolean) quits the tree when opening the file
2121+
{focus} (boolean) keep focus in the tree when opening the file
2122+
20592123
*nvim-tree-api.node.open.tab_drop()*
20602124
node.open.tab_drop({node})
20612125
Switch to tab containing window with selected file if it exists.
@@ -2065,15 +2129,31 @@ node.open.tab_drop({node})
20652129
Folder: expand or collapse
20662130
Root: change directory up
20672131

2068-
node.open.preview({node}) *nvim-tree-api.node.open.preview()*
2132+
node.open.preview({node}, {opts}) *nvim-tree-api.node.open.preview()*
20692133
|nvim-tree-api.node.edit()|, file buffer will have |bufhidden| set to `delete`.
20702134

2135+
Parameters: ~
2136+
{node} (Node|nil) file or folder
2137+
{opts} (table) optional parameters
2138+
2139+
Options: ~
2140+
• {quit_on_open} (boolean) quits the tree when opening the file
2141+
{focus} (boolean) keep focus in the tree when opening the file
2142+
20712143
*nvim-tree-api.node.open.preview_no_picker()*
2072-
node.open.preview_no_picker({node})
2144+
node.open.preview_no_picker({node}, {opts})
20732145
|nvim-tree-api.node.edit()|, file buffer will have |bufhidden| set to `delete`.
20742146
window picker will never be used as per
20752147
|nvim-tree.actions.open_file.window_picker.enable| `false`
20762148

2149+
Parameters: ~
2150+
{node} (Node|nil) file or folder
2151+
{opts} (table) optional parameters
2152+
2153+
Options: ~
2154+
• {quit_on_open} (boolean) quits the tree when opening the file
2155+
{focus} (boolean) keep focus in the tree when opening the file
2156+
20772157
node.navigate.git.next({node}) *nvim-tree-api.node.navigate.git.next()*
20782158
Navigate to the next item showing git status.
20792159

lua/nvim-tree/api.lua

+29-4
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,46 @@ Api.fs.copy.absolute_path = wrap_node(wrap_explorer_member("clipboard", "copy_ab
222222
Api.fs.copy.filename = wrap_node(wrap_explorer_member("clipboard", "copy_filename"))
223223
Api.fs.copy.basename = wrap_node(wrap_explorer_member("clipboard", "copy_basename"))
224224
Api.fs.copy.relative_path = wrap_node(wrap_explorer_member("clipboard", "copy_path"))
225+
---
226+
---@class NodeEditOpts
227+
---@field quit_on_open boolean|nil default false
228+
---@field focus boolean|nil default true
225229

226230
---@param mode string
227231
---@param node Node
228-
local function edit(mode, node)
232+
---@param edit_opts NodeEditOpts?
233+
local function edit(mode, node, edit_opts)
229234
local file_link = node:as(FileLinkNode)
230235
local path = file_link and file_link.link_to or node.absolute_path
236+
local cur_tabpage = vim.api.nvim_get_current_tabpage()
237+
231238
actions.node.open_file.fn(mode, path)
239+
240+
edit_opts = edit_opts or {}
241+
242+
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
243+
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then
244+
view.close(cur_tabpage)
245+
end
246+
247+
local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
248+
local focus = edit_opts.focus == nil or edit_opts.focus == true
249+
if not mode_unsupported_focus and not focus then
250+
-- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab
251+
if mode == "tabnew" then
252+
vim.cmd(":tabprev")
253+
end
254+
view.focus()
255+
end
232256
end
233257

234258
---@param mode string
235259
---@param toggle_group boolean?
236-
---@return fun(node: Node)
260+
---@return fun(node: Node, edit_opts: NodeEditOpts?)
237261
local function open_or_expand_or_dir_up(mode, toggle_group)
238262
---@param node Node
239-
return function(node)
263+
---@param edit_opts NodeEditOpts?
264+
return function(node, edit_opts)
240265
local root = node:as(RootNode)
241266
local dir = node:as(DirectoryNode)
242267

@@ -245,7 +270,7 @@ local function open_or_expand_or_dir_up(mode, toggle_group)
245270
elseif dir then
246271
dir:expand_or_collapse(toggle_group)
247272
elseif not toggle_group then
248-
edit(mode, node)
273+
edit(mode, node, edit_opts)
249274
end
250275
end
251276
end

lua/nvim-tree/view.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,12 @@ function M.close_all_tabs()
270270
end
271271
end
272272

273-
function M.close()
273+
---@param tabpage integer|nil
274+
function M.close(tabpage)
274275
if M.View.tab.sync.close then
275276
M.close_all_tabs()
277+
elseif tabpage then
278+
close(tabpage)
276279
else
277280
M.close_this_tab_only()
278281
end

0 commit comments

Comments
 (0)