14

IntelliJ does some odd formatting with Javascript code and I am trying to figure out how to get it to stop formatting this way. Whenever I chain jQuery functions together, it over indents the auto created code such as this:

$('#something').focus(function() {
    /* Do some stuff */
}).blur(function() {
        /* this is where the cursor and closing braces end up */
        })

I want it to look like this:

$('#something').focus(function() {
    /* Do some stuff */
}).blur(function() {
    /* cursor and closing braces indented normally */
});

How do I change this?

intargc
  • 249
  • 1
  • 2
  • 4
  • the fixes here didn't work for me so I got in touch with jetbrains and they've opened an issue. Please vote for it here: https://youtrack.jetbrains.com/issue/WEB-15066 – JonnyRaa Feb 04 '15 at 13:35
  • 5
    I am voting to close this question as off-topic because it is about a software tool, however, it is too old to migrate to Stack Overflow. –  May 12 '15 at 22:37

2 Answers2

11

As @intarg mentions, in the comments. You can change File|Settings|Code Style|General and on the Javascript tab set Continuation Indent to 0 for Javascript.

That will get you most of what you want. the problem is that your code is actually all 1 long statement.

$('#something').focus(
function() {
    /* Do some stuff */
}).blur(function() {
    /* this is where the cursor and closing braces end up */
})

Not sure that there is a relevant option to prevent a line break after focus(

Oh, and be sure that you have the Javascript code sample tab active when you are changing options - otherwise you will not change them for Javascript.

EDIT: Now that I have looked further. The Javascript formatter is coded to add a linebreak in that specific case, there isn't an option for it.

sylvanaar
  • 2,295
  • 1
  • 19
  • 26
0

All the settings you need to control code formatting in intellij are in File>Other Settings>Template Settings.

If Javascript is not listed (i.e you've not installed a Javascript plugin), you can define the code formatting under the General tab and then the Other tab. There will be options for controlling indentation and spacing.

Desolate Planet
  • 6,038
  • 3
  • 29
  • 38
  • I think the problem is that it thinks the function from .blur() is a continuation indent. The only way I can stop it from doing this is to set continuation indent to 0, but that makes other code look bad too. Would you happen to know of a way to tell it this is not a continuation indent or is that some internal stuff going on? – intargc Sep 04 '11 at 21:08
  • Have you installed a Javascript plugin into intellij? I know there are a few available. If your not using any of them, the IDE will simply fallback on standard indentation/spacing. You'd ideally use a plugin to make the IDE aware of these constructs. – Desolate Planet Sep 04 '11 at 21:36
  • Have you tried jetbrains? Also, have you tried doing this with Eclipse. I've not touched intellij in a while, but I know Eclipse has got pretty good plugin support and it's easy enough to control indentation, but you really shouldn't have a problem with intellij. – Desolate Planet Sep 04 '11 at 21:39
  • Since the community edition is open source, you can always get the source code, build it and insert breakpoints where it performs the formatting to rule out if there is an internal issue. If it doesn't do what you want, apply a fix and submit a patch to one of the repo owners, if you've got the time and inclination to do so:) – Desolate Planet Sep 04 '11 at 21:47
  • I do have the javascript plugin installed. I'll try jetbrains and see if anyone there has a clue. Thanks. – intargc Sep 04 '11 at 21:49