summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--packages/tool-edit-file/src/extension.ts43
1 files changed, 23 insertions, 20 deletions
diff --git a/packages/tool-edit-file/src/extension.ts b/packages/tool-edit-file/src/extension.ts
index 9be53da..3929634 100644
--- a/packages/tool-edit-file/src/extension.ts
+++ b/packages/tool-edit-file/src/extension.ts
@@ -14,26 +14,29 @@ export const extension: Extension = {
contributes: { tools: ["edit_file"] },
},
activate(host) {
- // Optional LSP integration: if the lsp extension is loaded AND has
- // activated before us, wire its getDiagnostics service as the post-edit
- // diagnostics hook. If absent (not loaded, or activates after us), edits
- // proceed without diagnostics (graceful degradation).
- let lspService: LspService | undefined;
- try {
- lspService = host.getService(lspServiceHandle);
- } catch {
- lspService = undefined;
- }
- const diagnostics: DiagnosticsHook | undefined = lspService
- ? async (opts) =>
- lspService.getDiagnostics({
- filePath: opts.filePath,
- text: opts.text,
- cwd: opts.cwd,
- timeoutMs: 60_000,
- minSeverity: 2, // errors + warnings only
- })
- : undefined;
+ // Lazy LSP lookup: the LSP extension activates AFTER us in the
+ // CORE_EXTENSIONS array, so host.getService would throw at activation
+ // time. Instead, defer the lookup to edit time — by then all extensions
+ // have activated. If LSP isn't loaded, the try/catch returns a no-op
+ // (graceful degradation: edits proceed without diagnostics).
+ const diagnostics: DiagnosticsHook = async (opts) => {
+ let lspService: LspService | undefined;
+ try {
+ lspService = host.getService(lspServiceHandle);
+ } catch {
+ return { formatted: "", slow: false, timedOut: false };
+ }
+ if (!lspService) {
+ return { formatted: "", slow: false, timedOut: false };
+ }
+ return lspService.getDiagnostics({
+ filePath: opts.filePath,
+ text: opts.text,
+ cwd: opts.cwd,
+ timeoutMs: 60_000,
+ minSeverity: 2, // errors + warnings only
+ });
+ };
host.defineTool(createEditFileTool(process.cwd(), diagnostics));
},