Skip to content

Commit 3570fb3

Browse files
committed
Restore infer type functionality in runtime plugin
1 parent dee4cb7 commit 3570fb3

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

kshell/src/main/kotlin/com/github/khud/sparklin/kshell/KShell.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.github.khud.sparklin.kshell.configuration.IntConverter
55
import com.github.khud.sparklin.kshell.configuration.ReplConfiguration
66
import com.github.khud.sparklin.kshell.wrappers.ResultWrapper
77
import kotlinx.coroutines.runBlocking
8-
import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback
98
import org.jetbrains.kotlin.scripting.ide_services.compiler.KJvmReplCompilerWithIdeServices
109
import org.jline.reader.EndOfFileException
1110
import org.jline.reader.LineReader
@@ -14,6 +13,7 @@ import org.jline.reader.UserInterruptException
1413
import org.jline.terminal.Terminal
1514
import org.jline.terminal.TerminalBuilder
1615
import java.io.File
16+
import java.util.*
1717
import java.util.concurrent.atomic.AtomicInteger
1818
import kotlin.script.experimental.api.*
1919
import kotlin.script.experimental.host.ScriptingHostConfiguration
@@ -87,8 +87,6 @@ open class KShell(val replConfiguration: ReplConfiguration,
8787
fun initEngine() {
8888
replConfiguration.load()
8989

90-
setIdeaIoUseFallback()
91-
9290
settings = Settings(replConfiguration)
9391

9492
val term = if (settings.overrideSignals) {
@@ -189,10 +187,14 @@ open class KShell(val replConfiguration: ReplConfiguration,
189187

190188
private fun nextLine(code: String) = code.toScriptSource("Line_${currentSnippetNo.incrementAndGet()}.${compilationConfiguration[ScriptCompilationConfiguration.fileExtension]}")
191189

190+
private fun tempLine(code: String) = code.toScriptSource("\$\$tempLine_${UUID.randomUUID()}.${compilationConfiguration[ScriptCompilationConfiguration.fileExtension]}")
191+
192192
suspend fun compile(code: String) = compiler.compile(nextLine(code), compilationConfiguration)
193193

194194
fun compile(code: SourceCode) = runBlocking { compiler.compile(code, compilationConfiguration) }
195195

196+
fun analyze(code: String, pos: SourceCode.Position) = runBlocking { compiler.analyze(tempLine(code), pos, compilationConfiguration) }
197+
196198
fun eval(source: String): ResultWrapper {
197199
return if (settings.overrideSignals) {
198200
evalThread.apply {

kshell/src/main/kotlin/com/github/khud/sparklin/kshell/plugins/RuntimePlugin.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import kotlin.reflect.full.declaredMemberProperties
1414
import kotlin.reflect.full.primaryConstructor
1515
import kotlin.reflect.full.valueParameters
1616
import kotlin.reflect.jvm.javaField
17-
import kotlin.script.experimental.api.ScriptCompilationConfiguration
18-
import kotlin.script.experimental.api.resultField
17+
import kotlin.script.experimental.api.*
1918
import kotlin.script.experimental.jvm.KJvmEvaluatedSnippet
2019
import kotlin.script.experimental.util.LinkedSnippet
2120

@@ -46,12 +45,13 @@ class RuntimePlugin : Plugin {
4645
val expr = line.substring(p + 1).trim()
4746

4847
// TODO: restore
49-
// val compileResult = repl.compile(CodeExpr(counter.getAndIncrement(), expr))
50-
// when (compileResult) {
51-
// is Result.Error -> repl.handleError(compileResult.error)
52-
// is Result.Success -> compileResult.data.classes.type?.let { println(it) }
53-
// }
54-
println("!not implemented!")
48+
val analysisResults = repl.analyze(expr, SourceCode.Position(0, 0))
49+
when (analysisResults) {
50+
is ResultWithDiagnostics.Failure -> repl.handleError(analysisResults)
51+
is ResultWithDiagnostics.Success<ReplAnalyzerResult> -> {
52+
analysisResults.value[ReplAnalyzerResult.renderedResultType]?.let { println(it) }
53+
}
54+
}
5555
}
5656

5757
override fun highlighter(): Highlighter = customHighlighter

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<properties>
7-
<kotlin.version>1.4.0-dev-6132</kotlin.version>
7+
<kotlin.version>1.4.0-dev-8551</kotlin.version>
88
<jvm.version>1.8</jvm.version>
99
<sparklin.version>0.2.7-SNAPSHOT</sparklin.version>
1010
</properties>

0 commit comments

Comments
 (0)