Skip to main content
Glama

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
NameRequiredDescriptionDefault
filePathYesAbsolute path to the file
startLineNoOptional start line (1-based) for range formatting
endLineNoOptional end line (1-based) for range formatting
tabSizeNoTab size. Default: 2
insertSpacesNoUse spaces instead of tabs. Default: true

Implementation Reference

  • 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));
      }
    }
  • 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"),
        }),
      },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/adainrivers/SvelteLS.MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server