Skip to content

Commit c5036c5

Browse files
authored
feat(buffers): show [No Name] buffers in buffers list (#799)
1 parent cef57e6 commit c5036c5

File tree

5 files changed

+57
-35
lines changed

5 files changed

+57
-35
lines changed

lua/neo-tree/sources/buffers/init.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ local follow_internal = function()
2323
if vim.bo.filetype == "neo-tree" or vim.bo.filetype == "neo-tree-popup" then
2424
return
2525
end
26-
local path_to_reveal = manager.get_path_to_reveal(true)
27-
if not utils.truthy(path_to_reveal) then
28-
return false
29-
end
26+
local bufnr = vim.api.nvim_get_current_buf()
27+
local path_to_reveal = manager.get_path_to_reveal(true) or tostring(bufnr)
3028

3129
local state = get_state()
3230
if state.current_position == "float" then
@@ -145,7 +143,7 @@ M.setup = function(config, global_config)
145143
manager.subscribe(M.name, {
146144
event = e,
147145
handler = function(args)
148-
if utils.is_real_file(args.afile) then
146+
if args.afile == "" or utils.is_real_file(args.afile) then
149147
M.buffers_changed()
150148
end
151149
end,

lua/neo-tree/sources/buffers/lib/items.lua

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ M.get_open_buffers = function(state)
2323
context.folders[root.path] = root
2424
local terminals = {}
2525

26+
local function add_buffer(bufnr, path)
27+
local is_loaded = vim.api.nvim_buf_is_loaded(bufnr)
28+
if is_loaded or state.show_unloaded then
29+
local is_listed = vim.fn.buflisted(bufnr)
30+
if is_listed == 1 then
31+
if path == "" then
32+
path = "[No Name]"
33+
end
34+
local success, item = pcall(file_items.create_item, context, path, "file", bufnr)
35+
if success then
36+
item.extra = {
37+
bufnr = bufnr,
38+
is_listed = is_listed,
39+
}
40+
else
41+
log.error("Error creating item for " .. path .. ": " .. item)
42+
end
43+
end
44+
end
45+
end
46+
2647
local bufs = vim.api.nvim_list_bufs()
2748
for _, b in ipairs(bufs) do
2849
local path = vim.api.nvim_buf_get_name(b)
@@ -45,25 +66,17 @@ M.get_open_buffers = function(state)
4566
if utils.is_subpath(state.path, abs_path) then
4667
table.insert(terminals, item)
4768
end
69+
elseif path == "" then
70+
add_buffer(b, path)
4871
else
49-
local rootsub = path:sub(1, #state.path)
50-
-- make sure this is within the root path
51-
if rootsub == state.path then
52-
local is_loaded = vim.api.nvim_buf_is_loaded(b)
53-
if is_loaded or state.show_unloaded then
54-
local is_listed = vim.fn.buflisted(b)
55-
if is_listed == 1 then
56-
local success, item = pcall(file_items.create_item, context, path, "file")
57-
if success then
58-
item.extra = {
59-
bufnr = b,
60-
is_listed = is_listed,
61-
}
62-
else
63-
log.error("Error creating item for " .. path .. ": " .. item)
64-
end
65-
end
72+
if #state.path > 1 then
73+
local rootsub = path:sub(1, #state.path)
74+
-- make sure this is within the root path
75+
if rootsub == state.path then
76+
add_buffer(b, path)
6677
end
78+
else
79+
add_buffer(b, path)
6780
end
6881
end
6982
end

lua/neo-tree/sources/common/commands.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,14 @@ local open_with_cmd = function(state, open_cmd, toggle_directory, open_file)
552552
local function open()
553553
M.revert_preview()
554554
local path = node.path or node:get_id()
555+
local bufnr = node.extra and node.extra.bufnr
555556
if node.type == "terminal" then
556557
path = node:get_id()
557558
end
558559
if type(open_file) == "function" then
559-
open_file(state, path, open_cmd)
560+
open_file(state, path, open_cmd, bufnr)
560561
else
561-
utils.open_file(state, path, open_cmd)
562+
utils.open_file(state, path, open_cmd, bufnr)
562563
end
563564
local extra = node.extra or {}
564565
local pos = extra.position or extra.end_position

lua/neo-tree/sources/common/file-items.lua

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,26 @@ end
6060

6161
local create_item, set_parents
6262

63-
function create_item(context, path, _type)
64-
-- avoid creating duplicate items
65-
if context.folders[path] or context.nesting[path] then
66-
return context.folders[path] or context.nesting[path]
67-
end
68-
63+
function create_item(context, path, _type, bufnr)
6964
local parent_path, name = utils.split_path(path)
65+
local id = path
66+
if path == "[No Name]" and bufnr then
67+
parent_path = context.state.path
68+
name = "[No Name]"
69+
id = tostring(bufnr)
70+
else
71+
-- avoid creating duplicate items
72+
if context.folders[path] or context.nesting[path] then
73+
return context.folders[path] or context.nesting[path]
74+
end
75+
end
7076

7177
if _type == nil then
7278
local stat = vim.loop.fs_stat(path)
7379
_type = stat and stat.type or "unknown"
7480
end
7581
local item = {
76-
id = path,
82+
id = id,
7783
name = name,
7884
parent_path = parent_path,
7985
path = path,

lua/neo-tree/utils.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,25 +501,29 @@ end
501501
---@param state table The state of the source
502502
---@param path string The file to open
503503
---@param open_cmd string The vimcommand to use to open the file
504-
M.open_file = function(state, path, open_cmd)
504+
---@param bufnr number|nil The buffer number to open
505+
M.open_file = function(state, path, open_cmd, bufnr)
505506
open_cmd = open_cmd or "edit"
506507
if open_cmd == "edit" or open_cmd == "e" then
507508
-- If the file is already open, switch to it.
508-
local bufnr = M.find_buffer_by_name(path)
509-
if bufnr > 0 then
509+
bufnr = bufnr or M.find_buffer_by_name(path)
510+
if bufnr <= 0 then
511+
bufnr = nil
512+
else
510513
open_cmd = "b"
511514
end
512515
end
513516

514517
if M.truthy(path) then
515-
local escaped_path = vim.fn.fnameescape(path)
518+
local escaped_path = bufnr or vim.fn.fnameescape(path)
516519
local events = require("neo-tree.events")
517520
local result = true
518521
local err = nil
519522
local event_result = events.fire_event(events.FILE_OPEN_REQUESTED, {
520523
state = state,
521524
path = path,
522525
open_cmd = open_cmd,
526+
bufnr = bufnr,
523527
}) or {}
524528
if event_result.handled then
525529
events.fire_event(events.FILE_OPENED, path)

0 commit comments

Comments
 (0)