有没有一种方法可以使用clang格式执行“仅缩进"? [英] Is there a way to use clang-format to do "indentation only"?

查看:78
本文介绍了有没有一种方法可以使用clang格式执行“仅缩进"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我的缩进代码基很大,非常不一致.
  • 我终于从开发人员那里购买了运行代码格式化程序的文件.
  • 我想首先修复缩进,因为缩进比运行带有样式的clang格式要轻.
    • 我只喜欢校正缩进,因为仅当执行缩进然后运行 git diff --ignore-space-at-eol --ignore-space-change --ignore-all-space 您得到零差异线.
    • I have a large code base with indentation that is very inconsistent.
    • I finally got some buy in from developers for running a code formatter.
    • I would like to start with just fixing the indentation because indentation is a lighter touch than running clang-format with a style.
      • I like correcting indentation only because when you do the indentation only and then run git diff --ignore-space-at-eol --ignore-space-change --ignore-all-space you get zero diff lines.

      我想避免格式化的代码以某种方式变得更糟的情况,因此人们避免了将来通过clang-format之类的方法来使我们的代码库更好的尝试.在我们的情况下,至少我们可以同意仅空格,制表符为4个空格.因此,仅改善缩进只是一件好事.

      I want to avoid a situation where the formatted code is somehow worse and so people avoid any future attempts at making our codebase better via something like clang-format. In our case at least we can agree on spaces-only, tabs are 4-spaces. So improving only the indentation can only be a good thing.

      Eclipse具有校正缩进"的功能.(通过菜单->源->正确缩进):

      Eclipse has a feature to "correct indentation" (via Menu --> Source --> Correct Indentation):

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

      Eclipse的正确缩进";仅执行缩进,但不是shell命令,我想要/需要shell命令,以便可以在所有源代码文件上运行该命令.

      Eclipse's "Correct Indentation" does just the indentation but it is not a shell command and i want/need a shell command so that I can run the command on all the source code files.

      例如只有空格的4个空格.

      For example with space-only, 4-spaces.

      推荐答案

      Clang格式始终使用默认格式.您可以对其进行自定义.如果您不指定样式,则选择clang格式默认值.[1],[2]

      Clang-format always works with a default format. You can just customize it. If you don't specify a style the clang-format default is chosen. [1],[2]

      不幸的是,您不一定必须仅修复缩进.

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

      In the comments to your question KamilCuk suggested to use indent probably referring to https://www.gnu.org/software/indent/

      我考虑过配置仅缩进的自定义样式,但是不幸的是,在遍历样式选项时,有些样式可能会更改代码库,具体取决于代码库的外观,例如 AllowShortIfStatementsOnASingleLine 这不允许

      I thought about configuring a custom style which does only indentation but, while going over the style options there are unfortunately some which might alter the code-base, depending on how it looks, like AllowShortIfStatementsOnASingleLine This disallows the co-existence of

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

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

      So, it might be possible that you find a certain configuration which works for your code-base, but this would be highly specific and brittle.

      [1]

      配置文件可以包含几个部分,每个部分具有不同的Language(语言):参数表示编程语言,此配置部分针对该语言.有关支持的语言列表,请参见下面的语言"选项的描述.第一部分可能没有设置语言,它将为所有语言设置默认样式选项.特定语言的配置部分将覆盖默认部分中设置的选项.

      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]

      本节列出了受支持的样式选项.为每个选项指定值类型.对于枚举类型,可能的值既指定为C ++枚举成员(带有前缀,例如LS_Auto),也指定为可在配置中使用的值(不带有前缀:Auto).

      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(字符串)未在配置中专门设置的所有选项所使用的样式.

      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

      这篇关于有没有一种方法可以使用clang格式执行“仅缩进"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆