From 7aa00813cfdd0c14533e352dd0689474ab4d9826 Mon Sep 17 00:00:00 2001 From: Dominik Polakovics Date: Fri, 31 Jan 2025 13:38:08 +0100 Subject: [PATCH] feat: change the lsp buffer naming and improve prompt --- lua/chatgpt_nvim/prompts.lua | 5 +++-- .../tools/lsp_robust_diagnostics.lua | 20 ++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lua/chatgpt_nvim/prompts.lua b/lua/chatgpt_nvim/prompts.lua index 8882f1c..941ddab 100644 --- a/lua/chatgpt_nvim/prompts.lua +++ b/lua/chatgpt_nvim/prompts.lua @@ -286,8 +286,9 @@ local M = { **Key Points**: - Always include `project_name: ""` in the same YAML as `tools`. - If you only need one tool, include just one object in the `tools` array. - - If multiple tools are needed, list them sequentially under `tools`. - - Allways run at least one tool (e.g., `readFile`, `editFile`, `executeCommand`), exept you have finished. + - If multiple tools are needed, list them sequentially in the `tools` array. + - Always run at least one tool (e.g., `readFile`, `editFile`, `executeCommand`), exept you have finished. + - Always just include one yaml in the response with all the tools you want to run in that yaml. - The plugin will verify the `project_name` is correct before running any tools. - If the response grows too large, I'll guide you to break it into smaller steps. ]], diff --git a/lua/chatgpt_nvim/tools/lsp_robust_diagnostics.lua b/lua/chatgpt_nvim/tools/lsp_robust_diagnostics.lua index 70c723d..8693c2b 100644 --- a/lua/chatgpt_nvim/tools/lsp_robust_diagnostics.lua +++ b/lua/chatgpt_nvim/tools/lsp_robust_diagnostics.lua @@ -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