nvim-tree/nvim-tree.lua

Prevent nvim-tree From Polluting Jump Lists

Opened this issue · 2 comments

Hi, sorry to use black issue templete.

nvim-tree uses NvimTree_1 as default name, this will be keeped in jumplist,if it is openned by jumps picker, and save file. this will create a real file, and then open nvim-tree you will get this error:

"NvimTree_1"  line 8 of 9 --88%-- col 7-5
"NvimTree_1" [New] --No lines in buffer--
"NvimTree_1"  line 8 of 9 --88%-- col 7-5
"NvimTree_1" [New] --No lines in buffer--
"NvimTree_1"  line 8 of 9 --88%-- col 7-5
"NvimTree_1" [New] --No lines in buffer--
"NvimTree_1" [New] 0L, 0B written
[NvimTree] Cannot rename C:\Users\wsdjeg\AppData\Local\nvim\init.lua -> C:\Users\wsdjeg\AppData\Local\nvim\init.lua: file already exists
Error executing Lua callback: ...undle_dir\nvim-tree\nvim-tree.lua/lua/nvim-tree/view.lua:104: Vim:E95: Buffer with this name already exists
stack traceback:
	[C]: in function 'nvim_buf_set_name'
	...undle_dir\nvim-tree\nvim-tree.lua/lua/nvim-tree/view.lua:104: in function 'create_buffer'
	...undle_dir\nvim-tree\nvim-tree.lua/lua/nvim-tree/view.lua:292: in function 'open'
	D:\bundle_dir\nvim-tree\nvim-tree.lua/lua/nvim-tree/lib.lua:34: in function 'open_view_and_draw'
	D:\bundle_dir\nvim-tree\nvim-tree.lua/lua/nvim-tree/lib.lua:129: in function 'open'
	...tree\nvim-tree.lua/lua/nvim-tree/actions/tree/toggle.lua:48: in function 'toggle'
	...e_dir\nvim-tree\nvim-tree.lua/lua/nvim-tree/commands.lua:36: in function <...e_dir\nvim-tree\nvim-tree.lua/lua/nvim-tree/commands.lua:35>

is it possile to use empty buffer name?

Looking at how other plugins handle this:

  • Telescope: uses [Scratch] and [Prompt] buffers
  • fugitive: uses temporary blame buffers with a unique name e.g. /tmp/nvim.alex/N0zPuK/3.fugitiveblame which come from tempname()
  • gitsigns: similar to fugitive for diff buffers

There are a lot of cases in which nvim-tree uses the buffer name:

--- 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|nil 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.api.nvim_buf_is_valid(bufnr) then
local bufname = vim.api.nvim_buf_get_name(bufnr)
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

I don't think we can safely use a scratch buffer or empty buffer name, as that has great potential to break things and will require a lot of work.

Using a tempname() buffer seems the safer course of action; however, that may break users who rely on the nvim-tree buffer name. I also prefer this as the user can still easily identify the nvim-tree buffers.

Recommendation:

  1. Make the changes to allow the use of a tempname buffer
  2. Add a new (default off) option to feature gate this, maybe view.temp_buffer

As usual, pull requests are most gratefully appreciated, see