Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What clang-format options control format of method calling parameters?

I know people have different opinions on how to format method calls in Objective-C, i.e.

[self presentViewController:scanViewController
                   animated:YES
                 completion:nil];

vs

[self presentViewController:scanViewController animated:YES completion:nil];

What options in my .clang-format file do I use to control this indenting? (If I don't want it, colons to line up, etc)

Also, is it just me or is this formatter ignorant of blocks? Notice how the if statement for the success block is not indented, nor is the NSLog function in the failure block.

[self.client getPath:path
    parameters:parameters
    success:^(AFHTTPRequestOperation *operation, id responseObject) {
    if ([from_id isEqualToString:self.from_id]) {
        self.image.image = [UIImage imageWithData:responseObject];
    }
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(error.description);
    }];
like image 969
Aaron Bratcher Avatar asked Mar 05 '14 13:03

Aaron Bratcher


People also ask

What is clang formatter?

Clang-Format is a widely-used C++ code formatter. As it provides an option to define code style options in YAML-formatted files — named . clang-format or _clang-format — these files often become a part of your project where you keep all code style rules.

Where does clang format look for clang format?

Standalone Tool 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 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.

How do you format a clang file?

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. The workflow to format your changed code: Make codes changes in Electron repository.


2 Answers

I looked into the clang-format source code where the formatting of objective-c method expressions is done and found it here: http://llvm.org/svn/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp

The code:

// If this '[' opens an ObjC call, determine whether all parameters fit
// into one line and put one per line if they don't.
if (Current.Type == TT_ObjCMethodExpr &&
    getLengthToMatchingParen(Current) + State.Column >
        getColumnLimit(State))
  BreakBeforeParameter = true;

As you can see the behavior is only controlled by the configuration option ColumnLimit. You could set it to 0 to suppress the line breaks. Unfortunately this has, of course, impact on the complete formatting.

Regarding the problem with the missing indention within blocks: I could not reproduce that with the latest Visual Studio Plugin (SVN r203967). Have you perhaps fiddled with ContinuationIndentWidth?

like image 141
Matthias Avatar answered Oct 07 '22 13:10

Matthias


My variable ColumnLimit is zero. Method calls are formatted like this:

[self presentViewController:scanViewController animated:YES completion:nil];

I would like to format them as follows without changing the ColumnLimit variable:

[self presentViewController:scanViewController
               animated:YES
             completion:nil];

It seems that there is no clang configuration option to achieve this. However, I found a solution that works for me:

If I add // and a line break right after the first parameter (scanViewController here), formatting the code using clang produces the desired result:

[self presentViewController:scanViewController //
                   animated:YES
                 completion:nil];

This means that the clang formatting puts all parameters on separate lines and aligns the colons.

like image 38
vomako Avatar answered Oct 07 '22 13:10

vomako