讓你的代碼自動格式化

前言

每一個團隊都應該有統一的代碼風格和規範,這帶來的好處我相信不言而喻,具體我就很少說了,你們都懂的😁。如何更有效率的去作這件事呢,我此次就來講說如何更好的自動格式化你的代碼。html

現狀

大多數 iOS 開發者應該都知道 Xcode 的插件 Clang Format,它是基於 clang-format 命令行工具的一個 Xcode 插件,可是這款插件在Xcode9上已經沒法使用了,由於Xcode9的插件機制已經變了。
如今可使用這一款XcodeClangFormat,具體的使用方式點擊連接,你們自行去看吧。這款有個缺點,就是不能像以前那款插件能夠設置在保存時自動格式化(這其實也不能怪做者,Xcode新的機制不容許)。 不過使用這種插件仍是不夠方便,你還得手動選中文件或者代碼再按快捷鍵來格式化,很容易忘,而致使把不規範的代碼直接提交到倉庫裏了。
那麼有沒有一種方式,可讓我在敲代碼的時候爲所欲爲,提交時又能提醒我而後自動幫我格式化嗎?git

該怎麼作

這裏我直接介紹一款神器Space Commander,它利用 Git Hooks ,在 commit 以前檢查代碼風格是否符合規範,只有符合規範的代碼才容許提交,列出不符合規範的文件,同時提供 Shell 腳原本自動格式化。接下來我介紹下如何使用。github

  • 1 clone Space Commander
    git clone https://github.com/square/spacecommander.git
  • 2 在項目中安裝Space Commander
    cd到你的項目根目錄,執行setup-repo.sh腳本(在你clone下來的項目中,因此要全路徑),執行完後會在項目根目錄多一個隱藏文件.clang-format,這是一個替身,指向Space Commander倉庫中的.clang-format文件,裏面默認包含了一系列代碼規則,若是你想要用本身的規則,能夠去Space Commander倉庫中改真身,也能夠用新的.clang-format文件替換掉這個替身。
  • 3 讓咱們提交代碼試試
    BasedOnStyle: Chromium
    IndentWidth: 4
    AlignConsecutiveAssignments: true
    AlignConsecutiveDeclarations: true
    ObjCSpaceAfterProperty: true
    PointerAlignment: Right
    BreakBeforeBraces: Attach
    這是我自定義的一些規則,具體.clang-format的寫法請參照這個
    好,讓咱們寫一下代碼
#import "ViewController.h"

@interface ViewController ()
@property(nonatomic, copy) NSString*    p;
@property(nonatomic, strong) UITextView *  textview;
@end

@implementation ViewController

-(void)formatTest:(NSString *)param{
if (param) {
 NSLog(@"sss");
    }
    int a=0;
       int b = 1;
    int c= 2;
    NSLog(@"%d%d%d",a,b,c);
}

-(void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)viewDidAppear:(BOOL)animated {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end 
複製代碼

這一看,就很不規範吧,先讓咱們提交看看shell

提交的時候明確提示ViewController文件須要格式化,這時候咱們可使用format-objc-file.sh腳本單獨格式化某個文件,也能夠format-objc-files.sh格式化全部的暫存文件,甚至使用format-objc-files-in-repo.sh格式化整個倉庫的文件。

再提交一遍

好,接下來咱們在看看代碼變成什麼樣子了

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, copy) NSString *    p;
@property (nonatomic, strong) UITextView *textview;
@end

@implementation ViewController

- (void)formatTest:(NSString *)param {
    if (param) {
        NSLog(@"sss");
    }
    int a = 0;
    int b = 1;
    int c = 2;
    NSLog(@"%d%d%d", a, b, c);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidAppear:(BOOL)animated {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

複製代碼

完美!😝bash

後續

能夠看到上面的命令太長了,必需要指定shell腳本的全路徑才能執行,咱們能夠簡化命令,若是你用的zsh,去修改 ~/.zshrc,若是是bash,則修改~/.bash_profile,markdown

// 初始化
alias clangformatsetup="/你本身的路徑/spacecommander/setup-repo.sh"
// 格式化對應文件
alias clangformatfile="/你本身的路徑/spacecommander/format-objc-file.sh"
// 格式化全部暫存文件
alias clangformatfiles="/你本身的路徑/spacecommander/format-objc-files.sh"
// 格式化整個倉庫
alias clangformatall="/你本身的路徑/spacecommander/format-objc-files-in-repo.sh 複製代碼

若是你還想知道更多的用法,直接去spacecommander的github主頁查看。工具

總結

這是我第一次在掘金上寫文章(別的地方也沒寫過多少😂),寫的很差,你們海涵吶,多多提意見哈😁。oop

相關文章
相關標籤/搜索