Giter Club home page Giter Club logo

xcode-formatter's Introduction

XCODE FORMATTER

Why a code formatter

A common problem with large iOS projects is the lack of support for a shared set of formatting rules. This leads over the time to unreadable and hard to maintain sources files.

Xcode Formatter uses uncrustify to easily format your source code as your team exactly wants it to be!

Simply 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, see this post : http://stackoverflow.com/questions/11862964/uncrustify-doesnt-support-for-the-new-objective-c-syntax-of-llvm-4-0/14202765#14202765

Add CodeFormatter directory beside your .xcodeproj file

CodeFormatter directory location

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.

  1. How to format opened files when building the project

Add a build phase "run script" containing the following line:

sh CodeFormatter/scripts/formatOpenedSources.sh

Add build phase

  1. How to format opened files with a shortcut

Add a shortcut to the Uncrustify_opened_Xcode_sources service

Go to Xcode > Services > Services preferences :

Create service shortcut

And set your shortcut :

Choose service shortcut

  1. How to format files in command line

To format currently opened files, use formatOpenedSources.sh:

$ sh CodeFormatter/scripts/formatOpenedSources.sh

To format all files, use formatAllSources.sh:

$ sh CodeFormatter/scripts/formatAllSources.sh PATH

PATH must be replaced by your sources path.

  1. How to change formatter’s rules

Edit CodeFormatter/uncrustify_objective_c.cfg

You can use UniversalIndentGUI (http://universalindent.sourceforge.net/) to simplify edition.

  1. FAQ

When formatting the current sources, an unknown error occurs and shows my code. What can I do?

Uncrustify (version 0.59) does not handle apple new literals for creating NSDictionary and NSArray like:

NSDictionary *dictionary = @{@0 : @"red",
 							 @1 : @"green",
 							 @2 : @"blue"};

Until a new version of uncrustify fixes this issue, you can use the old way with those literals:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"red", @1, 
                                                                       @"green", @2, 
                                                                       @"blue", @3, 
                                                                       nil];

I want to have a single configuration file for all my Xcode projects, is it possible?

Yes, everything is possible. Just move your configuration file to your home directory: '~/.uncrustify//uncrustify_objective_c.cfg' and remove 'CodeFormatter' directory from your project. If the configuration file is not found in project directory, this global file is used.

xcode-formatter's People

Contributors

hyukhur avatar mwalbrou avatar omartin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.