How to format objective-c block with clang-format?

你离开我真会死。 提交于 2021-02-07 05:33:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!