feat: change the lsp buffer naming and improve prompt

This commit is contained in:
2025-01-31 13:38:08 +01:00
parent 58da08e26f
commit 7aa00813cf
2 changed files with 18 additions and 7 deletions

View File

@@ -21,20 +21,28 @@ local function guess_filetype(path)
end
local function create_scratch_buffer(path, content)
-- Create a unique buffer name so we never clash with an existing one
local scratch_name = string.format("chatgpt-scratch://%s#%d", path, math.random(100000, 999999))
local bufnr = api.nvim_create_buf(false, true)
if bufnr == 0 then
return nil
end
api.nvim_buf_set_name(bufnr, path)
-- Assign the unique name to the buffer
api.nvim_buf_set_name(bufnr, scratch_name)
-- Convert content string to lines
local lines = {}
for line in (content.."\n"):gmatch("(.-)\n") do
table.insert(lines, line)
end
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
-- Mark it as a scratch buffer
api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
api.nvim_buf_set_option(bufnr, "swapfile", false)
return bufnr
end
@@ -61,7 +69,9 @@ local function send_did_open(bufnr, client_id, path, filetype)
if not client then
return "Invalid client ID."
end
local text = table.concat(api.nvim_buf_get_lines(bufnr, 0, -1, false), "\n")
-- Even though the buffer name is unique, the LSP server sees this file as if at 'path'
local uri = vim.uri_from_fname(path)
local didOpenParams = {
@@ -72,7 +82,6 @@ local function send_did_open(bufnr, client_id, path, filetype)
text = text,
}
}
client.rpc.notify("textDocument/didOpen", didOpenParams)
return nil
end
@@ -145,22 +154,23 @@ function M.lsp_check_file_content(path, new_content, timeout_ms)
local client_id, err = attach_existing_lsp_client(bufnr, filetype)
if not client_id then
vim.api.nvim_buf_delete(bufnr, { force = true })
api.nvim_buf_delete(bufnr, { force = true })
return "(LSP) " .. (err or "No suitable LSP client.")
end
local err2 = send_did_open(bufnr, client_id, path, filetype)
if err2 then
vim.api.nvim_buf_delete(bufnr, { force = true })
api.nvim_buf_delete(bufnr, { force = true })
return "(LSP) " .. err2
end
-- Optionally do a didChange
send_did_change(bufnr, client_id)
local diags = wait_for_diagnostics(bufnr, timeout_ms or 2000)
local diag_str = diagnostics_to_string(diags)
vim.api.nvim_buf_delete(bufnr, { force = true })
api.nvim_buf_delete(bufnr, { force = true })
return diag_str
end