Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 17, 2022
Merged
1 change: 1 addition & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 41 additions & 10 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -353,7 +352,14 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_change) })
end
if opts.hijack_cursor then
create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node })
create_nvim_tree_autocmd("CursorMoved", {
pattern = "NvimTree_*",
callback = function()
if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then
M.place_cursor_on_node()
end
end,
})
end
if opts.sync_root_with_cwd then
create_nvim_tree_autocmd("DirChanged", {
Expand All @@ -371,8 +377,16 @@ local function setup_autocommands(opts)
end

if not opts.actions.open_file.quit_on_open then
create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view._prevent_buffer_override })
create_nvim_tree_autocmd("BufWipeout", {
pattern = "NvimTree_*",
callback = function()
if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vim.bo.filetype is more luaish

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today I Learned

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vim.bo.filetype is also a shorthand for vim.bo[0].filetype.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic. I might merge these checks into is_nvim_tree_buf.

view._prevent_buffer_override()
end
end,
})
else
-- TODO merge #1637
create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view.abandon_current_window })
end

Expand All @@ -381,17 +395,27 @@ local function setup_autocommands(opts)
end

if opts.reload_on_bufenter and not has_watchers then
create_nvim_tree_autocmd("BufEnter", { pattern = "NvimTree_*", callback = reloaders.reload_explorer })
create_nvim_tree_autocmd("BufEnter", {
pattern = "NvimTree_*",
callback = function()
if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then
reloaders.reload_explorer()
end
end,
})
end

if opts.view.centralize_selection then
create_nvim_tree_autocmd("BufEnter", {
pattern = "NvimTree_*",
callback = function()
vim.schedule(function()
local keys = api.nvim_replace_termcodes("zz", true, false, true)
api.nvim_feedkeys(keys, "n", true)
end)
if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then
vim.schedule(function()
-- TODO merge #1632
local keys = api.nvim_replace_termcodes("zz", true, false, true)
api.nvim_feedkeys(keys, "n", true)
end)
end
end,
})
end
Expand All @@ -413,7 +437,14 @@ local function setup_autocommands(opts)
end

if opts.view.float.enable and opts.view.float.quit_on_focus_loss then
create_nvim_tree_autocmd("WinLeave", { pattern = "NvimTree_*", callback = view.close })
create_nvim_tree_autocmd("WinLeave", {
pattern = "NvimTree_*",
callback = function()
if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then
view.close()
end
end,
})
end
end

Expand Down
6 changes: 5 additions & 1 deletion lua/nvim-tree/live-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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 a.nvim_buf_get_option(0, "filetype") == "NvimTree" then
view.close()
end
end,
})
end

Expand Down
8 changes: 6 additions & 2 deletions lua/nvim-tree/renderer/components/full-name.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,19 @@ M.setup = function(opts)
group = group,
pattern = { "NvimTree_*" },
callback = function()
hide(M.popup_win)
if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then
hide(M.popup_win)
end
end,
})

api.nvim_create_autocmd({ "CursorMoved" }, {
group = group,
pattern = { "NvimTree_*" },
callback = function()
show()
if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then
show()
end
end,
})
end
Expand Down
11 changes: 11 additions & 0 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down