-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #641 from zhelenskiy/zhelenskiy/remove_internals
Replace compiler internal API with compiler main function call besides completion
- Loading branch information
Showing
20 changed files
with
535 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/kotlin/com/compiler/server/compiler/components/CliUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.compiler.server.compiler.components | ||
|
||
import com.compiler.server.model.CompilerDiagnostics | ||
import com.compiler.server.model.ErrorDescriptor | ||
import com.compiler.server.model.ProjectSeveriry | ||
import com.compiler.server.model.TextInterval | ||
import org.jetbrains.kotlin.cli.common.CLICompiler | ||
import org.jetbrains.kotlin.cli.common.CLITool | ||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity | ||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.* | ||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation | ||
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import java.util.* | ||
import kotlin.io.path.* | ||
|
||
|
||
sealed class CompilationResult<out T> { | ||
abstract val compilerDiagnostics: CompilerDiagnostics | ||
|
||
fun <R> flatMap(action: (T) -> CompilationResult<R>): CompilationResult<R> = when (this) { | ||
is Compiled -> { | ||
val innerResult = action(result) | ||
val newDiagnostics = (compilerDiagnostics.map.keys + innerResult.compilerDiagnostics.map.keys).associateWith { | ||
val l1 = compilerDiagnostics.map[it] | ||
val l2 = innerResult.compilerDiagnostics.map[it] | ||
if (l1 != null && l2 != null) l1 + l2 else (l1 ?: l2)!! | ||
}.let(::CompilerDiagnostics) | ||
when (innerResult) { | ||
is Compiled -> innerResult.copy(compilerDiagnostics = newDiagnostics) | ||
is NotCompiled -> innerResult.copy(compilerDiagnostics = newDiagnostics) | ||
} | ||
} | ||
is NotCompiled -> this | ||
} | ||
fun <R> map(action: (T) -> R): CompilationResult<R> = when (this) { | ||
is Compiled -> Compiled(compilerDiagnostics, action(result)) | ||
is NotCompiled -> this | ||
} | ||
} | ||
|
||
data class Compiled<T>(override val compilerDiagnostics: CompilerDiagnostics, val result: T) : CompilationResult<T>() | ||
|
||
data class NotCompiled(override val compilerDiagnostics: CompilerDiagnostics) : CompilationResult<Nothing>() | ||
|
||
fun CLICompiler<*>.tryCompilation(arguments: List<String>): CompilationResult<Unit> = tryCompilation(arguments) {} | ||
fun <T> CLICompiler<*>.tryCompilation(arguments: List<String>, onSuccess: () -> T): CompilationResult<T> { | ||
val diagnosticsMap = mutableMapOf<String?, MutableList<ErrorDescriptor>>() | ||
val exitCode = CLITool.doMainNoExit(this, arguments.toTypedArray(), object : MessageRenderer { | ||
override fun renderPreamble(): String = "" | ||
|
||
override fun render( | ||
severity: CompilerMessageSeverity, | ||
message: String, | ||
location: CompilerMessageSourceLocation? | ||
): String { | ||
val textInterval = location?.let { | ||
TextInterval( | ||
start = TextInterval.TextPosition(location.line, location.column), | ||
end = TextInterval.TextPosition(location.lineEnd, location.columnEnd) | ||
) | ||
} | ||
val messageSeverity: ProjectSeveriry = when (severity) { | ||
EXCEPTION, ERROR -> ProjectSeveriry.ERROR | ||
STRONG_WARNING, WARNING -> ProjectSeveriry.WARNING | ||
INFO, LOGGING, OUTPUT -> ProjectSeveriry.INFO | ||
} | ||
diagnosticsMap.getOrPut(location?.path) { mutableListOf() }.add(ErrorDescriptor(textInterval, message, messageSeverity, className = "WARNING".takeIf { messageSeverity == ProjectSeveriry.WARNING })) | ||
return "" | ||
} | ||
|
||
override fun renderUsage(usage: String): String = | ||
render(STRONG_WARNING, usage, null) | ||
|
||
override fun renderConclusion(): String = "" | ||
|
||
override fun getName(): String = "Redirector" | ||
}) | ||
val diagnostics = CompilerDiagnostics(diagnosticsMap) | ||
return when { | ||
diagnostics.any { it.severity == ProjectSeveriry.ERROR } -> NotCompiled(diagnostics) | ||
exitCode.code != 0 -> ErrorDescriptor( | ||
severity = ProjectSeveriry.ERROR, | ||
message = "Compiler finished with non-null exit code ${exitCode.code}: ${exitCode.name}", | ||
interval = null | ||
).let { NotCompiled(CompilerDiagnostics(mapOf(null to listOf(it)))) } | ||
|
||
else -> Compiled(result = onSuccess(), compilerDiagnostics = diagnostics) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalPathApi::class) | ||
fun <T> usingTempDirectory(action: (path: Path) -> T): T { | ||
val path = getTempDirectory() | ||
path.createDirectories() | ||
return try { | ||
action(path) | ||
} finally { | ||
path.deleteRecursively() | ||
} | ||
} | ||
|
||
private fun getTempDirectory(): Path { | ||
val dir = System.getProperty("java.io.tmpdir") | ||
val sessionId = UUID.randomUUID().toString().replace("-", "") | ||
return Paths.get(dir, sessionId) | ||
} | ||
|
||
fun List<KtFile>.writeToIoFiles(inputDir: Path): List<Path> { | ||
val ioFiles = map { inputDir / it.name } | ||
for ((ioFile, ktFile) in ioFiles zip this) { | ||
ioFile.writeText(ktFile.text) | ||
} | ||
return ioFiles | ||
} | ||
|
||
val PATH_SEPARATOR: String = java.io.File.pathSeparator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.