From 60406892b6b438a6f5280093601e61f95966f9a8 Mon Sep 17 00:00:00 2001 From: Hinell Date: Sat, 22 Apr 2023 12:25:55 +0300 Subject: [PATCH 1/2] feat(open-file): Add new node selection action based on :drop command --- doc/nvim-tree-lua.txt | 7 +++++++ lua/nvim-tree/actions/node/open-file.lua | 13 +++++++++++++ lua/nvim-tree/api.lua | 1 + 3 files changed, 21 insertions(+) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 7c29ef58c0c..389d9f25845 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1527,6 +1527,13 @@ node.open.edit() *nvim-tree-api.node.open.edit()* Folder: expand or collapse Root: change directory up + *nvim-tree-api.node.open.tab_drop()* +node.open.tab_drop() + Like edit(), but reuse already-opened window in tab. See also: |:drop| + File: open already-opened as per |nvim-tree.actions.open_file| + Folder: expand or collapse + Root: change directory up + *nvim-tree-api.node.open.replace_tree_buffer()* node.open.replace_tree_buffer() |nvim-tree-api.node.edit()|, file will be opened in place: in the diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 41302d92f54..0e42dc6e8bf 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -141,6 +141,7 @@ local function pick_win_id() return win_map[resp] end + local function open_file_in_tab(filename) if M.quit_on_open then view.close() @@ -148,6 +149,14 @@ local function open_file_in_tab(filename) vim.cmd("tabe " .. vim.fn.fnameescape(filename)) end +-- See :drop command. This will always focus already-opened tab +local function tab_drop(filename) + if M.quit_on_open then + view.close() + end + vim.cmd("tab :drop " .. vim.fn.fnameescape(filename)) +end + local function on_preview(buf_loaded) if not buf_loaded then vim.bo.bufhidden = "delete" @@ -284,6 +293,10 @@ function M.fn(mode, filename) return open_file_in_tab(filename) end + if mode == "tab_drop" then + return tab_drop(filename) + end + if mode == "edit_in_place" then return edit_in_current_buf(filename) end diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 49dc05c13cf..a4614eee5d0 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -162,6 +162,7 @@ local function open_preview(node) end Api.node.open.edit = wrap_node(open_or_expand_or_dir_up "edit") +Api.node.open.tab_drop = wrap_node(open_or_expand_or_dir_up "tab_drop") Api.node.open.replace_tree_buffer = wrap_node(open_or_expand_or_dir_up "edit_in_place") Api.node.open.no_window_picker = wrap_node(open_or_expand_or_dir_up "edit_no_picker") Api.node.open.vertical = wrap_node(open_or_expand_or_dir_up "vsplit") From d5b6eb9c5a85333457184c6f34eda5c582ed2829 Mon Sep 17 00:00:00 2001 From: gegoune Date: Sun, 23 Apr 2023 02:34:50 +0300 Subject: [PATCH 2/2] chore: reword and move docs, stylua --- doc/nvim-tree-lua.txt | 16 +++++++++------- lua/nvim-tree/actions/node/open-file.lua | 2 -- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 389d9f25845..d3deb629f93 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1527,13 +1527,6 @@ node.open.edit() *nvim-tree-api.node.open.edit()* Folder: expand or collapse Root: change directory up - *nvim-tree-api.node.open.tab_drop()* -node.open.tab_drop() - Like edit(), but reuse already-opened window in tab. See also: |:drop| - File: open already-opened as per |nvim-tree.actions.open_file| - Folder: expand or collapse - Root: change directory up - *nvim-tree-api.node.open.replace_tree_buffer()* node.open.replace_tree_buffer() |nvim-tree-api.node.edit()|, file will be opened in place: in the @@ -1553,6 +1546,15 @@ node.open.horizontal() *nvim-tree-api.node.open.horizontal()* node.open.tab() *nvim-tree-api.node.open.tab()* |nvim-tree-api.node.edit()|, file will be opened in a new tab. + *nvim-tree-api.node.open.tab_drop()* +node.open.tab_drop() + Switch to tab containing window with selected file if it exists. + Open file in new tab otherwise. + + File: open file using `tab :drop` + Folder: expand or collapse + Root: change directory up + node.open.preview() *nvim-tree-api.node.open.preview()* |nvim-tree-api.node.edit()|, file buffer will have |bufhidden| set to `delete`. diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 0e42dc6e8bf..00908576aca 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -141,7 +141,6 @@ local function pick_win_id() return win_map[resp] end - local function open_file_in_tab(filename) if M.quit_on_open then view.close() @@ -149,7 +148,6 @@ local function open_file_in_tab(filename) vim.cmd("tabe " .. vim.fn.fnameescape(filename)) end --- See :drop command. This will always focus already-opened tab local function tab_drop(filename) if M.quit_on_open then view.close()