Skip to content
Merged
100 changes: 90 additions & 10 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2007,43 +2007,99 @@ fs.print_clipboard() *nvim-tree-api.fs.print_clipboard()*
Parameters: ~
• {node} (Node|nil) file or folder

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

Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

*nvim-tree-api.node.open.replace_tree_buffer()*
node.open.replace_tree_buffer({node})
|nvim-tree-api.node.edit()|, file will be opened in place: in the
nvim-tree window.

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

node.open.vertical({node}) *nvim-tree-api.node.open.vertical()*
Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

node.open.vertical({node}, {opts}) *nvim-tree-api.node.open.vertical()*
|nvim-tree-api.node.edit()|, file will be opened in a new vertical split.

Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

*nvim-tree-api.node.open.vertical_no_picker()*
node.open.vertical_no_picker({node})
node.open.vertical_no_picker({node}, {opts})
|nvim-tree-api.node.vertical()|, window picker will never be used as per
|nvim-tree.actions.open_file.window_picker.enable| `false`

node.open.horizontal({node}) *nvim-tree-api.node.open.horizontal()*
Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

node.open.horizontal({node}, {opts}) *nvim-tree-api.node.open.horizontal()*
|nvim-tree-api.node.edit()|, file will be opened in a new horizontal split.

Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

*nvim-tree-api.node.open.horizontal_no_picker()*
node.open.horizontal_no_picker({node})
node.open.horizontal_no_picker({node}, {opts})
|nvim-tree-api.node.horizontal()|, window picker will never be used as per
|nvim-tree.actions.open_file.window_picker.enable| `false`

Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

*nvim-tree-api.node.open.toggle_group_empty()*
node.open.toggle_group_empty({node})
node.open.toggle_group_empty({node}, {opts})
Toggle |nvim-tree.renderer.group_empty| for a specific folder.
Does nothing on files.
Needs |nvim-tree.renderer.group_empty| set.

Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

node.open.drop({node}) *nvim-tree-api.node.open.drop()*
Switch to window with selected file if it exists.
Open file otherwise.
Expand All @@ -2053,9 +2109,17 @@ node.open.drop({node}) *nvim-tree-api.node.open.drop()*
Folder: expand or collapse
Root: change directory up

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

Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

*nvim-tree-api.node.open.tab_drop()*
node.open.tab_drop({node})
Switch to tab containing window with selected file if it exists.
Expand All @@ -2065,15 +2129,31 @@ node.open.tab_drop({node})
Folder: expand or collapse
Root: change directory up

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

Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

*nvim-tree-api.node.open.preview_no_picker()*
node.open.preview_no_picker({node})
node.open.preview_no_picker({node}, {opts})
|nvim-tree-api.node.edit()|, file buffer will have |bufhidden| set to `delete`.
window picker will never be used as per
|nvim-tree.actions.open_file.window_picker.enable| `false`

Parameters: ~
• {node} (Node|nil) file or folder
• {opts} (table) optional parameters

Options: ~
• {quit_on_open} (boolean) quits the tree when opening the file
• {focus} (boolean) keep focus in the tree when opening the file

node.navigate.git.next({node}) *nvim-tree-api.node.navigate.git.next()*
Navigate to the next item showing git status.

Expand Down
33 changes: 29 additions & 4 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,46 @@ Api.fs.copy.absolute_path = wrap_node(wrap_explorer_member("clipboard", "copy_ab
Api.fs.copy.filename = wrap_node(wrap_explorer_member("clipboard", "copy_filename"))
Api.fs.copy.basename = wrap_node(wrap_explorer_member("clipboard", "copy_basename"))
Api.fs.copy.relative_path = wrap_node(wrap_explorer_member("clipboard", "copy_path"))
---
---@class NodeEditOpts
---@field quit_on_open boolean|nil default false
---@field focus boolean|nil default true

---@param mode string
---@param node Node
local function edit(mode, node)
---@param edit_opts NodeEditOpts?
local function edit(mode, node, edit_opts)
local file_link = node:as(FileLinkNode)
local path = file_link and file_link.link_to or node.absolute_path
local cur_tabpage = vim.api.nvim_get_current_tabpage()

actions.node.open_file.fn(mode, path)

edit_opts = edit_opts or {}

local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then
view.close(cur_tabpage)
end

local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
local focus = edit_opts.focus == nil or edit_opts.focus == true
if not mode_unsupported_focus and not focus then
-- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab
if mode == "tabnew" then
vim.cmd(":tabprev")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
vim.cmd(":tabprev")
vim.cmd.tabprev()

When neovim gets around to exposing these as proper API we will be ready.

end
view.focus()
end
end

---@param mode string
---@param toggle_group boolean?
---@return fun(node: Node)
---@return fun(node: Node, edit_opts: NodeEditOpts?)
local function open_or_expand_or_dir_up(mode, toggle_group)
---@param node Node
Copy link
Member

Choose a reason for hiding this comment

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

Missing param, CI got upset: https://github.com/nvim-tree/nvim-tree.lua/actions/runs/12975816599/job/36187330634?pr=3054

Needs

  ---@param edit_opts NodeEditOpts?

I recommend setting up wiki: Lua Language Server, which will show a warning about these sorts of things.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok will do, thanks :)

return function(node)
---@param edit_opts NodeEditOpts?
return function(node, edit_opts)
local root = node:as(RootNode)
local dir = node:as(DirectoryNode)

Expand All @@ -245,7 +270,7 @@ local function open_or_expand_or_dir_up(mode, toggle_group)
elseif dir then
dir:expand_or_collapse(toggle_group)
elseif not toggle_group then
edit(mode, node)
edit(mode, node, edit_opts)
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,12 @@ function M.close_all_tabs()
end
end

function M.close()
---@param tabpage integer|nil
function M.close(tabpage)
if M.View.tab.sync.close then
M.close_all_tabs()
elseif tabpage then
close(tabpage)
else
M.close_this_tab_only()
end
Expand Down
Loading