Cleanup visitor classes
Summary:
When support for Kotlin 1.4 was added (D25524613 (https://github.com/facebookincubator/ktfmt/commit/8125fe127983a4063f34d73f4e0033311058b953)) it made sense to have multiple AST classes. But when support for 1.3 was dropped (D25732436 (https://github.com/facebookincubator/ktfmt/commit/1d2e0b26d62e1229a410d4c8890896a20a2c8eca)) that stopped being true.
This change consolidates the visitor classes and explicitly defines a minimum version (1.4).
Reviewed By: hick209
Differential Revision: D32729470
fbshipit-source-id: fdc57f6c45ec482cfe7cf9870e2f990dfefecf69
diff --git a/core/src/main/java/com/facebook/ktfmt/Formatter.kt b/core/src/main/java/com/facebook/ktfmt/Formatter.kt
index 2772030..e4a37ef 100644
--- a/core/src/main/java/com/facebook/ktfmt/Formatter.kt
+++ b/core/src/main/java/com/facebook/ktfmt/Formatter.kt
@@ -41,6 +41,8 @@
const val DEFAULT_MAX_WIDTH: Int = 100
+val MINIMUM_KOTLIN_VERSION = KotlinVersion(1, 4)
+
@JvmField
val GOOGLE_FORMAT = FormattingOptions(style = GOOGLE, blockIndent = 2, continuationIndent = 2)
@@ -152,22 +154,10 @@
}
fun createAstVisitor(options: FormattingOptions, builder: OpsBuilder): PsiElementVisitor {
- val visitorClassName =
- when {
- KotlinVersion.CURRENT.major == 1 && KotlinVersion.CURRENT.minor == 4 ->
- "com.facebook.ktfmt.Kotlin14InputAstVisitor"
- KotlinVersion.CURRENT.major == 1 && KotlinVersion.CURRENT.minor == 5 ->
- "com.facebook.ktfmt.Kotlin15InputAstVisitor"
- KotlinVersion.CURRENT.major == 1 && KotlinVersion.CURRENT.minor == 6 ->
- "com.facebook.ktfmt.Kotlin15InputAstVisitor"
- else ->
- throw RuntimeException("Unsupported runtime Kotlin version: " + KotlinVersion.CURRENT)
- }
-
- return Class.forName(visitorClassName)
- .asSubclass(KotlinInputAstVisitorBase::class.java)
- .getConstructor(FormattingOptions::class.java, OpsBuilder::class.java)
- .newInstance(options, builder)
+ if (KotlinVersion.CURRENT < MINIMUM_KOTLIN_VERSION) {
+ throw RuntimeException("Unsupported runtime Kotlin version: " + KotlinVersion.CURRENT)
+ }
+ return KotlinInputAstVisitor(options, builder)
}
private fun checkEscapeSequences(code: String) {
diff --git a/core/src/main/java/com/facebook/ktfmt/Kotlin14InputAstVisitor.kt b/core/src/main/java/com/facebook/ktfmt/Kotlin14InputAstVisitor.kt
deleted file mode 100644
index c1d8c0b..0000000
--- a/core/src/main/java/com/facebook/ktfmt/Kotlin14InputAstVisitor.kt
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) Facebook, Inc. and its affiliates.
- *
- * 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.facebook.ktfmt
-
-import com.google.googlejavaformat.OpsBuilder
-import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression
-
-class Kotlin14InputAstVisitor(options: FormattingOptions, builder: OpsBuilder) :
- KotlinInputAstVisitorBase(options, builder) {
- override fun visitCollectionLiteralExpression(expression: KtCollectionLiteralExpression) {
- super.actualVisitCollectionLiteralExpression(expression)
- }
-}
diff --git a/core/src/main/java/com/facebook/ktfmt/Kotlin15InputAstVisitor.kt b/core/src/main/java/com/facebook/ktfmt/Kotlin15InputAstVisitor.kt
deleted file mode 100644
index c22a1b8..0000000
--- a/core/src/main/java/com/facebook/ktfmt/Kotlin15InputAstVisitor.kt
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) Facebook, Inc. and its affiliates.
- *
- * 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.facebook.ktfmt
-
-import com.google.googlejavaformat.OpsBuilder
-import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression
-
-class Kotlin15InputAstVisitor(options: FormattingOptions, builder: OpsBuilder) :
- KotlinInputAstVisitorBase(options, builder) {
- override fun visitCollectionLiteralExpression(expression: KtCollectionLiteralExpression) {
- super.actualVisitCollectionLiteralExpression(expression)
- }
-}
diff --git a/core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitorBase.kt b/core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitor.kt
similarity index 99%
rename from core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitorBase.kt
rename to core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitor.kt
index c1911d8..6d87d4f 100644
--- a/core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitorBase.kt
+++ b/core/src/main/java/com/facebook/ktfmt/KotlinInputAstVisitor.kt
@@ -125,7 +125,7 @@
import org.jetbrains.kotlin.types.Variance
/** An AST visitor that builds a stream of {@link Op}s to format. */
-open class KotlinInputAstVisitorBase(
+class KotlinInputAstVisitor(
private val options: FormattingOptions,
private val builder: OpsBuilder
) : KtTreeVisitorVoid() {
@@ -2075,7 +2075,7 @@
* }
* ```
*/
- fun actualVisitCollectionLiteralExpression(expression: KtCollectionLiteralExpression) {
+ override fun visitCollectionLiteralExpression(expression: KtCollectionLiteralExpression) {
builder.sync(expression)
builder.block(ZERO) {
builder.token("[")