iOS plugin----Clangformat (code format Management plugin) (2016.1.12 Wang Bin)

Source: Internet
Author: User
Tags switch case

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:

  1. # base Style
  2. Basedonstyle:llvm
  3. # Indent Width
  4. Indentwidth:4
  5. # line wrapping for parentheses
  6. Breakbeforebraces:attach
  7. # support for one-line if
  8. Allowshortifstatementsonasingleline:true
  9. # switch Case Indentation
  10. Indentcaselabels:true
  11. # The indent width of the block for OC
  12. Objcblockindentwidth:4
  13. # for OC, add a space after the attribute name
  14. Objcspaceafterproperty:true
  15. # The length of each line of characters
  16. columnlimit:0
  17. # Justification of comments
  18. Aligntrailingcomments:true
  19. # Add a space after parentheses
  20. 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:

  1. # base Style
  2. Basedonstyle:llvm
  3. # Indent Width
  4. Indentwidth:4
  5. # line wrapping for parentheses
  6. Breakbeforebraces:attach
  7. # support for one-line if
  8. Allowshortifstatementsonasingleline: True
  9. # switch Case Indentation
  10. Indentcaselabels: True
  11. # The indent width of the block for OC
  12. Objcblockindentwidth:4
  13. # for OC, add a space after the attribute name
  14. Objcspaceafterproperty: True
  15. # The length of each line of characters
  16. columnlimit:0
  17. # Justification of comments
  18. Aligntrailingcomments: True
  19. # Add a space after parentheses
  20. Spaceaftercstylecast: True
  21. # do not add spaces in parentheses
  22. Spacesinparentheses: false
  23. # do not add spaces in brackets
  24. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.