VSCode Java Formatter don't linebreak when brackets omitted - visual-studio-code

When I type an if statement on one line
if (root == null) return null
VSCode's formatter is putting a linebreak, turning the above into
if (root == null)
return null;
How do I configure the formatter to leave statements like these on one line? I have gone through all the settings in java-formatter.xmland can't find one that sounds like it's talking about this.

you can find a setting in java-formatter.xml:
org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line
set it to true and try to re-format your source file.

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.

How do I modify the style that VSCode's javascript formatter uses to set a line continuation indent?

I am working in a JavaScript code base that has a style mandating 2 space indents, with 4 spaces for statements that continue onto the next line.
For example, the following is correctly formatted in this style guide:
if (cond1()
&& cond2()
&& cond3()) {
doSomething();
doSomethingElse();
}
Notice that doSomething() and && cond3() aren't aligned in the same column.
However, the VSCode JavaScript formatter, by default, formats as this:
if (cond1()
&& cond2()
&& cond3()) {
doSomething();
doSomethingElse();
}
Does the VS Code JavaScript formatter have settings that control this? Otherwise, is there a "standard" extension that replaces the built-in JavaScript formatter that supports this style?
I don't have that behaviour but I use the extension Prettier - Code formatter. Hope that can help you

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?

Auto Format Issue

I have to follow a particular coding standard, and I am having issues with netbeans formatting to one of the standards.
Here is how netbeans is formatting the code:
}else
{
Here is how I need it to format the code:
}
else
{
It does it for both else and elseif. Under Formatting in the Editor options, in the Braces category I have them all set to New Line, so why is it formatting the else and elseif on the same line as the closing brace?
That's indeed a bit misleading.
Under the category "Alignment" there is a group of checkboxes for "New Lines". You need to check there the "else" checkbox.

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