clang-format Configuration
关于
This Claude Skill handles all clang-format configuration tasks, triggered by mentions of the tool, requests to analyze code style, or needs to create or troubleshoot formatting rules. It provides workflows for generating .clang-format files from templates, analyzing existing code to match its style, and modifying configurations. Use it to define brace styles, indentation, spacing, and other formatting conventions while minimizing diffs.
技能文档
clang-format Configuration
Configure the clang-format code formatting tool using ready-to-use templates, integration scripts, and comprehensive reference documentation.
Purpose
This skill provides procedural workflows for clang-format configuration tasks:
- Create new .clang-format files from proven templates
- Analyze existing code style and generate matching configurations
- Set up editor and git integration with bundled scripts
- Troubleshoot formatting behavior using reference documentation
Workflow Routing by Trigger Type
Once invoked, route to appropriate workflow based on which trigger fired:
Trigger 1: Explicit clang-format mention → If user asks about specific options: Consult references/01-09.md for relevant category → If user needs complete reference: Direct to references/complete/clang-format-style-options.md → If user asks about CLI usage: Reference references/cli-usage.md
Trigger 2: Code style analysis request → Follow "Analyzing Existing Code Style" workflow below → Examine code samples systematically (braces→indentation→spacing→breaking→alignment) → Map patterns to closest template in assets/configs/ → Generate initial configuration hypothesis as a temporary configuration file "/tmp/<reponame>/hypothesis<number>.clang-format" → VERIFY IMPACT: Run clang-format --style="/tmp/<repo_name>/hypothesis_<number>.clang-format" file.cpp | diff - file.cpp on 3-5 representative samples → MEASURE IMPACT using weighted scoring: • Metric 1: Line count changes (lines added/removed) - weight 10 • Metric 2: In-line whitespace changes (spacing within existing lines) - weight 1 • Impact Score = (line_count_changes × 10) + (whitespace_changes × 1) • Lower score = lower impact to rebasing conflicts, future git-diff analysis, and merge request change reviews = better configuration → ITERATE: Adjust config options targeting highest-impact settings, re-test, compare scores → REPEAT until reaching minimal achievable score while maintaining consistent style enforcement → REPORT TO USER: Present winning configuration with: • Final impact score and breakdown (line changes vs whitespace changes) • Comparison table showing all tested hypotheses and their scores • Example diff snippets showing what will change, with commands for the user to test it themselves against files of their choice. • Rationale for selected configuration → AWAIT USER APPROVAL before finalizing configuration → Only after approval: provide final configuration file
Trigger 3: Configuration file operations → If creating new: Follow "Creating New Configuration from Template" workflow → If modifying existing: Read current config, identify changes needed, consult relevant category guide → If generating from code: Use Trigger 2 workflow (code style analysis)
Trigger 4: Formatting behavior investigation → Follow "Troubleshooting Formatting Issues" workflow below → Verify config detection with --dump-config → Identify affected category, consult relevant references/0X.md guide → Test isolated options with minimal config
Trigger 5: Style option inquiries → Map question to category: braces→03, indentation→04, spacing→05, alignment→01, breaking→02 → Reference specific category guide in references/ → Provide examples from quick-reference.md if applicable
Trigger 6: Minimal-disruption requests → Use "Analyzing Existing Code Style" workflow to match current patterns → Emphasize starting from closest template to minimize changes → Test on representative samples before project-wide application → Document which patterns were preserved vs normalized
Bundled Resources
Configuration Templates (assets/configs/)
Seven ready-to-use .clang-format templates optimized for common scenarios:
google-cpp-modified.clang-format- Google C++ style with 4-space indent, 120 column limitlinux-kernel.clang-format- Linux kernel coding standards (tabs, K&R braces)microsoft-visual-studio.clang-format- Microsoft/Visual Studio conventionsmodern-cpp17-20.clang-format- Modern C++17/20 style with contemporary idiomscompact-dense.clang-format- Compact style for space-constrained environmentsreadable-spacious.clang-format- Spacious style prioritizing readabilitymulti-language.clang-format- Multi-language configuration (C++, JavaScript, Java)
When to use templates: Start new projects, establish team standards, or quickly test formatting approaches.
Integration Scripts (assets/integrations/)
Three editor and git integration scripts:
pre-commit- Git pre-commit hook for automatic formatting of staged filesvimrc-clang-format.vim- Vim configuration for format-on-saveemacs-clang-format.el- Emacs configuration for clang-format integration
When to use integrations: Set up automatic formatting in development workflow.
Reference Documentation (references/)
Detailed documentation organized by category:
Quick Navigation:
index.md- Overview and documentation hubquick-reference.md- Complete working configurations with explanationscli-usage.md- Command-line usage, editor setup, CI/CD integration
Option Categories (01-09.md):
01-alignment.md- Vertical alignment of declarations, assignments, operators02-breaking.md- Line breaking and wrapping rules03-braces.md- Brace placement styles (K&R, Allman, GNU, etc.)04-indentation.md- Indentation rules and special cases05-spacing.md- Whitespace control around operators, keywords06-includes.md- Include/import organization and sorting07-languages.md- Language-specific options for C++, Java, JavaScript08-comments.md- Comment formatting and reflow09-advanced.md- Penalty system, raw string formatting, experimental features
Complete Reference (complete/):
clang-format-cli.md- Full command-line interface documentationclang-format-style-options.md- All 194 style options with examples
Common Workflows
Creating New Configuration from Template
To create a new .clang-format file:
- Identify requirements (style guide, team preferences, language)
- Select closest template from
assets/configs/ - Copy template to project root as
.clang-format - Test formatting:
clang-format --dry-run file.cpp - Customize specific options using references/01-09.md as needed
- Verify changes:
clang-format file.cpp | diff - file.cpp
Example:
# Copy Google C++ template
cp assets/configs/google-cpp-modified.clang-format /path/to/project/.clang-format
# Test on sample file
clang-format --dry-run /path/to/project/src/main.cpp
# Apply if satisfied
clang-format -i /path/to/project/src/*.cpp
Analyzing Existing Code Style
To generate configuration matching existing code:
- Examine code samples for formatting patterns
- Identify key characteristics:
- Brace placement → consult
references/03-braces.md - Indentation (spaces/tabs, width) → consult
references/04-indentation.md - Spacing (operators, keywords) → consult
references/05-spacing.md - Line breaking (column limit, wrapping) → consult
references/02-breaking.md - Alignment patterns → consult
references/01-alignment.md
- Brace placement → consult
- Map patterns to closest base style in
references/quick-reference.md - Start with that template from
assets/configs/ - Override specific options to match observed patterns
- Test on representative code samples
- Iterate until formatting matches existing style
This workflow minimizes whitespace-only changes when introducing clang-format to existing projects.
Setting Up Editor Integration
To enable format-on-save in editors:
Vim:
- Copy
assets/integrations/vimrc-clang-format.vimcontent to.vimrc - Restart Vim or source configuration
- Save any C/C++/Java file to trigger formatting
Emacs:
- Copy
assets/integrations/emacs-clang-format.elto Emacs config - Restart Emacs or evaluate configuration
- Save any supported file to trigger formatting
Other editors: Consult references/cli-usage.md for VS Code, CLion, and other editor setup instructions.
Setting Up Git Pre-commit Hook
To automatically format staged changes before commit:
- Copy
assets/integrations/pre-committo.git/hooks/pre-commit - Make executable:
chmod +x .git/hooks/pre-commit - Test by staging and committing changes
The hook formats only staged files, preserving unstaged changes.
Troubleshooting Formatting Issues
When formatting produces unexpected results:
- Verify configuration detection:
clang-format --dump-config file.cpp - Check command options in
references/cli-usage.md - Identify affected formatting category (braces, spacing, breaking, etc.)
- Consult relevant category guide in references/01-09.md
- Test isolated options: create minimal config with suspect option
- For comprehensive option details, check
references/complete/clang-format-style-options.md
Setting Up CI/CD Formatting Checks
To enforce formatting in continuous integration:
- Review CI examples in
references/cli-usage.md - Add clang-format check to pipeline:
# Check formatting without modifying files clang-format --dry-run --Werror src/**/*.{cpp,h} - Configure to fail build on formatting violations
- Document formatting requirements for contributors
Key Concepts
Base Styles: Predefined configurations (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU) provide starting points. Set with BasedOnStyle: Google then override specific options.
Multi-Language Support: Configure different languages separately in single file using Language: key. See assets/configs/multi-language.clang-format for example.
Penalty System: clang-format uses penalties to choose between formatting alternatives. Higher penalty values discourage specific choices. See references/09-advanced.md for details.
Progressive Refinement: Start with template closest to requirements, then customize incrementally. Test frequently on representative code samples.
Testing Configurations
# Preview changes without modifying file
clang-format --dry-run file.cpp
# Show diff of proposed changes
clang-format file.cpp | diff - file.cpp
# Apply formatting to file
clang-format -i file.cpp
# Format entire project
find src include -name '*.cpp' -o -name '*.h' | xargs clang-format -i
# Check formatting in CI (fail on violations)
clang-format --dry-run --Werror src/**/*.{cpp,h}
Navigation Strategy
For most tasks, follow this progression:
- Start with templates: Browse
assets/configs/for ready-to-use configurations - Quick reference: Check
references/quick-reference.mdfor complete configurations with explanations - Category guides: Consult
references/01-09.mdfor specific option categories - CLI usage: Reference
references/cli-usage.mdfor command-line and integration details - Complete reference: Use
references/complete/for exhaustive option documentation
When analyzing code or troubleshooting, identify the formatting aspect (braces, spacing, alignment, etc.) and jump directly to the relevant category guide in references/.
快速安装
/plugin add https://github.com/Jamie-BitFlight/claude_skills/tree/main/clang-format在 Claude Code 中复制并粘贴此命令以安装该技能
GitHub 仓库
相关推荐技能
analyzing-dependencies
元这个Claude Skill能自动分析项目依赖的安全漏洞、过时包和许可证合规问题。它支持npm、pip、composer、gem和go modules等多种包管理器,帮助开发者识别潜在风险。当您需要检查依赖安全性、更新过时包或确保许可证兼容时,可使用"check dependencies"等触发短语来调用。
work-execution-principles
其他这个Claude Skill为开发者提供了一套通用的工作执行原则,涵盖任务分解、范围确定、测试策略和依赖管理。它确保开发活动中的一致质量标准,适用于代码审查、工作规划和架构决策等场景。该技能与所有编程语言和框架兼容,帮助开发者系统化地组织代码结构和定义工作边界。
Git Commit Helper
元Git Commit Helper能通过分析git diff自动生成规范的提交信息,适用于开发者编写提交消息或审查暂存区变更时。它能识别代码变更类型并自动匹配Conventional Commits规范,提供包含功能类型、作用域和描述的标准化消息。开发者只需提供git diff内容即可获得即用型的提交消息建议。
algorithmic-art
元该Skill使用p5.js创建包含种子随机性和交互参数探索的算法艺术,适用于生成艺术、流场或粒子系统等需求。它能自动生成算法哲学文档(.md)和对应的交互式艺术代码(.html/.js),确保作品原创性避免侵权。开发者可通过定义计算美学理念快速获得可交互的艺术实现方案。
