clang-format:停止打破长方法

时间:2021-05-24 21:37:43

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:

在尝试使用Objective-C为项目设置.clang格式文件时,我遇到了一个问题,即使最大行宽为0,长Objective-C方法也会被切割成多行。例如,这个:

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.

编辑:这是我的.clang格式文件,如果有人感兴趣的话。我主要试图复制纽约时报的Objective-C风格。

---
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

1 个解决方案

#1


2  

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

我正在使用以下配置,它不会破坏long方法

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

And you can get the LLVM setting using the command

您可以使用该命令获取LLVM设置

clang-format -style=llvm -dump-config > .clang-format

#1


2  

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

我正在使用以下配置,它不会破坏long方法

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

And you can get the LLVM setting using the command

您可以使用该命令获取LLVM设置

clang-format -style=llvm -dump-config > .clang-format