ang格式换行符 [英] Clang-format line breaks

查看:236
本文介绍了ang格式换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 clang-format 设置,以防止该工具删除换行符。



例如,我将 ColumnLimit 设置为120,这里是重新格式化一些示例代码时会发生什么。



之前:

  #include< vector> 
#include< string>

std :: vector< std :: string> get_vec()
{
return std :: vector< std :: string> {
这是一个测试,
一些行更长,
比其他,但我想要,

};
}

int main()
{
auto vec = get_vec();
}

之后:

  #include< vector> 
#include< string>

std :: vector< std :: string> get_vec()
{
return std :: vector< std :: string> {这是一个测试,一些行更长,比其他,但我想
保持在不同的行};
}

int main()
{
auto vec = get_vec();
}

我想要的是工具打破超过120个字符的行,但不决定合并线只是因为它们小于120个字符。



有没有这样的选择?

解决方案

因此,在解析clang格式代码并创建了一些补丁后,我的两美分:




  • Clang格式基于


  • 使用 libclang 解析AST,这基本上消除了所有空格

  • 将令牌序列分解为unwrapped

  • 应用规则/配置信息有时会将打开的行拆分成更小的单位




这是不容易使它尊重原来的whitepsace,那种被扔的时候


  • 您可以通过

    最轻松地控制换行符的位置。


    • 使用bin pack parameters选项设置列限制


    • -

    • 在行尾添加注释(clang格式无法删除)


  • / ul>

    这里有一件事你可以尝试:

      std :: vector< std :: string> get_vec()
    {
    return std :: vector< std :: string> {//
    这是一个测试,//
    一些行更长,//
    比其他,但我想,//
    将它们保持在单独的行//
    };
    }

    这个优点超过 // clang-format关闭是,如果以后更改选项卡宽度或一些其他选项,那些代码行仍将获得这些格式更改,所以您不需要手动进入 / / clang-format off 地区来修复它。



    最后, clang-format 是非常关于强加一个统一格式在整个代码库,确保所有的字符串文字格式化为相同的样式在程序中的任何地方。如果你想对断线的决定进行微观层面的控制,这不是真正的工具精神,你必须做的事情,如禁用它。



    这有时是令人沮丧的esp。当你想使用数组做事情并且列对齐或者某些东西 - 例如,这里有一些来自lua C api的自然代码:

      static luaL_Reg const methods [] = {
    {matches,& dispatch :: intf_match_unit},
    {to_recall,& dispatch :: intf_put_recall_unit},
    { to_map,& dispatch :: intf_put_unit},
    {erase,& dispatch :: intf_erase_unit},
    {clone,intf_copy_unit},
    {extract ; dispatch :: intf_extract_unit},
    {advance,intf_advance_unit},
    };

    当clang格式运行时,它通常不会对齐右列,



    或者,如果你有4 x 4矩阵与OpenGL一起使用:

      constexpr float shadow_skew_hardcoded [16] = 
    {1.0f,0.0f,0.0f,0.0f,
    0.5f,0.5f,0.0f,0.0f,
    0.0f,0.0f,1.0f,0.0f,
    0.0f,0.0f,0.0f,1.0f}

    如果你让clang-format运行在这样的东西,它只是去它们,afaik没有简单的方法使它们格式化他们很好,所以你只需要诉诸于大量的琐碎的评论黑客,或使用clang格式关闭当你有这样的东西。这些只是工具的内在限制。如果你不快乐,不得不这样做,那么这可能不是你的工具。


    I'm looking for a clang-format setting to prevent the tool from removing line breaks.

    For example, I have my ColumnLimit set to 120, and here's what happens when I reformat some sample code.

    Before:

    #include <vector>
    #include <string>
    
    std::vector<std::string> get_vec()
    {
       return std::vector<std::string> {
          "this is a test",
          "some of the lines are longer",
          "than other, but I would like",
          "to keep them on separate lines"
       };
    }
    
    int main()
    {
       auto vec = get_vec();
    }
    

    After:

    #include <vector>
    #include <string>
    
    std::vector<std::string> get_vec()
    {
       return std::vector<std::string>{"this is a test", "some of the lines are longer", "than other, but I would like",
             "to keep them on separate lines"};
    }
    
    int main()
    {
       auto vec = get_vec();
    }
    

    What I would like is that the tool breaks lines that are over 120 characters, but doesn't decide to combine lines just because they are less than 120 characters.

    Is there such an option? Nothing in the docs stood out to me.

    解决方案

    So, having messed around in the clang format code and made some patches, here's my two cents:

    • Clang format is based on,

      • parsing the AST using libclang, which basically eliminates all whitespace
      • breaking up the token sequence into "unwrapped lines" which are like "logical" code lines
      • Applying rules / configuration info to sometimes split up "unwrapped lines" into smaller units
      • Spit it all back out again with new whitespace / indentation

      It's not easy to make it respect the original whitepsace, that sort of gets tossed when you first parse the code.

    • You can control where it places line breaks, most easily, by

      • setting the column limit
      • using the "bin pack parameters" options
      • setting penalties for various kinds of breaks -- break after return type of a function, break before first call parameter, break a string literal, break a comment...
      • placing comments at the end of a line (clang format cannot remove the comment and must therefore split the line)
      • use the clang-format off / on directives

    Here's one thing you could try:

    std::vector<std::string> get_vec()
    {
       return std::vector<std::string> {   //
          "this is a test",                //
          "some of the lines are longer",  //
          "than other, but I would like",  //
          "to keep them on separate lines" //
       };
    }
    

    The advantage of this over // clang-format off is that, if you later change the tab width or some other option, those code lines will still get those formatting changes so you don't need to manually go into the // clang-format off regions to fix it. However it's still a bit of a hack, YMMV.

    Ultimately, clang-format is very much about imposing a uniform format over an entire code base, making sure that all string literals are formatted in the same style everywhere in your program. If you want to have micro-level control over line-break decisions, that's not really in the spirit of the tool, and you'll have to do things like disable it.

    This can sometimes be frustrating esp. when you want to do things with arrays and have columns aligned or something -- for instance, here's some natural code from lua C api:

    static luaL_Reg const methods[] = {
        {"matches",               &dispatch::intf_match_unit},
        {"to_recall",             &dispatch::intf_put_recall_unit},
        {"to_map",                &dispatch::intf_put_unit},
        {"erase",                 &dispatch::intf_erase_unit},
        {"clone",                 intf_copy_unit},
        {"extract",               &dispatch::intf_extract_unit},
        {"advance",               intf_advance_unit},
    };
    

    When clang-format runs over that, it's generally not going to align the right column, its going to place it a fixed number of spaces after the commas and there's not much you can do about it afaik.

    Or, if you have 4 x 4 matrix for use with OpenGL:

          constexpr float shadow_skew_hardcoded[16] =
            { 1.0f, 0.0f, 0.0f, 0.0f,
              0.5f, 0.5f, 0.0f, 0.0f,
              0.0f, 0.0f, 1.0f, 0.0f,
              0.0f, 0.0f, 0.0f, 1.0f };
    

    If you let clang-format run over things like this it's just going to mangle them, and afaik there's no easy way to make it format them nicely, so you just have to resort either to the "lots of trivial comments" hack, or use clang-format off when you have something like this. These are just intrinsic limitations of the tool. If you aren't happy ever to have to do things like that then it's probably not the tool for you.

    这篇关于ang格式换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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