Skip to content

Commit

Permalink
Merge pull request #706 from JetBrains/revert-641-zhelenskiy/remove_i…
Browse files Browse the repository at this point in the history
…nternals

Revert "Replace compiler internal API..."
  • Loading branch information
zoobestik committed Nov 23, 2023
2 parents 2d3e522 + 6f9c8c6 commit 43a8023
Show file tree
Hide file tree
Showing 20 changed files with 343 additions and 535 deletions.
26 changes: 24 additions & 2 deletions common/src/main/kotlin/component/KotlinEnvironment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments
import org.jetbrains.kotlin.cli.common.createPhaseConfig
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.js.K2JsIrCompiler
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
Expand All @@ -14,6 +18,7 @@ import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.ir.backend.js.jsPhases
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.serialization.js.JsModuleDescriptor
import org.jetbrains.kotlin.serialization.js.KotlinJavascriptSerializationUtil
Expand All @@ -34,14 +39,15 @@ class KotlinEnvironment(
* See [org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments] and
* [org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments] for list of possible flags
*/
val additionalCompilerArguments: List<String> = listOf(
private val additionalCompilerArguments: List<String> = listOf(
"-opt-in=kotlin.ExperimentalStdlibApi",
"-opt-in=kotlin.time.ExperimentalTime",
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=kotlin.ExperimentalUnsignedTypes",
"-opt-in=kotlin.contracts.ExperimentalContracts",
"-opt-in=kotlin.experimental.ExperimentalTypeInference",
"-Xcontext-receivers",
"-XXLanguage:+RangeUntilOperator"
)
}

Expand Down Expand Up @@ -75,6 +81,22 @@ class KotlinEnvironment(
put(JSConfigurationKeys.WASM_ENABLE_ASSERTS, false)
}

private val messageCollector = object : MessageCollector {
override fun clear() {}
override fun hasErrors(): Boolean {
return false
}

override fun report(
severity: CompilerMessageSeverity,
message: String,
location: CompilerMessageSourceLocation?
) {
}
}

val jsIrPhaseConfig = createPhaseConfig(jsPhases, K2JsIrCompiler().createArguments(), messageCollector)

private val environment = KotlinCoreEnvironment.createForProduction(
parentDisposable = Disposer.newDisposable(),
configuration = configuration.copy(),
Expand Down Expand Up @@ -106,4 +128,4 @@ class KotlinEnvironment(
}
}
}
}
}
119 changes: 0 additions & 119 deletions src/main/kotlin/com/compiler/server/compiler/components/CliUtils.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.compiler.server.compiler.components
import com.compiler.server.compiler.KotlinFile
import com.compiler.server.compiler.KotlinResolutionFacade
import com.compiler.server.model.Analysis
import com.compiler.server.model.CompilerDiagnostics
import com.compiler.server.model.ErrorDescriptor
import com.compiler.server.model.ProjectType
import com.intellij.psi.PsiElement
import com.intellij.psi.tree.TokenSet
Expand Down Expand Up @@ -72,11 +72,11 @@ class CompletionProvider(
importVariants(file, prefix, errors, line, character, projectType)
} else emptyList()
descriptorInfo.descriptors.toMutableList().apply {
sortWith { a, b ->
sortWith(Comparator { a, b ->
val (a1, a2) = a.presentableName()
val (b1, b2) = b.presentableName()
("$a1$a2").compareTo("$b1$b2", true)
}
})
}.mapNotNull { descriptor -> completionVariantFor(prefix, descriptor, element) } +
keywordsCompletionVariants(KtTokens.KEYWORDS, prefix) +
keywordsCompletionVariants(KtTokens.SOFT_KEYWORDS, prefix) +
Expand Down Expand Up @@ -113,16 +113,15 @@ class CompletionProvider(
private fun importVariants(
file: KotlinFile,
prefix: String,
compilerDiagnostics: CompilerDiagnostics,
errors: Map<String, List<ErrorDescriptor>>,
line: Int,
character: Int,
projectType: ProjectType
): List<Completion> {
val importCompletionVariants = indexationProvider.getClassesByName(prefix, projectType)
?.map { it.toCompletion() } ?: emptyList()
val currentErrors = compilerDiagnostics.map[file.kotlinFile.name]?.filter {
it.interval != null &&
it.interval.start.line == line &&
val currentErrors = errors[file.kotlinFile.name]?.filter {
it.interval.start.line == line &&
it.interval.start.ch <= character &&
it.interval.end.line == line &&
it.interval.end.ch >= character &&
Expand Down Expand Up @@ -258,7 +257,7 @@ class CompletionProvider(

// This code is a fragment of org.jetbrains.kotlin.idea.completion.CompletionSession from Kotlin IDE Plugin
// with a few simplifications which were possible because webdemo has very restricted environment (and well,
// because requirements on completion' quality in web-demo are lower)
// because requirements on compeltion' quality in web-demo are lower)
private inner class VisibilityFilter(
private val inDescriptor: DeclarationDescriptor,
private val bindingContext: BindingContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import com.intellij.psi.PsiFile
import component.KotlinEnvironment
import model.Completion
import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.cli.js.klib.TopDownAnalyzerFacadeForWasmJs
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport
import org.jetbrains.kotlin.cli.js.klib.TopDownAnalyzerFacadeForJSIR
import org.jetbrains.kotlin.cli.js.klib.TopDownAnalyzerFacadeForWasmJs
import org.jetbrains.kotlin.cli.jvm.compiler.CliBindingTrace
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM
Expand All @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.incremental.components.InlineConstTracker
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.ir.backend.js.MainModule
import org.jetbrains.kotlin.ir.backend.js.ModulesStructure
import org.jetbrains.kotlin.js.config.ErrorTolerancePolicy
import org.jetbrains.kotlin.js.config.JsConfig
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
import org.jetbrains.kotlin.name.Name
Expand Down Expand Up @@ -59,15 +60,15 @@ class ErrorAnalyzer(
projectType: ProjectType
): ErrorsAndAnalysis {
val analysis = when {
projectType.isJvmRelated() -> analysisOf(files, coreEnvironment)
projectType.isJvmRelated() ->analysisOf(files, coreEnvironment)
projectType.isJsRelated() -> analyzeFileForJs(files, coreEnvironment)
projectType == ProjectType.WASM -> analyzeFileForWasm(files, coreEnvironment)
else -> throw IllegalArgumentException("Unknown platform: $projectType")
}
return ErrorsAndAnalysis(
errorsFrom(
analysis.analysisResult.bindingContext.diagnostics.all(),
CompilerDiagnostics(files.associate { it.name to analyzeErrorsFrom(it, projectType) }),
files.associate { it.name to anylizeErrorsFrom(it, projectType) },
projectType
),
analysis
Expand Down Expand Up @@ -232,13 +233,24 @@ class ErrorAnalyzer(

fun errorsFrom(
diagnostics: Collection<Diagnostic>,
compilerDiagnostics: CompilerDiagnostics,
errors: Map<String, List<ErrorDescriptor>>,
projectType: ProjectType
): CompilerDiagnostics = (compilerDiagnostics.map and errorsFrom(diagnostics, projectType)).map { (fileName, errors) ->
fileName to errors.sortedWith(Comparator.comparing({ it.interval?.start }, nullsFirst()))
}.toMap().let(::CompilerDiagnostics)
): Map<String, List<ErrorDescriptor>> {
return (errors and errorsFrom(diagnostics, projectType)).map { (fileName, errors) ->
fileName to errors.sortedWith { o1, o2 ->
val line = o1.interval.start.line.compareTo(o2.interval.start.line)
when (line) {
0 -> o1.interval.start.ch.compareTo(o2.interval.start.ch)
else -> line
}
}
}.toMap()
}

fun isOnlyWarnings(errors: Map<String, List<ErrorDescriptor>>) =
errors.none { it.value.any { error -> error.severity == ProjectSeveriry.ERROR } }

private fun analyzeErrorsFrom(file: PsiFile, projectType: ProjectType): List<ErrorDescriptor> {
private fun anylizeErrorsFrom(file: PsiFile, projectType: ProjectType): List<ErrorDescriptor> {
class Visitor : PsiElementVisitor() {
val errors = mutableListOf<PsiErrorElement>()
override fun visitElement(element: PsiElement) {
Expand Down Expand Up @@ -335,7 +347,7 @@ class ErrorAnalyzer(
}
}.groupBy { it.first }.map { it.key to it.value.map { (_, error) -> error } }.toMap()

private infix fun Map<out String?, List<ErrorDescriptor>>.and(errors: Map<String, List<ErrorDescriptor>>) =
private infix fun Map<String, List<ErrorDescriptor>>.and(errors: Map<String, List<ErrorDescriptor>>) =
(this.toList() + errors.toList())
.groupBy { it.first }
.map { it.key to it.value.fold(emptyList<ErrorDescriptor>()) { acc, (_, errors) -> acc + errors } }
Expand All @@ -350,4 +362,4 @@ class ErrorAnalyzer(
}
}

data class ErrorsAndAnalysis(val compilerDiagnostics: CompilerDiagnostics, val analysis: Analysis)
data class ErrorsAndAnalysis(val errors: Map<String, List<ErrorDescriptor>>, val analysis: Analysis)
Loading

0 comments on commit 43a8023

Please sign in to comment.