Skip to content

Commit 63a20dd

Browse files
authored
Improve org.pkl.formatter.Formatter JVM API (#1428)
Makes for less fussing around to consume this API from the Gradle side
1 parent 60f628e commit 63a20dd

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

buildSrc/src/main/kotlin/PklFormatterSpotless.kt

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved.
2+
* Copyright © 2025-2026 Apple Inc. and the Pkl project authors. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,20 +47,11 @@ class PklFormatterFunc(@Transient private val configuration: Configuration) :
4747

4848
private val formatterClass by lazy { classLoader.loadClass("org.pkl.formatter.Formatter") }
4949

50-
private val grammarVersionClass by lazy {
51-
classLoader.loadClass("org.pkl.formatter.GrammarVersion")
52-
}
53-
54-
private val grammarVersionLatestMethod by lazy { grammarVersionClass.getMethod("latest") }
55-
56-
private val formatMethod by lazy {
57-
formatterClass.getMethod("format", String::class.java, grammarVersionClass)
58-
}
50+
private val formatMethod by lazy { formatterClass.getMethod("format", String::class.java) }
5951

6052
private val formatterInstance by lazy { formatterClass.getConstructor().newInstance() }
6153

6254
override fun apply(input: String): String {
63-
val latestGrammarVersion = grammarVersionLatestMethod(null)
64-
return formatMethod(formatterInstance, input, latestGrammarVersion) as String
55+
return formatMethod(formatterInstance, input) as String
6556
}
6657
}

pkl-formatter/src/main/kotlin/org/pkl/formatter/Formatter.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved.
2+
* Copyright © 2025-2026 Apple Inc. and the Pkl project authors. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@ class Formatter {
3131
* @return the formatted Pkl source code as a string
3232
* @throws java.io.IOException if the file cannot be read
3333
*/
34+
@JvmOverloads
3435
fun format(path: Path, grammarVersion: GrammarVersion = GrammarVersion.latest()): String {
3536
return format(Files.readString(path), grammarVersion)
3637
}
@@ -42,6 +43,7 @@ class Formatter {
4243
* @param grammarVersion grammar compatibility version
4344
* @return the formatted Pkl source code as a string
4445
*/
46+
@JvmOverloads
4547
fun format(text: String, grammarVersion: GrammarVersion = GrammarVersion.latest()): String {
4648
val parser = GenericParser()
4749
val builder = Builder(text, grammarVersion)
@@ -60,7 +62,6 @@ enum class GrammarVersion(val version: Int, val versionSpan: String) {
6062
V2(2, "0.30+");
6163

6264
companion object {
63-
@JvmStatic
64-
fun latest(): GrammarVersion = entries.maxBy { it.version }
65+
@JvmStatic fun latest(): GrammarVersion = entries.maxBy { it.version }
6566
}
6667
}

0 commit comments

Comments
 (0)