11

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?

egorFiNE
  • 303
  • 1
  • 3
  • 13
  • I use stupid workaround: Editor->Syntax Coloring->swift, select all, re-indent, and back to Editor->Syntax Coloring->Default for file type. – Tzoiker Jul 28 '15 at 14:00
  • 10 years later .. Editor -> Syntax Coloring **it appears Xcode now supports javascript** – Fattie Mar 02 '20 at 13:59
  • .. but, it does a horrific job. Change to "C" coloring as in the excellent suggestion below. – Fattie Mar 06 '20 at 16:08

5 Answers5

6

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.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
3

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/.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
2

A quick solution is to choose View -> Syntax Coloring -> Simple Coloring. If you are okay to edit Javascript without fancy syntax highlight, this is pretty useful.

This way only the numbers and strings are colored, and the indentation stops being so annoying.

  • In my version of xcode this option is under Editor >> Syntax Coloring but I can't find a Simple Coloring option... There is a None option which I tried but didn't solve my problem... – Nathan Prather Aug 01 '13 at 14:00
2

Unfortunatelly XCode has very limited formatting options. But you can use uncrustify which is pretty good. You can find some information here:
Xcode source automatic formatting
Objective-C Tidy

These articles all talk about formatting objective-c code, however uncrustify can format the source code of various languages ;)

Hope this helps.

Community
  • 1
  • 1
Moszi
  • 3,236
  • 2
  • 22
  • 20
0

Uncrustify requires a .cfg file to setup which can be somewhat overwhelming. There's an alternative here

Community
  • 1
  • 1
cube
  • 1,774
  • 4
  • 23
  • 33