Skip to content

Commit a214db3

Browse files
fix(buffers): Fix fallback for finding a buffer by filename (#1126)
1 parent 3fb33c6 commit a214db3

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

lua/orgmode/state/buffers.lua

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,56 @@ function OrgBuffers.get_buffer_by_filename(filename)
2929
return OrgBuffers._bufs[resolved_filename]
3030
end
3131

32-
-- If filename does not have an org extension, try to find the buf number and return it if filetype is org
32+
local bufnr = vim.fn.bufnr(resolved_filename)
33+
34+
if bufnr < 0 then
35+
return -1
36+
end
37+
38+
-- If filename does not have an org extension, return the buffer only if it has correct filetype
3339
if not OrgBuffers._is_valid_file_name(resolved_filename) then
34-
local bufnr = vim.fn.bufnr(resolved_filename)
35-
if bufnr > -1 and vim.bo[bufnr].filetype == 'org' then
40+
if vim.bo[bufnr].filetype == 'org' then
3641
return bufnr
3742
end
43+
44+
return -1
45+
end
46+
47+
-- bufnr() can return wrong buffer number in cases when there are multiple files matching, for example:
48+
-- * `/path/to/orgfiles/todos.org`
49+
-- * `/path/to/orgfiles/todos.org_archive`
50+
-- Doing `bufnr('/path/to/orgfiles/todos.org')` can return buffer number for `/path/to/orgfiles/todos.org_archive`.
51+
-- Resolve the filename of the found buffer, and make sure it matches the resolved filename we are looking for
52+
-- If not, fallback to nvim_list_bufs
53+
local buffer_filename = OrgBuffers._resolve_filename(vim.api.nvim_buf_get_name(bufnr))
54+
55+
if buffer_filename == resolved_filename then
56+
return OrgBuffers.add(bufnr)
57+
end
58+
59+
local all_bufs = vim.api.nvim_list_bufs()
60+
for _, buf in ipairs(all_bufs) do
61+
local valid_buffer_name = OrgBuffers.get_valid_buffer_name(buf)
62+
if valid_buffer_name and valid_buffer_name == resolved_filename then
63+
return OrgBuffers.add(buf)
64+
end
3865
end
3966

4067
return -1
4168
end
4269

4370
---Add the buffer to the list
4471
---@param bufnr number
72+
---@return number bufnr if the buffer is valid and added, -1 otherwise
4573
function OrgBuffers.add(bufnr)
4674
local name = OrgBuffers.get_valid_buffer_name(bufnr)
4775

4876
if name then
4977
OrgBuffers._bufs[name] = bufnr
78+
return bufnr
5079
end
80+
81+
return -1
5182
end
5283

5384
---Remove the buffer from the list
@@ -63,13 +94,13 @@ end
6394
---Get valid buffer name if the buffer is an org file
6495
---@param bufnr number
6596
function OrgBuffers.get_valid_buffer_name(bufnr)
66-
local name = OrgBuffers._resolve_filename(vim.api.nvim_buf_get_name(bufnr))
97+
local bufname = vim.api.nvim_buf_get_name(bufnr)
6798

68-
if OrgBuffers._is_valid_file_name(name) then
69-
return name
99+
if not OrgBuffers._is_valid_file_name(bufname) then
100+
return nil
70101
end
71102

72-
return nil
103+
return OrgBuffers._resolve_filename(bufname)
73104
end
74105

75106
---Resolve and normalize the filename

0 commit comments

Comments
 (0)