fix(#1639): ensure tree autocommands match filetype as well as name (#1640)

* 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 e7136078f7.

* fix(#1629): nvim start with file named *NvimTree* treats file as tree

* fix(#1629): nvim start with file named *NvimTree* treats file as tree

* fix(#1639): ensure tree autocommands match filetype as well as name

* fix(#1639): fix bad merge

* fix(#1639): ensure tree autocommands match filetype as well as name
This commit is contained in:
Alexander Courtis
2022-10-17 12:31:41 +11:00
committed by GitHub
parent c995ce0878
commit 48992fd3e8
4 changed files with 51 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
local a = vim.api
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local Iterator = require "nvim-tree.iterators.node-iterator"
local M = {
@@ -30,7 +31,11 @@ local function remove_overlay()
a.nvim_create_autocmd("WinLeave", {
pattern = "NvimTree_*",
group = a.nvim_create_augroup("NvimTree", { clear = false }),
callback = view.close,
callback = function()
if utils.is_nvim_tree_buf(0) then
view.close()
end
end,
})
end

View File

@@ -2,6 +2,7 @@ local M = {}
local api = vim.api
local fn = vim.fn
local utils = require "nvim-tree.utils"
local function hide(win)
if win then
@@ -67,7 +68,9 @@ M.setup = function(opts)
group = group,
pattern = { "NvimTree_*" },
callback = function()
hide(M.popup_win)
if utils.is_nvim_tree_buf(0) then
hide(M.popup_win)
end
end,
})
@@ -75,7 +78,9 @@ M.setup = function(opts)
group = group,
pattern = { "NvimTree_*" },
callback = function()
show()
if utils.is_nvim_tree_buf(0) then
show()
end
end,
})
end

View File

@@ -468,13 +468,23 @@ 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
---Is the buffer named NvimTree_[0-9]+ a tree? filetype is "NvimTree" or not readable file.
---This is cheap, as the readable test should only ever be needed when resuming a vim session.
---@param bufnr number may be 0 or nil for current
---@return boolean
function M.is_nvim_tree_buf(bufnr)
if bufnr == nil then
bufnr = 0
end
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
if vim.fn.fnamemodify(bufname, ":t"):match "^NvimTree_[0-9]+$" then
if vim.bo[bufnr].filetype == "NvimTree" then
return true
elseif vim.fn.filereadable(bufname) == 0 then
return true
end
end
end
return false
end