Refactoring scripting infrastructure:
- add a helper for new constructing Compilation Configuration only when necessary - add more converters for legacy diagnostics - add helpers for simpler creating of failure results
This commit is contained in:
@@ -138,6 +138,24 @@ operator fun <R> List<ScriptDiagnostic>.plus(result: ResultWithDiagnostics<R>):
|
||||
fun <R> R.asSuccess(reports: List<ScriptDiagnostic> = listOf()): ResultWithDiagnostics.Success<R> =
|
||||
ResultWithDiagnostics.Success(this, reports)
|
||||
|
||||
/**
|
||||
* Makes Failure result with optional diagnostic [reports]
|
||||
*/
|
||||
fun makeFailureResult(reports: List<ScriptDiagnostic>): ResultWithDiagnostics.Failure =
|
||||
ResultWithDiagnostics.Failure(reports)
|
||||
|
||||
/**
|
||||
* Makes Failure result with optional diagnostic [reports]
|
||||
*/
|
||||
fun makeFailureResult(vararg reports: ScriptDiagnostic): ResultWithDiagnostics.Failure =
|
||||
ResultWithDiagnostics.Failure(reports.asList())
|
||||
|
||||
/**
|
||||
* Makes Failure result with diagnostic [message] with optional [path] and [location]
|
||||
*/
|
||||
fun makeFailureResult(message: String, path: String? = null, location: SourceCode.Location? = null): ResultWithDiagnostics.Failure =
|
||||
ResultWithDiagnostics.Failure(message.asErrorDiagnostics(path, location))
|
||||
|
||||
/**
|
||||
* Converts the receiver Throwable to the Failure results wrapper with optional [customMessage], [path] and [location]
|
||||
*/
|
||||
|
||||
@@ -34,6 +34,16 @@ open class ScriptCompilationConfiguration(baseConfigurations: Iterable<ScriptCom
|
||||
companion object : ScriptCompilationConfigurationKeys
|
||||
|
||||
object Default : ScriptCompilationConfiguration()
|
||||
|
||||
/**
|
||||
* An alternative to the constructor with base configuration, which returns a new configuration only if [body] adds anything
|
||||
* to the original one, otherwise returns original
|
||||
*/
|
||||
fun withUpdates(body: Builder.() -> Unit = {}): ScriptCompilationConfiguration {
|
||||
val newConfiguration = ScriptCompilationConfiguration(this, body = body)
|
||||
return if (newConfiguration == this) this
|
||||
else newConfiguration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -39,5 +39,17 @@ fun mapToLegacyScriptReportSeverity(severity: ScriptDiagnostic.Severity): Script
|
||||
fun mapLegacyScriptPosition(pos: ScriptContents.Position?): SourceCode.Location? =
|
||||
pos?.let { SourceCode.Location(SourceCode.Position(pos.line, pos.col)) }
|
||||
|
||||
fun mapLegacyScriptPosition(pos: ScriptReport.Position?): SourceCode.Location? =
|
||||
pos?.let {
|
||||
val endPos =
|
||||
if (pos.endLine == null || pos.endColumn == null) null
|
||||
else SourceCode.Position(pos.endLine!!, pos.endColumn!!)
|
||||
SourceCode.Location(SourceCode.Position(pos.startLine, pos.startColumn), endPos)
|
||||
}
|
||||
|
||||
fun mapToLegacyScriptReportPosition(pos: SourceCode.Location?): ScriptReport.Position? =
|
||||
pos?.let { ScriptReport.Position(pos.start.line, pos.start.col) }
|
||||
|
||||
fun Iterable<ScriptReport>.mapToDiagnostics(): List<ScriptDiagnostic> = map { (message, severity, position: ScriptReport.Position?) ->
|
||||
ScriptDiagnostic(message, mapLegacyDiagnosticSeverity(severity), null, mapLegacyScriptPosition(position))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user