Skip to content

Commit 2fd26f3

Browse files
alex-courtisHinell
authored and
Hinell
committed
feat(open-file): Add new node selection action based on :drop command
1 parent 967865c commit 2fd26f3

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

doc/nvim-tree-lua.txt

+20
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,26 @@ node.open.edit() *nvim-tree-api.node.open.edit()*
15271527
Folder: expand or collapse
15281528
Root: change directory up
15291529

1530+
*nvim-tree-api.node.open.tab_drop()*
1531+
node.open.tab_drop()
1532+
Like edit(), but reuse already-opened window in tab. See also: |:drop|
1533+
File: open already-opened as per |nvim-tree.actions.open_file|
1534+
Folder: expand or collapse
1535+
Root: change directory up
1536+
Usage example: >
1537+
1538+
require("nvim-tree").setup({
1539+
on_attach = function(bufnr)
1540+
local function opts(desc)
1541+
return { desc = 'nvim-tree: ' .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
1542+
end
1543+
local ok, api = pcall(require, "nvim-tree.api")
1544+
assert(ok, "api module is not found")
1545+
vim.keymap.set("n", "<CR>", api.node.open.tab_drop, opts("Tab drop"))
1546+
end
1547+
})
1548+
<
1549+
15301550
*nvim-tree-api.node.open.replace_tree_buffer()*
15311551
node.open.replace_tree_buffer()
15321552
|nvim-tree-api.node.edit()|, file will be opened in place: in the

lua/nvim-tree/actions/node/open-file.lua

+13
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,22 @@ local function pick_win_id()
141141
return win_map[resp]
142142
end
143143

144+
144145
local function open_file_in_tab(filename)
145146
if M.quit_on_open then
146147
view.close()
147148
end
148149
vim.cmd("tabe " .. vim.fn.fnameescape(filename))
149150
end
150151

152+
-- See :drop command. This will always focus already-opened tab
153+
local function tab_drop(filename)
154+
if M.quit_on_open then
155+
view.close()
156+
end
157+
vim.cmd("tab :drop " .. vim.fn.fnameescape(filename))
158+
end
159+
151160
local function on_preview(buf_loaded)
152161
if not buf_loaded then
153162
vim.bo.bufhidden = "delete"
@@ -284,6 +293,10 @@ function M.fn(mode, filename)
284293
return open_file_in_tab(filename)
285294
end
286295

296+
if mode == "tab_drop" then
297+
return tab_drop(filename)
298+
end
299+
287300
if mode == "edit_in_place" then
288301
return edit_in_current_buf(filename)
289302
end

lua/nvim-tree/api.lua

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ local function open_preview(node)
162162
end
163163

164164
Api.node.open.edit = wrap_node(open_or_expand_or_dir_up "edit")
165+
Api.node.open.tab_drop = wrap_node(open_or_expand_or_dir_up "tab_drop")
165166
Api.node.open.replace_tree_buffer = wrap_node(open_or_expand_or_dir_up "edit_in_place")
166167
Api.node.open.no_window_picker = wrap_node(open_or_expand_or_dir_up "edit_no_picker")
167168
Api.node.open.vertical = wrap_node(open_or_expand_or_dir_up "vsplit")

0 commit comments

Comments
 (0)