Is there a source code formatter for Groovy? [closed]
Asked Answered
O

8

29

I use the commercial version of Jalopy for my Java projects but it doesn't work on Groovy files. IntelliJ has a serviceable formatter but I don't like requiring a particular IDE.

Offense answered 24/9, 2008 at 23:4 Comment(2)
groovy is a language, implemented on the JVM. see groovy.org . Groovy is a superset of the java lanaguage apparently.Dipterocarpaceous
The comment about Groovy being a superset of Java is inaccurate. For example, Groovy doesn't support inner classes (yet).Kerril
R
9

Try "BUSL"

2022-10-26 NOTE: HISTORIC. "BUSL" seems to be dead. Last archived webpage is from 2020.

I've found that BUSL works really well on Groovy files. It's standalone too, so you can use it from your text editor or whatever.

Reece answered 25/9, 2008 at 1:33 Comment(2)
FWIW, BUSL appears to be mostly dead. It had a "beta" release in 2005 and no activity since.Leatherworker
BUSL has dropped off the internetBroadcast
O
5

Try npm-groovy-lint

You can use npm-groovy-lint via command line with --format option :)

https://www.npmjs.com/package/npm-groovy-lint

Obligatory answered 2/5, 2020 at 22:22 Comment(3)
Sadly npm-groovy-lint has many bugs with the prettifier.Hispid
I encountered four parse problems with npm-groovy-lint, and it didnt say which files where a problem, whereas I had one parse problem with spotless which was easy to isolate as it told me which file caused the java exception.Broadcast
After figuring out there is nothing public right now exists I've created my own small dependency free library to do it: npmjs.com/package/groovy-beautifyDiamagnet
I
4

spidasoftware/format extracts the groovy eclipse plugin and provides a command line interface to it.

Instructions:

git clone [email protected]:spidasoftware/format.git && \
cd format/bin && \
./format /path/to/groovy/file

caveat: this project is no longer maintained however still works as of the time of this post

Impower answered 21/4, 2017 at 14:41 Comment(1)
This is a wrapper around github.com/groovy/groovy-eclipse/blob/master/ide/… . spotless is also a wrapper around this.Broadcast
D
2

the latest eclipse plugin will do some formatting and refactoring: http://groovy.codehaus.org/Eclipse+Plugin+Refactoring

Dichroism answered 3/12, 2008 at 1:35 Comment(0)
C
2

Groovy support for Jalopy is coming later this year. There is a tiny sneak preview on YouTube showcasing the Eclipse plug-in:

http://www.youtube.com/watch?v=PNFUzvOZei0

Cordeliacordelie answered 19/3, 2011 at 20:18 Comment(2)
That's what the OP said he used already.Paillette
Groovy isnt mentioned on their website google.com/…Broadcast
S
2

Older groovyc had some formatter

2022-10-26 NOTE: HISTORIC. The formatter seems to have disappeared in newer versions. See comment.

Actually groovyc came with a builtin formatter (kind of). If you set the environment variable JAVA_OPTS to -Dantlr.ast and run groovyc test.groovy a file called test.groovy.pretty.groovy is generated.

But be aware: From what I found in the internet about this, this formatter is not configurable and strips comments!

Satinet answered 8/12, 2016 at 12:46 Comment(1)
Using v3.0.8, this doesnt appear to work anymoreBroadcast
Q
2

Spotless also formats groovy. Seems to be impossible to run without gradle/maven though. :(

Quitclaim answered 3/5, 2019 at 15:50 Comment(0)
P
1

I have yet to find a good solution for this, and I really wish that there was one. Regarding @Gizmomogwai's tip, it doesn't exactly work as you'd think.

First of all, you need to export JAVA_OPTS=-Dantlr.ast:groovy. However, the file produced by groovyc is clearly not "pretty" in the sense that it is pretty to humans. The "pretty" output generates a file which will be parsed by the next stage of the compiler. Effectively, this means that it not only strips comments but also will add and alter newlines and whitespace. It is definitely not suitable for checking code formatting.

Pigtail answered 29/11, 2017 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.