Rename ScriptSource to SourceCode, extract location to another i-face...
other related refactorings. The goal - make interface terminologically more suitable for usage in REPL.
This commit is contained in:
@@ -10,7 +10,7 @@ package kotlin.script.experimental.api
|
||||
data class ScriptDiagnostic(
|
||||
val message: String,
|
||||
val severity: Severity = Severity.ERROR,
|
||||
val location: ScriptSource.Location? = null,
|
||||
val location: SourceCode.Location? = null,
|
||||
val exception: Throwable? = null
|
||||
) {
|
||||
enum class Severity { FATAL, ERROR, WARNING, INFO, DEBUG }
|
||||
@@ -56,10 +56,10 @@ operator fun <R> List<ScriptDiagnostic>.plus(res: ResultWithDiagnostics<R>): Res
|
||||
fun <R> R.asSuccess(reports: List<ScriptDiagnostic> = listOf()): ResultWithDiagnostics.Success<R> =
|
||||
ResultWithDiagnostics.Success(this, reports)
|
||||
|
||||
fun Throwable.asDiagnostics(customMessage: String? = null, location: ScriptSource.Location? = null): ScriptDiagnostic =
|
||||
fun Throwable.asDiagnostics(customMessage: String? = null, location: SourceCode.Location? = null): ScriptDiagnostic =
|
||||
ScriptDiagnostic(customMessage ?: message ?: "$this", ScriptDiagnostic.Severity.ERROR, location, this)
|
||||
|
||||
fun String.asErrorDiagnostics(location: ScriptSource.Location? = null): ScriptDiagnostic =
|
||||
fun String.asErrorDiagnostics(location: SourceCode.Location? = null): ScriptDiagnostic =
|
||||
ScriptDiagnostic(this, ScriptDiagnostic.Severity.ERROR, location)
|
||||
|
||||
fun<R> ResultWithDiagnostics<R>.resultOrNull(): R? = when (this) {
|
||||
|
||||
@@ -120,7 +120,7 @@ class RefineConfigurationOnSectionsData(
|
||||
interface ScriptCompiler {
|
||||
|
||||
suspend operator fun invoke(
|
||||
script: ScriptSource,
|
||||
script: SourceCode,
|
||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
): ResultWithDiagnostics<CompiledScript<*>>
|
||||
}
|
||||
|
||||
@@ -10,16 +10,19 @@ package kotlin.script.experimental.api
|
||||
import java.net.URL
|
||||
import kotlin.script.experimental.util.PropertiesCollection
|
||||
|
||||
interface ScriptSource {
|
||||
val location: URL?
|
||||
val text: String?
|
||||
interface SourceCode {
|
||||
val text: String
|
||||
|
||||
data class Position(val line: Int, val col: Int, val absolutePos: Int? = null)
|
||||
data class Range(val start: Position, val end: Position)
|
||||
data class Location(val start: Position, val end: Position? = null)
|
||||
}
|
||||
|
||||
data class ScriptSourceNamedFragment(val name: String?, val range: ScriptSource.Range)
|
||||
interface ExternalSourceCode : SourceCode {
|
||||
val externalLocation: URL
|
||||
}
|
||||
|
||||
data class ScriptSourceNamedFragment(val name: String?, val range: SourceCode.Range)
|
||||
|
||||
enum class ScriptBodyTarget {
|
||||
Constructor,
|
||||
@@ -53,7 +56,7 @@ val ScriptCollectedDataKeys.foundAnnotations by PropertiesCollection.key<List<An
|
||||
val ScriptCollectedDataKeys.foundFragments by PropertiesCollection.key<List<ScriptSourceNamedFragment>>()
|
||||
|
||||
class ScriptConfigurationRefinementContext(
|
||||
val source: ScriptSource,
|
||||
val script: SourceCode,
|
||||
val compilationConfiguration: ScriptCompilationConfiguration,
|
||||
val collectedData: ScriptCollectedData? = null
|
||||
)
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ abstract class BasicScriptingHost(
|
||||
open fun <T> runInCoroutineContext(block: suspend CoroutineScope.() -> T): T = runBlocking { block() }
|
||||
|
||||
open fun eval(
|
||||
script: ScriptSource,
|
||||
script: SourceCode,
|
||||
scriptCompilationConfiguration: ScriptCompilationConfiguration,
|
||||
configuration: ScriptEvaluationConfiguration?
|
||||
): ResultWithDiagnostics<EvaluationResult> =
|
||||
|
||||
@@ -9,15 +9,8 @@ import java.io.File
|
||||
import java.net.URL
|
||||
import kotlin.script.experimental.api.*
|
||||
|
||||
fun ScriptSource.getScriptText(): String = when {
|
||||
text != null -> text!!
|
||||
location != null ->
|
||||
location!!.openStream().bufferedReader().readText()
|
||||
else -> throw RuntimeException("unable to get text from null script")
|
||||
}
|
||||
|
||||
fun getMergedScriptText(script: ScriptSource, configuration: ScriptCompilationConfiguration?): String {
|
||||
val originalScriptText = script.getScriptText()
|
||||
fun getMergedScriptText(script: SourceCode, configuration: ScriptCompilationConfiguration?): String {
|
||||
val originalScriptText = script.text
|
||||
val sourceFragments = configuration?.get(ScriptCompilationConfiguration.sourceFragments)
|
||||
return if (sourceFragments == null || sourceFragments.isEmpty()) {
|
||||
originalScriptText
|
||||
@@ -45,16 +38,15 @@ fun getMergedScriptText(script: ScriptSource, configuration: ScriptCompilationCo
|
||||
}
|
||||
}
|
||||
|
||||
open class FileScriptSource(val file: File) : ScriptSource {
|
||||
override val location: URL? get() = file.toURI().toURL()
|
||||
override val text: String? get() = null
|
||||
open class FileScriptSource(val file: File) : ExternalSourceCode {
|
||||
override val externalLocation: URL get() = file.toURI().toURL()
|
||||
override val text: String by lazy { file.readText() }
|
||||
}
|
||||
|
||||
fun File.toScriptSource(): ScriptSource = FileScriptSource(this)
|
||||
fun File.toScriptSource(): SourceCode = FileScriptSource(this)
|
||||
|
||||
open class StringScriptSource(val source: String) : ScriptSource {
|
||||
override val location: URL? get() = null
|
||||
override val text: String? get() = source
|
||||
open class StringScriptSource(val source: String) : SourceCode {
|
||||
override val text: String get() = source
|
||||
}
|
||||
|
||||
fun String.toScriptSource(): ScriptSource = StringScriptSource(this)
|
||||
fun String.toScriptSource(): SourceCode = StringScriptSource(this)
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
package kotlin.script.experimental.repl
|
||||
|
||||
import kotlin.script.experimental.api.ScriptSource
|
||||
import kotlin.script.experimental.api.SourceCode
|
||||
|
||||
interface ReplSnippetSource : ScriptSource, ReplSnippetId
|
||||
interface ReplSnippetSource : SourceCode, ReplSnippetId
|
||||
|
||||
|
||||
+2
-2
@@ -78,7 +78,7 @@ class KJvmCompiledScript<out ScriptBase : Any>(
|
||||
class KJvmCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) : KJvmCompilerProxy {
|
||||
|
||||
override fun compile(
|
||||
script: ScriptSource,
|
||||
script: SourceCode,
|
||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
||||
val messageCollector = ScriptDiagnosticsMessageCollector()
|
||||
@@ -229,7 +229,7 @@ class ScriptDiagnosticsMessageCollector : MessageCollector {
|
||||
}
|
||||
if (mappedSeverity != null) {
|
||||
val mappedLocation = location?.let {
|
||||
ScriptSource.Location(ScriptSource.Position(it.line, it.column))
|
||||
SourceCode.Location(SourceCode.Position(it.line, it.column))
|
||||
}
|
||||
_diagnostics.add(ScriptDiagnostic(message, mappedSeverity, mappedLocation))
|
||||
}
|
||||
|
||||
+6
-6
@@ -14,16 +14,16 @@ import kotlin.script.experimental.jvmhost.impl.KJvmCompilerImpl
|
||||
import kotlin.script.experimental.jvmhost.impl.withDefaults
|
||||
|
||||
interface CompiledJvmScriptsCache {
|
||||
fun get(script: ScriptSource, scriptCompilationConfiguration: ScriptCompilationConfiguration): CompiledScript<*>?
|
||||
fun store(compiledScript: CompiledScript<*>, script: ScriptSource, scriptCompilationConfiguration: ScriptCompilationConfiguration)
|
||||
fun get(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration): CompiledScript<*>?
|
||||
fun store(compiledScript: CompiledScript<*>, script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration)
|
||||
|
||||
object NoCache : CompiledJvmScriptsCache {
|
||||
override fun get(
|
||||
script: ScriptSource, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
): CompiledScript<*>? = null
|
||||
|
||||
override fun store(
|
||||
compiledScript: CompiledScript<*>, script: ScriptSource, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
compiledScript: CompiledScript<*>, script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ open class JvmScriptCompiler(
|
||||
) : ScriptCompiler {
|
||||
|
||||
override suspend operator fun invoke(
|
||||
script: ScriptSource,
|
||||
script: SourceCode,
|
||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
||||
val refineConfigurationFn = scriptCompilationConfiguration[ScriptCompilationConfiguration.refineConfigurationBeforeParsing]
|
||||
@@ -61,7 +61,7 @@ open class JvmScriptCompiler(
|
||||
|
||||
interface KJvmCompilerProxy {
|
||||
fun compile(
|
||||
script: ScriptSource,
|
||||
script: SourceCode,
|
||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
): ResultWithDiagnostics<CompiledScript<*>>
|
||||
}
|
||||
|
||||
+4
-4
@@ -9,7 +9,7 @@ package kotlin.script.experimental.jvm.compat
|
||||
import kotlin.script.dependencies.ScriptContents
|
||||
import kotlin.script.dependencies.ScriptDependenciesResolver
|
||||
import kotlin.script.experimental.api.ScriptDiagnostic
|
||||
import kotlin.script.experimental.api.ScriptSource
|
||||
import kotlin.script.experimental.api.SourceCode
|
||||
import kotlin.script.experimental.dependencies.ScriptReport
|
||||
|
||||
fun mapLegacyDiagnosticSeverity(severity: ScriptDependenciesResolver.ReportSeverity): ScriptDiagnostic.Severity = when (severity) {
|
||||
@@ -28,8 +28,8 @@ fun mapToLegacyScriptReportSeverity(severity: ScriptDiagnostic.Severity): Script
|
||||
ScriptDiagnostic.Severity.DEBUG -> ScriptReport.Severity.DEBUG
|
||||
}
|
||||
|
||||
fun mapLegacyScriptPosition(pos: ScriptContents.Position?): ScriptSource.Location? =
|
||||
pos?.let { ScriptSource.Location(ScriptSource.Position(pos.line, pos.col)) }
|
||||
fun mapLegacyScriptPosition(pos: ScriptContents.Position?): SourceCode.Location? =
|
||||
pos?.let { SourceCode.Location(SourceCode.Position(pos.line, pos.col)) }
|
||||
|
||||
fun mapToLegacyScriptReportPosition(pos: ScriptSource.Location?): ScriptReport.Position? =
|
||||
fun mapToLegacyScriptReportPosition(pos: SourceCode.Location?): ScriptReport.Position? =
|
||||
pos?.let { ScriptReport.Position(pos.start.line, pos.start.col) }
|
||||
|
||||
+1
-1
@@ -84,7 +84,7 @@ class BridgeDependenciesResolver(
|
||||
internal fun List<ScriptDiagnostic>.mapScriptReportsToDiagnostics() =
|
||||
map { ScriptReport(it.message, mapToLegacyScriptReportSeverity(it.severity), mapToLegacyScriptReportPosition(it.location)) }
|
||||
|
||||
internal fun ScriptContents.toScriptSource(): ScriptSource = when {
|
||||
internal fun ScriptContents.toScriptSource(): SourceCode = when {
|
||||
text != null -> text!!.toString().toScriptSource()
|
||||
file != null -> file!!.toScriptSource()
|
||||
else -> throw IllegalArgumentException("Unable to convert script contents $this into script source")
|
||||
|
||||
Reference in New Issue
Block a user