How to format empty braces on same line (Eclipse formatter) - eclipse

Where are the formatting settings in Eclipse to format an empty constructor like this
public ThisIsAConstructor() {} //this is supposed to be on one line
instead of
public ThisIsAConstructor() {
} //this is supposed to be on one line
I set everything in tab 'Brace positions' --> Same line. I am using the Google style guide (https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml) with some custom adaptions.
Edit: I had some whitespaces in between, which prevented the expected formatting. If I have code like
public ThisIsAConstructor() {
} //this is supposed to be on one line
I have to format twice (one for removing the empty line via tab 'Blank Lines' an one for putting braces on one line. Is there a way to do that in one step?

There is an option to keep empty braces on the same line for most Java constructs, but unfortunately not for class constructors, as far as I could make out. If you haven't discovered it yet, here is how you get there:
In the Eclipse preferences, go to Java -> Code Style -> Formatter and click on Edit....
This should open a new window where you can edit your profile.
On the hand left side, go to New Lines -> Keep braced code on one line. Here you can find a couple of constructs. For each you can set a condition in which the braces should be on one line. Choose If empty respectively.

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

highlight expressions on netbeans

I want to know if there is a way to highlight the full content in an expression like if, while, etc.
I have to work in a code that is bad indented where expressions can cover over 1000 lines and I can't format it.
for exemple :
if ($somethingTrue) {
while ($somethingHappen) {
// 500 lines...
} }
if ($someCondition) {
}
look, it's very hard to see the end of the first if and what it cover...
Does someone know if there is a native feature or plugin on netbeans that do this job ?
Thanks !
You don't need any plugin/external jar file for achieving the same. Whichever expression you want to check the body of, just click on the opening brace after that expression and NetBeans will automatically show you the closing brace for that expression.
Basically, Netbeans shows the ending brace for a corresponding starting brace entry---for each of if-statements,loop-statements,method declarations,etc.
In the shown figure, see my if-statement starting with yellow brace(cursor blinking there), and the corresponding ending brace for the if-statement.
EDIT :-
You also can also have a brand new-code fold,just by typing fcom, and hitting (Tab) button on KeyBoard. And, then put whatever block you want inside it and done. Expand whenever you wish and collapse whenever you want.
Check the position of my mouse-pointer which shows the current block of if-statement. You can expand and collapse as per your wish. And, also you can have several of them for each of your expression-tree.

Netbeans generated code settings

I was wondering where can I change the way Netbeans generates code. For example, I want the opening bracket to be on a new line.
I've been looking in the [Tools] -> [Options] menu but I didn't find anything.
Solution 1 should solve your problem. For any auto-generated code formatting that Solution 1 won't solve, use Solution 2. Beware the "Code Templates" tab though, it has been relatively buggy for me in the past.
Important Note: Attempting to modify the "Code Templates" for the placement of braces won't work correctly because it's overridden by the preferences in Solution 1. Any changes to formatting wanted should first be attempted with Solution 1, as the "Formatting" tab in Netbeans takes precedence over the "Code Templates" tab.
Solution 1:
Go to [Tools] -> [Options] -> [Editor] -> [Formatting]. Choose the language you are using, then, for the Category drop down box, select Braces. Directly under that you'll see Braces Placement. You can modify the placement of the braces for a class declaration, method declaration, or "other". Options are:
Same Line - places bracket on same line as class, method, or "other".
New Line - places bracket on a new line under the class, method, or "other".
New Line Half Indented - same as "New Line" with a half indention added.
New Line Indented - same as "New Line" with a full indention added.
Solution 2:
Go to [Tools] -> [Options] -> [Editor] -> [Code Templates]. Choose the language you are using, then modify the templates according to what formatting you'd prefer.
For example, if Java is selected, I can scroll to the "pm" abbreviation and see how Netbeans generates private methods. Here is a incomplete but helpful abbreviation-to-meaning list for a few common Java abbreviations you might want to change.
m - Method
p - Private
f - Final
s - Static
Therefore, if you want to change the formatting for an auto-generated method that is listed as "private", you can change the formatting for the abbreviation "pm" in the "Expanded Text" area, which shows the auto-generation rules/formatting for a private method.
Note: Combinations of these letters can sometimes mean something different, for example "pr" is expanded to "private", but "prm" is listed as a protected method, not a private method.

Force "same line" on curly braces in Eclipse formatter

In my java code formatter, in my profile I have (actually it's the eclipse's default) all choices in the "braces" tab set to "same line". Nevertheless when I have a piece of code like this:
interface TestI
{
}
and I invoke the formatter (key binding, context menu, whatever) all I get is
interface TestI
{
}
it doesn't move the opening braces to the same line, the same applies to methods, control statements etc. then when I manually change it to
interface TestI {
}
it then formats it to:
interface TestI {
}
So it generally respects the format I did manually but only formats the spaces between the braces and other elements.
Other formatting options in my formatter work as as specified. I'm using the latest Eclipse Indigo release 3.7.1, I had the same with 3.7, and my friend tried it with a 3.5 release, so it makes me think that this is on purpose, but then again why is there this "braces" tab at all if I can't reformat the code using this rule ...
I hope I'm missing something and that you will make me fell stupid in a moment and the problem will go away, because I have a gazillion of classes to cleanup and I don't want to do it by hand .. :)
UPDATE: when I set the rule not to "same line" but "next line" then formatting works as expected, it forces the rule regardless of how I've written the code, as soon as I change it back to "same line" the the formatter doesn't force this rule.
Found it... it was caused by:
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>
which in the Formatter setting GUI is under "Line Wrapping/Never join already wrapped lines"
yes now I remember setting it, but I never thought that this would override the braces setting ...
a nice-to-have feature of the formatter would be to have warnings if you set options that are no-ops because of other options that you've set override them.
For those wondering how to make this apply to array initializers, the settings are
New Lines > After opening brace of array initializer
New Lines > Before closing brace of array initializer

Help with Eclipse line wrapping

I'm trying to get something like:
someObject.firstFunctionCall().secondFunctionCall().thirdFunctionCall();
to look like:
someObject.firstFunctionCall()
.secondFunctionCall()
.thirdFunctionCall()
I played around with the formatting editor and tried searching to no avail. I just can't think of the name for multiple function calls in one statement. I can do it myself but then it reverts to the top example every time I run the formatter.
You'll have to format it yourself.
To configure the formatter to not rewrap already wrapped lines, you can select this option in the "Line Wrapping" section of the formatter:
Never join already wrapped lines
Though it will change the indentation of the two lines.