Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer
    
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 4 '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 4 '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 4 '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 4 '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 4 '11 at 21:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.