23

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.

onlynone
  • 7,602
  • 3
  • 31
  • 50
  • possible duplicate of [Wrapping chained method calls on a separate line in Eclipse for Java](http://stackoverflow.com/questions/6275785/wrapping-chained-method-calls-on-a-separate-line-in-eclipse-for-java) – Frank Bryce Jul 15 '15 at 18:35
  • 3
    Not quite a duplicate of that. – Anubian Noob Jul 15 '15 at 18:36
  • Just curious: Can you tell why? – Aleksandr M Jul 15 '15 at 21:19
  • 1
    My company's style guide dictates it. We have a Checkstyle format defined that flags any whitespace before a period as an error. – onlynone Jul 16 '15 at 16:41
  • 4
    With the last release of eclipse (Mars) "JDT now provides an extension point (org.eclipse.jdt.core.javaFormatter) to allow third-party source code formatters as extensions." So you can define your own formatter... – rlm Jul 22 '15 at 13:22
  • Have a look at this . It is exactly what you want. http://stackoverflow.com/questions/6275785/wrapping-chained-method-calls-on-a-separate-line-in-eclipse-for-java – lhoak Jul 27 '15 at 04:01
  • No, it's exactly what I don't want. I explained this in my edit to the question after others also didn't read my question. – onlynone Aug 03 '15 at 15:43
  • 1
    As mentioned above the only way is with your own code formatter extension. However, surely your time would be better spent getting your company to jettison that ridiculous style rule and join pretty much everyone else on earth and break before the dot! – John Hascall Dec 18 '15 at 01:13
  • Thanks for the question. I'd love for a way to do this as well as I highly prefer the style of wrapping after the period. Wrapping after the period allows one to see the statement continues on the next line. Beginning a wrapped line with a period does not add clarity, in my opinion, since the indentation already tells that it is a continuation of a statement. @JohnHascall Does the Java world really have that strong of a preference for wrapping after the period? I come from more of a Ruby background where the preferences seemed to be mixed. – Steve Apr 21 '16 at 19:50
  • In my experience, yes. I've never seen "your" style. The Eclipse formatted has lots and lots of options, so I would think if period-break had any followers at all there would by an option for it. FWIW, the break-period style is the JavaScript "standard" as well (giant dot chains being more common there, esp with frameworks like jQuery). – John Hascall Apr 22 '16 at 02:14
  • 1
    The code for the line-wrapping seems to be around here: http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/ – Bludzee Sep 12 '16 at 16:05

3 Answers3

3

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:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

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.

-2

I think if you install the eclipse java mars which is the new version of eclipse, your problem would be solved. I am saying that because I am using the newest version and I am not facing this problem. Sorry again if I could not help I am still a beginner

Beginnerprogrammer
  • 95
  • 1
  • 1
  • 10