【问题标题】:Is align assignments in clang format broken?clang 格式的对齐作业是否损坏?
【发布时间】:2016-03-19 11:50:51
【问题描述】:

以下是我的clang格式

---
AccessModifierOffset: '-4'
AlignConsecutiveAssignments: 'true'
AlignOperands: 'true'
AlignTrailingComments: 'true'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortIfStatementsOnASingleLine: 'true'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakTemplateDeclarations: 'true'
BinPackArguments: 'true'
BinPackParameters: 'true'
BreakBeforeBraces: Allman
BreakConstructorInitializersBeforeComma: 'true'
ColumnLimit: '80'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
Cpp11BracedListStyle: 'true'
IndentCaseLabels: 'false'
IndentWidth: '4'
MaxEmptyLinesToKeep: '2'
NamespaceIndentation: All
PointerAlignment: Left
SpaceAfterCStyleCast: 'true'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpacesBeforeTrailingComments: '1'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
Standard: Auto
TabWidth: '4'
UseTab: Always

...

但是当我在 c++ 文件中运行它时,我得到如下结果(代码是胡言乱语的复制粘贴,尽管未对齐分配的问题区域是我在代码中看到的被破坏的内容的逐字副本)

template <class X>
void prettyPrint(std::ostream& o, const X* x)
{
    o << "*{";
    if (x)
    {
        prettyPrint(o, *x);
    }
    else
    {
        o << "NULL";
    }
    // I wanted the following assignments to align !!!!
    using value_type           = std::decay_t<decltype(state)>;
    using difference_type   = std::ptrdiff_t;
    using reference         = value_type&;
    using pointer             = value_type*;
    using iterator_category = std::input_iterator_tag;

    o << "}";
}

设置好了

AlignConsecutiveAssignments: 'true'

我发现上述行为是错误的,我的 .clang-format 的其余部分是否有什么东西弄乱了结果,还是我应该将此报告为错误?

【问题讨论】:

    标签: c++ clang-format


    【解决方案1】:

    当您需要填充至少从一个制表位到下一个制表位的空白时,使用UseTab: Always 会使用制表符。但是,这些制表符不会对齐,因为它们前面的语句长度不同,并且会打印出到不同制表位的制表符。

    一个合适的替代方法是使用UseTab: ForIndentation,顾名思义,它仅使用制表符进行缩进。

    甚至是UseTab: Never,它从不使用标签。

    【讨论】:

      【解决方案2】:

      我可以通过删除您发布的 .clang-format sn-p 中的 UseTab: Always 行来按照您想要的方式进行格式化:

      template <class X>
      void prettyPrint(std::ostream& o, const X* x)
      {
          o << "*{";
          if (x)
          {
              prettyPrint(o, *x);
          }
          else
          {
              o << "NULL";
          }
          // I wanted the following assignments to align !!!!
          using value_type        = std::decay_t<decltype(state)>;
          using difference_type   = std::ptrdiff_t;
          using reference         = value_type&;
          using pointer           = value_type*;
          using iterator_category = std::input_iterator_tag;
      
          o << "}";
      }
      

      至于为什么,我猜不出来……

      编辑:UseTab: ForIndentationNever 也可以,只有 Always 会破坏它

      【讨论】:

        猜你喜欢
        • 2010-12-15
        • 1970-01-01
        • 2016-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        • 2019-02-01
        • 2013-12-02
        相关资源
        最近更新 更多