I currently have Eclipse formatter set up to format an 'if-else' statement like so:
if(condition) {
return foo;
}
else{
return bar;
}
Note the space between the closing parenthesis of the condition, and the opening bracket of the true block - I'd like to remove this space.
In the formatter profile, under Whitespace -> Control statements -> 'if else', there is no option for 'after closing parenthesis'. In the Blocks section, there is an option for 'before opening brace', however this appears to only apply to the false block (and is turned off anyway).
I must be missing an option somewhere - how can I turn this whitespace off?
I am using Eclipse Mars 4.5.0.
You can do that in the Windows -> Preferences -> Java -> Code Style ->formatter -> Edit profile section
I think it does once you select the other option in drop down that you can see below
This is due to Eclipse Bug # 471145 (specific to Mars / 4.5.0), which was reported on 2015-06-26, fixed on 2015-08-01, and will be included in Eclipse 4.5.1 - which should be complete and ready for download by the end of September 2015 as part of the Mars coordinated service release.
Go to Windows > Preferences > Java > Code Style > Formatter > White Space and in Control statements > Blocks, unselect the "before opening brace" option :)
Related
I am using IntelliJ code style format definition published at https://github.com/airlift/codestyle/blob/f20834967969cdafce461ee203788e567f842e1e/IntelliJIdea2019/Airlift.xml
IntelliJ 2020.3.4 (and I think all previous versions I used) would format single line throwing lambda like this
Consumer<String> unimplemented = value -> { throw new UnsupportedOperationException(); };
While formatting the above, IntelliJ 2021.2.3 removes spaces inside curly braces:
Consumer<String> unimplemented = value -> {throw new UnsupportedOperationException();};
How to make IntelliJ 2021.2.3 format the code the way older versions did?
In the Code Style settings, go to the "Spaces" tab and find "Within -> Code Braces". Turn it on.
Or if you want to edit the code style XML directly, you can add:
<option name="SPACE_WITHIN_BRACES" value="true" />
in the block
<codeStyleSettings language="JAVA">
...
</codeStyleSettings>
Previous versions of eclipse block comment style for Ctrl+Shift+/
/*
line 1
line 2
line 3
*/
Current Version
/*
* line 1
* line 2
* line 3
*/
It fhifts to right and adds extra spaces and format changes , when i do ctrl+shift+\
others lines of code
whitespaces
whitespaces line 1
whitespaces line 2
whitespaces line 3
whitespaces
others lines of code
Even if i do ctrl+shift = F the format doesn't changes ,
How do i get old formatting??
You can change the formatting as per your preference in eclipse.
Eclipse use profile to save your formatting preferences, You can use the built-in profile or create a new profile.
In Menu Bar
**Window > Preferences > Java > Code Style > Formatter **
Under Active Profile you will find Eclipse build-in, Now you can select this and edit or create a new one.
Best practice will be to create a new profile and **Export it **, So in future, you can import your preferences in the New system or Eclipse.
There are multiple options are available to customize when you edit the profile.
If you are facing issue regarding shortcuts edit your preferences in :
Window > Preferences > General > Keys
You can also export this as CSV for future use.
eclipse.
In IntelliJ idea when I insert the foreach live template it will put newline after ':' so it will look like this:
for ( :
) {
}
I want to have the for statement on one line like this:
for ( : ) {
}
I tried to change my code formatting preferences, but could not figure out what setting influences this particular case.
So my question is how to set code style options to achieve the desired behavior?
Use the iter live template rather than the foreach. foreach is under the Android block, and the default style for that is what adds the newline.
Update:
As of at least 2018.1.1 (not sure when it was added), you can now type the <name of your collection>.for then tab and it will expand out into a foreach loop.
It's also brought in the same surrounding/expansion for stuff like <array>.stream then tab and probably a few others I'm not aware of.
Go to File -> Settings -> Editor -> Code Style -> Live Template.
At the right side open Android list and stay on foreach .
In the Options area uncheck Reformat according to style.
You can see how to do it in the IntelliJ IDEA settings foreach style
You can change the template for the enhanced for loop in IntelliJ by changing the setting in Live Templates.
Go to File -> Settings -> Editor -> Live Templates. In the right side, choose iterations -> "iter (Iterate Iterable | Array in J2SDK 5.0 syntax)". At the bottom you can see the template text and you can change it by introducing the newline where you want it. Change
for ($ELEMENT_TYPE$ $VAR$ : $ITERABLE_TYPE$) {
$END$
}
to
for ($ELEMENT_TYPE$ $VAR$ :
$ITERABLE_TYPE$) {
$END$
}
and apply your changes.
In the source code editor, choose Code -> Insert Live Template... -> iter, then IntelliJ will insert the code template as you've specified, with boxes around the variable names for changing them.
for (String arg :
args)
{
}
Using Eclipse Juno I've got the following issue using the Java code formatter:
If a method call leads to a line break, Eclipse inserts a whitespace before the "dot" of the method invocation (_ shall denote the whitespace)
int nbo = new Foo()//linebreak
_.method();
Unfortunately this whitespace triggers a Checkstyle warning (NoWhitespaceBefore rule). Is there some possibility to stop eclipse inserting the whitespace? Or is it a Checkstyle configuration issue?
Thanks for your help in advance.
You can increase Maximum line width to 120 or bigger number.
On menu Windows -> Preferences
Navigate to Java -> Code Style -> Formatter
Click on Edit button to open a new dialog
On Line Wrapping tab, change Maximum line width
Finally, change Profile name and click OK
You can resolve this either by changing the formatter settings or by changing the Checkstyle configuration.
Option 1 - Formatter Settings (for Eclipse 4.3, but should be the same for Juno)
Window → Preferences → Java → Code Style → Formatter → Edit
Line Wrapping → Function Calls → Qualified Invocations
Set to Do Not wrap
Option 2 - Checkstyle Config
Remove the DOT token from the NoWhitespaceBefore rule:
<module name="NoWhitespaceBefore">
<property name="tokens" value="SEMI,POST_DEC,POST_INC"/>
</module>
I want the formatter to stop deleting white spaces in declaration fields
This is what the formatter does when I hit ctrl+shift+f
and this is how I want it to be(the alignment of the values should be kept):
I just don't find where to change it in Formatter settings
In eclipse 3.7 it works this way (Preferences > Java > Code Style > Formatter):