diff options
Diffstat (limited to 'js/src/tool')
| -rw-r--r-- | js/src/tool/diagnostics.ts | 56 | ||||
| -rw-r--r-- | js/src/tool/edit.ts | 18 | ||||
| -rw-r--r-- | js/src/tool/index.ts | 1 | ||||
| -rw-r--r-- | js/src/tool/view.ts | 3 |
4 files changed, 67 insertions, 11 deletions
diff --git a/js/src/tool/diagnostics.ts b/js/src/tool/diagnostics.ts new file mode 100644 index 000000000..a01f39c7c --- /dev/null +++ b/js/src/tool/diagnostics.ts @@ -0,0 +1,56 @@ +import { z } from "zod"; +import { Tool } from "./tool"; +import path from "path"; +import { LSP } from "../lsp"; +import { App } from "../app"; + +export const DiagnosticsTool = Tool.define({ + name: "diagnostics", + description: `Get diagnostics for a file and/or project. + +WHEN TO USE THIS TOOL: +- Use when you need to check for errors or warnings in your code +- Helpful for debugging and ensuring code quality +- Good for getting a quick overview of issues in a file or project + +HOW TO USE: +- Provide a path to a file to get diagnostics for that file +- Results are displayed in a structured format with severity levels + +FEATURES: +- Displays errors, warnings, and hints +- Groups diagnostics by severity +- Provides detailed information about each diagnostic + +LIMITATIONS: +- Results are limited to the diagnostics provided by the LSP clients +- May not cover all possible issues in the code +- Does not provide suggestions for fixing issues + +TIPS: +- Use in conjunction with other tools for a comprehensive code review +- Combine with the LSP client for real-time diagnostics`, + parameters: z.object({ + path: z.string().describe("The path to the file to get diagnostics."), + }), + execute: async (args) => { + const app = await App.use(); + const normalized = path.isAbsolute(args.path) + ? args.path + : path.join(app.root, args.path); + await LSP.file(normalized); + const diagnostics = await LSP.diagnostics(); + const file = diagnostics[normalized]; + return { + metadata: { + diagnostics, + }, + output: file?.length + ? file.map(LSP.Diagnostic.pretty).join("\n") + : "No errors found", + }; + }, +}); + +const x: number = "asd"; + diff --git a/js/src/tool/edit.ts b/js/src/tool/edit.ts index 96d8e0c54..42cd02bb4 100644 --- a/js/src/tool/edit.ts +++ b/js/src/tool/edit.ts @@ -118,17 +118,15 @@ export const edit = Tool.define({ FileTimes.read(filePath); let output = ""; - await LSP.run((client) => client.refreshDiagnostics({ path: filePath })); - const diagnostics = await LSP.run(async (client) => client.diagnostics); - for (const diagnostic of diagnostics) { - for (const [file, params] of diagnostic.entries()) { - if (params.length === 0) continue; - if (file === filePath) { - output += `\nThis file has errors, please fix\n<file_diagnostics>\n${JSON.stringify(params)}\n</file_diagnostics>\n`; - continue; - } - output += `\n<project_diagnostics>\n${JSON.stringify(params)}\n</project_diagnostics>\n`; + await LSP.file(filePath); + const diagnostics = await LSP.diagnostics(); + for (const [file, issues] of Object.entries(diagnostics)) { + if (issues.length === 0) continue; + if (file === filePath) { + output += `\nThis file has errors, please fix\n<file_diagnostics>\n${issues.map(LSP.Diagnostic.pretty).join("\n")}\n</file_diagnostics>\n`; + continue; } + output += `\n<project_diagnostics>\n${issues.map(LSP.Diagnostic.pretty).join("\n")}\n</project_diagnostics>\n`; } return { diff --git a/js/src/tool/index.ts b/js/src/tool/index.ts index d7027c529..b18f85012 100644 --- a/js/src/tool/index.ts +++ b/js/src/tool/index.ts @@ -5,3 +5,4 @@ export * from "./glob"; export * from "./grep"; export * from "./view"; export * from "./ls"; +export * from "./diagnostics"; diff --git a/js/src/tool/view.ts b/js/src/tool/view.ts index 5b429c63d..138f9ad3a 100644 --- a/js/src/tool/view.ts +++ b/js/src/tool/view.ts @@ -115,7 +115,8 @@ export const view = Tool.define({ } output += "\n</file>"; - await LSP.run((client) => client.notify.open({ path: filePath })); + // just warms the lsp client + LSP.file(filePath); FileTimes.read(filePath); return { |
