format_document
Apply project formatting rules to Svelte files or specific line ranges, ensuring consistent code style and structure.
Instructions
Format a file (or a range of lines) using the project's formatting rules.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Absolute path to the file | |
| startLine | No | Optional start line (1-based) for range formatting | |
| endLine | No | Optional end line (1-based) for range formatting | |
| tabSize | No | Tab size. Default: 2 | |
| insertSpaces | No | Use spaces instead of tabs. Default: true |
Implementation Reference
- src/tools/editing.ts:98-153 (handler)The handler function for the `format_document` tool, which manages the LSP request for document formatting or range formatting and applies the resulting text edits.
async ({ filePath, startLine, endLine, tabSize, insertSpaces, }): Promise<ToolResult> => { try { const prep = await prepareDocumentRequest(lsp, filePath); if ("error" in prep) return textResult(prep.error); const options = { tabSize, insertSpaces, trimTrailingWhitespace: true, insertFinalNewline: true, trimFinalNewlines: true, }; let result: any; if (startLine != null) { const sl = startLine - 1; const el = (endLine ?? startLine) - 1; result = await lsp.request("textDocument/rangeFormatting", { textDocument: { uri: prep.uri }, range: { start: { line: sl, character: 0 }, end: { line: el + 1, character: 0 }, }, options, }); } else { result = await lsp.request("textDocument/formatting", { textDocument: { uri: prep.uri }, options, }); } if (!Array.isArray(result) || result.length === 0) { return textResult( `No formatting changes needed in ${basename(filePath)}.` ); } const count = await applyTextEdits(lsp, filePath, result); const rangeDesc = startLine != null ? ` (lines ${startLine}-${endLine ?? startLine})` : ""; return textResult( `Formatted ${basename(filePath)}${rangeDesc}: ${count} edit(s) applied.` ); } catch (ex) { return textResult(formatError(ex)); } } - src/tools/editing.ts:75-97 (registration)Tool registration for `format_document` including title, description, and input schema.
server.registerTool( "format_document", { title: "Format Document", description: "Format a file (or a range of lines) using the project's formatting rules.", inputSchema: z.object({ filePath: z.string().describe("Absolute path to the file"), startLine: z .number() .optional() .describe("Optional start line (1-based) for range formatting"), endLine: z .number() .optional() .describe("Optional end line (1-based) for range formatting"), tabSize: z.number().default(2).describe("Tab size. Default: 2"), insertSpaces: z .boolean() .default(true) .describe("Use spaces instead of tabs. Default: true"), }), },