From e7136078f7bdf6f90272cc57667270c53fef039b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 9 Oct 2022 12:57:11 +1100 Subject: [PATCH 1/4] fix(#1629): nvim start with file named *NvimTree* opens tree instead of buffer --- lua/nvim-tree.lua | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 215bcb0f0e1..024b93b54cb 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -133,13 +133,6 @@ function M.tab_change() end end -local function find_existing_windows() - return vim.tbl_filter(function(win) - local buf = api.nvim_win_get_buf(win) - return api.nvim_buf_get_name(buf):match "NvimTree" ~= nil - end, api.nvim_list_wins()) -end - local function is_file_readable(fname) local stat = luv.fs_stat(fname) return stat and stat.type == "file" and luv.fs_access(fname, "R") @@ -258,13 +251,7 @@ function M.on_enter(netrw_disabled) end end - -- Session that left a NvimTree Buffer opened, reopen with it - local existing_tree_wins = find_existing_windows() - if existing_tree_wins[1] then - api.nvim_set_current_win(existing_tree_wins[1]) - end - - if should_open or existing_tree_wins[1] ~= nil then + if should_open then lib.open(cwd) if should_focus_other_window then From a48ee4e6011e0821b1bc04a21e15459d9346c7cb Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 10 Oct 2022 11:15:08 +1100 Subject: [PATCH 2/4] Revert "fix(#1629): nvim start with file named *NvimTree* opens tree instead of buffer" This reverts commit e7136078f7bdf6f90272cc57667270c53fef039b. --- lua/nvim-tree.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 024b93b54cb..215bcb0f0e1 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -133,6 +133,13 @@ function M.tab_change() end end +local function find_existing_windows() + return vim.tbl_filter(function(win) + local buf = api.nvim_win_get_buf(win) + return api.nvim_buf_get_name(buf):match "NvimTree" ~= nil + end, api.nvim_list_wins()) +end + local function is_file_readable(fname) local stat = luv.fs_stat(fname) return stat and stat.type == "file" and luv.fs_access(fname, "R") @@ -251,7 +258,13 @@ function M.on_enter(netrw_disabled) end end - if should_open then + -- Session that left a NvimTree Buffer opened, reopen with it + local existing_tree_wins = find_existing_windows() + if existing_tree_wins[1] then + api.nvim_set_current_win(existing_tree_wins[1]) + end + + if should_open or existing_tree_wins[1] ~= nil then lib.open(cwd) if should_focus_other_window then From 444f94c1585b6aa4a3373ba0ebe8b5f70a173e0a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 10 Oct 2022 11:20:52 +1100 Subject: [PATCH 3/4] fix(#1629): nvim start with file named *NvimTree* treats file as tree --- lua/nvim-tree.lua | 3 +-- lua/nvim-tree/utils.lua | 11 +++++++++++ lua/nvim-tree/view.lua | 3 ++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 215bcb0f0e1..1b6c52c16dd 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -135,8 +135,7 @@ end local function find_existing_windows() return vim.tbl_filter(function(win) - local buf = api.nvim_win_get_buf(win) - return api.nvim_buf_get_name(buf):match "NvimTree" ~= nil + return utils.is_nvim_tree_buf(api.nvim_win_get_buf(win)) end, api.nvim_list_wins()) end diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 37ff8ebe58d..e1849e80b03 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -462,4 +462,15 @@ function M.inject_node(f) end end +---Is the buffer a tree? +---@param bufnr number +---@return boolean +function M.is_nvim_tree_buf(bufnr) + if vim.fn.bufexists(bufnr) then + local bufname = a.nvim_buf_get_name(bufnr) + return vim.fn.fnamemodify(bufname, ":t"):match "^NvimTree_[0-9]+$" and vim.fn.filereadable(bufname) == 0 + end + return false +end + return M diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 1d0d7841989..7617218ac2c 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -3,6 +3,7 @@ local a = vim.api local M = {} local events = require "nvim-tree.events" +local utils = require "nvim-tree.utils" local function get_win_sep_hl() -- #1221 WinSeparator not present in nvim 0.6.1 and some builds of 0.7.0 @@ -76,7 +77,7 @@ end local function wipe_rogue_buffer() for _, bufnr in ipairs(a.nvim_list_bufs()) do - if not matches_bufnr(bufnr) and a.nvim_buf_get_name(bufnr):match "NvimTree" ~= nil then + if not matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then pcall(a.nvim_buf_delete, bufnr, { force = true }) end end From 645bc2412b087433c7f574576731a2ea0be4bea5 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 10 Oct 2022 12:36:00 +1100 Subject: [PATCH 4/4] fix(#1629): nvim start with file named *NvimTree* treats file as tree --- doc/nvim-tree-lua.txt | 1 + lua/nvim-tree/utils.lua | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 53fa6e62286..3fe47377995 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -204,6 +204,7 @@ Subsequent calls to setup will replace the previous configuration. }, float = { enable = false, + quit_on_focus_loss = true, open_win_config = { relative = "editor", border = "rounded", diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index e1849e80b03..39af59a0780 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -462,7 +462,7 @@ function M.inject_node(f) end end ----Is the buffer a tree? +---Is the buffer a tree? Like /path/to/NvimTree_2 and not a readable file. ---@param bufnr number ---@return boolean function M.is_nvim_tree_buf(bufnr)