clang格式的对齐分配是否已损坏?

时间:2016-03-19 11:50:51

标签: c++ clang-format

以下是我的铿锵表格

---
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其他部分有什么东西搞砸了结果,或者我应该把它报告为错误?

2 个答案:

答案 0 :(得分:2)

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

一个合适的替代方案是使用UseTab: ForIndentation,顾名思义,它仅使用标签进行缩进。

甚至从不使用标签的UseTab: Never

答案 1 :(得分:1)

我能够通过删除您发布的.clang格式代码段中的行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打破