apply_code_action
Apply quick fixes and refactorings in Svelte code by selecting available actions to resolve issues or improve code structure.
Instructions
Apply a code action (quick fix, refactoring) by its title. Use get_code_actions first to see available actions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absolute path to the file | |
| startLine | Yes | Start line (1-based) | |
| actionTitle | Yes | Title of the code action to apply (case-insensitive partial match) | |
| endLine | No | End line (1-based) | |
| kind | No | Filter by kind: quickfix, refactor, etc. |
Implementation Reference
- src/tools/code-actions.ts:107-186 (handler)The handler function for 'apply_code_action' which fetches, identifies, resolves, and applies the code action.
async ({ filePath, startLine, actionTitle, endLine, kind, }): Promise<ToolResult> => { try { const { actions, error } = await fetchCodeActions( lsp, filePath, startLine, endLine, kind ); if (error) return textResult(error); if (!actions) return textResult("No code actions available."); // Find matching action by partial title let match = actions.find((a: any) => (a.title ?? "") .toLowerCase() .includes(actionTitle.toLowerCase()) ); // Exact match fallback if (!match) { match = actions.find( (a: any) => (a.title ?? "").toLowerCase() === actionTitle.toLowerCase() ); } if (!match) { return textResult( `No code action matching '${actionTitle}' found. Use get_code_actions to see available actions.` ); } if (match.disabled) { return textResult( `Code action '${match.title}' is disabled: ${match.disabled.reason}.` ); } // If action has a direct edit, use it. Otherwise resolve. let edit = match.edit; if (!edit) { const resolved = await lsp.request("codeAction/resolve", match); if (!resolved) { return textResult( `Failed to resolve code action '${match.title}'.` ); } edit = resolved.edit; } if (!edit) { if (match.command) { return textResult( `Code action '${match.title}' requires command execution which is not supported.` ); } return textResult( `Code action '${match.title}' returned no edits.` ); } const matchTitle = match.title ?? actionTitle; const applied = await applyWorkspaceEdit(lsp, edit); const summary = formatWorkspaceEdit(edit, `Applied '${matchTitle}'`); return textResult( applied ? summary : `(dry-run, edits NOT applied)\n\n${summary}` ); } catch (ex) { return textResult(formatError(ex)); } } - src/tools/code-actions.ts:86-106 (registration)Registration of the 'apply_code_action' tool with its schema definition.
server.registerTool( "apply_code_action", { title: "Apply Code Action", description: "Apply a code action (quick fix, refactoring) by its title. Use get_code_actions first to see available actions.", inputSchema: z.object({ filePath: z.string().describe("Absolute path to the file"), startLine: z.number().describe("Start line (1-based)"), actionTitle: z .string() .describe( "Title of the code action to apply (case-insensitive partial match)" ), endLine: z.number().optional().describe("End line (1-based)"), kind: z .string() .optional() .describe("Filter by kind: quickfix, refactor, etc."), }), },