formatter
Format code or apply LLM-powered syntax fixes to files locally. Choose between standard formatting and AI-assisted corrections for improved code quality.
Instructions
Run formatter or LLM-assisted syntax fixes.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | No | Action: run (format code), fix (LLM-powered syntax fixes) | |
| files | No | Specific files to process | |
| command | No | Custom format command | |
| check | No | Check only, do not modify (for run action) | |
| difficulty | No | LLM fix difficulty (for fix action) | |
| dryRun | No | Preview fixes without applying (for fix action) | |
| maxFixes | No | Maximum fixes to apply (for fix action) | |
| timeout | No | Timeout in milliseconds |
Implementation Reference
- src/tools/execution.ts:469-506 (handler)The runFormatter method in ExecutionTools class which serves as the handler for the formatter tool, capable of detecting the formatter and executing the formatting command.
async runFormatter(options?: { command?: string; files?: string[]; check?: boolean; // Just check, don't modify timeout?: number; }): Promise<ExecutionResult> { let command = options?.command; if (!command) { command = await this.detectFormatCommand(options?.check); if (!command) { return { success: false, exitCode: -1, stdout: '', stderr: 'Could not detect formatter. Please provide a format command.', duration: 0, command: '', }; } } // Append specific files if provided if (options?.files && options.files.length > 0) { // For prettier, replace glob pattern with specific files if (command.includes('prettier')) { command = command.replace(/"[^"]*"/, options.files.join(' ')); } else { command = `${command} ${options.files.join(' ')}`; } } return this.executeCommand( command, this.workspaceRoot, options?.timeout ?? this.defaultTimeout ); } - src/tools/execution.ts:315-371 (helper)Helper method to automatically detect the appropriate formatter command based on the workspace files (package.json, pyproject.toml, etc.).
private async detectFormatCommand(check: boolean = false): Promise<string | undefined> { const packageJsonPath = join(this.workspaceRoot, 'package.json'); if (existsSync(packageJsonPath)) { try { const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); // Check scripts const scriptName = check ? 'format:check' : 'format'; if (pkg.scripts?.[scriptName]) { return `npm run ${scriptName}`; } // Check for Prettier config const prettierConfigs = [ '.prettierrc', '.prettierrc.js', '.prettierrc.json', 'prettier.config.js', 'prettier.config.mjs', 'prettier.config.cjs', ]; for (const config of prettierConfigs) { if (existsSync(join(this.workspaceRoot, config))) { return check ? 'npx prettier --check "**/*.{ts,js,tsx,jsx,json,css,md}"' : 'npx prettier --write "**/*.{ts,js,tsx,jsx,json,css,md}"'; } } // Check devDependencies const deps = { ...pkg.dependencies, ...pkg.devDependencies }; if (deps.prettier) { return check ? 'npx prettier --check .' : 'npx prettier --write .'; } } catch { // Ignore parse errors } } // Python projects if (existsSync(join(this.workspaceRoot, 'pyproject.toml'))) { return check ? 'black --check .' : 'black .'; } // Go projects if (existsSync(join(this.workspaceRoot, 'go.mod'))) { return 'go fmt ./...'; } // Rust projects if (existsSync(join(this.workspaceRoot, 'Cargo.toml'))) { return check ? 'cargo fmt -- --check' : 'cargo fmt'; } return undefined; }