c++ - 有没有办法使用 clang-format 来做 "indentation only"?

标签 c++ c automation clang-format

  • 我有一个大型代码库,其中的缩进非常不一致。
  • 我终于从开发人员那里得到了运行代码格式化程序的一些支持。
  • 我想从修复缩进开始,因为缩进比使用样式运行 clang-format 更轻松。
    • 我喜欢纠正缩进只是因为当你只做缩进然后运行 ​​git diff --ignore-space-at-eol --ignore-space-change --ignore-all-space你得到零差异线。

我想避免格式化代码变得更糟的情况,这样人们就可以避免在未来尝试通过 clang-format 之类的东西来改进我们的代码库。在我们的例子中,至少我们可以就纯空格达成一致,制表符是 4 个空格。所以只改进缩进只能是一件好事。

Eclipse 具有“纠正缩进”的功能(通过菜单 --> 源代码 --> 纠正缩进):

  • 正确的缩进
  • 将简单的缩进规则应用于当前选择,或者如果没有选择则包含光标的行
  • Ctrl+I

Eclipse 的“正确缩进”只是缩进,但它不是 shell 命令,我想要/需要一个 shell 命令,以便我可以在所有源代码文件上运行该命令。

有没有办法使用 clang-format 来“仅缩进”?如果是,如何?

例如只有空格,4 个空格。

最佳答案

Clang-format 始终使用默认格式。您可以自定义它。如果您不指定样式,则会选择 clang-format 默认值。 [1],[2]

不幸的是,您不一定只修复缩进。

在对您的问题的评论中,KamilCuk 建议使用 indent 可能指的是 https://www.gnu.org/software/indent/

我考虑过配置一个只做缩进的自定义样式,但是不幸的是,在检查样式选项时,有一些可能会改变代码库,具体取决于它的外观,比如 AllowShortIfStatementsOnASingleLine 这不允许共存

if (a)
  return ;
else {
  return;
}
if (b) return ;
else {
  return;
}

因此,您可能会找到适用于您的代码库的特定配置,但这将非常具体且脆弱。

[1]

The configuration file can consist of several sections each having different Language: parameter denoting the programming language this section of the configuration is targeted at. See the description of the Language option below for the list of supported languages. The first section may have no language set, it will set the default style options for all lanugages. Configuration sections for specific language will override options set in the default section.

https://clang.llvm.org/docs/ClangFormatStyleOptions.html#configuring-style-with-clang-format

[2]

This section lists the supported style options. Value type is specified for each option. For enumeration types possible values are specified both as a C++ enumeration member (with a prefix, e.g. LS_Auto), and as a value usable in the configuration (without a prefix: Auto).

BasedOnStyle (string) The style used for all options not specifically set in the configuration.

https://clang.llvm.org/docs/ClangFormatStyleOptions.html#configurable-format-style-options

关于c++ - 有没有办法使用 clang-format 来做 "indentation only"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64805558/

相关文章:

c++ - 解析文本文件而不存储(tftp)

c - 使用可移植 int

c# - Selenium - 强制点击不可见元素

java - 如何使用 Cucumber runner 加载 Spring 应用程序上下文

c++ - 用 Chudnovsky 算法计算 Pi 数

c++ - 默认虚拟驱动器

c++ - 我可以从静态 const char* 数组定义以下宏 "unstringifying"吗?

c - 地址运算符和变量标记连接宏

javascript - 在桌面应用程序上调用 AWS Lambda

c++ - 是否可以定义另一个预处理器指令?