From e7136078f7bdf6f90272cc57667270c53fef039b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 9 Oct 2022 12:57:11 +1100 Subject: [PATCH 1/7] 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/7] 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/7] 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/7] 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) From 3a7a2e505c93d2dcd63870d70a387ddb9ff032b4 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 10 Oct 2022 13:34:12 +1100 Subject: [PATCH 5/7] fix(#1639): ensure tree autocommands match filetype as well as name --- lua/nvim-tree.lua | 48 +++++++++++++++---- lua/nvim-tree/live-filter.lua | 6 ++- .../renderer/components/full-name.lua | 8 +++- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 1b6c52c16dd..7ea70bae91c 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -352,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", { @@ -370,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 + view._prevent_buffer_override() + end + end, + }) else + -- TODO merge #1637 create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view.abandon_current_window }) end @@ -380,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 @@ -412,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 diff --git a/lua/nvim-tree/live-filter.lua b/lua/nvim-tree/live-filter.lua index 4d8ae7cff82..000f0a83e99 100644 --- a/lua/nvim-tree/live-filter.lua +++ b/lua/nvim-tree/live-filter.lua @@ -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 diff --git a/lua/nvim-tree/renderer/components/full-name.lua b/lua/nvim-tree/renderer/components/full-name.lua index b589b47c125..a853510d50c 100644 --- a/lua/nvim-tree/renderer/components/full-name.lua +++ b/lua/nvim-tree/renderer/components/full-name.lua @@ -67,7 +67,9 @@ 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, }) @@ -75,7 +77,9 @@ M.setup = function(opts) group = group, pattern = { "NvimTree_*" }, callback = function() - show() + if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then + show() + end end, }) end From 506acd671ee343a6e75078a3121db4d1bf020257 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 15 Oct 2022 12:58:39 +1100 Subject: [PATCH 6/7] fix(#1639): fix bad merge --- lua/nvim-tree.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 5e63238f3d0..4ee89d34090 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -386,8 +386,6 @@ local function setup_autocommands(opts) }) end - create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view.abandon_current_window }) - if opts.hijack_directories.enable then create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory }) end From de20ac762d0734c6779341bdcde701ad45df40dc Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 15 Oct 2022 13:39:20 +1100 Subject: [PATCH 7/7] fix(#1639): ensure tree autocommands match filetype as well as name --- lua/nvim-tree.lua | 8 ++++---- lua/nvim-tree/live-filter.lua | 3 ++- lua/nvim-tree/renderer/components/full-name.lua | 5 +++-- lua/nvim-tree/utils.lua | 16 +++++++++++++--- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 4ee89d34090..6e192419e85 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -339,7 +339,7 @@ local function setup_autocommands(opts) create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = function() - if vim.bo.filetype == "NvimTree" then + if utils.is_nvim_tree_buf(0) then view._prevent_buffer_override() end end, @@ -365,7 +365,7 @@ local function setup_autocommands(opts) create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = function() - if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then + if utils.is_nvim_tree_buf(0) then M.place_cursor_on_node() end end, @@ -394,7 +394,7 @@ local function setup_autocommands(opts) create_nvim_tree_autocmd("BufEnter", { pattern = "NvimTree_*", callback = function() - if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then + if utils.is_nvim_tree_buf(0) then reloaders.reload_explorer() end end, @@ -435,7 +435,7 @@ local function setup_autocommands(opts) create_nvim_tree_autocmd("WinLeave", { pattern = "NvimTree_*", callback = function() - if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then + if utils.is_nvim_tree_buf(0) then view.close() end end, diff --git a/lua/nvim-tree/live-filter.lua b/lua/nvim-tree/live-filter.lua index 000f0a83e99..9143d9fe335 100644 --- a/lua/nvim-tree/live-filter.lua +++ b/lua/nvim-tree/live-filter.lua @@ -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 = { @@ -31,7 +32,7 @@ local function remove_overlay() pattern = "NvimTree_*", group = a.nvim_create_augroup("NvimTree", { clear = false }), callback = function() - if a.nvim_buf_get_option(0, "filetype") == "NvimTree" then + if utils.is_nvim_tree_buf(0) then view.close() end end, diff --git a/lua/nvim-tree/renderer/components/full-name.lua b/lua/nvim-tree/renderer/components/full-name.lua index a853510d50c..41c2425b140 100644 --- a/lua/nvim-tree/renderer/components/full-name.lua +++ b/lua/nvim-tree/renderer/components/full-name.lua @@ -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,7 @@ M.setup = function(opts) group = group, pattern = { "NvimTree_*" }, callback = function() - if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then + if utils.is_nvim_tree_buf(0) then hide(M.popup_win) end end, @@ -77,7 +78,7 @@ M.setup = function(opts) group = group, pattern = { "NvimTree_*" }, callback = function() - if api.nvim_buf_get_option(0, "filetype") == "NvimTree" then + if utils.is_nvim_tree_buf(0) then show() end end, diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 6d6af956198..e81327ff76b 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -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