fix(nvim): wipe pristine un-named buffers on nvim_open_file and unlist scratch buffers in nvim_open_buffer

This commit is contained in:
Riz Ashraf committed 2026-10-05 10:55:57 +01:00
1 parent 6e0ccf8082
commit e45db33297
1 file changed
+16 -5
+16 -5
View File
@@ -1319,7 +1319,16 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
let args_rmp = json_to_rmpv(args);
let code = "
local args = ...
local curr_buf = vim.api.nvim_get_current_buf()
local is_empty_unnamed = vim.api.nvim_buf_get_name(curr_buf) == ''
and vim.api.nvim_buf_get_option(curr_buf, 'modified') == false
and vim.api.nvim_buf_line_count(curr_buf) <= 1
and (vim.api.nvim_buf_get_lines(curr_buf, 0, 1, false)[1] or '') == ''
vim.cmd('edit ' .. vim.fn.fnameescape(args.file))
local new_buf = vim.api.nvim_get_current_buf()
if is_empty_unnamed and curr_buf ~= new_buf and vim.api.nvim_buf_is_valid(curr_buf) then
pcall(vim.api.nvim_buf_delete, curr_buf, { force = true })
end
if args.filetype and args.filetype ~= '' then
vim.bo.filetype = args.filetype
end
@@ -1331,10 +1340,12 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
let args_rmp = json_to_rmpv(args);
let code = "
local args = ...
local buf = vim.api.nvim_create_buf(true, true)
if args.name and args.name ~= '' then
pcall(vim.api.nvim_buf_set_name, buf, args.name)
end
local name = (args.name and args.name ~= '') and args.name or '[Antigravity Scratch]'
local listed = args.listed == true
local buf = vim.api.nvim_create_buf(listed, true)
pcall(vim.api.nvim_buf_set_name, buf, name)
vim.bo[buf].buftype = 'nofile'
vim.bo[buf].bufhidden = 'hide'
if args.content then
local lines = vim.split(args.content, '\\n')
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
@@ -1343,7 +1354,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
vim.bo[buf].filetype = args.filetype
end
vim.api.nvim_win_set_buf(0, buf)
return 'Opened buffer ' .. tostring(buf)
return 'Opened buffer ' .. tostring(buf) .. ' (' .. name .. ')'
";
handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
}