code_fodder
code_fodder

Reputation: 16401

clang format line break in the wrong place - part 2

This might be related to this: clang format line break in the wrong place - but the line break is in a different place/different reason

We have a line limit of 120 chars so clang format turns this (which is what I want) - just over 120 chars:

    void print_table(
        std::ofstream &ss,
        const std::string &title,
        const std::set<part_reporter_pair> &version_set) const;

into this:

    void // <--- return type on newline :(
    print_table(std::ofstream &ss, const std::string &title, const std::set<part_reporter_pair> &version_set) const;

But if I make the line slightly longer (so even without the return type its over 120 chars) it formats it correctly:

    void print_table(
        std::ofstream &output_ss, // <---- made this var longer
        const std::string &title,
        const std::set<part_reporter_pair> &version_set) const;

I really don't want the return type on its own - don't like that format, I want to break the variables out as a higher priority. It might not be possible, but this is the dream!

clang-format version 10.0.0-4ubuntu1~18.04.2

.clang-format contents:

---
Language:        Cpp
# BasedOnStyle:  WebKit
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands:   false
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
# Don't allow function on one line, e.g. 'int f() {return 1;}' - unless its inline
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
  AfterCaseLabel:  true
  AfterClass:      true
  AfterControlStatement: true
  AfterEnum:       true
  AfterFunction:   true
  AfterNamespace:  true
  AfterObjCDeclaration: true
  AfterStruct:     true
  AfterUnion:      true
  AfterExternBlock: true
  BeforeCatch:     true
  BeforeElse:      true
  IndentBraces:    true
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
# have line breaks after operators: 'if (a ||' instead of on the next line ' || b'
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
# effectively the line length limit before clang-format starts to use multiple lines
ColumnLimit:     120
CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
# format braces lists (uniform init) like a function param list
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
  - foreach
  - Q_FOREACH
  - BOOST_FOREACH
IncludeBlocks:   Preserve
IncludeCategories:
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
Priority:        2
SortPriority:    0
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
Priority:        3
SortPriority:    0
  - Regex:           '.*'
Priority:        1
SortPriority:    0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: true
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth:     4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
# 'int *p' instead of 'int* p;'
PointerAlignment: Right
ReflowComments:  true
SortIncludes:    true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard:        Latest
StatementMacros:
  - Q_UNUSED
  - QT_REQUIRE_VERSION
TabWidth:        8
UseCRLF:         false
UseTab:          Never
...

Upvotes: 1

Views: 642

Answers (2)

a1ezh
a1ezh

Reputation: 422

Two parameters need to be configured:

  1. PenaltyReturnTypeOnItsOwnLine: 9000. It should be rather high (more then the second). It may be 9000 like @anton-tokar have suggested. Official description.
  2. PenaltyBreakOpenParenthesis: 1. It allows to break early before the first argument. Official description.

With thease changes you should get this result:

    void print_table(
        std::ofstream &ss,
        const std::string &title,
        const std::set<part_reporter_pair> &version_set) const;

Upvotes: 0

Anton Tokar
Anton Tokar

Reputation: 26

You can use PenaltyReturnTypeOnItsOwnLine option with higher value to affect this behavior, making such situations less likely.

PenaltyReturnTypeOnItsOwnLine: 9000

In your case, though, it places const-qualifier in a separate line. AFAIK, this is not possible to fix now.

    void print_table(std::ofstream& ss, const std::string& title, const std::set<part_reporter_pair>& version_set)
        const;

Upvotes: 1

Related Questions