Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get eclipse to wrap lines after a period instead of before

Tags:

I've looked throughout Preferences -> Java -> Code Style -> Formatter and can't find any way to get eclipse to format my code like:

something.     someMethod().     anotherMethod().     lastMethod(); 

Instead of:

something     .someMethod()     .anotherMethod()     .lastMethod(); 

I know that's non-standard, but that's what I need.

Edit: This is not about getting lines to wrap. It's specifically about where the wrap happens in relation to the period. I want the period at the end of the line, before the newline and right now eclipse wants the period at the start of the next line.

Edit2: Even if I could find out where eclipse's source code it decides where to wrap the line, that might help. I think it might be in the JDT project, but I'm not 100% and there's a lot in there.

like image 554
onlynone Avatar asked Jul 15 '15 18:07

onlynone


People also ask

How do you line wrap in eclipse?

There is a "Toggle Word Wrap" command hidden in the "Window > Editor" menu (default shortcut is Alt+Shift+Y). Or you can use the Quick Access bar: Ctrl+3, and type wrap.


2 Answers

First of all, I don't think there is a way to get the standard Eclipse Java code formatter break lines after a . ... apart from modifying the formatter code itself. (And that means you are not running the standard formatter anymore!)


All is not lost. I did a Google search for "eclipse formatter plugin", and came across a link to the Jindent plugin in the Eclipse Marketplace. Digging a bit further, I found the documentation for the Jindent Formatter Settings. According to "5.6.1.9.4 Method Calls", Jindent can be told to break lines after a "." rather than before one.

So ... if you are prepared to pay for a Jindent license, you can get an Eclipse plugin for it for free that will indent your Java code the way that you prefer.

Disclaimers:

  • Jindent is a commercial product. You will need to pay for a license.
  • I have never used it, so this is not a recommendation or endorsement.
  • I have no association with the company or people who provide this product.

Current Java style guides recommend that you break lines before . symbols, operators and so on. I would advise that you learn to live with the "standard" style ... because that is what other people who read your code are most likely to be expecting.

Reference:

  • "Wrapping Lines" in Java Style Guidelines (DRAFT v6) which are the code style guidelines for the OpenJDK codebase.
like image 182
Stephen C Avatar answered Sep 30 '22 21:09

Stephen C


Try this.

Window > Preferences > Java > Code Style > Formatter > Edit > Line wrapping > Function Calls, set the 'Line wrapping policy' as 'Wrap all elements, every element on a new line'.

If found already then cool.

like image 35
user5247180 Avatar answered Sep 30 '22 19:09

user5247180