【问题标题】:What's clang-format's equivalent to rustfmt's indent_style=Block?clang-format 相当于 rustfmt 的 indent_style=Block 是什么?
【发布时间】:2020-09-01 06:00:18
【问题描述】:

如果 clang-format 是 rustfmt,它将有这个包罗万象的 indent_style 配置选项,它指的是缩进样式的基本区别:

视觉(默认为 clang 格式):

ReturnType<std::vector<int>> ClassName::functionName(int a,
                                                     bool b,
                                                     float c,
                                                     double d,
                                                     long double complex e);

static const char* names[] = {"a",
                              "b",
                              "c"};

阻止(rustfmt 中的默认设置):

ReturnType<std::vector<int>> ClassName::functionName(
    int a,
    bool b,
    float c,
    double d,
    long double complex e);

static const char* names[] = {
    "a",
    "b",
    "c",
};

如何配置 clang-format 以在所有语法范围内进行块缩进?

或者是否有任何 C++ 格式化程序支持块缩进?

【问题讨论】:

    标签: c++ code-formatting clang-format


    【解决方案1】:

    默认样式设置为 Microsoft 样式(与左括号对齐)。使用BasedOnStyle设置为Google获取块缩进:

    BasedOnStyle: Google
    

    【讨论】:

    • 你确定吗?我能找到的最近的BasedOnStyle 是WebKit,但它不会在第一个元素之前换行。
    • 它在模拟器中工作。它在您的系统上不起作用吗?
    • 哦,它在模拟器中工作!但不仅仅是BasedOnStyle: Google.clang-format 文件中以及我的示例。不过,它会做一些事情,比如将缩进级别从 4 更改为 2。我的 clang 格式版本是 10.0.0。
    【解决方案2】:

    Uncrustify 可以做到这一点,至少就 C++ 必须提供的每个可以想象的语法范围找到相应的选项而言——我没有看到任何整体选项。

    首先:

    nl_func_def_start_multi_line = true
    nl_func_decl_start_multi_line = true
    nl_func_call_start_multi_line = true
    
    nl_after_brace_open=true
    nl_type_brace_init_lst_open = true
    nl_enum_own_lines = add
    
    nl_constr_init_args = add
    pos_constr_colon = lead_break
    pos_constr_comma = lead_break
    

    【讨论】:

      【解决方案3】:
      1. 可以配置什么:

        (包括函数、ctor init 列表和枚举,但不包括列表)

        AlignAfterOpenBracket: AlwaysBreak
        BreakConstructorInitializers: BeforeComma
        BinPackArguments: 'false'
        BinPackParameters: 'false'
        

        BinPack* 选项并不严格用于块缩进, 但要像问题所示那样将一个或所有项目放在同一行。

      2. 对于列表,clang-format 在使用后缀逗号的地方使用块缩进,在使用中缀逗号的地方使用视觉缩进:

        static const char* postfixCommaDelimited[] = {
            "a",
            "b",
            "c",
        };
        static const char* infixCommaDelimited[] = {"a", "b",
                                                    "c"};
        

        如果所有中缀逗号分隔的列表都是单行的,我认为这是有道理的, 但我希望 clang-format 可以做些什么 多行列表。 Rustfmt 将它们重写为后缀逗号,这在 C++ 中我也很喜欢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-21
        • 1970-01-01
        相关资源
        最近更新 更多