From 416b1a6c01f16254819c50ce1088482ec0603dcf Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Tue, 31 Jan 2023 12:25:35 +1100 Subject: [PATCH] feat(api): api.tree.open feature parity with api.tree.toggle --- doc/nvim-tree-lua.txt | 4 +++- lua/nvim-tree.lua | 56 ++++++++++++++++++++++++------------------- lua/nvim-tree/api.lua | 4 +++- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 8014d0b9bae..5cdaad72bd1 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1256,6 +1256,8 @@ api.tree.open({opts}) *nvim-tree.api.tree.open()* Options: ~ • {path} (string) root directory for the tree • {current_window} (boolean, false) open the tree in the current window + • {find_file} (boolean, false) find the current buffer + • {update_root} (boolean, false) see |nvim-tree.update_focused_file.update_root| api.tree.toggle({opts}) *nvim-tree.api.tree.toggle()* Open or close the tree. @@ -1266,9 +1268,9 @@ api.tree.toggle({opts}) *nvim-tree.api.tree.toggle()* Options: ~ • {path} (string) root directory for the tree • {current_window} (boolean, false) open the tree in the current window - • {focus} (boolean, true) focus the tree when opening • {find_file} (boolean, false) find the current buffer • {update_root} (boolean, false) see |nvim-tree.update_focused_file.update_root| + • {focus} (boolean, true) focus the tree when opening api.tree.close() *nvim-tree.api.tree.close()* Close the tree, affecting all tabs as per |nvim-tree.tab.sync.close| diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index a0c7797deb1..22a9446c7df 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -71,31 +71,6 @@ end ---@deprecated M.on_keypress = require("nvim-tree.actions.dispatch").dispatch ----Open the tree, focusing if already open. ----@param opts ApiTreeOpenOpts|nil|string -function M.open(opts) - -- legacy arguments - if type(opts) == "string" then - opts = { - path = opts, - } - end - - opts = opts or {} - - -- sanitise path - if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then - opts.path = nil - end - - if view.is_visible() then - lib.set_target_win() - view.focus() - else - lib.open(opts) - end -end - function M.open_replacing_current_buffer(cwd) if view.is_visible() then return @@ -180,6 +155,37 @@ function M.find_file(with_open, bufnr, bang) find_file(with_open, bufnr, bang) end +---Open the tree, focusing if already open. +---@param opts ApiTreeOpenOpts|nil|string +function M.open(opts) + -- legacy arguments + if type(opts) == "string" then + opts = { + path = opts, + } + end + + opts = opts or {} + + local previous_buf = vim.api.nvim_get_current_buf() + + -- sanitise path + if type(opts.path) ~= "string" or vim.fn.isdirectory(opts.path) == 0 then + opts.path = nil + end + + if view.is_visible() then + lib.set_target_win() + view.focus() + else + lib.open(opts) + end + + if _config.update_focused_file.enable or opts.find_file then + find_file(false, previous_buf, opts.update_root) + end +end + ---Toggle the tree. ---@param opts ApiTreeToggleOpts|nil|boolean function M.toggle(opts, no_focus, cwd, bang) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 6067db36533..11f80b2d922 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -19,15 +19,17 @@ end ---@class ApiTreeOpenOpts ---@field path string|nil path ---@field current_window boolean|nil default false +---@field find_file boolean|nil default false +---@field update_root boolean|nil default false Api.tree.open = require("nvim-tree").open ---@class ApiTreeToggleOpts ---@field path string|nil ---@field current_window boolean|nil default false ----@field focus boolean|nil default true ---@field find_file boolean|nil default false ---@field update_root boolean|nil default false +---@field focus boolean|nil default true Api.tree.toggle = require("nvim-tree").toggle