Xcode source automatic formatting

后端 未结 19 632
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 16:10

As a C# developer, I have become highly dependent on the automatic formatting in Visual Studio 2008. Specifically, I will use the CTRL + K , D

19条回答
  •  遥遥无期
    2020-11-29 16:27

    We can use Xcode Formatter which uses uncrustify to easily format your source code as your team exactly wants to be!.

    Installation The recommended way is to clone GitHub project or download it from https://github.com/octo-online/Xcode-formatter and add the CodeFormatter directory in your Xcode project to get : Xcode shortcut-based code formatting: a shortcut to format modified sources in the current workspace automatic code formatting: add a build phase to your project to format current sources when application builds all sources formatting: format all your code with one command line your formatting rules shared by project: edit and use a same configuration file with your project dev team 1) How to setup the code formatter for your project Install uncrustify The simplest way is to use brew: $ brew install uncrustify

    To install brew: $ ruby –e “$(curl –fsSkl raw.github.com/mxcl/homebrew/go)”

    Check that uncrustify is located in /usr/local/bin $ which uncrustify

    If your uncrustify version is lower than 0.60, you might have to install it manually since modern Objective-C syntax has been added recently. Add CodeFormatter directory beside your .xcodeproj file

    Check that your Xcode application is named "Xcode" (default name) You can see this name in the Applications/ directory (or your custom Xcode installation directory). Be carefull if you have multiple instances of Xcode on your mac: ensure that project's one is actually named "Xcode"! (Why this ? This name is used to find currently opened Xcode files. See CodeFormatter/Uncrustify_opened_Xcode_sources.workflow appleScript). Install the automator service Uncrustify_opened_Xcode_sources.workflow Copy this file to your ~/Library/Services/ folder (create this folder if needed).Be careful : by double-clicking the .workflow file, you will install it but the file will be removed! Be sure to leave a copy of it for other users.

    How to format opened files when building the project Add a build phase "run script" containing the following line:

    sh CodeFormatter/scripts/formatOpendSources.sh

    How to format files in command line

    To format currently opened files, use formatOpenedSources.sh:

    $sh CodeFormatter/scripts/formatOpendSources.sh

    To format all files, use formatAllSources.sh:

    $sh CodeFormatter/scripts/formatAllSources.sh PATH

    PATH must be replaced by your sources path.

    E:g; if project name is TestApp then the command will be

    $sh CodeFormatter/scripts/formatAllSources.sh TestApp

    it will look for all files in the project and will format all the files as configured in uncrustify_objective_c.cfg file.

    How to change formatter’s rules

    Edit CodeFormatter/uncrustify_objective_c.cfg open with TextEdit

提交回复
热议问题