Are there any hidden code formatting settings for Javascript in NetBeans? - netbeans

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?

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

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?

Related

How can I use curly braces in Netbeans code templates for example for an slf4j template?

Creating a Netbeans code template for creating an slf logger is described here:
http://wiki.netbeans.org/SLF4JCodeTemplate
However creating code templates for log statements, e.g.
logger.debug("Something: {}", var);
is harder than expected because the template language doesn't balance curly braces. This means it will end the capture at the first ending curly brace.
There exist some examples, like for example How to get current class name in Netbeans code template? but they do not touch into the curly brace issue.
I have tried to escape them in every way I could think of so farm including:
${LOGGER default="logger" editable=false}.debug("${logMessage}${: '{}'}", ${EXP instanceof="<any>" default="exp"});
and
${LOGGER default="logger" editable=false}.debug("${logMessage}${: \{\}}", ${EXP instanceof="<any>" default="exp"});
but no luck. Also my google skills have been failing me so far.
Turns out there is a simple solution. I didn't find it anywhere near anything about netbeans code templates, but under a question about freemarker:
How to output ${expression} in Freemarker without it being interpreted?
Basically the answer is to use r"..." around the code, like this:
${LOGGER default="logger" editable=false}.debug("${logMessage}${:r"{}"}", ${EXP instanceof="<any>" default="exp"});
Now this can be assigned to sld, so I can type slt, expand it to:
logger.debug("logMessage: {}", <last variable>);
Where "logMessage" is selected (so I can overwrite it with something useful, one tab selects ": {}" so I can delete it if I want to log without parameters and a last tab selects which is the last assigned value (in case I want to replace or remove it).

How do I modify the EL opening template in Eclipse?

Whenever I am working in a JSP file and I type ${ to start an el (Expression Language) tag, Eclipse will automatically add } (with a space before the closing brace) after the cursor so that I get ${ } instead of ${}.
Is there a code template in Preferences that I can modify to change this behavior, or is it beyond user preference control?
I have checked in Preferences: Web: JSP Files: Editor: Templates, but none of those templates match. I've also looked in several other sections in Preferences but haven't found anything promising.
What #Mero provided (see comments on answer above) might not be an exact answer, but creating a JSP Template probably the closest thing that I've found.
A few notes for anyone that wants to go that route:
Create a new template through menu Window->Preferences, then in the drill down menu navigate to Web->JSP Files->Editor->Templates. Click New.
Name is a shortcut you can type (the same way typing sysout ctrl+space in Java is a shortcut for System.out.println()). I suggest something simple like el. This allows you to type e l ctrl-space instead of $ { ctrl-space to pull it up.
Context tells it when it should appear in intellisense. I suggest creating two of this template where one has a context of JSP Attribute value and the other has a context of All JSP.
Description is just informative. Put whatever you want. I put 'EL Script' myself.
Pattern is where you put what will be inserted. Put $${${cursor}} or $${${script}}, depending on preference. See below for explanation on the differences.
In Eclipse Templates ${} is how you put variables in the template, so to make it actually print ${} you have to escape the $ with a $$ leading to $${}.
The predefined variable ${cursor} defines where the cursor is after intellisense replaces the el, so to have the cursor appear in between the curly braces you want to do this: $${${cursor}}.
Using any variable that is not predefined (in this case, ${script}) will simply put in that variable with a box around it and allow you to type over it and press enter when you're done, allowing you to move to the end of the closing curly brace.
Note: I understand that this is not an actual answer, but rather is a workaround. I'm putting it here simply so that those who are fine with a workaround can know how to go about doing it.
Edit
For those that don't like having to type ctrl-space, a workaround could be to have the template name start with< since on JSP pages, the < opens the intellisense, so for instance, you could have the name be <el or <$.
A workaround but not an answer:
Disable auto-close of EL tags. You type ${expression} and get ${expression}|, rather than typing ${expression and getting ${expression| }. (| denotes the cursor location)
See this answer, from when this same question was asked of Eclipse Kepler: https://stackoverflow.com/a/20258401/1021426

Shortcut for "code block" macro in Confluence editor

We use confluence for documentaion but i find very time consuming to select the code macro; it's a 5 step process. Even typing the macro by hand is not efficient.
In the Stack Overflow editor all we have to do is select the text and press a button or hit ctrlK, and the text is formatted as code.
Is there a way to do this in Confluence?
even typing by hand is also not efficient
I use the code macro extensively and always use the autocomplete feature by typing { and choosing 'Code Macro' from the list (It's enough to type co for the code macro).
This is a very efficient.
Of course a keyboard shortcut would be faster, but there is no shortcut for the code macro. (AFAIK there is no keyboard shortcut for a specific macro at all)
I use Ctrl-Shift-D then wrap the text in {code}.
This also fixes the problem with formatting being stripped from pasted text.
In Confluence 5.x if you edit a page, you can type {cod<enter} and it puts a Code Block box on the page, but when code is pasted into this box it can strip out end of line characters.
Open the Insert Markup window using Ctrl-Shift-D
Paste in your code as plain text This way the formatting is not stripped out.
Add {code} tags.
You can also type three back ticks ``` to create a code block as you would in vanilla Markdown. This creates an empty code block very quickly. I never have with problems with formatting when pasting code in this way.
The one irritating feature of this method is that you can't specify the language as you do in Markdown, you have to select the language from a list.

Is there a way to fold eclipse sub-blocks like an "if" statement?

Currently Eclipse only fold the java doc and at function level, but when reading long methods, there could be quite a lot of if/else etc, is there a way to fold them?
I found the Coffee-Bytes plugin. I downloaded it from this link and found this guide by the author, for using it.
You can find more details in these references:
What code folding plugins work on Eclipse 3.6?
How to use Coffee-Bytes code folding
in updated versions of Eclipse
Change folding preferences at:
Window -> Preferences -> C/C++ -> Editor -> Folding -> Enable folding of preprocessor branches (#if/#else)
Enable folding using ctrl + shift + /
No, in the Preferences Dialog (Menu Window/Prefernces): Java/Editor/Folding you may choose,
Comments
Head Comments
Inner Types
Members and Imports
if Enable Folding is checked.
If you wan't to do this because the blocks are so long that can't reconize the structure
you should consider to split if/else blocks into methods using Alt-Shift-M (Extract Method)
It appears Eclipse does not have built-in support for folding if/else statements but allows folding for other more complex cases like Anonymous inner classes. Try looking for plugins like this one (last modified 2007, try it if it supports your Eclipse version).
Ok, this is a little bit older, but maybe someone could find this useful:
In most cases you can surround the piece of code by an additional pair of scope brackets, and to remember what you folded you can add a line comment.
For example, if you want to collapse the following:
int SectionA_var1;
int SectionA_var2;
int SectionA_var3;
int SectionA_var4;
int SectionA_var5;
int SectionB_var1;
just add the brackets an the comment:
{ // SectionA
int SectionA_var1;
int SectionA_var2;
int SectionA_var3;
int SectionA_var4;
int SectionA_var5;
}
int SectionB_var1;
Then you get the (-) sign and you can collapse the whole section to this:
{ // SectionA[...]
int SectionB_var1;
No plugin necessary, and until now I had no situation where this gave me any downsides, except that you cannot use it on a top level declaration to collapse methods.
For now, there is built-in function.
Click "Window->Preferences->C/C++->Editor->Folding" and then enable appropriate option you want.
Apply, close and either refresh project or reopen eclipse.
As weird as it looks like, sounds like developers never thought about that. if you have a big if statement or any switch/loop ... just use notepad++ to be able to fold/unfold
For Python, i.e. Eclipse/PyDev, go to Windows > Preferences > PyDev > Editor > Code Folding and check all the boxes.
Fold java source code like "if else for" statement
install pluins com.cb.eclipse.folding
restart your Eclipse make sure the pluins enabled
Click "Window->Preferences->Java->Editor->Folding"
Select folding: select "Coffee Bytes Java Folding"
Switch to "User Defined Regions"
"Start Identifier" = { ; End Identifier = }
click "Apply and Close"
Reopen java source editor you will see "if" or "for" block is collapsable

Custom key actions in Eclipse

Today I have question about Eclipse. I use this IDE very long and I think it is good, but last time I miss for some functionalities...
Is it possible to set some shortcut which will do something like:
Mark some text ('Hello world'), trigger shortcut (Ctrl+T) and it will do something with that text - in example adds text before and after selected text ($this->_('Hello world'))
?
Thanks for any sugestion !
From this, it appears you have to implement your own command in a plugin. The process looks more involved than simply setting a menu choice.
Equivalent functionality can be defined without commands, if you're willing to give up the keyboard shortcut and use content assist instead.
I'm not sure if it will work with the language you're using (PHP?), but with Java in Eclipse it is possible to use Code Templates.
You would define your own template, when it was applicable and what it would do. This could then be accessed with Ctrl+Space through the possible content assist methods. So in the context of Java statements, I can define:
this.call(${word_selection});
So when I highlight a word, such as "Hello, world", I can use the template to change it to:
this.call("Hello, world");
(There are ways to limit it to only String types instead of word selections, but that will most likely not apply to your language, so I didn't pursue exactly how to do it.)
The Code Templates menu is available through Window->Preferences.