iOS plugin detailed----clangformat (code format Management) (2016.1.12 Wang Bin)
Although in the early stages of project creation and team building, we have set the public agreement and some specifications, and since our code is version control through Git, the Web directly supports the markdown format of the Readme file, you can always see the latest version, But this specification can only rely on personal consciousness, or through the code review to solve, and when the code review, you also embarrassed always write a bunch of such as "here to add a space", "where to add line" comment? If no matter, over time, because everyone's habits are different, the code presents a variety of styles, it does not look like a mature team made products.
To compensate for the Xcode code formatting of the short board, we chose to introduce a third-party plugin:Clangformat.
Specific process:
1. Installing Clangformat
Installation method: Search and install directly in the Package Manager, if you do not want to install the Package Manager, you can directly clone the above GitHub code, compile, run in Xcode, and then restart Xcode.
Installation Scenario Two:
GitHub Address: Https://github.com/travisjeffery/ClangFormat-Xcode
Download Plugin--Open clangformat.xcodeproj file, com+b Run, restart Xcode, done.
2. Configure Clangformat (You can skip this step if you don't need to customize)
Although the Clangformat itself has some standardized code formatting schemes built in, it can be customized as well, and we've adopted a custom approach.
Specifically, create a ". Clang-format" file in the project catalog or workspace directory, and add parameters similar to the following:
- # base Style
- Basedonstyle:llvm
- # Indent Width
- Indentwidth:4
- # line wrapping for parentheses
- Breakbeforebraces:attach
- # support for one-line if
- Allowshortifstatementsonasingleline:true
- # switch Case Indentation
- Indentcaselabels:true
- # The indent width of the block for OC
- Objcblockindentwidth:4
- # for OC, add a space after the attribute name
- Objcspaceafterproperty:true
- # The length of each line of characters
- columnlimit:0
- # Justification of comments
- Aligntrailingcomments:true
- # Add a space after parentheses
- Spaceaftercstylecast:true
Then select "File" in Xcode's "Edit", "CLang format" and let the penultimate line show "Disable format on Save".
After this look at the actual situation, need not be in the file at any time to save the format, if you like to use shortcut keys, in the "System Preferences" can be set to all the menu options shortcut keys, set a "Format File in Focus" shortcut key is also very useful.
Attach all available parameters of Clangformat document: http://clang.llvm.org/docs/ClangFormatStyleOptions.html
Attach the format I am currently using:
- # base Style
- Basedonstyle:llvm
- # Indent Width
- Indentwidth:4
- # line wrapping for parentheses
- Breakbeforebraces:attach
- # support for one-line if
- Allowshortifstatementsonasingleline: True
- # switch Case Indentation
- Indentcaselabels: True
- # The indent width of the block for OC
- Objcblockindentwidth:4
- # for OC, add a space after the attribute name
- Objcspaceafterproperty: True
- # The length of each line of characters
- columnlimit:0
- # Justification of comments
- Aligntrailingcomments: True
- # Add a space after parentheses
- Spaceaftercstylecast: True
- # do not add spaces in parentheses
- Spacesinparentheses: false
- # do not add spaces in brackets
- Spacesinsquarebrackets: false
Three, problems during installation: Starting with Xcode 5, Apple requires a UUID certificate to ensure the stability of the plugin. Therefore, after the Xcode version is updated, you need to add Xcode's uuid to the clangformat.xcodeproj info.plist file. The steps are as follows: Method One, view Xcode's UUID inTerminal execution Defaults read/applications/xcode.app/contents/info dvtplugincompatibilityuuidcopies the selected string. Locate Xcode.app in the/applications directory, right-click "Show Package Contents" and go to Contentsfolder, double-click info.plist to open, find Dvtplugincompatibilityuuid, copy the following string. Way two, add Xcode's uuid to Clangformat.xcodeproj's Info.plist file 1, openthe directory where the Xcode plugin is located: ~/library/application support/developer/shared/xcode/plug-ins;2, select the installed plug-in such as Clangformat, right-click "Show Package Contents"; 3. Locate the Info.plist file, locate the Dvtplugincompatibilityuuids project, add an item, Value for the previous Xcode uuid, save. Plug-in installation Experience Summary: In each Xcode upgrade after his dvtplugincompatibilityuuids content will be updated, but the plug-in is not updated, so to download a good plug-in configuration and then run, so it will be a success.
Four, with Xcode's own format operation, it is very good
Select the content combination action:
First step: Clangformat (Control+u)
Step Two: Xcodeformat (control+i)
Select the file combination operation:
First step: Clangformat (Control+shift+u)
Step Two: Xcodeformat (control+a,control+i)
Modify the contents of the trvsclangformat.m file in the Clangformat.xcodeproj project to implement the shortcut key functions (Control+u and Control+shift+u):
1-(void) Addactioningmenuitemstoformatmenu {2 Nsmenuitem *formatactivefileitem = [[Nsmenuitem alloc] 3 initwith Title:nslocalizedstring (@ "Format File in Focus", nil) 4 action: @selector (formatactivefile) 5 Keyequival ent:@ ""]; 6 [Formatactivefileitem SetTarget:self.formatter]; 7 [Self.formatmenu Additem:formatactivefileitem]; 8 Nsmenuitem *formatselectedcharacters = [[Nsmenuitem alloc] 9 initwithtitle:nslocalizedstring (@ "Format Selected T Ext ", nil) Action: @selector (formatselectedcharacters) One keyequivalent:@" U "]; Modified by Kenmu12 [Formatselectedcharacters Setkeyequivalentmodifiermask:nscontrolkeymask]; Created by Kenmu, in order to use shortcut key to access it.13 [Formatselectedcharacters settarget:self.formatter];14 [Self.formatmenu additem:formatselectedcharacters];15 Nsmenuitem *formatselectedfilesitem = [[NSMenuItem alloc]16 Initwithtitle:nslocalizedstring (@ "Format Selected Files", nil) 17 Action: @selector (Formatselectedfiles) keyequivalent:@ "U"]; Modified by Kenmu19 [Formatselectedfilesitem setkeyequivalentmodifiermask:nscontrolkeymask | Nsshiftkeymask]; Created by, in order to use shortcut key to access it. Kenmu20 [Formatselectedfilesitem settarget:self.formatter];21 [Self.formatmenu additem:formatselectedfilesitem];22 }
The same way as the Vvdocumenter Specification Note Generator is installed:
Download Open source project The plugin will be installed automatically when the Xcode is recompiled and you can restart Xcode to use the
PS: You can use System Preferences to set the keyboard shortcut keys for an application, as follows:
Plugin settings:
How to use:
iOS plugin----Clangformat (code format Management plugin) (2016.1.12 Wang Bin)