get_code_actions
List available code actions like quick fixes and refactorings for specific lines in Svelte files to improve code quality and structure.
Instructions
List available code actions (quick fixes, refactorings) for a line or range in a file.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absolute path to the file | |
| startLine | Yes | Start line (1-based) | |
| endLine | No | End line (1-based). Defaults to startLine | |
| kind | No | Filter by kind: quickfix, refactor, refactor.extract, refactor.inline, refactor.rewrite, source, source.organizeImports, source.fixAll |
Implementation Reference
- src/tools/code-actions.ts:38-83 (handler)The handler logic for 'get_code_actions' which fetches and formats code actions.
async ({ filePath, startLine, endLine, kind }): Promise<ToolResult> => { try { const { actions, error } = await fetchCodeActions( lsp, filePath, startLine, endLine, kind ); if (error) return textResult(error); if (!actions || actions.length === 0) { return textResult( `No code actions available at line ${startLine}` + (kind ? ` (kind: ${kind})` : "") + "." ); } const lines: string[] = [ `Found ${actions.length} code action(s) at line ${startLine}` + (endLine != null && endLine !== startLine ? `-${endLine}` : "") + ":", "", ]; for (let i = 0; i < actions.length; i++) { const action = actions[i]; const title = action.title ?? "?"; const actionKind = action.kind ?? ""; const isPreferred = action.isPreferred === true; const disabled = action.disabled?.reason; let entry = ` ${i + 1}. ${title}`; if (actionKind) entry += ` [${actionKind}]`; if (isPreferred) entry += " (preferred)"; if (disabled) entry += ` (disabled: ${disabled})`; lines.push(entry); } return textResult(lines.join("\n")); } catch (ex) { return textResult(formatError(ex)); } } - src/tools/code-actions.ts:17-37 (registration)Registration of the 'get_code_actions' tool with its schema definition.
server.registerTool( "get_code_actions", { title: "Get Code Actions", description: "List available code actions (quick fixes, refactorings) for a line or range in a file.", inputSchema: z.object({ filePath: z.string().describe("Absolute path to the file"), startLine: z.number().describe("Start line (1-based)"), endLine: z .number() .optional() .describe("End line (1-based). Defaults to startLine"), kind: z .string() .optional() .describe( "Filter by kind: quickfix, refactor, refactor.extract, refactor.inline, refactor.rewrite, source, source.organizeImports, source.fixAll" ), }), }, - src/tools/code-actions.ts:190-224 (helper)Helper function that performs the actual LSP request to fetch code actions.
async function fetchCodeActions( lsp: LspClient, filePath: string, startLine: number, endLine?: number, kind?: string ): Promise<{ actions?: any[]; error?: string }> { const prep = await prepareDocumentRequest(lsp, filePath); if ("error" in prep) return { error: prep.error }; // Fetch diagnostics from cache for context const cachedDiags = lsp.getCachedDiagnostics(prep.uri); const sl = startLine - 1; const el = (endLine ?? startLine) - 1; const diagnosticsForRange = cachedDiags.filter((d: any) => { const diagStart = d.range?.start?.line ?? -1; const diagEnd = d.range?.end?.line ?? -1; return diagEnd >= sl && diagStart <= el; }); const context: any = { diagnostics: diagnosticsForRange }; if (kind) context.only = [kind]; const result = await lsp.request("textDocument/codeAction", { textDocument: { uri: prep.uri }, range: { start: { line: sl, character: 0 }, end: { line: el + 1, character: 0 }, }, context, }); return { actions: Array.isArray(result) ? result : [] }; }