Eclipse Formatter - Ignore content in if conditions - java

I have been setting up the eclipse formatter and quite close to our desired outcome except for the following. I'm trying to ignore line wrapping for the conditions within the if statement
For example
My desired outcome is
if (callback.getType().equals("NameCallback")) {
callback.getInput()
.get(0)
.setValue(testUser.getUsername());
}
but instead I'm getting
if (callback.getType()
.equals("NameCallback")) {
callback.getInput()
.get(0)
.setValue(testUser.getUsername());
}
In the formatter I have set Line wrapping -> Function Calls -> Qualified Innovations to a line wrapping policy of 'Wrap all elements, except first element if not necessary' and force split. This works fine but our preference is to not have this rule invoked for if statement conditions or inside of () if easier. I know I can setup the On/Off tags for the formatter but hoping there is a way to get the same result without having to do this. Any ideas would be appreciated
Thanks

In order to customize the way you format an if else blocks, you need to jump to Control Statements :

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

How to get eclipse code formatter to wrap assignment statements onwards = operator

I'm using eclipse code formatter, and I've set maximum line length to 120.
If an assignment statement is longer than 120 characters, for example
private Map<Instruction, LocalRegisterAssignmentInformation> instructionRegisterMap = new IdentityHashMap<Instruction, LocalRegisterAssignmentInformation>();
I would like the formatter to wrap this line and make the statement look like this:
private Map<Instruction, LocalRegisterAssignmentInformation> instructionRegisterMap
= new IdentityHashMap<Instruction, LocalRegisterAssignmentInformation>();
But the formatter doesn't seem to wrap it. I tried finding relevant options in the eclipse formatter profile settings, but couldn't find any.
Edit your Java formatter profile as follows:
In Line Wrapping select Expressions > Assignments
Set Line wrapping policy to Wrap where necessary
Check the checkbox Wrap before operator

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

Getting Eclipse to wrap the parenthetical of a for loop

I understand the importance of succinct, clear code and that code line wrapping should be avoided, if possible. However, the requirement for this project is that no line should go beyond column 80 and I'm being asked to use verbose variable naming. Therefore, something as simple as a for loop parenthetical will need to be wrapped and that's where I'm finding Eclipse falling short.
It doesn't appear that Eclipse is capable of wrapping the parenthetical of a for loop or preserving the wrapping set. For example, my initialization statement, expression and update/counter are on separate lines like so:
for (initialization;
expression;
update/counter;)
{
//code...
}
When pressing Ctrl+Shift+f, Eclipse makes it:
for (initialization; expression; update/counter;)
{
//code...
}
Is there a way to get Eclipse to preserve this formatting. I've created a custom Eclipse formatter, but can't find any setting that will wrap the for loop parenthetical. I did see one post that suggested using //, but that won't work inside of a for loop's parenthesis.
I do not remember of an option to preserve the line feeds after each of the for's "initialization", "expression", "update". The rest can be done, but not the wrapping inside the for loop's parenthesis.
If you really need to preserve such a wrapping, you may want to disable the formatter altogether on these lines? If you go to the formater settings (Window > preferences > java > code style > formatter, then click "edit..."), then on the "Off/On Tags" tab (appeared in Eclipse 3.6 IIRC), you can enable tags to disable the formatter on specific parts of the code.
With the default tags, that would give something like :
// #formatter:off
for (initialization;
expression;
update/counter)
// #formatter:on
{
//code...
}
You can also use this way:
for (/**/initialization;
/**/expression;
/**/update/counter;)
/**/{
//code...
}
This also works:
for (initialization; //
expression; //
update/counter) {
// code...
}

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