Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 655
orkenstein Avatar asked Sep 23 '14 08:09

orkenstein


People also ask

Does clang-format work for C?

clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.

How do you add a format to clang?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

How do you customize your clang-format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

Can clang-format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.

What is Clang format in C++?

clang-format. clang-format is a tool to format C/C++/… code according to a set of rules and heuristics. Like most tools, it is not perfect nor covers every single case, but it is good enough to be helpful. clang-format can be used for several purposes: Quickly reformat a block of code to the kernel style.

How do I create an Objective-C project in Clang?

At the root of your project (probably where your .git folder is), create a file called .clang-format The .clang-format file contains the formatting rules for a project. Its structure is YAML and is what the clang-format CLI can read to format your project's Objective-C files.

How to override the default Clang format in Linux?

To do so, you can override the defaults by writing another .clang-format file in a subfolder. The tool itself has already been included in the repositories of popular Linux distributions for a long time. Search for clang-format in your repositories.

How to avoid Clang-Format formatting some portion of a file?

To avoid clang-format formatting some portion of a file, you can do: int formatted_code; // clang-format off void unformatted_code ; // clang-format on void formatted_code_again;


2 Answers

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
like image 138
xu tong Avatar answered Nov 01 '22 04:11

xu tong


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
like image 42
orkenstein Avatar answered Nov 01 '22 04:11

orkenstein