23
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Alcatraz(パッケージ管理ソフト)をXcodeにいれてClangFormatを手軽につかえるまでの手順

Last updated at Posted at 2016-02-04

背景

複数人でリファクタリングをするに当たって、コード規約どうするかと同じくらい大切なのは、それをどう守っていくかということです。
できるなら、人力でやるより、自動化するのが楽だということで、コードをいい感じにフォーマットしてくれるClangFormatを導入することにしました。

公式ドキュメント

導入

インストール手順はこれだけ

curl -fsSL https://raw.githubusercontent.com/supermarin/Alcatraz/deploy/Scripts/install.sh | sh

アンインストールするときはこれ

rm -rf ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin

キャッシュを削除するときはこれ

rm -rf ~/Library/Application\ Support/Alcatraz

実行すると

hoge-no-MacBook-Pro:Desktop tac$ curl -fsSL https://raw.githubusercontent.com/supermarin/Alcatraz/deploy/Scripts/install.sh | sh
2016-02-04 17:42:39.404 defaults[98535:1903663] 
The domain/default pair of (/Users/tac/Library/Preferences/com.apple.dt.Xcode, DVTPlugInManagerNonApplePlugIns-Xcode-7.2) does not exist
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   590    0   590    0     0    493      0 --:--:--  0:00:01 --:--:--   493
 15  106k   15 16975    0     0   6382      0  0:00:17  0:00:02  0:00:15 16182x Alcatraz.xcplugin/
x Alcatraz.xcplugin/Contents/
x Alcatraz.xcplugin/Contents/Info.plist
x Alcatraz.xcplugin/Contents/MacOS/
x Alcatraz.xcplugin/Contents/Resources/
x Alcatraz.xcplugin/Contents/Resources/ATZPackageListTableCellView.nib
x Alcatraz.xcplugin/Contents/Resources/ATZPluginWindowController.nib
x Alcatraz.xcplugin/Contents/Resources/bitbucket_grayscale.tiff
x Alcatraz.xcplugin/Contents/Resources/en.lproj/
x Alcatraz.xcplugin/Contents/Resources/eye_icon.tiff
x Alcatraz.xcplugin/Contents/Resources/git_grayscale.tiff
x Alcatraz.xcplugin/Contents/Resources/github_grayscale.tiff
x Alcatraz.xcplugin/Contents/Resources/link_icon.tiff
x Alcatraz.xcplugin/Contents/Resources/en.lproj/InfoPlist.strings
x Alcatraz.xcplugin/Contents/Resources/en.lproj/Localizable.strings
100  106k  100  106k    0     0  35754      0  0:00:03  0:00:03 --:--:-- 76027

Alcatraz successfully installed!!1!🍻  Please restart your Xcode (7.2).

Xcodeをリスタートすると脅されます。
要約すると、"これ、アップル製じゃないから、このプラグインをいれるとXcodeなどの安定性やセキュリティに悪い影響を与えますよ"の意味。

Xcodeプラグイン「Alcatraz」の導入_-_Qiita.png

そして、xcodeを開くと

スクリーンショット_2016-02-04_17_59_05.png

PackageManagerが表示されます。
そして、ChangFormatと入力。

スクリーンショット_2016-02-04_18_00_19.png

インストール後Xcodeを再起動。

read_swift_1.png

また出ます。

Load BundleをクリックしてXcodeを開くと、

スクリーンショット 2016-02-04 18.03.56.png

ChangFormatが使えるようになっています。

使い方としては、自分のPCのホームディレクトリに.clang-formatファイルを置いて、xcode > Edit > Clang Format > Format File in Force をクリックすると整形されます。

.clang-formatのサンプル 

自分の設定ですが、こんな感じです。

BasedOnStyle: LLVM
AlignTrailingComments: true
BreakBeforeBraces: Attach
ColumnLimit: 120
IndentWidth: 4
Language: Cpp
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
UseTab: Never

ClangFormatのOption

公式ドキュメントはこちら
http://clang.llvm.org/docs/ClangFormatStyleOptions.html

  • BasedOnStyle
    基本的なスタイルを設定します。
    自分はObjctive-Cでの設定なのでLLVMコーディングのスタイルで設定。他にもGoogleなどがあります。

  • BreakBeforeBraces
    波括弧{}のフォーマットスタイル

    • Attach
      常に周囲のコンテキストに波括弧{}を付ける
  • Allman
    Attachとは逆

  • ColumnLimit
    カラムの上限

  • Language

    • Cpp
      公式ドキュメントによるとObjCも含むのでこれを指定

こちらの記事がとても良くまとまっていたのでOptionについて細かく解説するよりかは、こちらを見ていただくほうが正確だと思われます。

23
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?