From 91bff90bf2f00622bc8054c8bbabc9ae9c362345 Mon Sep 17 00:00:00 2001 From: Peter Vingelmann Date: Wed, 14 Sep 2016 03:46:07 +0200 Subject: [PATCH] Attach colon to constructor header and break line before member initializers --- src/ASFormatter.cpp | 105 ++++++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 47 deletions(-) diff --git a/src/ASFormatter.cpp b/src/ASFormatter.cpp index b0fd42e..2a58da2 100644 --- a/src/ASFormatter.cpp +++ b/src/ASFormatter.cpp @@ -1209,6 +1209,64 @@ string ASFormatter::nextLine() } } // (isPotentialHeader && !isInTemplate) + if (currentChar == ':' + && previousChar != ':' // not part of '::' + && peekNextChar() != ':') // not part of '::' + { + if (isInCase) + { + isInCase = false; + if (shouldBreakOneLineStatements) + passedColon = true; + } + else if (isCStyle() // for C/C++ only + && isOkToBreakBlock(bracketTypeStack->back()) + && shouldBreakOneLineStatements + && !foundQuestionMark // not in a ?: sequence + && !foundPreDefinitionHeader // not in a definition block (e.g. class foo : public bar + && previousCommandChar != ')' // not immediately after closing paren of a method header, e.g. ASFormatter::ASFormatter(...) : ASBeautifier(...) + && !foundPreCommandHeader // not after a 'noexcept' + && !squareBracketCount // not in objC method call + && !isInObjCMethodDefinition // not objC '-' or '+' method + && !isInObjCInterface // not objC @interface + && !isInObjCSelector // not objC @selector + && !isDigit(peekNextChar()) // not a bit field + && !isInEnum // not an enum with a base type + && !isInAsm // not in extended assembler + && !isInAsmOneLine // not in extended assembler + && !isInAsmBlock) // not in extended assembler + { + passedColon = true; + } + + if (isCStyle() + && shouldPadMethodColon + && (squareBracketCount > 0 || isInObjCMethodDefinition || isInObjCSelector) + && !foundQuestionMark) // not in a ?: sequence + padObjCMethodColon(); + + if (isInObjCInterface) + { + appendSpacePad(); + if ((int) currentLine.length() > charNum + 1 && !isWhiteSpace(currentLine[charNum + 1])) + currentLine.insert(charNum + 1, " "); + } + + if (isClassInitializer()) + { + isInClassInitializer = true; + if (!isCharImmediatelyPostComment && !isCharImmediatelyPostLineComment) + { + // Attach colon to the constructor header and force a + // new line before the member initializer list + appendSpacePad(); + appendCurrentChar(false); + shouldBreakLineAtNextChar = true; + continue; + } + } + } + if (isInLineBreak) // OK to break line here { breakLine(); @@ -1255,53 +1313,6 @@ string ASFormatter::nextLine() resetEndOfStatement(); } - if (currentChar == ':' - && previousChar != ':' // not part of '::' - && peekNextChar() != ':') // not part of '::' - { - if (isInCase) - { - isInCase = false; - if (shouldBreakOneLineStatements) - passedColon = true; - } - else if (isCStyle() // for C/C++ only - && isOkToBreakBlock(bracketTypeStack->back()) - && shouldBreakOneLineStatements - && !foundQuestionMark // not in a ?: sequence - && !foundPreDefinitionHeader // not in a definition block (e.g. class foo : public bar - && previousCommandChar != ')' // not immediately after closing paren of a method header, e.g. ASFormatter::ASFormatter(...) : ASBeautifier(...) - && !foundPreCommandHeader // not after a 'noexcept' - && !squareBracketCount // not in objC method call - && !isInObjCMethodDefinition // not objC '-' or '+' method - && !isInObjCInterface // not objC @interface - && !isInObjCSelector // not objC @selector - && !isDigit(peekNextChar()) // not a bit field - && !isInEnum // not an enum with a base type - && !isInAsm // not in extended assembler - && !isInAsmOneLine // not in extended assembler - && !isInAsmBlock) // not in extended assembler - { - passedColon = true; - } - - if (isCStyle() - && shouldPadMethodColon - && (squareBracketCount > 0 || isInObjCMethodDefinition || isInObjCSelector) - && !foundQuestionMark) // not in a ?: sequence - padObjCMethodColon(); - - if (isInObjCInterface) - { - appendSpacePad(); - if ((int) currentLine.length() > charNum + 1 && !isWhiteSpace(currentLine[charNum + 1])) - currentLine.insert(charNum + 1, " "); - } - - if (isClassInitializer()) - isInClassInitializer = true; - } - if (currentChar == '?') foundQuestionMark = true;