如何让 clang 在格式化过程中忽略缩进?

问题描述 投票:0回答:1

我的设备上正确安装了 clang-format。问题是每当我在编辑器中格式化时,我总是得到这个:

int main() {

  printf("Hello world!");

  for (int i = 0; i < 5; i++) {
    printf("%d", i);
  }

  return 0;
}

我的预期输出是这样的:

int main() {

    printf("Hello world!");

    for (int i = 0; i < 5; i++) {
        printf("%d", i);
    }

    return 0;

}

如您所见,在我的预期输出中,我希望它有一个缩进(每当我制作花括号时),但是在我得到的输出中,缩进减少到每级两个空格。

我尝试在 YouTube 上寻找解决方案,但找不到。

c clang clang-format
1个回答
0
投票

clang-format 中的默认缩进可能设置为 2 个空格,这就是您在格式化代码中看到的原因。如果您喜欢 4 个缩进空间,则需要更改 .clang-format 文件中的 IndentWidth 设置。

这是一个将缩进宽度设置为 4 个空格的示例配置:

BasedOnStyle: LLVM
IndentWidth: 4

这个工具派上用场:https://zed0.co.uk/clang-format-configurator/

© www.soinside.com 2019 - 2024. All rights reserved.