From c66cbdfc25ce115db50cfe3dca8b96a8a1e9b931 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Tue, 11 Oct 2022 10:00:03 +1100 Subject: [PATCH] fix(#1629): nvim start with file named *NvimTree* opens tree instead of buffer (#1634) * fix(#1629): nvim start with file named *NvimTree* opens tree instead of buffer * Revert "fix(#1629): nvim start with file named *NvimTree* opens tree instead of buffer" This reverts commit e7136078f7bdf6f90272cc57667270c53fef039b. * fix(#1629): nvim start with file named *NvimTree* treats file as tree * fix(#1629): nvim start with file named *NvimTree* treats file as tree --- doc/nvim-tree-lua.txt | 1 + lua/nvim-tree.lua | 3 +-- lua/nvim-tree/utils.lua | 11 +++++++++++ lua/nvim-tree/view.lua | 3 ++- 4 files changed, 15 insertions(+), 3 deletions(-) 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.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..39af59a0780 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? Like /path/to/NvimTree_2 and not a readable file. +---@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