Consider this code snippet
StringBuilder sb = new StringBuilder();
sb.append(1).append(2).append(3).append(4).append(5).append(6).append(7).append(8).append(9);
Let's say we want to format this using the Eclipse formatter, with a short line length limit so it looks nice on StackOverflow. We might hope to get something like this:
StringBuilder sb = new StringBuilder();
sb.append(1).append(2).append(3).append(4)
.append(5).append(6).append(7).append(8)
.append(9);
Instead, it doesn't get wrapped AT ALL, because apparently the formatter doesn't know that breaking on "dot" is allowed. Indeed, even if I wrap it myself, and then later tell it to format it, it will undo my wrapping. Things get even worse when the function calls take two arguments, because then it thinks: Aha! I can break after the comma. The result looks so bad I don't even want to show it here.
Now, admittedly, there are workarounds, such as //@formatter:off
, and even some that are fairly lightweight such as
StringBuilder sb = new StringBuilder();
sb.append(1).append(2).append(3).append(4)//
.append(5).append(6).append(7).append(8)//
.append(9);
Where the comment acts as a "barrier" to prevent it from combining the lines. But this is all "fighting the formatter" though. Is there anyway I get it to work with me rather than against me? (Possibly the right thing to do is to just tell it to preserve all line breaks, but I can't help but feel that it should still be able to introduce line breaks that are missing, even if the developer left them out.)
Given the current feature set, is this something the formatter can do?