【问题标题】:Is there a way with .clang-format to break before one line function?.clang-format 有没有办法在单行函数之前中断?
【发布时间】:2020-04-04 17:55:47
【问题描述】:

我在documentation 中找不到任何东西,即使BreakBeforeBraces: Allman 格式化了我已经拆分成的单行函数

void foo() { bar(); }

我想要类似的东西

void foo()
{
    bar();
}

我想要这个来组织代码和统一性,因为这是每个多行函数的样子。

你能帮帮我吗?

【问题讨论】:

  • 更新:如果您设置ColumnLimit: '0',它将让您已经格式化的函数单独运行,如果您在此函数的任何位置设置换行符,它将正确格式化它们。这是一个巨大的进步,但我希望它 100% 自动完成......

标签: format clang clang-format


【解决方案1】:

要在单独的行上有一个简短的函数体,请将其添加到 .clang-format 文件中:

AllowShortFunctionsOnASingleLine: Empty

【讨论】:

    【解决方案2】:
    • 是的,您可以这样做。 最简单的方法是设置
    BreakBeforeBraces: Stroustrup
    
    • 其次,您可以通过在SplitEmptyFunction 中设置true 来执行此操作。举个例子
    "BraceWrapping":
            "AfterClass":             false
            "AfterControlStatement":  false
            "AfterEnum":              false
            "AfterFunction":          false
            "AfterNamespace":         false
            "AfterObjCDeclaration":   false
            "AfterStruct":            false
            "AfterUnion":             false  
            "BeforeCatch":            false
            "BeforeElse":             false
            "IndentBraces":           false
            "SplitEmptyFunction":     true <-- set this as true
            "SplitEmptyRecord":       true
            "SplitEmptyNamespace":    true
    
    

    如果是true输出将会是

    void foo()
    {
        bar();
    }
    

    但如果是 false 输出将是

    void foo(){
        bar();
    }
    

    Source

    编辑 -:编辑您的 clang 文件,使其看起来像这样

    BasedOnStyle:  WebKit
    TabWidth:        4
    IndentWidth:     4
    UseTab:          Always
    ColumnLimit: 100
    
    DisableFormat:   false
    Standard: Cpp11
    
    AccessModifierOffset: -4
    AlignAfterOpenBracket: true
    AlignConsecutiveAssignments: false
    AlignConsecutiveDeclarations: false
    AlignEscapedNewlinesLeft: false
    AlignOperands:   true
    AlignTrailingComments: false
    AllowAllParametersOfDeclarationOnNextLine: true
    AllowShortBlocksOnASingleLine: false
    AllowShortCaseLabelsOnASingleLine: false
    AllowShortFunctionsOnASingleLine: Empty
    AllowShortIfStatementsOnASingleLine: false
    AllowShortLoopsOnASingleLine: false
    AlwaysBreakAfterDefinitionReturnType: false
    AlwaysBreakAfterReturnType: None
    AlwaysBreakBeforeMultilineStrings: false
    AlwaysBreakTemplateDeclarations: true
    BinPackArguments: false
    BinPackParameters: false
    
    BraceWrapping: {
        AfterClass: 'true'
        AfterControlStatement: 'true'
        AfterEnum : 'true'
        AfterFunction : 'true'
        AfterNamespace : 'true'
        AfterStruct : 'true'
        AfterUnion : 'true'
        BeforeCatch : 'true'
        BeforeElse : 'true'
        IndentBraces : 'false'
        AfterExternBlock : 'true'
        SplitEmptyFunction : 'false'
        SplitEmptyRecord : 'false'
        SplitEmptyNamespace : 'true'
    }
    
    BreakAfterJavaFieldAnnotations: true
    BreakBeforeInheritanceComma: false
    BreakBeforeBinaryOperators: None
    BreakBeforeTernaryOperators: true
    BreakConstructorInitializersBeforeComma: true
    BreakStringLiterals: true
    
    CommentPragmas:  '^ IWYU pragma:'
    CompactNamespaces: false
    ConstructorInitializerAllOnOneLineOrOnePerLine: false
    ConstructorInitializerIndentWidth: 4
    ContinuationIndentWidth: 4
    Cpp11BracedListStyle: true
    SpaceBeforeCpp11BracedList: false
    DerivePointerAlignment: false
    ExperimentalAutoDetectBinPacking: false
    ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
    IndentCaseLabels: false
    FixNamespaceComments: true
    IndentWrappedFunctionNames: false
    KeepEmptyLinesAtTheStartOfBlocks: true
    MacroBlockBegin: ''
    MacroBlockEnd:   ''
    JavaScriptQuotes: Double
    MaxEmptyLinesToKeep: 1
    NamespaceIndentation: None
    ObjCBlockIndentWidth: 4
    ObjCSpaceAfterProperty: true
    ObjCSpaceBeforeProtocolList: true
    PenaltyBreakBeforeFirstCallParameter: 19
    PenaltyBreakComment: 300
    PenaltyBreakFirstLessLess: 120
    PenaltyBreakString: 1000
    
    PenaltyExcessCharacter: 1000000
    PenaltyReturnTypeOnItsOwnLine: 60
    PointerAlignment: Left
    SpaceAfterCStyleCast: false
    SpaceBeforeAssignmentOperators: true
    SpaceBeforeParens: Never
    SpaceInEmptyParentheses: false
    SpacesBeforeTrailingComments: 1
    SpacesInAngles:  false
    SpacesInContainerLiterals: true
    SpacesInCStyleCastParentheses: false
    SpacesInParentheses: false
    SpacesInSquareBrackets: false
    SpaceAfterTemplateKeyword: true
    SpaceBeforeInheritanceColon: true
    
    SortUsingDeclarations: true
    SortIncludes: true
    
    ReflowComments: false
    
    IncludeBlocks: Preserve
    IndentPPDirectives: AfterHash
    

    Source

    【讨论】:

    • 复制您的代码时出错:Formatting failed: /fake_path/.vscode-server/extensions/ms-vscode.cpptools-0.26.2/bin/../LLVM/bin/clang-format -style=file -fallback-style=LLVM -assume-filename=/fake_path/test.cpp YAML:4:16: error: Unrecognized character while tokenizing. "BraceWrapping":{ ^ Error reading /fake_path/.clang-format: Invalid argument 编辑:已修复(在您的帖子中)
    • 谢谢,但它仍然没有做我想要的,它把单行功能留在一行上,就像我的第一个解决方案一样。当我插入换行符时,它会正确执行,但在同一行上会保持原样
    • 只做我告诉BreakBeforeBraces: Stroustrup的第一步
    • 不,文档在最终格式中说它仍将位于一行
    【解决方案3】:

    回答问题:

    使用 .clang 格式的文件无法实现此特定行为。对不起所有希望在这里找到方法的人,我希望我至少可以节省您一些时间。

    最近的:

    BreakBeforeBraces: Allman
    ColumnLimit: '0'
    

    这将使您的格式化函数保持正确,并且如果它们被拉伸到至少 2 行,则它们的格式正确。

    【讨论】:

    猜你喜欢
    • 2020-03-18
    • 2021-02-24
    • 2016-05-20
    • 2017-10-17
    • 1970-01-01
    • 2019-09-27
    • 2020-09-15
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多