Improve org.pkl.formatter.Formatter JVM API (#1428)

Makes for less fussing around to consume this API from the Gradle side
This commit is contained in:
Jen Basch
2026-02-11 09:22:42 -08:00
committed by GitHub
parent 60f628eb36
commit 63a20dd453
2 changed files with 7 additions and 15 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved. * Copyright © 2025-2026 Apple Inc. and the Pkl project authors. All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -47,20 +47,11 @@ class PklFormatterFunc(@Transient private val configuration: Configuration) :
private val formatterClass by lazy { classLoader.loadClass("org.pkl.formatter.Formatter") } private val formatterClass by lazy { classLoader.loadClass("org.pkl.formatter.Formatter") }
private val grammarVersionClass by lazy { private val formatMethod by lazy { formatterClass.getMethod("format", String::class.java) }
classLoader.loadClass("org.pkl.formatter.GrammarVersion")
}
private val grammarVersionLatestMethod by lazy { grammarVersionClass.getMethod("latest") }
private val formatMethod by lazy {
formatterClass.getMethod("format", String::class.java, grammarVersionClass)
}
private val formatterInstance by lazy { formatterClass.getConstructor().newInstance() } private val formatterInstance by lazy { formatterClass.getConstructor().newInstance() }
override fun apply(input: String): String { override fun apply(input: String): String {
val latestGrammarVersion = grammarVersionLatestMethod(null) return formatMethod(formatterInstance, input) as String
return formatMethod(formatterInstance, input, latestGrammarVersion) as String
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved. * Copyright © 2025-2026 Apple Inc. and the Pkl project authors. All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@ class Formatter {
* @return the formatted Pkl source code as a string * @return the formatted Pkl source code as a string
* @throws java.io.IOException if the file cannot be read * @throws java.io.IOException if the file cannot be read
*/ */
@JvmOverloads
fun format(path: Path, grammarVersion: GrammarVersion = GrammarVersion.latest()): String { fun format(path: Path, grammarVersion: GrammarVersion = GrammarVersion.latest()): String {
return format(Files.readString(path), grammarVersion) return format(Files.readString(path), grammarVersion)
} }
@@ -42,6 +43,7 @@ class Formatter {
* @param grammarVersion grammar compatibility version * @param grammarVersion grammar compatibility version
* @return the formatted Pkl source code as a string * @return the formatted Pkl source code as a string
*/ */
@JvmOverloads
fun format(text: String, grammarVersion: GrammarVersion = GrammarVersion.latest()): String { fun format(text: String, grammarVersion: GrammarVersion = GrammarVersion.latest()): String {
val parser = GenericParser() val parser = GenericParser()
val builder = Builder(text, grammarVersion) val builder = Builder(text, grammarVersion)
@@ -60,7 +62,6 @@ enum class GrammarVersion(val version: Int, val versionSpan: String) {
V2(2, "0.30+"); V2(2, "0.30+");
companion object { companion object {
@JvmStatic @JvmStatic fun latest(): GrammarVersion = entries.maxBy { it.version }
fun latest(): GrammarVersion = entries.maxBy { it.version }
} }
} }