Skip to content

Commit c8e634f

Browse files
committed
Support Palantir Java Format
1 parent 01c87a7 commit c8e634f

File tree

12 files changed

+116
-6
lines changed

12 files changed

+116
-6
lines changed

build.sbt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ lazy val plugin = project
1616
ScmInfo(url("https://github.com/sbt/sbt-java-formatter"), "scm:git:[email protected]:sbt/sbt-java-formatter.git")),
1717
developers := List(
1818
Developer("ktoso", "Konrad 'ktoso' Malawski", "<[email protected]>", url("https://github.com/ktoso"))),
19-
libraryDependencies ++= Seq("com.google.googlejavaformat" % "google-java-format" % "1.24.0"),
19+
libraryDependencies ++= Seq(
20+
// "com.google.googlejavaformat" % "google-java-format" % "1.24.0",
21+
"com.palantir.javaformat" % "palantir-java-format" % "2.62.0"
22+
),
2023
startYear := Some(2015),
2124
description := "Formats Java code in your project.",
2225
licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html")),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2015 sbt community
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.util.List;
18+
import java.util.Objects;
19+
import java.util.stream.Collectors;
20+
21+
public class BadFormatting {
22+
BadFormatting () {example();}
23+
public void example () {
24+
var a = List.of("", "a").stream().filter(s -> !s.isEmpty()).map(s -> String.format("%s-%s-%s", s, s.toLowerCase(), s.toUpperCase())).map(Objects::toString).collect(Collectors.joining());
25+
}
26+
}

plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package com.github.sbt
1818

1919
import _root_.sbt.Keys._
2020
import _root_.sbt.{ Def, _ }
21-
import com.google.googlejavaformat.java.JavaFormatterOptions
2221
import com.github.sbt.javaformatter.JavaFormatter
22+
import com.palantir.javaformat.java.JavaFormatterOptions
2323

2424
@deprecated("Use javafmtOnCompile setting instead", "0.5.1")
2525
object AutomateJavaFormatterPlugin extends AutoPlugin {

plugin/src/main/scala/com/github/sbt/javaformatter/JavaFormatter.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import _root_.sbt.Keys._
2020
import _root_.sbt._
2121
import _root_.sbt.util.CacheImplicits._
2222
import _root_.sbt.util.{ CacheStoreFactory, FileInfo, Logger }
23-
import com.google.googlejavaformat.java.{ Formatter, JavaFormatterOptions }
23+
import com.palantir.javaformat.java.{ Formatter, JavaFormatterOptions }
2424
import scala.collection.immutable.Seq
2525

2626
object JavaFormatter {
@@ -33,7 +33,7 @@ object JavaFormatter {
3333
cacheStoreFactory: CacheStoreFactory,
3434
options: JavaFormatterOptions): Unit = {
3535
val files = sourceDirectories.descendantsExcept(includeFilter, excludeFilter).get().toList
36-
cachedFormatSources(cacheStoreFactory, files, streams.log)(new Formatter(options))
36+
cachedFormatSources(cacheStoreFactory, files, streams.log)(Formatter.createFormatter(options))
3737
}
3838

3939
def check(
@@ -45,7 +45,8 @@ object JavaFormatter {
4545
cacheStoreFactory: CacheStoreFactory,
4646
options: JavaFormatterOptions): Boolean = {
4747
val files = sourceDirectories.descendantsExcept(includeFilter, excludeFilter).get().toList
48-
val analysis = cachedCheckSources(cacheStoreFactory, baseDir, files, streams.log)(new Formatter(options))
48+
val analysis =
49+
cachedCheckSources(cacheStoreFactory, baseDir, files, streams.log)(Formatter.createFormatter(options))
4950
trueOrBoom(analysis)
5051
}
5152

plugin/src/sbt-test/sbt-java-formatter/aosp-style/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import com.google.googlejavaformat.java.JavaFormatterOptions
1+
import com.palantir.javaformat.java.JavaFormatterOptions
22
// no settings needed
33

44
ThisBuild / javafmtStyle := JavaFormatterOptions.Style.AOSP
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import com.palantir.javaformat.java.JavaFormatterOptions
2+
// no settings needed
3+
4+
ThisBuild / javafmtStyle := JavaFormatterOptions.Style.PALANTIR
5+
ThisBuild / javafmtOnCompile := true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % System.getProperty("plugin.version"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.lightbend;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
import java.util.stream.Collectors;
6+
7+
public class BadFormatting {
8+
BadFormatting() {
9+
example();
10+
}
11+
12+
public void example() {
13+
var a = List.of("", "a", "b", "c", "d").stream()
14+
.filter(s -> !s.isEmpty())
15+
.map(s -> String.format("%s-%s-%s", s, s.toLowerCase(), s.toUpperCase()))
16+
.map(Objects::toString)
17+
.collect(Collectors.joining());
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.lightbend;
2+
3+
import java.util.stream.Collectors;
4+
import java.util.Objects;
5+
import java.util.List;
6+
7+
public class BadFormatting {
8+
BadFormatting () {example();}
9+
public void example () {
10+
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());
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.lightbend;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
import java.util.stream.Collectors;
6+
7+
public class BadFormatting {
8+
BadFormatting() {
9+
example();
10+
}
11+
12+
public void example() {
13+
var a = List.of("", "a", "b", "c", "d").stream()
14+
.filter(s -> !s.isEmpty())
15+
.map(s -> String.format("%s-%s-%s", s, s.toLowerCase(), s.toUpperCase()))
16+
.map(Objects::toString)
17+
.collect(Collectors.joining());
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.lightbend;
2+
3+
import java.util.stream.Collectors;
4+
import java.util.Objects;
5+
import java.util.List;
6+
7+
public class BadFormatting {
8+
BadFormatting () {example();}
9+
public void example () {
10+
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());
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# compile should trigger formatting
2+
> Test/compile
3+
4+
#$ exec echo "====== FORMATTED ======"
5+
#$ exec cat src/main/java/com/lightbend/BadFormatting.java
6+
#$ exec echo "====== EXPECTED ======"
7+
#$ exec cat src/main/java-expected/com/lightbend/BadFormatting.java
8+
9+
#$ exec echo "====== DIFF ======"
10+
$ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java
11+
12+
$ exec diff src/test/java/com/lightbend/BadFormatting.java src/test/java-expected/com/lightbend/BadFormatting.java

0 commit comments

Comments
 (0)