diffplug / spotless

Keep your code spotless
Apache License 2.0
4.55k stars 456 forks source link

Enabling Parrot Parser for Groovy Eclipse #975

Open jamesdh opened 3 years ago

jamesdh commented 3 years ago

Since spotless depends on groovy-eclipse for groovy parsing/formatting, and since groovy-eclipse doesn't enable the parrot parser by default, Spotless fails when configured for groovy code that utilizes Groovy 3+ syntax.

After experiencing some pain with spotless + groovy, I stumbled upon #246 which led me to dig into groovy-eclipse. Turns out groovy-eclipse leaves the new antlr4 based Parrot Parser disabled by default, which means any newer Groovy 3 syntax that contains, for example, Java lambdas, blows up.

I can manually add -Dgroovy.antlr4=true to my Gradle execution and that gets it working for me locally, but there should be some way to configure this right within build.gradle? I'm trying to find a solution that is more amenable to a team environment.

In the past I've modified JavaExec tasks and the like to include forked JVM options. But there seems to be no way to do that for SpotlessTask.

Am I missing something obvious here?

jamesdh commented 3 years ago

So I didn't think this workaround would work since spotlessGroovy runs before the compileGroovy task, but it did...

Assuming you use gradle, If you add a config.groovy file anywhere to your project containing the following:

configuration.pluginFactory = org.codehaus.groovy.control.ParserPluginFactory.antlr4()

and then reference that file in your build.gradle a la:

compileGroovy.groovyOptions.configurationScript = file('config.groovy')

It should get it working with groovy 3+ syntax. This really should work by default though.

jamesdh commented 3 years ago

I stand corrected...my previous example doesn't work. There seems to be no way to enable this short of passing JVM options when running gradle 😞

fvgh commented 3 years ago

@jamesdh Thanks for the hint. I would not like to extend the interface for this one, but just accept groovy.antlr4 as a property in the configuration files.

Setting a system property is not an option, but eclipse-groovy allows the configuration per PU.

Unfortunately it creates the PU statically

That could be solved by declaring this option as not changeable, as we did it for WTP.

I am afraid this one is not high on my list. PRs are welcome... You need to enable the _ext projects to build spotless-eclipse-groovy.

fvgh commented 3 years ago

@jamesdh Sorry, was writing my previous comment while you were writing yours :smile:

I really don't want to add too much code on Spotless side. In my opinion the best solution would be on eclipse-groovy side, making the groovy.antlr4 configurable per Eclipse project.

jamesdh commented 3 years ago

That could be solved by declaring this option as not changeable, as we did it for WTP.

I'm struggling to understand what you mean by this. Could you give a bit more context? Thanks for the help already!

fvgh commented 3 years ago

For the Eclipse WTP formatter you cannot configure everything per project. Some things are still configured for per workspace. The WTP stores them statically. The spotless user doesn't see this difference. In a multi-project build, you can configure multiple WTP based formatters.

So I propose for groovy.antlr4:

  1. Store value statically
  2. If GrEclipseFormatterStepImpl is called is twice with different values, throw an IllegalArgumentException