Reputation: 920
I'm trying to use clang-format (in VS code) to format my C++ files and configure it to my preferred style. For an array of structs (for getopts) it is adding a load of extra spaces and messing up the brace wrapping:
I'll append my .clang-format at the end of this query
Here's the way I want my array to appear:
int main()
{
const struct option longopts[]=
{
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0}
};
}
Here's how it actually appears:
int main()
{
const struct option longopts[] =
{
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0}};
}
My .clang-format file contains:
BasedOnStyle: LLVM
IndentWidth: 2
AlignAfterOpenBracket: Align
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
BinPackParameters: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 0
ConstructorInitializerAllOnOneLineOrOnePerLine: false
IndentCaseLabels: true
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: All
PointerAlignment: Right
SortIncludes: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesInContainerLiterals: false
SpacesInAngles: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
UseTab: Never
Any solutions welcome!
Upvotes: 1
Views: 3609
Reputation: 5050
I'm not sure that exactly what you want could be achived, but sometimes easiest way is to slighly modify the source file to help-up the clang-format utility. At first you need to add an ContinuationIndentWidth: 2
option to your format file. Then add a comma after the last item in the array:
{nullptr, 0, nullptr, 0}, // <---
And finnaly move the first curly brace on the same line as array name. The resulting file will be like this:
int main()
{
const struct option longopts[] = {
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0},
};
}
Running the clang-format will leave it as it is. Tested on clang-format from the LLVM snapshot build LLVM-9.0.0-r351376-win64.exe
.
Upvotes: 3