From 0df384b6c0fa62ff1333634d56ee4df0be5d34e1 Mon Sep 17 00:00:00 2001 From: gegoune <69750637+gegoune@users.noreply.github.com> Date: Sun, 30 Apr 2023 09:35:25 +0200 Subject: [PATCH] feat(api): add `node.open.drop()` (#2164) Co-authored-by: Alexander Courtis --- doc/nvim-tree-lua.txt | 9 +++++++++ lua/nvim-tree/actions/node/open-file.lua | 11 +++++++++++ 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 a70e86df..f085647c 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1608,6 +1608,15 @@ node.open.vertical() *nvim-tree-api.node.open.vertical()* node.open.horizontal() *nvim-tree-api.node.open.horizontal()* |nvim-tree-api.node.edit()|, file will be opened in a new horizontal split. +node.open.drop() *nvim-tree-api.node.open.drop()* + Switch to window with selected file if it exists. + Open file otherwise. + See: `:h :drop`. + + File: open file using `:drop` + Folder: expand or collapse + Root: change directory up + node.open.tab() *nvim-tree-api.node.open.tab()* |nvim-tree-api.node.edit()|, file will be opened in a new tab. diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 6990c896..ab9cc1c1 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -160,6 +160,13 @@ local function open_file_in_tab(filename) vim.cmd("tabe " .. vim.fn.fnameescape(filename)) end +local function drop(filename) + if M.quit_on_open then + view.close() + end + vim.cmd("drop " .. vim.fn.fnameescape(filename)) +end + local function tab_drop(filename) if M.quit_on_open then view.close() @@ -303,6 +310,10 @@ function M.fn(mode, filename) return open_file_in_tab(filename) end + if mode == "drop" then + return drop(filename) + end + if mode == "tab_drop" then return tab_drop(filename) end diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index a4614eee..24b5d144 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.drop = wrap_node(open_or_expand_or_dir_up "drop") 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")