2015-10-12 47 views
2

什麼鐺格式使得:防止鐺格式從縮進lambda表達式

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(); 
}); 

有沒有辦法可以讓鐺格式不縮進拉姆達機構?在文檔中找不到任何關於它的內容。

這是我到目前爲止有:

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

的版本上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有lambda表達式沒有特殊的成員。