jlanza
jlanza

Reputation: 1258

Eclipse formatter with checkstyle and sonarlint

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

Answers (1)

skomisa
skomisa

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:

neverJoinImage

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:

  • Format some code with each chained method on a new line.
  • Temporarily turn off CheckStyle: {project} -> right click -> CheckStyle -> Deactivate Checkstyle
  • Format your code (Ctrl-Shift-F). You should see that the formatting problem persists.

How should I proceed to fix this?

You need to modify the settings of your formatter:

  • Window > Preferences > Java > Code Style > Formatter
  • Click the Edit... button. (This assumes that you have already created your own Active Profile for formatting. If not, you will need to click the New... button and do that first.)
  • In the window that opens for modifying your profile settings, in the left column navigate to Line Wrapping > Wrapping settings > Function Calls > Qualified invocations.
  • There are three icons on that line, each of which must be set correctly:

    • Click the leftmost icon and select the final option Wrap all elements, except first element if not necessary from the context menu.
    • Click the middle icon. This is a toggle switch and it will acquire a border around it when clicked once. This sets the option Force split, even if line shorter than maximum line width.
    • Click the rightmost icon and select the final option Indent on column from the context menu.

      Invocations

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

Related Questions