M

Monitor Smart Formatting

Boost productivity using this smart, code, formatting, based. Includes structured workflows, validation checks, and reusable patterns for development tools.

HookClipticsdevelopment toolsv1.0.0MIT
0 views0 copies

Monitor Smart Formatting

Automatically formats code using the appropriate language-specific formatter after every file modification, supporting Prettier, Black, gofmt, rustfmt, and more.

When to Use This Hook

Attach this hook when you need to:

  • Automatically format code in the correct language-specific style after every file edit without remembering which formatter to use
  • Maintain consistent formatting across a polyglot codebase where different languages require different formatting tools
  • Eliminate formatting discussions in code reviews by ensuring all code is formatted before it reaches the review stage

Consider alternatives when:

  • Your project uses a single language and you already have format-on-save configured in your editor
  • You need custom formatting rules that differ from the standard formatter defaults and require extensive configuration

Quick Start

Configuration

name: monitor-smart-formatting type: hook trigger: PostToolUse category: development-tools

Example Trigger

# Claude edits src/utils/parser.ts # Hook fires: # Smart Formatting: Formatting src/utils/parser.ts with Prettier...

Example Output

Smart Formatting: src/utils/parser.ts
Detected Language: TypeScript
Formatter: Prettier
Config: .prettierrc found in project root
Changes Applied: 3 formatting adjustments
  - Line 14: trailing comma added
  - Line 22: indent adjusted from 4 to 2 spaces
  - Line 35: line length wrapped at 80 characters
Status: Formatted successfully

Core Concepts

Formatter Selection Overview

AspectDetails
JS/TS/CSS/HTML/JSONPrettier via npx prettier --write
PythonBlack via black for consistent Python formatting
Gogofmt via gofmt -w (standard Go formatting)
Rustrustfmt via rustfmt for Rust code
PHPphp-cs-fixer via php-cs-fixer fix for PHP code
TriggerPostToolUse on Edit and MultiEdit operations

Smart Detection Workflow

PostToolUse (Edit/MultiEdit)
    |
    v
[Read $CLAUDE_TOOL_FILE_PATH extension]
    |
    +--- .js/.ts/.jsx/.tsx/.json/.css/.html --> [Prettier]
    |
    +--- .py --> [Black]
    |
    +--- .go --> [gofmt]
    |
    +--- .rs --> [rustfmt]
    |
    +--- .php --> [php-cs-fixer]
    |
    +--- Other --> [Skip formatting]
    |
    v
[Run formatter with --write/in-place flag]
    |
    v
[Report: changes applied or already formatted]
    |
    v
[Exit 0 (non-blocking, errors suppressed)]

Configuration

ParameterTypeDefaultDescription
matcherstringEdit|MultiEditTool types that trigger formatting
prettier_argsstring--writeAdditional arguments for Prettier execution
black_argsstring(none)Additional arguments for Black Python formatter
gofmt_argsstring-wAdditional arguments for gofmt
suppress_errorsbooleantrueWhether to suppress formatter errors and continue silently

Best Practices

  1. Install all required formatters before enabling - The hook suppresses errors silently by default, which means a missing formatter will simply skip formatting without notification. Install Prettier, Black, gofmt, and other formatters your project needs before activating the hook.

  2. Configure Prettier with a project-level config - Create a .prettierrc file in your project root to define formatting rules. Without a config file, Prettier uses defaults that may not match your team's preferences. Include the config in version control for consistency.

  3. Use Black with default settings for Python - Black is intentionally opinionated and provides minimal configuration options. Embrace its defaults to eliminate formatting debates. If you need Black-incompatible formatting, consider switching to autopep8 which offers more flexibility.

  4. Let gofmt handle Go formatting exclusively - The Go community standardizes on gofmt output. Do not add custom formatting rules for Go files. The hook correctly uses gofmt -w which formats files in-place following the Go standard.

  5. Test formatter compatibility with your linter - Formatters and linters can conflict when they have overlapping rules with different preferences. Run both tools on the same file and verify they converge on the same output. For JavaScript, use eslint-config-prettier to resolve conflicts.

Common Issues

  1. Prettier reformats files that were already correctly formatted - This usually indicates a mismatch between Prettier's config and the formatting Claude Code produces. Ensure your .prettierrc settings match the formatting style Claude Code uses, or accept that Prettier will normalize to its configured style.

  2. Black is not installed and Python files are not formatted - The hook suppresses errors with || true, so missing Black installation fails silently. Install Black with pip install black and verify it is available in your PATH before relying on auto-formatting for Python files.

  3. gofmt modifies files that cause compilation errors - gofmt only handles formatting, not syntax correctness. If a Go file has syntax errors, gofmt will either fail silently or make formatting changes that highlight the underlying syntax issue. Fix compilation errors before expecting clean formatting.

Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates