change-to-tools #2

Merged
dominik.polakovics merged 5 commits from change-to-tools into main 2025-01-31 13:38:30 +01:00
2 changed files with 18 additions and 7 deletions
Showing only changes of commit 7aa00813cf - Show all commits

View File

@@ -286,8 +286,9 @@ local M = {
**Key Points**: **Key Points**:
- Always include `project_name: "<actual_project_name>"` in the same YAML as `tools`. - Always include `project_name: "<actual_project_name>"` in the same YAML as `tools`.
- If you only need one tool, include just one object in the `tools` array. - If you only need one tool, include just one object in the `tools` array.
- If multiple tools are needed, list them sequentially under `tools`. - If multiple tools are needed, list them sequentially in the `tools` array.
- Allways run at least one tool (e.g., `readFile`, `editFile`, `executeCommand`), exept you have finished. - 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. - 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. - If the response grows too large, I'll guide you to break it into smaller steps.
]], ]],

View File

@@ -21,20 +21,28 @@ local function guess_filetype(path)
end end
local function create_scratch_buffer(path, content) 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) local bufnr = api.nvim_create_buf(false, true)
if bufnr == 0 then if bufnr == 0 then
return nil return nil
end 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 = {} local lines = {}
for line in (content.."\n"):gmatch("(.-)\n") do for line in (content.."\n"):gmatch("(.-)\n") do
table.insert(lines, line) table.insert(lines, line)
end end
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) 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, "bufhidden", "wipe")
api.nvim_buf_set_option(bufnr, "swapfile", false) api.nvim_buf_set_option(bufnr, "swapfile", false)
return bufnr return bufnr
end end
@@ -61,7 +69,9 @@ local function send_did_open(bufnr, client_id, path, filetype)
if not client then if not client then
return "Invalid client ID." return "Invalid client ID."
end end
local text = table.concat(api.nvim_buf_get_lines(bufnr, 0, -1, false), "\n") 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 uri = vim.uri_from_fname(path)
local didOpenParams = { local didOpenParams = {
@@ -72,7 +82,6 @@ local function send_did_open(bufnr, client_id, path, filetype)
text = text, text = text,
} }
} }
client.rpc.notify("textDocument/didOpen", didOpenParams) client.rpc.notify("textDocument/didOpen", didOpenParams)
return nil return nil
end 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) local client_id, err = attach_existing_lsp_client(bufnr, filetype)
if not client_id then 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.") return "(LSP) " .. (err or "No suitable LSP client.")
end end
local err2 = send_did_open(bufnr, client_id, path, filetype) local err2 = send_did_open(bufnr, client_id, path, filetype)
if err2 then if err2 then
vim.api.nvim_buf_delete(bufnr, { force = true }) api.nvim_buf_delete(bufnr, { force = true })
return "(LSP) " .. err2 return "(LSP) " .. err2
end end
-- Optionally do a didChange
send_did_change(bufnr, client_id) send_did_change(bufnr, client_id)
local diags = wait_for_diagnostics(bufnr, timeout_ms or 2000) local diags = wait_for_diagnostics(bufnr, timeout_ms or 2000)
local diag_str = diagnostics_to_string(diags) 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 return diag_str
end end