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

Add Support for Eclipse Code Formatter #4578

Open
x80486 opened this issue Oct 13, 2024 · 2 comments
Open

Add Support for Eclipse Code Formatter #4578

x80486 opened this issue Oct 13, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@x80486
Copy link

x80486 commented Oct 13, 2024

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 😬

@x80486 x80486 added the enhancement New feature or request label Oct 13, 2024
@timtebeek
Copy link
Contributor

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.

@x80486
Copy link
Author

x80486 commented Oct 14, 2024

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 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

2 participants