EclipseCodeFormatter icon indicating copy to clipboard operation
EclipseCodeFormatter copied to clipboard

Methods starting with “when(…)” in test files are left unformatted

Open mminot-yseop opened this issue 1 year ago • 1 comments
trafficstars

Hi.

With 23.4.223.000.0-Eclipse_2023-03, I run into a strange issue: some methods are just skipped rather than formatted, while the rest of the file is processed just fine, and the conditions for this to happen seem to be:

  • Be in a test class rather than a business class. (Edit: This might be a misconception from before I noticed that methods other than when(…) worked fine. We don’t use Mockito in business code, so…)

AND

  • The first statement of the method, ignoring comments, is an unqualified Mockito when(…) call (rather than Mockito.when(…)).

Adding another statement before the when(…), even if it’s just a lone ;, fixes the issue. Other methods do not seem to cause this issue; even other Mockito methods like mock() seem to be OK.

What steps will reproduce the issue?

Somewhere under a Test Sources Root directory, create a file like:

image

import static org.mockito.Mockito.when;

public class FormatterBug {

    void foo() {
        when(null);
           //   Random indentation
             //       Random indentation
          // Random indentation
    }
}

What is the expected result?

Something consistent, along the lines of (depending on the config):

import static org.mockito.Mockito.when;

public class FormatterBug {

    void foo() {
        when(null);
        // Random indentation
        // Random indentation
        // Random indentation
    }
}

What happens instead?

The whole method is not formatted, only what surrounds it is. There is no crash though: the formatter looks happy.

Сonsider attaching a screenshot, screencast, or a code sample when it's hard to articulate the problem.

eclipse-adapter-formatter-bug-static.webm

Paste information about IDE and OS (it can be copied from Help | About dialog).

IntelliJ IDEA 2024.1 (Ultimate Edition)
Build #IU-241.14494.240, built on March 28, 2024
Licensed to XXX
Subscription is active until XXX.
Runtime version: 17.0.10+8-b1207.12 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.15.0-107-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 3982M
Cores: 8
Non-Bundled Plugins:
  Pythonid (241.14494.314)
  EclipseCodeFormatter (23.4.223.000.0-Eclipse_2023-03)
  org.asciidoctor.intellij.asciidoc (0.41.14)
  com.haulmont.jpab (241.14494.158)
Kotlin: 241.14494.240-IJ
Current Desktop: XFCE

mminot-yseop avatar May 24 '24 13:05 mminot-yseop

With Maven project maven.compiler.release = 21 I tried converting an if-else chain with startsWith to a switch as per https://stackoverflow.com/a/77595373/2746335 Looks like the when clause confuses the formatter. I'm on Intellij Build #IU-241.17011.2, built on May 7, 2024

image

delanym avatar May 31 '24 10:05 delanym

This is/was an issue in JDT, see https://github.com/eclipse-jdt/eclipse.jdt.core/issues/609

I do not know exaclty what version of eclipse formatter is the earliest, that fixes it but at least in 2024-03/4.31 it is fixed.

@krasa any chance to get an updated version of this plugin with an updated eclipse version?

dodgex avatar Sep 23 '24 09:09 dodgex

I opened https://github.com/krasa/EclipseCodeFormatter/pull/272 to update to the Eclipse to 2024-09.

dodgex avatar Sep 23 '24 12:09 dodgex

Got the update right now and it seems to work. Thanks!

mminot-yseop avatar Sep 25 '24 15:09 mminot-yseop