I'm trying to make my .clang-format config. Everything is fine, but there is still a little problem.
When I format this:
struct testStruct data[] = {
{.a = 0,
.b = 0},
{.a = 0,
.b = 0},
};
The output will put every element into a single line like this:
struct testStruct data[] = {
{.a = 0, .b = 0},
{.a = 0, .b = 0},
};
If you add a comma at the end of the last element the format will be:
struct testStruct data[] = {
{
.a = 0,
.b = 0,
},
{
.a = 0,
.b = 0,
},
};
I know the better way may is initializing each structure separately:
struct testStruct data[];
data[0] = {
.a = 0,
.b = 0,
}
But, who is controlling these? I can only guess at the AllowShortBlocksOnASingleLine
option, there are too many of them.
My config is here: https://gist.github.com/SourLemonJuice/047f051f2d6365aed547826d22b2a516
Now is:
---
BasedOnStyle: Microsoft
IndentWidth: 4
UseTab: Never
LineEnding: DeriveLF
MaxEmptyLinesToKeep: 2 # for me, 2 empty lines were so common.
# Sorry Linux... But we need some C++ stuff here.
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: MultiLine
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeWhile: false
# BreakBeforeBraces End
AllowShortBlocksOnASingleLine: Never
AllowShortIfStatementsOnASingleLine: Never
IndentCaseLabels: false
IndentPPDirectives: BeforeHash
IncludeBlocks: Preserve # because google c++ guide specifies the order of #include blocks.
SortIncludes: CaseInsensitive
ColumnLimit: 120 # same as Microsoft, don't ask me, I just think it's great.
InsertNewlineAtEOF: true # Who doesn't like \n in the file end.
BinPackParameters: true # Microsoft has set this, but I also saw this config.
AlignAfterOpenBracket: Align # TBD, now same as Microsoft.
AlignTrailingComments:
Kind: Always
OverEmptyLines: 0
AlignOperands: AlignAfterOperator
AlignEscapedNewlines: DontAlign
...
As far as I can tell, it is not possible to keep that part {.a ...
. There will always be a line break after the first curly bracket, or never a line break till the closing curly bracket.
With AlignArrayOfStructures: None
(Left
, Right
) and ContinuationIndentWidth: 4
you can get some modifications, but that's pretty much it.
The trailing comma is a good option. Also for list edits and extension, as well as a reduced diff and better git-blame.
If you really need that style, you posted, you can do the following:
// clang-format off
struct testStruct data[] = {
{.a = 0,
.b = 0},
{.a = 0,
.b = 0},
};
// clang-format on
Note that all formatting will be disabled between those two comments. And that this will be laborious if you run into that situation often.
You can read more about that in the Clang-Format Style Options.