Skip to content

Instantly share code, notes, and snippets.

@nicklasfrahm
Last active March 11, 2024 12:22
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicklasfrahm/4a4fff24381f85ada76ccb651f555e1f to your computer and use it in GitHub Desktop.
Save nicklasfrahm/4a4fff24381f85ada76ccb651f555e1f to your computer and use it in GitHub Desktop.
Opinionated .clang-format for JS boys coming from prettier and stumbling into C++
AccessModifierOffset: 2
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: false
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakStringLiterals: false
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
CompactNamespaces: false
IndentCaseLabels: true
IndentWidth: 2
TabWidth: 2
UseTab: Never
@nicklasfrahm
Copy link
Author

Feel free to improve and append. At some point I got bored reading through this. Maybe somebody else has more patience. Maybe one could mimick the prettier behaviour.

@dortamiguel
Copy link

dortamiguel commented Dec 30, 2019

thanks!
Im using this config

Language: Cpp
ColumnLimit: 80
ContinuationIndentWidth: 2
UseTab: Never
IndentWidth: 2
TabWidth: 2
IndentCaseLabels: true
SortIncludes: true
SortUsingDeclarations: false
AlignConsecutiveMacros: false
AlignEscapedNewlines: DontAlign
AlignAfterOpenBracket: AlwaysBreak
AlignOperands: false
AlignTrailingComments: false
BinPackArguments: false
BinPackParameters: false
SpacesInContainerLiterals: false
Cpp11BracedListStyle: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Always
FixNamespaceComments: false
ReflowComments: false
NamespaceIndentation: All
IncludeBlocks: Merge
BreakStringLiterals: false
ConstructorInitializerIndentWidth: 2
SpaceBeforeCtorInitializerColon: true
BreakBeforeInheritanceComma: false
BreakInheritanceList: AfterColon
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: AfterColon
ConstructorInitializerAllOnOneLineOrOnePerLine: true

@JonnyHaystack
Copy link

This was a good starting point and I managed to get quite a bit closer and at least get stuff in braces to be formatted just as Prettier would. I'm quite happy with this and felt like sharing because I spent hours searching for something like this and going through clang-format options. Note that I use 4 space indentation which you can change that if you prefer.

The key things I changed that made it work much better were AlignAfterOpenBracket: BlockIndent, AllowAllArgumentsOnNextLine: false, and AllowAllParametersOfDeclarationOnNextLine: false

AlignAfterOpenBracket: BlockIndent
AlignConsecutiveMacros: false
AlignEscapedNewlines: DontAlign
AlignOperands: Align
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BinPackArguments: false
BinPackParameters: false
BreakBeforeInheritanceComma: false
BreakConstructorInitializers: AfterColon
BreakConstructorInitializersBeforeComma: false
BreakInheritanceList: AfterColon
BreakStringLiterals: false
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DeriveLineEnding: true
FixNamespaceComments: false
IncludeBlocks: Merge
IndentCaseLabels: true
IndentWidth: 4
LambdaBodyIndentation: OuterScope
Language: Cpp
NamespaceIndentation: All
PackConstructorInitializers: CurrentLine
PointerAlignment: Right
QualifierAlignment: Left
ReferenceAlignment: Right
ReflowComments: true
SeparateDefinitionBlocks: Always
SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
  - Regex:           '"[[:alnum:]._-]+"'
    Priority:        1
    SortPriority:    1
  - Regex:           '^((<|").*/)'
    Priority:        2
    SortPriority:    2
  - Regex:           '<[[:alnum:]._-]+>'
    Priority:        3
    SortPriority:    3
SortUsingDeclarations: false
SpaceBeforeCtorInitializerColon: true
SpacesInContainerLiterals: false
TabWidth: 4
UseTab: Never

@dortamiguel
Copy link

@JonnyHaystack which version of clang-format are you using?

I get this error on 13.0.1

error: unknown enumerated scalar
AlignAfterOpenBracket: BlockIndent

@JonnyHaystack
Copy link

Latest 14.0. I use vim with coc.nvim clangd extension and it installs the latest automatically. And for VS Code on Windows I just installed LLVM 14.0 from github.

@dortamiguel
Copy link

I see, looks like homebrew in mac didn't updated yet

@JonnyHaystack
Copy link

Ah, shame. I didn't even know it was a new option because it doesn't say the version in which each value was introduced. But definitely worth upgrading for imo. Also when I said braces before, I meant parentheses, but arrays in braces work as well if you use a trailing comma.

@dortamiguel
Copy link

@JonnyHaystack thank you for your config, it improved mine one, now it looks like this

# https://clang.llvm.org/docs/ClangFormatStyleOptions.html

ColumnLimit: 80
ContinuationIndentWidth: 2
IndentWidth: 2
TabWidth: 2
ConstructorInitializerIndentWidth: 2
IndentCaseLabels: true
UseTab: Never
SortIncludes: true
SortUsingDeclarations: false
AlignConsecutiveMacros: false
AlignEscapedNewlines: DontAlign
AlignAfterOpenBracket: BlockIndent
AlignOperands: false
AlignTrailingComments: false
BinPackArguments: false
BinPackParameters: false
SpacesInContainerLiterals: false
Cpp11BracedListStyle: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Always
FixNamespaceComments: false
ReflowComments: false
NamespaceIndentation: All
IncludeBlocks: Merge
BreakStringLiterals: false
BreakConstructorInitializers: AfterColon
IndentPPDirectives: BeforeHash
IncludeCategories:
  - Regex: '"[[:alnum:]._-]+"'
    Priority: 1
    SortPriority: 1
  - Regex: '^((<|").*/)'
    Priority: 2
    SortPriority: 2
  - Regex: "<[[:alnum:]._-]+>"
    Priority: 3
    SortPriority: 3

@dortamiguel
Copy link

@JonnyHaystack I like your IncludeCategories config but I found a case that sorts includes like this

#include "selectedTile.hpp"
#include "../../input.hpp"
#include "../../size.hpp"
#include "../../tiles/style.hpp"
#include "../tilemap.hpp"

do you think that it will be possible push to the bottom directories that are more deep like this?

#include "selectedTile.hpp"
#include "../tilemap.hpp"
#include "../../input.hpp"
#include "../../size.hpp"
#include "../../tiles/style.hpp"

@JonnyHaystack
Copy link

Hmm, not sure how to make it treat those differently and essentially reverse the sort order while keeping them in the same group.

@arnoson
Copy link

arnoson commented Mar 16, 2023

@dortamiguel thanks, this works great for me!
I only had to add one more setting:
PenaltyReturnTypeOnItsOwnLine: 200
Otherwise I ended up with something like this:

bool
myFunction(/* some args that would fit into one line */) {
  // ...
}

@dortamiguel
Copy link

PenaltyReturnTypeOnItsOwnLine

thank you! that is great :)

@jowillianto
Copy link

For anyone looking for the full version :

# https://clang.llvm.org/docs/ClangFormatStyleOptions.html

ColumnLimit: 80
ContinuationIndentWidth: 2
IndentWidth: 2
TabWidth: 2
ConstructorInitializerIndentWidth: 2
IndentCaseLabels: true
PenaltyReturnTypeOnItsOwnLine: 200
UseTab: Never
SortIncludes: true
SortUsingDeclarations: false
AlignConsecutiveMacros: false
AlignEscapedNewlines: DontAlign
AlignAfterOpenBracket: BlockIndent
AlignOperands: false
AlignTrailingComments: false
BinPackArguments: false
BinPackParameters: false
SpacesInContainerLiterals: false
Cpp11BracedListStyle: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Always
FixNamespaceComments: false
ReflowComments: false
NamespaceIndentation: All
IncludeBlocks: Merge
BreakStringLiterals: false
BreakConstructorInitializers: AfterColon
IndentPPDirectives: BeforeHash
IncludeCategories:
  - Regex: '"[[:alnum:]._-]+"'
    Priority: 1
    SortPriority: 1
  - Regex: '^((<|").*/)'
    Priority: 2
    SortPriority: 2
  - Regex: "<[[:alnum:]._-]+>"
    Priority: 3
    SortPriority: 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment