Reputation: 1258
I have this plugins installed and it seems that the Eclipse Formatter is not taken into consideration.
For instance, I have my chain funtions to look like the below code in Formatter:
stringB.append("a")
.append("b")
.append("c")
But when I ask eclipse to autoformat (I guess using the Formatter) the code is placed like:
stringB.append("a").append("b").append("c")
Any idea why is this happening? Is CheckStyle overriding my Eclipse defined Formatter. How should I proceed to fix this?
Upvotes: 0
Views: 1891
Reputation: 17383
Any idea why is this happening?
It is probably happening because that is probably the default behavior of the formatter you are using. To prevent it you could check the box named Never joined already wrapped lines:
However, there is a better solution (detailed below) that doesn't even require that box to be checked.
Is CheckStyle overriding my Eclipse defined Formatter.
No, but you can easily confirm that this issue is not caused by Checkstyle:
How should I proceed to fix this?
You need to modify the settings of your formatter:
There are three icons on that line, each of which must be set correctly:
Click the rightmost icon and select the final option Indent on column from the context menu.
Apply those changes, and then reformat your code. The formatter should now behave as you specified. Here's an example of some code with various problems with the formatting of chained method calls:
List<String> months = Arrays.asList("January", "February", "March", "April", "May", "June");
List<String> months5 = months.stream().filter(s -> s.length() == 5).collect(Collectors.toList());
StringBuilder stA = new StringBuilder();
StringBuilder stringB = new StringBuilder();
StringBuilder thisIsStringC = new StringBuilder();
stA.append("a").append("b").append("c");
stringB.append("a")
.append("b")
.append("c");
stringB.append("d").append("e")
.append("f");
thisIsStringC .append("a")
.append("b")
.append("c");
thisIsStringC.append("d")
.append("e");
And here's what it looks like after reformatting (Ctrl-Shift-F) using the settings described above:
List<String> months = Arrays.asList("January", "February", "March", "April", "May", "June");
List<String> months5 = months.stream()
.filter(s -> s.length() == 5)
.collect(Collectors.toList());
StringBuilder stA = new StringBuilder();
StringBuilder stringB = new StringBuilder();
StringBuilder thisIsStringC = new StringBuilder();
stA.append("a")
.append("b")
.append("c");
stringB.append("a")
.append("b")
.append("c");
stringB.append("d")
.append("e")
.append("f");
thisIsStringC.append("a")
.append("b")
.append("c");
thisIsStringC.append("d")
.append("e");
Upvotes: 1