Skip to main content
jeremylongshore

formatter

by jeremylongshorev1.0.0

Code formatting plugin using hooks to auto-format on save

Installation guide โ†’
1 skill 2 commandshooksproductivity GitHub

Commands

format-check

Check if files are properly formatted without making changes

format

Format code files using Prettier

Documentation

# Code Formatter Plugin v2.0.1

A comprehensive code formatting plugin for Claude Code that automatically formats your code using Prettier and other formatting tools. Includes automatic formatting hooks, validation commands, and Agent Skills for intelligent formatting assistance.

## โœจ Features

- **Automatic Formatting** - Format code automatically after edits using PostToolUse hooks
- **Format Validation** - Check if files are properly formatted without changes
- **Agent Skills** - Intelligent formatting assistance that activates on keywords
- **Multiple File Types** - Supports JS, TS, JSON, CSS, MD, YAML, HTML, Vue, Svelte
- **Custom Commands** - `/format` and `/format-check` for manual control
- **CI/CD Ready** - Integration scripts for GitHub Actions and other pipelines
- **Configurable** - Use your own Prettier config or defaults

## ๐Ÿš€ Installation

```bash
# Add the marketplace
/plugin marketplace add jeremylongshore/claude-code-plugins

# Install the formatter plugin
/plugin install formatter@claude-code-plugins-plus
```

## ๐Ÿ“‹ Prerequisites

The plugin requires Node.js and npm/npx to be installed:

```bash
# Check if Node.js is installed
node --version

# Install Prettier globally (optional)
npm install -g prettier

# Or use npx (no installation needed)
npx prettier --version
```

## ๐ŸŽฏ Usage

### Automatic Formatting (Hooks)

The plugin automatically formats files after you edit them:

1. When Claude uses the `Write` or `Edit` tool
2. The PostToolUse hook triggers
3. Files are automatically formatted with Prettier
4. You see the formatting applied immediately

### Manual Commands

**Format files:**
```bash
# Format all files in project
/format

# Format specific directory
/format src/

# Format specific file
/format src/app.js

# Format with custom config
/format --config .prettierrc.strict src/
```

**Check formatting:**
```bash
# Check all files
/format-check

# Check specific files
/format-check **/*.js

# Use in CI/CD (exits with error if unformatted)
/format-check || exit 1
```

### Agent Skills

The plugin's Agent Skill activates when you mention:
- "format my code"
- "fix formatting"
- "apply code style"
- "check formatting"
- "make code consistent"
- "clean up code"

Example:
```
User: "Please format my JavaScript files"
Claude: [Activates code-formatter skill and formats all JS files]
```

## โš™๏ธ Configuration

### Prettier Configuration

Create a `.prettierrc` file in your project:

```json
{
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "es5",
  "printWidth": 100,
  "bracketSpacing": true,
  "arrowParens": "avoid"
}
```

### Ignore Files

Create `.prettierignore` to skip files:

```
# Dependencies
node_modules/
vendor/

# Build outputs
dist/
build/
*.min.js
*.min.css

# Generated files
coverage/
*.lock
```

### Hook Configuration

The plugin's hooks are configured in `hooks/hooks.json`:

- **PostToolUse** - Runs after Write/Edit operations
- **PreToolUse** - Optional validation before editing (disabled by default)

To disable automatic formatting, edit the hooks.json and set `"enabled": false`.

## ๐Ÿ”ง Scripts

The plugin includes two main scripts:

### format.sh
- Main formatting script
- Handles file detection and Prettier execution
- Supports multiple file types
- Includes error handling and logging

### validate-format.sh (NEW)
- Validates formatting without changes
- Perfect for pre-commit checks
- Returns exit codes for CI/CD
- Detailed logging and debugging

Both scripts are executable and support debug mode:

```bash
# Enable debug mode
export FORMATTER_DEBUG=true

# Check logs
tail -f plugins/examples/formatter/logs/formatter.log
```

## ๐Ÿงช Testing

To test the plugin installation:

```bash
# 1. Check if plugin is installed
/plugin list

# 2. Test format checking
echo 'const x={a:1,b:2}' > test.js
/format-check test.js

# 3. Test formatting
/format test.js
cat test.js  # Should show formatted code

# 4. Test Agent Skill
# Just say: "format my code"
```

## ๐Ÿ“Š Supported File Types

| Extension | Language | Support Level |
|-----------|----------|--------------|
| .js, .jsx | JavaScript | โœ… Full |
| .ts, .tsx | TypeScript | โœ… Full |
| .json | JSON | โœ… Full |
| .css, .scss | CSS/SASS | โœ… Full |
| .md, .mdx | Markdown | โœ… Full |
| .yaml, .yml | YAML | โœ… Full |
| .html | HTML | โœ… Full |
| .vue | Vue | โœ… Full |
| .svelte | Svelte | โœ… Full |
| .graphql | GraphQL | โœ… Full |
| .xml | XML | โš ๏ธ Limited |

## ๐Ÿšจ Troubleshooting

### Plugin Not Loading

```bash
# Reinstall the plugin
/plugin uninstall formatter@claude-code-plugins-plus
/plugin install formatter@claude-code-plugins-plus

# Check plugin status
/plugin list
```

### Prettier Not Found

```bash
# Install Prettier globally
npm install -g prettier

# Or ensure npx is available
which npx
```

### Formatting Not Working

1. Check if Node.js is installed: `node --version`
2. Verify script permissions: `ls -la scripts/`
3. Check for syntax errors in target files
4. Enable debug mode and check logs
5. Ensure files aren't in .prettierignore

### Hook Not Triggering

1. Check if hooks are enabled in hooks.json
2. Verify the matcher pattern matches your operation
3. Check the plugin is properly installed
4. Look for errors in Claude Code output

## ๐Ÿ”„ Updates

### v2.0.1 (2025-12-13)
- ๐Ÿ› Fixed duplicate hooks loading error (Issue #149)
- ๐Ÿ”ง Removed redundant hooks reference from plugin.json
- โœ… Claude Code now auto-discovers hooks/hooks.json

### v2.0.0 (2025-12-12)
- โœ… Fixed missing validate-format.sh script (Issue #147)
- โœจ Added comprehensive Agent Skills
- ๐Ÿ“ Enhanced plugin description with trigger phrases
- ๐ŸŽฏ Added format and format-check commands
- ๐Ÿ“š Complete documentation rewrite
- ๐Ÿ”ง Improved error handling and logging

### v1.0.0 (2025-10-11)
- Initial release with basic formatting hooks

## ๐Ÿ‘จโ€๐Ÿ’ป Contributing

Found a bug or have a suggestion? Please open an issue at:
https://github.com/jeremylongshore/claude-code-plugins/issues

## ๐Ÿ“œ License

MIT License - See LICENSE file for details

## ๐Ÿ™ Acknowledgments

- Thanks to @beepsoft for reporting Issue #147
- Prettier team for the excellent formatting tool
- Claude Code community for feedback and suggestions

---

**Author**: Jeremy Longshore
**Email**: [email protected]
**Repository**: https://github.com/jeremylongshore/claude-code-plugins