Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format: Stop breaking long methods up

In attempting to set up a .clang-format file for a project using Objective-C, I've run into an issue where, even with 0 maximum line width, long Objective-C methods are getting cut into multiple lines. For example, this:

AFHTTPRequestOperation *returnOperation = [self POST:endpoint parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFormData:[[secureStore valueForKey:storeKey] dataUsingEncoding:NSUTF8StringEncoding] name:tokenKey];
        if ([provider isEqualToString:kTwitterKey]) {
            [formData appendPartWithFormData:[[secureStore twitterSecretToken] dataUsingEncoding:NSUTF8StringEncoding] name:kTwitterSecretKey];
            [formData appendPartWithFormData:[email dataUsingEncoding:NSUTF8StringEncoding] name:kEmailKey];
        }
        [formData appendPartWithFormData:[[secureStore token] dataUsingEncoding:NSUTF8StringEncoding] name:tokenKey];
    } success:^(AFHTTPRequestOperation *operation, NSDictionary *response) {
        [secureStore setToken:response[kTokenKey]];
        if (completionHandler) {
            completionHandler(nil, response);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (completionHandler) {
            completionHandler(error, nil);
        }
    }];

gets turned into this:

AFHTTPRequestOperation *returnOperation = [self POST:endpoint
        parameters:nil
        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
          [formData appendPartWithFormData:[[secureStore valueForKey:storeKey] dataUsingEncoding:NSUTF8StringEncoding] name:tokenKey];
          if ([provider isEqualToString:kTwitterKey]) {
              [formData appendPartWithFormData:[[secureStore twitterSecretToken] dataUsingEncoding:NSUTF8StringEncoding] name:kTwitterSecretKey];
              [formData appendPartWithFormData:[email dataUsingEncoding:NSUTF8StringEncoding] name:kEmailKey];
          }
          [formData appendPartWithFormData:[[secureStore token] dataUsingEncoding:NSUTF8StringEncoding] name:kTokenKey];
        }
        success:^(AFHTTPRequestOperation *operation, NSDictionary *response) {
          [secureStore setToken:response[kTokenKey]];
          if (completionHandler) {
              completionHandler(nil, response);
          }
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          if (completionHandler) {
              completionHandler(error, nil);
          }
        }];

I'd like to leave the splitting of methods into multiple lines to the developer but still have the formatter check for proper brace placement, for example. Suggestions for how to fix that weird interior indention would also be appreciated.

Edit: Here's my .clang-format file, if anyone's interested. I'm trying to replicate the New York Times Objective-C styles, mostly.

---
Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: false
AlignEscapedNewlinesLeft: false
AlignOperands: false
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Stroustrup
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 0
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: true
IndentWrappedFunctionNames: false
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 0
PenaltyBreakComment: 0
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 0
PenaltyExcessCharacter: 0
PenaltyReturnTypeOnItsOwnLine: 0
PointerAlignment: Right
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
like image 391
Jon Shier Avatar asked May 07 '15 19:05

Jon Shier


1 Answers

I'm using the following config which will not break up the long method

BasedOnStyle: LLVM
IndentWidth: 4
AllowShortIfStatementsOnASingleLine: true
IndentCaseLabels: false
ColumnLimit: 0

And you can get the LLVM setting using the command

clang-format -style=llvm -dump-config > .clang-format
like image 101
xi.lin Avatar answered Oct 15 '22 05:10

xi.lin