fix: safe open file (#2289)

This commit is contained in:
yetone
2025-06-21 20:45:55 +08:00
committed by GitHub
parent fe4f7d836b
commit 92460fab6a

View File

@@ -985,6 +985,19 @@ function M.get_chat_mentions()
return mentions
end
local function safe_open_file(filename)
local ok, _ = pcall(vim.cmd, "noautocmd edit " .. filename)
if ok then
-- Manually trigger necessary events
vim.cmd("doautocmd BufRead")
else
-- Fallback solution
vim.cmd("enew")
vim.api.nvim_buf_set_name(0, filename)
vim.bo.filetype = vim.fn.fnamemodify(filename, ":e")
end
end
---@param path string
---@param set_current_buf? boolean
---@return integer bufnr
@@ -995,7 +1008,7 @@ function M.open_buffer(path, set_current_buf)
local bufnr
if set_current_buf then
vim.cmd("edit " .. abs_path)
safe_open_file(abs_path)
bufnr = vim.api.nvim_get_current_buf()
else
bufnr = vim.fn.bufnr(abs_path, true)