Add Support for Eclipse Code Formatter
Opened this issue ยท 2 comments
Description
Eclipse's code formatter is open-source and highly configurable. It comes with sensible default options that are well documented, making it easy to further customize, if needed, the configuration settings.
Describe the Solution You Would Like
When org.openrewrite.recommendations.CodeStyle
(or similar) is added, another style (org.eclipse.Formatter
) is available, along with the options to refer to a custom configuration file โ e.g.: eclipse-jdt-formatter.xml
<?xml version="1.0" encoding="UTF-8"?>
<profiles version="23">
<profile kind="CodeFormatterProfile" name="Some Company" version="23">
<setting id="org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter" value="false" />
...
</profile>
</profiles>
Have you Considered any Alternatives or Workarounds?
I've tried some of the other built-in styles, but somehow it's not possible to correct most errors in the files after running ./gradlew rewriteRun
.
Additional Context
Not sure if this makes sense in OpenRewrite, but since there are some styles already in place, and the capability to do the auto-formatting, probably adding this one could be something you may consider. Personally, I use the Spotless
extension and it works flawlessly:
configure<SpotlessExtension> {
encoding = StandardCharsets.UTF_8
java {
cleanthat().version("2.17") // Must be applied before the selected formatter(s)
eclipse("4.29").configFile(project.layout.projectDirectory.file("config/spotless/eclipse-jdt-formatter.xml"))
endWithNewline()
formatAnnotations()
importOrder("", "javax", "java")
indentWithSpaces(2)
removeUnusedImports()
trimTrailingWhitespace()
}
kotlinGradle {
endWithNewline()
indentWithSpaces(2)
ktlint("1.1.0")
trimTrailingWhitespace()
}
lineEndings = LineEnding.UNIX
}
So I was thinking it may be a matter of adding just support for the Eclipse code formatter โ but it may not be that simple ๐ฌ
hi @x80486 ! We already support a number of named styles, including Checkstyle, IntelliJ and Spring. As a first step we could add a new named style for Eclipse to make that easily available to folks looking to adopt the defaults. Beyond that we could then look at parsing an Eclipse code formatter profile to set the corresponding values in a custom style.
Do note that we also have automatic detection of the style, and match any detected style when introducing changed. So if a project is already consistently formatted there's a good chance no customization is needed. It's only when formatting is not consistent that you might then run into trouble.
We'd welcome a PR that adds an explicit named style that matches the Eclipse defaults, if you're up to such a change. Ideally that'd be declarative, like the linked Spring example above.
Hi @timtebeek ๐
I realize I didn't explain this correctly initially. My understanding is that Eclipse doesn't provide a widely recognized or established set of code conventions. Instead, it offers a flexible file format for configuring this.
I use it all the time with the Spotless (Gradle) plugin, and I can set it up to align with Google's Code Conventions or Oracle's Code Conventions โ and then customize the settings to create a custom profile.
It would be great if OpenRewrite could also support the Eclipse formatting engine, allowing it to read the profile file and automatically format code according to the defined rules, and the reason I was mentioning it may not be that simple.
Moreover, I see OpenRewrite already have a DSL for accomplishing this, so depending on how complete it is, I'm not sure if it's worth to explore other options ๐คทโโ๏ธ