Are there any hidden code formatting settings for Javascript in NetBeans?
Asked Answered
V

2

7

I do PHP/Javascript development in NetBeans, and I really like the environment, except for one thing - in Javascript, when I press ENTER after a statement, and type the opening brace, it is indented. Like this:

if ( a == b )
    {}

I want the brace to stay on the same level, like this:

if ( a == b )
{}

So that when I press ENTER again, I'd get this:

if ( a == b )
{

}

Can this be done and how?

Vertebral answered 13/1, 2011 at 23:45 Comment(1)
I also seek a solution to javascript formatting for javascript on netbeans 7.x but I'm too thrifty to start a bounty for it.Messaline
C
2

Sorry, I don't have the answer to your question. I too have searched in vain for somewhere in NetBeans 6 to configure JavaScript formatting.

However, you should note the following:
In languages like Java it's legitimate to choose between opening brace on the same line vs. opening brace on a newline. In JavaScript, however, you should really stick to the former as the latter can give rise to ambiguities that can affect the interpretation of the code. See here. (I know the example you cite relates to the if statement, but presumably you want to be consistent.)

Christiansand answered 29/5, 2011 at 14:49 Comment(1)
Yes, I'm aware of this, but in most cases it presents no problems, so I'd like a consistent formatting in all my code.Vertebral
M
1

Great news for netbeans devotes here: (netbeans 7.0)

Tools -> Options > Editor > Code Templates: choose Language (Javascript in this case)

Look for "if" Abbreviation:

Change Expanded text definition:

from this:

if (${expr}){
    ${cursor}
}

to this:

if (${expr})
{
    ${cursor}
}

Save options.

Now inside a js file type if and press [tab]... and you got it...

Can you imagine all possibilities with these templates?

Match answered 30/6, 2011 at 0:53 Comment(4)
Cute, but not exactly what I wanted.Vertebral
@Vilx Yes, is not exactly the same, but are you tried?. You just push 3 keys and cursor is positioned to enter the argument and braces are positioned as you want. You can change [tab] to [enter] with "Expand Template on" at the bottom of the options window!Match
I'd like to adapt my IDE to my typing habits, instead of adapting my typing habits to my IDE.Vertebral
All wants the same. Look, I arrived to this topic looking for a solution for exactly the same problem you say. I have another one: make a code template with just 2 braces opened and assign it to one key (in my case is _ (spanish keyboard) that is near to [Enter] key). So now with just 2 keystrokes I have braces opened to the left margin! If that is not adapt the IDE I don't know what more can be. Ie: just delete if (${expr}) in the Expanded text definition I showed.Match

© 2022 - 2024 — McMap. All rights reserved.