From 2c046641c584b7f9c1d02ff1cfea9b2c28093085 Mon Sep 17 00:00:00 2001 From: traceyyoshima Date: Mon, 16 Oct 2023 16:59:56 -0600 Subject: [PATCH] Call `StringUtilRt.convertLineSeparators` on sourceCode before creating LVF. Added code to generate IR to KotlinParser. Updated names in ClassDeclarationTest#companionObject to prevent namespace conflicts. --- .../org/openrewrite/kotlin/KotlinParser.java | 20 ++++++++++++++++--- .../kotlin/internal/KotlinParserVisitor.kt | 1 - .../kotlin/tree/ClassDeclarationTest.java | 6 +++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/KotlinParser.java b/src/main/java/org/openrewrite/kotlin/KotlinParser.java index e5a06c91c..a9e228d12 100644 --- a/src/main/java/org/openrewrite/kotlin/KotlinParser.java +++ b/src/main/java/org/openrewrite/kotlin/KotlinParser.java @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.cli.jvm.config.JvmContentRootsKt; import org.jetbrains.kotlin.com.intellij.openapi.Disposable; import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer; +import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt; import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.kotlin.com.intellij.psi.FileViewProvider; import org.jetbrains.kotlin.com.intellij.psi.PsiManager; @@ -43,8 +44,7 @@ import org.jetbrains.kotlin.fir.FirSession; import org.jetbrains.kotlin.fir.declarations.FirFile; import org.jetbrains.kotlin.fir.java.FirProjectSessionProvider; -import org.jetbrains.kotlin.fir.pipeline.AnalyseKt; -import org.jetbrains.kotlin.fir.pipeline.FirUtilsKt; +import org.jetbrains.kotlin.fir.pipeline.*; import org.jetbrains.kotlin.fir.resolve.ScopeSession; import org.jetbrains.kotlin.fir.session.FirSessionConfigurator; import org.jetbrains.kotlin.fir.session.FirSessionFactoryHelper; @@ -56,6 +56,7 @@ import org.jetbrains.kotlin.psi.KtFile; import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices; import org.jetbrains.kotlin.utils.PathUtil; +import org.junit.platform.commons.util.StringUtils; import org.openrewrite.*; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.JavaParser; @@ -369,7 +370,7 @@ public CompiledSource parse(List sources, Disposable disposable, E fileName = source.getPath().toString(); } - VirtualFile vFile = new LightVirtualFile(fileName, KotlinFileType.INSTANCE, source.getSource(ctx).readFully()); + VirtualFile vFile = new LightVirtualFile(fileName, KotlinFileType.INSTANCE, StringUtilRt.convertLineSeparators(source.getSource(ctx).readFully())); final FileViewProvider fileViewProvider = new SingleRootFileViewProvider( PsiManager.getInstance(environment.getProject()), vFile @@ -431,6 +432,19 @@ public CompiledSource parse(List sources, Disposable disposable, E List rawFir = FirUtilsKt.buildFirFromKtFiles(firSession, ktFiles); Pair> result = AnalyseKt.runResolution(firSession, rawFir); AnalyseKt.runCheckers(firSession, result.getFirst(), result.getSecond(), diagnosticsReporter); +// ModuleCompilerAnalyzedOutput analyzedOutput = new ModuleCompilerAnalyzedOutput(firSession, result.getFirst(), result.getSecond()); +// FirResult firResult = new FirResult(singletonList(analyzedOutput)); +// +// Fir2IrExtensions extensions = Fir2IrExtensions.Default.INSTANCE; +// Fir2IrConfiguration irConfiguration = new Fir2IrConfiguration( +// languageVersionSettings, +// compilerConfiguration.getBoolean(JVMConfigurationKeys.LINK_VIA_SIGNATURES), +// compilerConfiguration.putIfAbsent(EVALUATED_CONST_TRACKER, EvaluatedConstTracker.Companion.create()) +// ); +// // TODO: add generation extensions +// List irGenerationExtensions = new ArrayList<>(); +// Fir2IrActualizedResult actualizedResult = convertToIrAndActualizeForJvm(firResult, extensions, irConfiguration, irGenerationExtensions, diagnosticsReporter); + assert kotlinSources.size() == result.getSecond().size(); List second = result.getSecond(); for (int i = 0; i < second.size(); i++) { diff --git a/src/main/kotlin/org/openrewrite/kotlin/internal/KotlinParserVisitor.kt b/src/main/kotlin/org/openrewrite/kotlin/internal/KotlinParserVisitor.kt index e357fac93..887357e16 100644 --- a/src/main/kotlin/org/openrewrite/kotlin/internal/KotlinParserVisitor.kt +++ b/src/main/kotlin/org/openrewrite/kotlin/internal/KotlinParserVisitor.kt @@ -87,7 +87,6 @@ import kotlin.collections.HashMap import kotlin.math.max import kotlin.math.min - class KotlinParserVisitor( kotlinSource: KotlinSource, relativeTo: Path?, diff --git a/src/test/java/org/openrewrite/kotlin/tree/ClassDeclarationTest.java b/src/test/java/org/openrewrite/kotlin/tree/ClassDeclarationTest.java index de355048a..f78bd99d5 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/ClassDeclarationTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/ClassDeclarationTest.java @@ -328,21 +328,21 @@ void companionObject() { kotlin("object Companion"), kotlin( """ - class Test { + class TestA { companion object } """ ), kotlin( """ - class Test { + class TestB { companion object Foo } """ ), kotlin( """ - class Test { + class TestC { companion object Companion } """