Force "same line" on curly braces in Eclipse formatter - eclipse

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

Related

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

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.

Eclipse code formatter multiline function call closing bracket indention

Currently my eclipse formatter formats a multiline function call like this:
someObject.doSomething(
some().long().chain().of().methods()
);
But what I want is for eclipse to align the closing bracket with the method call:
someObject.doSomething(
some().long().chain().of().methods()
);
I have tried playing around with new line and wrapping rules in the code formatter but haven't been able to achieve this. What would be the solution?
After some time of digging I found a similar question which has an accepted answer but seems not to answer the same question:
Can the Eclipse formatter be configured to indent multiple lines between parenthesis properly?
The author of this question also states:
Edit: I found the settings for "Line Wrapping" -> "Default indentation
for wrapped lines" and "Default indentation for array initializes" and
have set them to "1" instead of "0". That is better for the array
initializers, but still doesn't indent closing parethesis to match the
opening parenthesis the way that I want it to:
The latest proposal on this does not take into account the closing );, but the first expression.
See Eclipse 4.23 (Q1 2022):
Method invocation wrapping indentation
It turns out it's not obvious how to indent a wrapped method invocation when the preceding expression itself is complex enough to also be wrapped into multiple lines.
Should the indentation be added to the existing indentation at the end of the expression, or just reset and assume that only the indentation of expression's first line matters?
Previously only the former behavior was available, now there's a setting to choose the latter.
The checkbox called Indent from the base expression's first line is located in the Line Wrapping > Wrapping settings > Function calls section, right under the Qualified invocations setting.

Disable line breaks after loops without body

Consider a Java while-loop without a body, for example this:
while (map.values().remove(value));
When formatting it (Shift + Alt + F), Eclipse breaks the line before the semicolon and indents it:
while (map.values().remove(value))
;
How can I configure the formatter to not add these line breaks?
Be aware that by setting this semicolon to a new line, you may avoid unintended endless loops and a long search to find them.
However, in Eclipse all code formatting properties are 1) in the project properties and 2) in the Eclipse properties. The project properties inherit from the Eclipse properties.
So if you open the project properties, type "format" in the search field. It will show you a "Formatter" menu item, that may depend on the current language (at least in Java it is called "Formatter"). Then, in this example, allow the project-specific settings, and then click edit. You will see a flood of possible details you can decide.
See here:
Then, in the tab "New Lines", uncheck the checkbox "Put empty statement on new line", in the section "Empty statements".
Often you have to try and search a bit because there are really many options. The part at right shows you a preview of the code formatting.

Eclipse Formatter Allow Multi Line ;

I have the Eclipse formatter configured to allow me to do one liners as much as possible. However sometimes I want related commands on the same line such as
temp.add; temp.flush(); ⇦Problem
Also I configured the formatter to allow my if statements to look like this
if(true) return;
But if I have multiple commands it will push it down to the next line but I want it to format like this
if(true) {n++; return;} ⇦Problem
It formats like this
if(true) {
n++;
return;
}
Is there a setting to retain these multi line commands but still format the spacing and everything? It gets to the point that I purposely use ternary operators as much as possible because I hate doing one logical step on multiple lines. If there is no way to configure this or a plugin to fix this I think I am going to just turn the formatter off. I know I can just use ctrl+shift+f but I enjoy it formatting every time I run my program.
You can use off/on tags to avoid formatting in certain regions of editor. if you are using eclipse version 3.6 onwards.
This post explains how to do this How to turn off the Eclipse code formatter for certain sections of Java code?

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.