This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 214902 - Introduce Preserve option for formatting of chained method calls
Summary: Introduce Preserve option for formatting of chained method calls
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Editor (show other bugs)
Version: 7.1.2
Hardware: PC Windows 7
: P3 normal with 1 vote (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-28 10:24 UTC by phansson
Modified: 2012-06-29 08:21 UTC (History)
1 user (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description phansson 2012-06-28 10:24:33 UTC
If you search the Netbeans mailing lists / forums you can find examples where people have asked for how to do proper formatting of chained method calls. This is not surprising as quite a few Java libraries now encourage the use of chained methods, typically in so-called 'builder' methods.

Examples are builders in JavaFX or in Google's Protocol Buffers. There are many other examples.

The IDE does a really lousy job when formatting such statements. Example of such a statement:

  myFoo = myObject.getBuilder()
          .setFieldA(value)
          .setFieldB(value)
          .setFieldC(value)
          .setFieldD(value)
          .build();


Out-of-the box the IDE will join the above lines to a single line. It is possible to fiddle with the options under Options --> Editor --> Formatting --> Language: Java --> Category : Wrapping --> "Chained Method Calls". There are currently 3 options available:

1. Never
This is the default. In example above all of the lines will be joined to a single line of text (which then becomes stupidly long). Not what we want. !

2. If Long
The formatter will insert line breaks at its own discretion if the line would otherwise become too long. Not what we want!

3. Always.
The formatter will break any chained method call no matter how short the line may be. This would mean for example that 
System.out.println(" X = " + getRecord().getX())

will be broken into
System.out.println(" X = " + getRecord().getX())

Even worse is that it will format:

myVar = foo.getX();

into 

myVar = foo
     .getX();

when then line length would otherwise exceed the maximum (default:80). This was just a side-note. But what is going on here? "foo.getX()" is not a chained method call. Ouch!. That seems to me to be a bug. (or I haven't understood what a chained method call is!). Bottom line: we can clearly not use the "Always" option.

Anyway coming back on track we've established that neither of the existing methods (Never /& If Long / Always) will give us an acceptable result. So what is an acceptable result?. Very simple to answer: PRESERVE.

'Preserve' means that the formatter should not try to change the formatting that the user has done manually in his code. In short:  the suggestion is to introduce a fourth option which would be "Preserve".
Whether the new suggested option should be introduced for all categories or only for chained method calls I do not know.

There are probably different opinions on how a chained method call should be formatted. This is why 'Preserve' is such a good idea: everybody can have it their own way.

This request seems to be a duplicate but is specific to JavaFX :		  
http://netbeans.org/bugzilla/show_bug.cgi?id=208768 (about formatting builders in Java FX). I suggest to keep this one and fold the other one into this request.
Comment 1 phansson 2012-06-28 10:28:21 UTC
Should have read:


This would mean for example that 
System.out.println(" X = " + getRecord().getX());

will be broken into
System.out.println(" X = " + getRecord()
         .getX());