前言
每次在寫代碼寫完一行或一個文件時风宁,都要為了一些縮進、代碼不整齊蛹疯、空行太多等等戒财,對于此現(xiàn)象,有的開發(fā)人員是避之捺弦,導致了代碼寫的太亂饮寞,有的開發(fā)人員則是一個一個去review自己的代碼,但同時也花了一些時間在做這件事情列吼,用clang-format可以減輕花在這上面的時間幽崩,讓開發(fā)人員可以更加關注業(yè)務開發(fā),同時讓代碼更加規(guī)范優(yōu)雅寞钥。
使用步驟
1慌申、首先用xcode Package Manager(沒有安裝的要先安裝)安裝包安裝Clang Format(安裝成功后會在xcode–》Edit里面查找)。
2理郑、然后將.clang-format文件放到項目文件夾里面(和.xcodeproj同級目錄)蹄溉。
3咨油、前兩個過程做好之后,最后一步打開開發(fā)工具xcode編寫代碼类缤,每次編寫完之后按command+s保存時會自動格式化代碼臼勉。
?.clang-format配置
Language: Cpp
#If true, analyze the formatted file for the most common alignment of & and *. PointerAlignment is then used only as fallback.
DerivePointerAlignment: false
IndentWidth: 4
#@[]里面兩邊空格邻吭,原true
SpacesInContainerLiterals: false
#Add a space after @property in Objective-C, i.e. use \@property (readonly) instead of \@property(readonly).
ObjCSpaceAfterProperty: true
#The number of characters to use for indentation of ObjC blocks.
ObjCBlockIndentWidth: 4
#If true, if (a) return; can be put on a single line.
AllowShortIfStatementsOnASingleLine: true
#If false, spaces will be removed before assignment operators.
SpaceBeforeAssignmentOperators: true
#Pointer and reference alignment style.
PointerAlignment: Right
#The maximum number of consecutive empty lines to keep.
MaxEmptyLinesToKeep: 1
#每行字符的長度
ColumnLimit: 0
#注釋對齊
AlignTrailingComments: true
#括號后加空格
SpaceAfterCStyleCast: true
—
Language: JavaScript
# Use 100 columns for JS.
ColumnLimit: 0
?.clang-format文件
我?guī)缀鯂L試了所有的可能的配置餐弱,最后整理出一個非常好用的配置文件,我把它放到了github上了囱晴,大家要是想用的話膏蚓,直接從我的github上面下載下來放到項目根目錄即可。
結語
配置里面只是用到了一些實用的代碼格式化規(guī)則畸写,還有更多的規(guī)則如block驮瞧,switch規(guī)則等等,可以在“參考文檔”里面一一親測感受一下枯芬,相信使用過clang-format的同學們肯定感覺深刻论笔。
iOS技術交流:566912706