Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sealed interfaces are not correctly formatted #776

Closed
nils-christian opened this issue Aug 21, 2023 · 3 comments
Closed

Sealed interfaces are not correctly formatted #776

nils-christian opened this issue Aug 21, 2023 · 3 comments
Labels

Comments

@nils-christian
Copy link

Describe the bug

Hi. This is very likely still the same issue as #715.

Versions (OS, Maven, Java, and others, as appropriate):

  • Plugin Version 2.23.0
  • Java Version 17

To Reproduce
In our project we have a sealed interface permitting two classes:

public sealed interface A permits B, C {

}

public final class B implements A {

}

public final class C implements A {

}

The formatter of Eclipse does not change the above interface A in any way. However, the formatter plugin does:

public sealed
interface A
permits B, C
{

}

Expected behavior
The formatting of the maven plugin should be the same as the one of Eclipse.

@nils-christian nils-christian changed the title Sealed interface are not correctly formatted Sealed interfaces are not correctly formatted Aug 21, 2023
@nils-christian
Copy link
Author

Hi,

I created a project to reproduce the issue: https://github.com/nils-christian/formatter-maven-plugin-issue-776

Simply execute the batch file (or run mvn -q net.revelc.code.formatter:formatter-maven-plugin:format@formatter-format manually) and the file "A.java" will be changed. Eclipse, on the other hand, won't change the file with the same formatter settings.

@ctubbsii ctubbsii added invalid and removed bug labels Aug 21, 2023
@ctubbsii
Copy link
Member

ctubbsii commented Aug 21, 2023

Your formatter configuration is quite old. If I import it into a newer version of Eclipse, and then re-export it, it includes additional configuration items that pertain to styling for newer Java language elements, such as the sealed keyword. When you import it into Eclipse, it fills in those additional items and formats correctly. If you use the old formatter config file as-is, the Eclipse formatter API, which this plugin uses, will not do that. If you want to this plugin to format newer language elements, you need to use a config file that specifies the formatting for those elements. The current formatter config file you have will not do that.

Oh, and as a request, if you want to provide an example project in future to report a bug or potential bug, that's fine, but please make it self-contained, rather than have it rely on a separate external parent POM that I have to track down. I modified your example locally to get rid of the parent POM before I could reproduce:

diff --git a/pom.xml b/pom.xml
index ec75c94..fee83a2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,20 +3,16 @@
 
 	<modelVersion>4.0.0</modelVersion>
 
-	<parent>
-		<groupId>org.springframework.boot</groupId>
-		<artifactId>spring-boot-starter-parent</artifactId>
-		<version>2.7.14</version>
-		<relativePath />
-	</parent>
-
 	<groupId>com.example</groupId>
 	<artifactId>demo</artifactId>
 	<version>0.0.1-SNAPSHOT</version>
 	<name>demo</name>
 
 	<properties>
-		<java.version>17</java.version>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+		<maven.compiler.source>17</maven.compiler.source>
+		<maven.compiler.target>17</maven.compiler.target>
 	</properties>
 
 	<build>
@@ -34,7 +30,7 @@
 						<goals>
 							<goal>format</goal>
 						</goals>
-						<phase>none</phase>
+						<phase>package</phase>
 					</execution>
 					<execution>
 						<id>formatter-validate</id>
@@ -51,4 +47,4 @@
 		</plugins>
 	</build>
 
-</project>
\ No newline at end of file
+</project>

This is the is diff after merely importing your formatter config and re-exporting it:

diff --git a/src/main/java/com/example/demo/A.java b/src/main/java/com/example/demo/A.java
index bc20c7b..e0d39e0 100644
--- a/src/main/java/com/example/demo/A.java
+++ b/src/main/java/com/example/demo/A.java
@@ -1,5 +1,6 @@
 package com.example.demo;
 
-public sealed interface A permits B, C {
+public sealed interface A
+		permits B, C {
 
 }

More details about the differences between what Eclipse does in the UI and what it does in the formatting library API that is used by this plugin can be found in the discussion at https://bugs.eclipse.org/bugs/show_bug.cgi?id=545603

@ctubbsii ctubbsii closed this as not planned Won't fix, can't repro, duplicate, stale Aug 21, 2023
@nils-christian
Copy link
Author

Hi,

Yes, I can confirm that simply re-exporting the formatting configuration solves the issue. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants