如何保持块格式的方式?

时间:2016-05-26 09:34:07

标签: clang-format

我使用clang-format格式化我的代码。 一切正常,除了块代码,你可以看到下面的代码,失败块有缩进4空格......

_loginOperation = [CheckTIMPreparation tim_CheckPreparationSuccess:^{
    [weakSelf checkIsJoinGroup];
} failure:^(NSString *errorString) {
    [weakSelf showloginingViewWithFail];
}];

格式化后:

_loginOperation = [CheckTIMPreparation tim_CheckPreparationSuccess:^{
  [weakSelf checkIsJoinGroup];
}
    failure:^(NSString *errorString) {
      [weakSelf showloginingViewWithFail];
    }];

如何自定义我的clang-format配置?

这是我的配置:

BasedOnStyle: LLVM  
IndentWidth: 4  
BreakBeforeBraces: Attach  
AllowShortIfStatementsOnASingleLine: true  
IndentCaseLabels: true    
ObjCBlockIndentWidth: 4   
ObjCSpaceAfterProperty: true
ColumnLimit: 0  
AlignTrailingComments: true  
SpaceAfterCStyleCast: true 
SpacesInParentheses: false  
SpacesInSquareBrackets: false

1 个答案:

答案 0 :(得分:0)

现在已经四年了,但是也许仍然有人希望看到这个问题的答案...


可以肯定的是,您的问题标题似乎在询问如何防止clang-format修改代码块。如果仅此而已,可以通过在该代码周围放置// clang-format off// clang-format on来实现。有关更多详细信息,请参见documentation


您没有提到您正在使用的clang-format版本,不幸的是,您的代码被clang-format的不同版本格式化。 (我使用configurator探索了这些版本)。

  • 版本3.5.2提供:
    _loginOperation = [CheckTIMPreparation tim_CheckPreparationSuccess:^{
        [weakSelf checkIsJoinGroup];
    } failure:^(NSString *errorString) { [weakSelf showloginingViewWithFail]; }];
    
  • 3.6.0版提供:
    _loginOperation = [CheckTIMPreparation tim_CheckPreparationSuccess:^{
      [weakSelf checkIsJoinGroup];
    } failure:^(NSString *errorString) {
      [weakSelf showloginingViewWithFail];
    }];
    
  • 版本3.7.0至6.0.1给出:
    _loginOperation = [CheckTIMPreparation tim_CheckPreparationSuccess:^{
        [weakSelf checkIsJoinGroup];
    }
        failure:^(NSString *errorString) {
            [weakSelf showloginingViewWithFail];
        }];
    
  • 版本7.0.1至10.0.0给出:
    _loginOperation = [CheckTIMPreparation
        tim_CheckPreparationSuccess:^{
            [weakSelf checkIsJoinGroup];
        }
        failure:^(NSString *errorString) {
            [weakSelf showloginingViewWithFail];
        }];
    

因此,您可能正在使用6.0.0版之类的东西,而只是升级到clang-format的较新版本,可能会给您带来更理想的格式。


此外,您可能还需要设置样式选项AllowShortBlocksOnASingleLine: true(有关详细信息,请参见documentation)。对于clang-format版本10.0.0,您的代码将如下所示:

_loginOperation = [CheckTIMPreparation
    tim_CheckPreparationSuccess:^{ [weakSelf checkIsJoinGroup]; }
    failure:^(NSString *errorString) { [weakSelf showloginingViewWithFail]; }];
相关问题