防止clang格式缩进lambdas

时间:2015-10-12 21:39:03

标签: clang-format

铿锵格式:

 QObject::connect(&w, &sap::Window::keyPress, [&w](auto* event)
                 {
                     if(event->key() == Qt::Key_Escape)
                         w.close();
                 });

我想要的是什么:

QObject::connect(&w, &sap::Window::keyPress, [&w](auto* event)
{
    if(event->key() == Qt::Key_Escape)
    w.close();
});

有没有办法让clang-format不缩进lambda体?在文档中找不到任何相关内容。

这是我到目前为止所做的:

BasedOnStyle: LLVM,
BreakBeforeBraces: Allman,
NamespaceIndentation: All,
SpaceBeforeParens: Never,
AccessModifierOffset: -4,

AllowShortIfStatementsOnASingleLine: false,
AllowShortBlocksOnASingleLine: false,
AllowShortFunctionsOnASingleLine: None,
AllowShortCaseLabelsOnASingleLine: false,
AllowShortLoopsOnASingleLine: false,

ColumnLimit: 100,
AlwaysBreakTemplateDeclarations: true,
PenaltyReturnTypeOnItsOwnLine: 9999,
IndentWidth: 4,
PointerAlignment: Left

1 个答案:

答案 0 :(得分:1)

您使用clang-format上的哪个版本?

最新版本(v3.9.0或v 3.8.0)的默认配置几乎可以满足您的需求:

QObject::connect(&w, &sap::Window::keyPress, [&w](auto *event) {
  if (event->key() == Qt::Key_Escape)
  w.close();
});

您可以在线试用:http://zed0.co.uk/clang-format-configurator/

但是对于更长的参数包,默认配置返回:

QObject::connect(sender, &sap::ClassName::signalName, receiver,
                 &sap::OtherClass::slotFunc,
                 [this](auto dummy, const auto* event) {
                     if (event->key() == Qt::Key_Escape)
                         doStuff();
                 });

.clang-format这样:

BasedOnStyle: LLVM
IndentWidth:  4
ColumnLimit:  80
Language:     Cpp

AlignAfterOpenBracket: AlwaysBreak
BinPackArguments:      false
BinPackParameters:     false

PointerAlignment:      Left

你会得到:

QObject::connect(&w, &sap::Window::keyPress, [&w](auto* event) {
    if (event->key() == Qt::Key_Escape)
        w.close();
});

QObject::connect(
    sender,
    &sap::ClassName::signalName,
    receiver,
    &sap::OtherClass::slotFunc,
    [this](auto dummy, const auto* event) {
        if (event->key() == Qt::Key_Escape)
            doStuff();
    });

目前,BraceWrapping没有lambdas的特殊成员。