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 google-java-format #33

Closed
nedtwigg opened this issue Oct 7, 2016 · 9 comments
Closed

Add support for google-java-format #33

nedtwigg opened this issue Oct 7, 2016 · 9 comments

Comments

@nedtwigg
Copy link
Member

nedtwigg commented Oct 7, 2016

The google java format is popular, and until 2014 it could be enforced using the eclipse code formatter.

Now they have a separate jar called google-java-format. It would be nice if Spotless had a step:

spotless {
    java {
        googleJavaFormat()
    }
}

Which used the google-java-gormat jar.

One tricky issue is it seems that these jars are evolving, and there is some desire in the community to have access to snapshot versions of the jar. Very easy to support a fixed version of the jar (it is available on maven central, but I'm not sure how to support multiple versions...

If any contributors are interested in adding this functionality, all that is required is a method here: JavaExtension.java.

@jbduncan
Copy link
Member

jbduncan commented Oct 7, 2016

Interesting you raise this issue, because I actually use google-java-format in one my own Gradle projects via Spotless's custom step (instead of, say, google-java-format-gradle-plugin), with something like the following.

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'com.google.googlejavaformat:google-java-format:1.1'
  }
}

spotless {
  java {
    custom('google-java-format') { String source ->
      new com.google.googlejavaformat.java.Formatter().formatSource(source)
    }
  }
}

I'm open to the idea of adding the functionality for a single version of GJF myself. However, I anticipate I'll be very busy soon, and I'm also inexperienced with writing Gradle plugins and I've never written software that had to support multiple versions of something before. So I don't know when I'll have the time to start, or if I could go beyond a single version of GJF.

@nedtwigg
Copy link
Member Author

nedtwigg commented Oct 7, 2016

Cool! Sounds like this would be a good first project for you. I'm in no hurry, and your manual workaround above is great. I think it's fine to only support a single version - if someone else figures out how to support multiple versions later, we can just add googleJavaFormat('1.1') or something like that.

Btw, you can speed this up a huge amount using customLazyGroovy by reusing the Formatter between files rather than creating a new one for every file.

buildscript {
    repositories {
        jcenter()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
    dependencies {
        classpath 'com.google.googlejavaformat:google-java-format:1.2-SNAPSHOT'
    }
}

plugins {
    id "com.diffplug.gradle.spotless" version "2.1.0"
}

spotless {
    java {
        customLazyGroovy('google-java-format') {
            com.google.googlejavaformat.java.Formatter formatter = new com.google.googlejavaformat.java.Formatter()
            return { source -> formatter.formatSource(source) }
        }
    }
}

@nedtwigg
Copy link
Member Author

nedtwigg commented Oct 7, 2016

Released in 2.2.0.

Sorry @jbduncan for swiping it before you had a chance, but looking at google-java-format's lack of gradle instructions made me want to get this to their users ASAP.

@nedtwigg nedtwigg closed this as completed Oct 7, 2016
@jbduncan
Copy link
Member

jbduncan commented Oct 7, 2016

That's fine! Thanks for putting it in so quickly! (I'm impressed by the speed at which you managed to do so, so kudos to you.)

Also, thanks for sharing that tip regarding customLazyGroovy. Shame there's now a better option than that in this case! 😆

@sormuras
Copy link
Contributor

sormuras commented Oct 7, 2016

Updating to 2.2.0 as well... 👍

@TimvdLippe
Copy link

Spotless has recently removed customLazyGroovy (#635). It took me a bit of tinkering, but I was able to make things work again with the following snippet:

Before:

spotless {
    java {
        customLazyGroovy('google-java-format') {
            com.google.googlejavaformat.java.JavaFormatterOptions options = new com.google.googlejavaformat.java.JavaFormatterOptions.Builder()
                    .style(com.google.googlejavaformat.java.JavaFormatterOptions.Style.AOSP)
                    .formatJavadoc(false)
                    .build()
            com.google.googlejavaformat.java.Formatter formatter = new com.google.googlejavaformat.java.Formatter(options)
            return { source -> formatter.formatSource(source) }
        }
    }
}

After:

spotless {
    java {
        custom 'google-java-format', { source -> 
            com.google.googlejavaformat.java.JavaFormatterOptions options = new com.google.googlejavaformat.java.JavaFormatterOptions.Builder()
                    .style(com.google.googlejavaformat.java.JavaFormatterOptions.Style.AOSP)
                    .formatJavadoc(false)
                    .build()
            com.google.googlejavaformat.java.Formatter formatter = new com.google.googlejavaformat.java.Formatter(options)
            return formatter.formatSource(source)
        }
    }
}

@nedtwigg
Copy link
Member Author

@TimvdLippe Any reason in particular you're not just using this? https://github.com/diffplug/spotless/tree/main/plugin-gradle#google-java-format

@TimvdLippe
Copy link

@nedtwigg Yes, formatJavadoc isn't available there, so we had to roll a custom version. That's a recent flag that got added to google-java-format, so I mostly assumed Spotless hadn't updated just yet.

@nedtwigg
Copy link
Member Author

Roger. Happy to take a PR to change that. Some useful snippets:

public GoogleJavaFormatConfig reflowLongStrings() {
return reflowLongStrings(true);
}

Doing #524 for google-java-format would be a good first-step towards making it easier.

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

No branches or pull requests

4 participants