include a .clang-format file to have formatting done by clang-format
NOTE:
I tried to apply clang-format at Apr 2022 at other project. See https://github.com/redboltz/mqtt_cpp/pull/927#discussion_r846594115
At that time, lambda capture list and template <> adjustment is not enough for me.
I expect the following format:
template <
typename T,
std::enable_if_t<
std::is_same_v<T, int>
>
>
foo(
T param1,
char param2,
double param3
) {
async_function(
arg1,
arg2,
[
capture1,
capture2 = std::move(outer),
capture3
]
() mutable -> std::size_t {
// body
++capture1;
}
);
}
The important points I want to achieve are as follows:
-
Avoiding longer lines. The following example is not good.
void foo() { [lambda_capture_argument1, lambda_capture_argument2 = std::move(lambda_capture_argument2)] () mutable { } } -
Replacing tool (e.g. perl) friendly. The following one is good because only one element for each line.
[ capture1, capture2 = std::move(outer), capture3 ]But the following is not good befause capture1 and capture2 are not tool friendly.
[capture1, capture2 = std::move(outer), capture3 ]
Here is the setting file when I tried:
Language: Cpp
# Indent
IndentWidth: 4
AccessModifierOffset: -4
#IndentBraces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: NoIndent
AlignAfterOpenBracket: AlwaysBreak
AllowAllParametersOfDeclarationOnNextLine: true
IndentGotoLabels: false
IndentPPDirectives: None
IndentRequires: true
IndentWrappedFunctionNames: false
LambdaBodyIndentation: Signature
UseTab: Never
#namespace
#AfterNamespace: false
#SplitEmptyNamespace: false
CompactNamespaces: false
FixNamespaceComments: true
# Align
AlignAfterOpenBracket: BlockIndent
#AlignAfterOpenBracket: AlwaysBreak
AlignArrayOfStructures: Right
AlignConsecutiveAssignments: AcrossEmptyLinesAndComments
AlignConsecutiveBitFields: AcrossEmptyLinesAndComments
AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: true
PointerAlignment: Left
ReferenceAlignment: Left
# Break
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
PenaltyBreakTemplateDeclaration: 0
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: false
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: true
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: AfterColon
BreakStringLiterals: false
AllowAllConstructorInitializersOnNextLine: true
PackConstructorInitializers: Never
# SingleLine
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: false
#SplitEmptyFunction: true
#SplitEmptyRecord: true
#Macro
#Space
Cpp11BracedListStyle: false
DerivePointerAlignment: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: false
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
# Other
FixNamespaceComments: true
IncludeBlocks: Preserve
MaxEmptyLinesToKeep: 2
SortIncludes: CaseSensitive
SortUsingDeclarations: true
I also tried to have clang-format break the template parameter list into individual lines and having > on a seperate line, but no luck. I would like to keep this issue open and come back to it later if clang-format gets better or an alternative pops up.
Yes, clang-format 's template parameter formatting and lambda related formatting is not enough for me. Unfortunately, I don't have much time to treat the clang-format but if you or someone report a feature request https://github.com/llvm/llvm-project or pull request to clang format, it would be helpful.
BTW, my template formatting is based on Dave Abrahams one.