Skip to content

Support Palantir Java Format #224

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ lazy val plugin = project
ScmInfo(url("https://github.com/sbt/sbt-java-formatter"), "scm:git:[email protected]:sbt/sbt-java-formatter.git")),
developers := List(
Developer("ktoso", "Konrad 'ktoso' Malawski", "<[email protected]>", url("https://github.com/ktoso"))),
libraryDependencies ++= Seq("com.google.googlejavaformat" % "google-java-format" % "1.24.0"),
libraryDependencies ++= Seq(
"com.google.googlejavaformat" % "google-java-format" % "1.24.0",
"com.palantir.javaformat" % "palantir-java-format" % "2.62.0"
),
startYear := Some(2015),
description := "Formats Java code in your project.",
licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html")),
Expand Down
21 changes: 21 additions & 0 deletions plugin/src/main/java/com/github/sbt/javaformatter/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2015 sbt community
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.sbt.javaformatter;

public enum Formatter {
GOOGLE, PALANTIR
}
23 changes: 19 additions & 4 deletions plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ package com.github.sbt

import _root_.sbt.Keys._
import _root_.sbt.{ Def, _ }
import com.github.sbt.javaformatter.{ Formatter, JavaFormatter }
import com.google.googlejavaformat.java.JavaFormatterOptions
import com.github.sbt.javaformatter.JavaFormatter
import com.palantir.javaformat.java.{ JavaFormatterOptions => PalantirJavaFormatterOptions }

@deprecated("Use javafmtOnCompile setting instead", "0.5.1")
object AutomateJavaFormatterPlugin extends AutoPlugin {
Expand All @@ -42,10 +43,15 @@ object JavaFormatterPlugin extends AutoPlugin {
"Execute the javafmtCheck task for all configurations in which it is enabled. " +
"(By default this means the Compile and Test configurations.)")
val javafmtOnCompile = settingKey[Boolean]("Format Java source files on compile, off by default.")
val javafmtFormatter = settingKey[Formatter]("Define formatter, Google (default) or Palantir")
val javafmtStyle =
settingKey[JavaFormatterOptions.Style]("Define formatting style, Google Java Style (default) or AOSP")
val javafmtOptions = settingKey[JavaFormatterOptions](
"Define all formatting options such as style or enabling Javadoc formatting. See _JavaFormatterOptions_ for more")
val javafmtPalantirStyle =
settingKey[PalantirJavaFormatterOptions.Style]("Define formatting style, Palantir (default) or Google or AOSP")
val javafmtPalantirOptions = settingKey[PalantirJavaFormatterOptions](
"Define all formatting options such as style or enabling Javadoc formatting. See _JavaFormatterOptions_ for more")
}

import autoImport._
Expand All @@ -69,20 +75,27 @@ object JavaFormatterPlugin extends AutoPlugin {
}

override def globalSettings: Seq[Def.Setting[?]] =
Seq(javafmtOnCompile := false, javafmtStyle := JavaFormatterOptions.Style.GOOGLE)
Seq(
javafmtOnCompile := false,
javafmtStyle := JavaFormatterOptions.Style.GOOGLE,
javafmtFormatter := Formatter.GOOGLE,
javafmtPalantirStyle := PalantirJavaFormatterOptions.Style.PALANTIR)

def toBeScopedSettings: Seq[Setting[?]] =
List(
(javafmt / sourceDirectories) := List(javaSource.value),
javafmtOptions := JavaFormatterOptions.builder().style(javafmtStyle.value).build(),
javafmtPalantirOptions := PalantirJavaFormatterOptions.builder().style(javafmtPalantirStyle.value).build(),
javafmt := {
val streamz = streams.value
val sD = (javafmt / sourceDirectories).value.toList
val iF = (javafmt / includeFilter).value
val eF = (javafmt / excludeFilter).value
val cache = streamz.cacheStoreFactory
val formatter = javafmtFormatter.value
val options = javafmtOptions.value
JavaFormatter(sD, iF, eF, streamz, cache, options)
val palantirOptions = javafmtPalantirOptions.value
JavaFormatter(sD, iF, eF, streamz, cache, formatter, options, palantirOptions)
},
javafmtCheck := {
val streamz = streams.value
Expand All @@ -91,8 +104,10 @@ object JavaFormatterPlugin extends AutoPlugin {
val iF = (javafmt / includeFilter).value
val eF = (javafmt / excludeFilter).value
val cache = (javafmt / streams).value.cacheStoreFactory
val formatter = javafmtFormatter.value
val options = javafmtOptions.value
JavaFormatter.check(baseDir, sD, iF, eF, streamz, cache, options)
val palantirOptions = javafmtPalantirOptions.value
JavaFormatter.check(baseDir, sD, iF, eF, streamz, cache, formatter, options, palantirOptions)
},
javafmtDoFormatOnCompile := Def.settingDyn {
if (javafmtOnCompile.value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ import _root_.sbt.Keys._
import _root_.sbt._
import _root_.sbt.util.CacheImplicits._
import _root_.sbt.util.{ CacheStoreFactory, FileInfo, Logger }
import com.google.googlejavaformat.java.{ Formatter, JavaFormatterOptions }
import com.google.googlejavaformat.java.{
Formatter => GoogleFormatter,
JavaFormatterOptions => GoogleJavaFormatterOptions
}
import com.palantir.javaformat.java.{
Formatter => PalantirFormatter,
JavaFormatterOptions => PalantirJavaFormatterOptions
}

import scala.collection.immutable.Seq

object JavaFormatter {
Expand All @@ -31,9 +39,14 @@ object JavaFormatter {
excludeFilter: FileFilter,
streams: TaskStreams,
cacheStoreFactory: CacheStoreFactory,
options: JavaFormatterOptions): Unit = {
formatter: Formatter,
options: GoogleJavaFormatterOptions,
palantirOptions: PalantirJavaFormatterOptions): Unit = {
val files = sourceDirectories.descendantsExcept(includeFilter, excludeFilter).get().toList
cachedFormatSources(cacheStoreFactory, files, streams.log)(new Formatter(options))
cachedFormatSources(cacheStoreFactory, files, streams.log)(
formatter,
new GoogleFormatter(options),
PalantirFormatter.createFormatter(palantirOptions))
}

def check(
Expand All @@ -43,9 +56,15 @@ object JavaFormatter {
excludeFilter: FileFilter,
streams: TaskStreams,
cacheStoreFactory: CacheStoreFactory,
options: JavaFormatterOptions): Boolean = {
formatter: Formatter,
options: GoogleJavaFormatterOptions,
palantirOptions: PalantirJavaFormatterOptions): Boolean = {
val files = sourceDirectories.descendantsExcept(includeFilter, excludeFilter).get().toList
val analysis = cachedCheckSources(cacheStoreFactory, baseDir, files, streams.log)(new Formatter(options))
val analysis =
cachedCheckSources(cacheStoreFactory, baseDir, files, streams.log)(
formatter,
new GoogleFormatter(options),
PalantirFormatter.createFormatter(palantirOptions))
trueOrBoom(analysis)
}

Expand Down Expand Up @@ -73,7 +92,10 @@ object JavaFormatter {
}

private def cachedCheckSources(cacheStoreFactory: CacheStoreFactory, baseDir: File, sources: Seq[File], log: Logger)(
implicit formatter: Formatter): Analysis = {
implicit
formatter: Formatter,
googleFormatter: GoogleFormatter,
palantirFormatter: PalantirFormatter): Analysis = {
trackSourcesViaCache(cacheStoreFactory, sources) { (outDiff, prev) =>
log.debug(outDiff.toString)
val updatedOrAdded = outDiff.modified & outDiff.checked
Expand All @@ -89,7 +111,10 @@ object JavaFormatter {
log.warn(s"${file.toString} isn't formatted properly!")
}

private def checkSources(baseDir: File, sources: Seq[File], log: Logger)(implicit formatter: Formatter): Analysis = {
private def checkSources(baseDir: File, sources: Seq[File], log: Logger)(implicit
formatter: Formatter,
googleFormatter: GoogleFormatter,
palantirFormatter: PalantirFormatter): Analysis = {
if (sources.nonEmpty) {
log.info(s"Checking ${sources.size} Java source${plural(sources.size)}...")
}
Expand All @@ -104,7 +129,9 @@ object JavaFormatter {
}

private def cachedFormatSources(cacheStoreFactory: CacheStoreFactory, sources: Seq[File], log: Logger)(implicit
formatter: Formatter): Unit = {
formatter: Formatter,
googleFormatter: GoogleFormatter,
palantirFormatter: PalantirFormatter): Unit = {
trackSourcesViaCache(cacheStoreFactory, sources) { (outDiff, prev) =>
log.debug(outDiff.toString)
val updatedOrAdded = outDiff.modified & outDiff.checked
Expand All @@ -117,7 +144,10 @@ object JavaFormatter {
}
}

private def formatSources(sources: Set[File], log: Logger)(implicit formatter: Formatter): Unit = {
private def formatSources(sources: Set[File], log: Logger)(implicit
formatter: Formatter,
googleFormatter: GoogleFormatter,
palantirFormatter: PalantirFormatter): Unit = {
val cnt = withFormattedSources(sources.toList, log)((file, input, output) => {
if (input != output) {
IO.write(file, output)
Expand All @@ -142,11 +172,16 @@ object JavaFormatter {
}

private def withFormattedSources[T](sources: Seq[File], log: Logger)(onFormat: (File, String, String) => T)(implicit
formatter: Formatter): Seq[Option[T]] = {
formatter: Formatter,
googleFormatter: GoogleFormatter,
palantirFormatter: PalantirFormatter): Seq[Option[T]] = {
sources.map { file =>
val input = IO.read(file)
try {
val output = formatter.formatSourceAndFixImports(input)
val output = formatter match {
case Formatter.PALANTIR => palantirFormatter.formatSourceAndFixImports(input)
case _ => googleFormatter.formatSourceAndFixImports(input)
}
Some(onFormat(file, input, output))
} catch {
case e: Exception => Some(onFormat(file, input, input))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import com.github.sbt.javaformatter.Formatter
// no settings needed

ThisBuild / javafmtFormatter := Formatter.PALANTIR
ThisBuild / javafmtOnCompile := true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % System.getProperty("plugin.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.lightbend;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class BadFormatting {
BadFormatting() {
example();
}

public void example() {
var a = List.of("", "a", "b", "c", "d").stream()
.filter(s -> !s.isEmpty())
.map(s -> String.format("%s-%s-%s", s, s.toLowerCase(), s.toUpperCase()))
.map(Objects::toString)
.collect(Collectors.joining());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.lightbend;

import java.util.stream.Collectors;
import java.util.Objects;
import java.util.List;

public class BadFormatting {
BadFormatting () {example();}
public void example () {
var a = List.of("", "a", "b", "c", "d").stream().filter(s -> !s.isEmpty()).map(s -> String.format("%s-%s-%s", s, s.toLowerCase(), s.toUpperCase())).map(Objects::toString).collect(Collectors.joining());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.lightbend;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class BadFormatting {
BadFormatting() {
example();
}

public void example() {
var a = List.of("", "a", "b", "c", "d").stream()
.filter(s -> !s.isEmpty())
.map(s -> String.format("%s-%s-%s", s, s.toLowerCase(), s.toUpperCase()))
.map(Objects::toString)
.collect(Collectors.joining());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.lightbend;

import java.util.stream.Collectors;
import java.util.Objects;
import java.util.List;

public class BadFormatting {
BadFormatting () {example();}
public void example () {
var a = List.of("", "a", "b", "c", "d").stream().filter(s -> !s.isEmpty()).map(s -> String.format("%s-%s-%s", s, s.toLowerCase(), s.toUpperCase())).map(Objects::toString).collect(Collectors.joining());
}
}
12 changes: 12 additions & 0 deletions plugin/src/sbt-test/sbt-java-formatter/palantir-style/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# compile should trigger formatting
> Test/compile

#$ exec echo "====== FORMATTED ======"
#$ exec cat src/main/java/com/lightbend/BadFormatting.java
#$ exec echo "====== EXPECTED ======"
#$ exec cat src/main/java-expected/com/lightbend/BadFormatting.java

#$ exec echo "====== DIFF ======"
$ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java

$ exec diff src/test/java/com/lightbend/BadFormatting.java src/test/java-expected/com/lightbend/BadFormatting.java