Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Xcode for proper javascript formatting?

Xcode3 (and newer ;) doesn't properly format Javascript when I type something like:

doSomething(somewhere, function(err, result) { 

It inserts a huge indentation. I tried to change Xcode formatting rules but no luck. One way or another it breaks indentations.

Is there a proper way to use Xcode for javascript development?

like image 687
egorFiNE Avatar asked Jan 11 '11 13:01

egorFiNE


2 Answers

With some tweaking it is possible to modify the way Xcode indents JavaScript by supplying a custom xclangspec file. Xcode for formatting and syntax uses language definitions in xclangspec files kept in directory SharedFrameworks/DVTFoundation.framework/Versions/A/Resources. Since the formatting for C language works actually better for JavaScript than the Xcode original JavaScript formatting, it is possible to use some parts of definition for C in JS definition. More details you can find at http://www.conhar.com/xcode-and-javascript/.

like image 114
Wojtek Surowka Avatar answered Oct 24 '22 00:10

Wojtek Surowka


What worked fairly well for me is to switch the .js files to use C syntax coloring.

In XCode 4: Editor -> Syntax Coloring -> C

It still highlights numbers and strings and comments, and it indents braces in a sane way.

like image 6
Claudiu Avatar answered Oct 23 '22 22:10

Claudiu