问题
I have the following code, for example:
[cardRegistrationVC setCancelBlock:^{
[weakSelf.navigationController popViewControllerAnimated:YES];
}];
When I apply clang-format on it, it turns into:
[cardRegistrationVC setCancelBlock:^{ [weakSelf.navigationController popViewControllerAnimated:YES]; }];
As you can see, code inside the block appears on the same line. But I should be always on a new line.
How to set up clang-format correct? My following settings file:
BasedOnStyle: LLVM
AllowShortIfStatementsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
IndentCaseLabels: true
ColumnLimit: 120
ObjCSpaceAfterProperty: true
KeepEmptyLinesAtTheStartOfBlocks: true
PenaltyBreakString: 1000000
SpacesInContainerLiterals: false
回答1:
Just add this to the setting file(.clang-format).
ObjCBlockIndentWidth: 4
Then the block will like this.
[cardRegistrationVC setCancelBlock:^{
[weakSelf.navigationController popViewControllerAnimated:YES];
}];
Hope help you.
At the same time I would like add :
UseTab: Never
IndentWidth: 4
回答2:
Finally I ended up writing blocks like this:
[cardRegistrationVC setCancelBlock:^{
[weakSelf.navigationController popViewControllerAnimated:YES];
}];
Empty line at the end works ok. Or you have to disable column limit:
#ColumnLimit: 120
来源:https://stackoverflow.com/questions/25990881/how-to-format-objective-c-block-with-clang-format