gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:35:51 27 4
gpt4 key购买 nike

  • 我有一个大型代码库,其中的缩进非常不一致。
  • 我终于从开发人员那里得到了运行代码格式化程序的一些支持。
  • 我想从修复缩进开始,因为缩进比使用样式运行 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com