Aaron Bratcher
Aaron Bratcher

Reputation: 6451

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);
    }];

Upvotes: 20

Views: 3775

Answers (3)

Vlad Papko
Vlad Papko

Reputation: 13302

I had the same issue: I wanted to disable colon alignment in methods with block parameters.

Finally I solved it by changing its behavior in Clang-Format source code:
I changed BreakBeforeParamater = true to BreakBeforeParameter = false everywhere in ContinuationIndenter.cpp (Thanks to @Matthias for reference to code).

This solution isn't beautiful but works. Now Clang-Format doesn't split ObjC params to new lines.

You can download modified tool from Dropbox.

PS. Original code was cloned from this repo: http://llvm.org/git/clang.git

Upvotes: 1

John
John

Reputation: 8548

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.

Upvotes: 2

Matthias
Matthias

Reputation: 1352

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?

Upvotes: 11

Related Questions