krasa / EclipseCodeFormatter

IntelliJ plugin - use Eclipse's code formatter directly from IntelliJ. https://plugins.jetbrains.com/plugin/6546
Apache License 2.0
484 stars 118 forks source link

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

Open mminot-yseop opened 3 months ago

mminot-yseop commented 3 months ago

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:

AND

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
delanym commented 3 months ago

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