Stop Code Format from removing whitespaces in eclipse - java

I've started using Java again recently and I'm setting up my dev environment in eclipse which has a handy format function that formats the code for you. This is great cause I've been able to set the settings to fit my code standard almost perfectly except for local variable declaration. I like to keep my declarations lined up (there is even an option for class parameter declaration that does what I want)
Here is what I would like :
void jad()
{
int alpha = 1;
String beta = "jad";
MyOwnObject SomeReallyLongName = new MyOwnObject();
}
Instead, eclipse's format gives me :
void jad()
{
int alpha = 1;
String beta = "jad";
MyOwnObject SomeReallyLongName = new MyOwnObject();
}
I don't mind if it doesn't format it the way I want it too, I would just love it if it didn't remove the whitespaces that I put there myself >.< (if it can do it for me, that would be even better of course).
Anybody know how to get eclipse to do something like this? (couldn't find any option in the Code Style editor)

Actually the formatter will do this alignment for you. Go into Preference -> Java -> Code Style -> Formatter and Edit the profile. Look at the Indentation tab, there will be an option to Align Fields in Columns.
But it just aligns the class, not the method variables, so not what you want.

Related

How to stop Eclipse from indenting extra spaces in this specific scenario?

When I am writing anonymous classes, I want my anonymous class to look like:
SaleTodayOnly sale = new SaleTodayOnly() // line 1
{ // line 2
some implementation
}
But when I hit enter after line 1, Eclipse will automatically position my cursor at | on line 2:
SaleTodayOnly sale = new SaleTodayOnly() // line 1
| // line 2
some implementation
And when I backspace my way to the front and write {, Eclipse will reposition this { to:
SaleTodayOnly sale = new SaleTodayOnly() // line 1
{ // line 2
some implementation
How can I set my own indentation preferences (for this specific scenario only)?
edit: I have my anonymous class set to next line. It's probably a wrapping issue.
edit2: I give up. I'll just use java conventions of { on the same line as the anonymous class declaration...
edit3: after hunting around the Preference window, toggling without much effect + seeing how Format produces the right output whereas the problem described still persists -- I'd agree that this is probably a bug and I will file a report when I have time.
Go into your preferences. (Window -> Preferences, probably; on mac it'll be under the leftmost menu option ('Eclipse')) - in the filter type 'formatter' to find the entry Java > Code Style > Formatter.
The behaviour you are witnessing is non-standard so you must already have a format defined; you picked this indent behavior, or somebody did who set this as default formatter.
edit this format. Alternatively, check if your project has a custom formatting rule in which case, this same answer applies, but instead go via your project's properties and update the formatting rules there.
The specific rule you are looking for is Brace positions, Anonymous class declaration. You have this set to Next line indented. Set it to something else. It sounds like you want Next line (not indented).

Eclipse force code onto 2 lines

I like to use ctrl+shift+f to make the format nice looking. However sometimes I want to force code on a seperate line and prevent it from being brought into one line. How can I do this?
For example
MyClass test = new MyClass(1,1,5,
"abc",
2,4,5);
You have 2 options the way I see it:
If none of your code is highlighted, the formatter formats the entire class. If you want to selectively format portions of your code (e.g. not the lines you mentioned) you can highlight the other portions of code and then apply the formatter (CMD+SHIFT+F).
If you want to be able to format the entire class, you can enable formatter tags. On the menu bar go to Eclipse → Java → Code Style → Formatter → Edit... → Off/On Tags tab and check Enable Off/On tags. Then you can include the following tags in your code if you want Eclipse to leave a section unformatted:
// formatted code
// #formatter:off
// unformatted code
MyClass test = new MyClass(1,1,5,
"abc",
2,4,5);
// #formatter:on
Good question, not sure why someone would thumbs down such an excellent question. In VB usually you use an underscore to indicate you want code on a separate line. In Java the closest equivalent is to just use // instead.
MyClass test = new MyClass(1,1,5, //
"abc", //
2,4,5); //
This will prevent the formatter from combining it into one line and if you want you can put a little note as to why you want it on a separate line for example
MyClass test = new MyClass(1,1,5, // coordinates
"abc", // name
2,4,5); // color

java eclipse auto-formatting braces indentation

My problem with eclipse's auto format is that it's taking this:
public List<JSON> getIdentityQualifiers(String identity)
throws DatabaseException
{
//....code
}
to this:
public List<JSON> getIdentityQualifiers(String identity)
throws DatabaseException
{
//....code
}
Obviously, I want to keep the previous formatting, but I can't find where to control this.
Go to Project -> Properties -> Java Code Style -> Formatter and you will see the formatting options. There are a lot of different styles.
My company provides us with a custom XML which defines exactly how they want things formatted, which was created internally. Eclipse allows you to define your formatting exactly in this format. There will certainly be tutorials online if this is important to you. You may be able to write your own or find many examples in Eclipse marketplace and online.
You could also try like that , first select segment of code which one you want to indent for e.g
if(true)
{
System.out.println("INDENTED");
}
select and type Ctrl + Shift + F.
After that you get.
if (true) {
System.out.println("INDENTED");
}
For customization you can find from there *Project --> Properties --> Java Code Style --> *
I think basically it may have been my version of Eclipse or something obscure as this stopped happening a long time ago.

Eclipse Formatter Options for '.' Alignment (Java) [duplicate]

This question already has answers here:
Wrapping chained method calls on a separate line in Eclipse for Java
(7 answers)
Closed 7 years ago.
I would like Eclipse to format some code which calls a builder as follows:
final Item item = new Item.Builder()
.name("something")
.field("a value")
.build();
i.e. aligning on the '.' character from the first line. I can manually convince Eclipse to do this by aligning the second line (the .name("something")) by hand, but any auto-formatting destroys this.
I've played around with all of the various formatting options that I can find in Eclipse and none of them seem to do what I want. Any ideas on if there are format options to lay out the code in this way?
Aligning exactly at the dot position is not possible, but the linebreaks for each method call can be done automatically. I've created this output
void format() {
Test test = new Test()
.a() // this call can also be configured to be one line above
.b()
.c();
}
by selecting the Line Wrapping tab in the formatter settings, selecting Function Calls, Qualified invocations, switching the Line wrapping policy in the combo box to Wrap all elements, every element on a new line and finally checking the checkbox Force split, even if line shorter than maximum line width.
You can get the first method call into the first line by selecting the "...except first element" policy instead.
Create a new formater:
Properties -> Formater -> Configure Workspace Settings -> New
then on the tab Off/On Tags set Enable Off/On tags
And then just surround your code like this:
/* #formatter:off */
final Item item = new Item.Builder()
.name("something")
.field("a value")
.build();
/* #formatter:on */
I think, you can achieve this by using 'jalopy'. There is a plugin for Maven, if you use Maven.
In case, you would like to use jalopy then, the setting that you would be interested is <methodCallChain> in the alignment element whose value you should set to true.
Edited: maven jalopy plugin. Make sure you target the clean phase and use the goal format

Can Eclipse Formatter Wrap Where Necessary, Every Element on a New Line?

I'm tying to figure out how I can customize the Eclipse code formatter to break lines more to my liking. I'm trying to set the style for parameter lists, either in method declarations or calls. Looking for a mix of Wrap where necessary and Wrap all elements, every element on a new line. I want to Wrap where necessary, every element on a new line, which doesn't seem to exist. My logic is that no break is necessary for short lines, my eye can scan the parameter list horizontally:
public void myMethod(int p1, int p2, int p3) {
But for lists that do need to be broken, I would like every element on a new line, so I can scan vertically:
public void myMethodWithALotOfParams(
ReallyLongClassName param1,
AnotherLongName aLongParamName,
int p3) {
I can't seem to make this happen. I can wrap everything, including short lists. I can wrap only long lines, and continue stacking parameters on each line until I reach the margin. I can't trigger wrapping on long lines, then put each parameter on its own line.
This style can be seen in several places in Code Complete (2nd Ed).
UPDATE >>
I don't think there is anything built in to Eclipse to handle this, but I'm not afraid to write code. :) Eclipse is open source, so I tried to find the code that handles formatting, in hopes of building in the preferred behavior. Didn't have much luck on the first try, lots of abstraction, not much parsing and formatting. Hints?
look at this link
Window -> Preferences -> Java -> Code Style -> Formatter -> New (Profile) -> Edit -> Line Wrapping -> Never join already wrapped lines
Or change other parameters if you want to change line wrapping parameters.
I would like to have such a feature too, unfortunatly (as you already guessed) it's not possible, yet. If you like you can file a bug at the eclipse-bugzilla, here you will find some bugs about formatting in jdt: https://bugs.eclipse.org/bugs/buglist.cgi?quicksearch=jdt+formatter. Let us know if you file a new bug, so everyone interested can vote for it!

Categories