【问题标题】:How to configure Clang Format for Allman style for iOS coding?如何为 iOS 编码配置 Allman 风格的 Clang 格式?
【发布时间】:2015-12-22 22:56:42
【问题描述】:

我对 Clang 格式 API 有很多困惑。

  1. 我无法打开 .clangformat 文件,因此我可以查看并根据我的配置进行配置。
  2. 我需要以 Allman 样式格式化我的代码。
  3. 我也看过很多关于 Google 和 Stack Overflow 的文档,但我没有得到任何帮助来实现 Allman 样式格式。

我遇到了http://clangformat.com/,但我也没有得到任何帮助来实现 Allman 风格。

这是我想要的问题和解决方案。

问题 #1:

[[NSNotificationCenter defaultCenter]
      addObserver:self
         selector:@selector(captureSearchFiltersNotificationWithSelection:)
             name:kSearchFiltersNotificationWithSelection
           object:nil];

需要 #1:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSearchFiltersNotificationWithSelection:)name:kSearchFiltersNotificationWithSelection object:nil];

问题 #2:

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {
                //        listings = [NSMutableArray new];
                self.navTitle = @"Buy";
                searchFilters = [SearchFilter new];

                if ([LocationManager locationManager].location == nil) {
                        selectedSortOption = kSortBuyRefineOptionUndefined;
                }

                else {
                        selectedSortOption = kSortBuyRefineOptionNearMeAsc;
                }
        }
        return self;
}

需要 #2:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil 
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        listings = [NSMutableArray new];
        self.navTitle = @"Buy";
        searchFilters = [SearchFilter new];

        if ([LocationManager locationManager].location == nil) 
        {
            selectedSortOption = kSortBuyRefineOptionUndefined;
        }

        else 
        {
            selectedSortOption = kSortBuyRefineOptionNearMeAsc;
        }
    }

    return self;
}

【问题讨论】:

  • @halfer 知道答案吗?
  • 回复:“我无法打开 .clangformat 文件,以便我可以查看并根据我的配置进行配置。”您能否澄清您的意思,因为不清楚?文件名通常应为.clang-format_clang-format 并放在项目目录中。
  • @l'L'l 在哪个编辑器中可以看到“.clang-format”文件的内容?
  • @Developer 当然是任何编辑器

标签: ios code-formatting clang-format


【解决方案1】:

您需要在项目根目录中添加一个 .clang 格式的文件。根据要求,您可以在 TextEditor 中编辑此文件。以下是“Allman”风格的格式:

BasedOnStyle: None
AlignTrailingComments: true
BreakBeforeBraces: Allman
ColumnLimit: 0
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
TabWidth: 8
UseTab: Never

以下是一些对您有帮助的链接:

http://staxmanade.com/2015/01/how-to-install-clang-format-and-formatting-objective-c-files/

http://tonyarnold.com/2014/05/31/autoformatting-your-code.html

http://clang.llvm.org/docs/ClangFormatStyleOptions.html

http://blog.manbolo.com/2015/05/14/code-beautifier-in-xcode

生成和安装 clang 格式文件的 Xcode 项目链接: https://github.com/travisjeffery/ClangFormat-Xcode/blob/master/README.md

修复 .clang 格式问题: https://github.com/travisjeffery/ClangFormat-Xcode/issues/68

Apple 的源代码格式化选项: https://developer.apple.com/legacy/library/documentation/DeveloperTools/Reference/XcodeUserDefaultRef/100-Xcode_User_Defaults/UserDefaultRef.html#//apple_ref/doc/uid/TP40005535-CH3-SW57

希望这会有所帮助!

【讨论】:

  • 你应该再读一遍我的问题。还有关于 Clang 格式。我想你没有问我问题。
  • 我在根目录中有 .clang-format 类,但我需要知道如何为 Allman Style 配置它。如果你愿意,我可以把那个文件发给你,或者你把格式化的文件发给你。
  • 我已经阅读了所有的文档,并且还配置了您提到的文件,但它仍然无法正常工作。请使用我的问题创建一个示例代码并测试您的文件。如果这对你有用,请告诉我。您也可以分享屏幕录像。
  • 为了实现你的风格,我需要花费额外的时间,我必须对 .clang 格式的文件配置做一些研究。但是,是的,这是获得所需格式的方法。您甚至可以转到 Xcode -> Preferences -> Text Editing -> Indentation 进行 Xcode 默认格式设置。如果您实现了这一点,请告诉我,因为这将是一项很好的研究,并且可以对其他人有所帮助。
  • @Developer 让别人分享屏幕录像是非常不现实的。这是一个非常有用的答案。
猜你喜欢
  • 2016-03-27
  • 2018-06-25
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-21
  • 1970-01-01
相关资源
最近更新 更多