You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过clang-format配置仅缩进case标签后的大括号(保持case标签不缩进)

How to Configure clang-format for Case Labels with Indented Braces (No Label Indent)

Great question! This exact formatting style is absolutely achievable with clang-format—you just need to combine a few specific settings instead of relying solely on IndentCaseLabels. Here's how to set it up:

Required Configuration

Add these settings to your .clang-format file (adjust IndentWidth to match your preferred indent size):

---
Language: Cpp
IndentWidth: 4
IndentCaseLabels: false
BreakBeforeBraces: Custom
BraceWrapping:
  AfterCaseLabel: true
IndentCaseBlocks: true

What Each Setting Does

  • IndentCaseLabels: false: Ensures case labels stay aligned with the switch statement (no extra indentation on the label itself).
  • BraceWrapping: AfterCaseLabel: true: Forces the opening brace after a case label to start on a new line, instead of being inline with case X:.
  • IndentCaseBlocks: true: Indents the brace block under the case label by one level, while keeping subsequent statements like break aligned with the case label.
  • IndentWidth: 4: Sets the number of spaces for each indent (adjust to 2 if you prefer smaller indents).

Resulting Formatted Code

With these settings, your code will be formatted exactly as you requested:

switch(a) {
case 1:
    {
        do_some_thing();
    }
    break;
}

Why Your Initial Attempts Didn't Work

  • Setting IndentCaseLabels: true indents both the case label and all its content, which isn't what you wanted.
  • Setting IndentCaseLabels: false keeps the label unindented but also leaves the brace at the same level as the label—missing the indentation on the brace block that you needed. The combination of BraceWrapping and IndentCaseBlocks fixes this gap.

内容的提问来源于stack exchange,提问作者ZegWe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 17:52:41