【问题标题】:Method under use jumps to next line instead of jumping the parameters使用中的方法跳转到下一行而不是跳转参数
【发布时间】:2020-07-05 18:59:25
【问题描述】:

在这种情况下,我无法专门配置我的 clang 格式文件。这可能是一个愚蠢的问题,但我尝试了几种组合,但我无法设置它。

我有什么:

  bool res = MethodName(<ParameterList>);
  res      = res && AdtVec_Equal(<ParameterList>);
  res      = res && AdtVec_Equal(<ParameterList>);
  res      = res &&
        AdtVec_Equal(<ParameterList>); //same num of parameters, longer names

我想要什么:

  bool res = MethodName(<ParameterList>);
  res      = res && AdtVec_Equal(<ParameterList>);
  res      = res && AdtVec_Equal(<ParameterList>);
  res      = res && AdtVec_Equal(<ParamA>, <ParamB>,
                                 <ParamC>);

我认为这是由于 columnLimit 值而发生的,但我不想将其设置得更长。有什么想法吗?

我的 .clang 格式文件如下:

BasedOnStyle: LLVM

AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignConsecutiveMacros: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  BeforeElse: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Stroustrup
BreakBeforeTernaryOperators: false
BreakInheritanceList: AfterColon
ColumnLimit: 80
ContinuationIndentWidth: 8
IncludeBlocks: Regroup
IndentCaseLabels: true
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: false
PenaltyReturnTypeOnItsOwnLine: 100
PointerAlignment: Left
ReflowComments: false
SortIncludes: true
SpacesBeforeTrailingComments: 2
Standard: Auto

提前致谢。

【问题讨论】:

    标签: llvm-clang clang-format


    【解决方案1】:

    简短回答:我看不出有任何合理的方法可以避免这种情况。

    clang-format 的确切行为取决于参数的长度,以及函数调用是单独调用还是表达式的一部分。为了查看差异,我创建了一个示例输入文件:

    int f()
    {
      bool res = MethodName(a, b, c, d);
      res      = res && AdtVec_Equal(a, b, c, d);
      res      = res && AdtVec_Equal(a, b, c, d);
      res      = res && AdtVec_Equal(aLooooooooong, bLooooooong, cLoooooooong, dLoooooong);
      res      = res && AdtVec_Equal(aLooooooooonger, bLooooooonger, cLoooooooonger, dLoooooonger);
    
      AdtVec_Equal(a, b, c, d);
      AdtVec_Equal(aLooooooooooooong, bLooooooooooong, cLoooooooooooong, dLoooooooooong);
    }
    

    你的.clang-format 设置然后产生这个:

    int f()
    {
      bool res = MethodName(a, b, c, d);
      res      = res && AdtVec_Equal(a, b, c, d);
      res      = res && AdtVec_Equal(a1, b1, c1, d1);
      res      = res &&
            AdtVec_Equal(aLooooooooong, bLooooooong, cLoooooooong, dLoooooong);
      res = res && AdtVec_Equal(aLooooooooonger, bLooooooonger, cLoooooooonger,
                                dLoooooonger);
    
      AdtVec_Equal(a, b, c, d);
      AdtVec_Equal(aLooooooooooooong, bLooooooooooong, cLoooooooooooong,
                   dLoooooooooong);
    }
    

    请注意,如果参数真的很长,您可以按照自己喜欢的方式获取参数(AdtVec_Equalres 在同一行,参数会中断并在第一个参数)。但是有一个窗口,其中参数足够长以防止所有内容都在一行中,但又足够短以允许 AdtVec_Equal 调用适合一行,并且在此窗口内,clang-format 真的想将AdtVec_Equal 调用完全放在一行上。

    可能的解决方法:

    1. 您可能不喜欢它,但您可以将PenaltyExcessCharacter 设置为非常小的值,例如10。这导致了这个输出:

      int f()
      {
        bool res = MethodName(a, b, c, d);
        res      = res && AdtVec_Equal(a, b, c, d);
        res      = res && AdtVec_Equal(a1, b1, c1, d1);
        res = res && AdtVec_Equal(aLooooooooong, bLooooooong, cLoooooooong, dLoooooong);
        res = res && AdtVec_Equal(aLooooooooonger, bLooooooonger, cLoooooooonger,
                                  dLoooooonger);
      
        AdtVec_Equal(a, b, c, d);
        AdtVec_Equal(aLooooooooooooong, bLooooooooooong, cLoooooooooooong,
                     dLoooooooooong);
      }
      

      这可以避免在与res 不同的行上调用AdtVec_Equal。但要实现这一点,它会错误地对齐等号,并将行扩展到 80 个字符之外。它也可能会弄乱代码的其他部分。

    2. 您可以按照自己喜欢的方式手动格式化代码,然后用// clang-format off// clang-format on 包围代码。


    我使用clang-format 6.0.0 进行了测试,还使用configurator 测试了10.0.0。所以这种行为已经存在了一段时间,如果没有人创建一个新的样式选项并提交一个补丁来实现它,它可能不会改变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 2023-03-13
      • 2018-06-21
      • 2016-10-08
      相关资源
      最近更新 更多