Skip to content

Commit

Permalink
Make buffer open strategy more robust
Browse files Browse the repository at this point in the history
Fixes #501.
  • Loading branch information
epwalsh committed Mar 19, 2024
1 parent 02f0136 commit b2df42f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Don't open picker for tags when there aren't any matches.
- Fixed overwriting frontmatter when creating daily note with template.
- Fixed default date format for the alias of daily notes.
- Made buffer open strategy more robust.

## [v3.7.3](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.3) - 2024-03-13

Expand Down
24 changes: 12 additions & 12 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -799,20 +799,18 @@ end
---
---@return function () -> (integer, string)|?
util.get_named_buffers = function()
local bufnr = 0
local max_bufnr = vim.fn.bufnr "$"
local idx = 0
local buffers = vim.api.nvim_list_bufs()

---@return integer|?
---@return string|?
return function()
bufnr = bufnr + 1
while bufnr <= max_bufnr and (vim.fn.bufexists(bufnr) == 0 or vim.fn.bufname(bufnr) == "") do
bufnr = bufnr + 1
end
if bufnr > max_bufnr then
return nil
else
return bufnr, vim.api.nvim_buf_get_name(bufnr)
while idx < #buffers do
idx = idx + 1
local bufnr = buffers[idx]
if vim.api.nvim_buf_is_loaded(bufnr) then
return bufnr, vim.api.nvim_buf_get_name(bufnr)
end
end
end
end
Expand Down Expand Up @@ -1046,8 +1044,10 @@ util.open_buffer = function(path, opts)
---@type integer|?
local result_bufnr

-- Check for existing buffer and use 'drop' command if one is found.
for bufnr, bufname in util.get_named_buffers() do
-- Check for buffer in windows and use 'drop' command if one is found.
for _, winnr in ipairs(vim.api.nvim_list_wins()) do
local bufnr = vim.api.nvim_win_get_buf(winnr)
local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname == tostring(path) then
cmd = "drop"
result_bufnr = bufnr
Expand Down

0 comments on commit b2df42f

Please sign in to comment.