Add API to get locations of collected script annotations

#KT-38404 fixed

also:
- Add wrapper class for the location combined with the location id
- Add source code location parameters to external dependency resolvers
- Add tests for locations in annotations
- Add tests for order of annotation resolution for dependencies resolvers
This commit is contained in:
Mathias Quintero
2020-05-20 13:42:08 +02:00
committed by Ilya Chernikov
parent 1539128c3f
commit 83087291df
19 changed files with 550 additions and 85 deletions
@@ -33,6 +33,14 @@ data class ScriptDiagnostic(
*/
enum class Severity { FATAL, ERROR, WARNING, INFO, DEBUG }
constructor(
code: Int,
message: String,
severity: Severity = Severity.ERROR,
locationWithId: SourceCode.LocationWithId?,
exception: Throwable? = null
) : this(code, message, severity, locationWithId?.codeLocationId, locationWithId?.locationInText, exception)
override fun toString(): String = render()
/**
@@ -211,6 +219,12 @@ fun makeFailureResult(vararg reports: ScriptDiagnostic): ResultWithDiagnostics.F
fun makeFailureResult(message: String, path: String? = null, location: SourceCode.Location? = null): ResultWithDiagnostics.Failure =
ResultWithDiagnostics.Failure(message.asErrorDiagnostics(ScriptDiagnostic.unspecifiedError, path, location))
/**
* Makes Failure result with diagnostic [message] with optional [locationWithId]
*/
fun makeFailureResult(message: String, locationWithId: SourceCode.LocationWithId?): ResultWithDiagnostics.Failure =
ResultWithDiagnostics.Failure(message.asErrorDiagnostics(ScriptDiagnostic.unspecifiedError, locationWithId))
/**
* Converts the receiver Throwable to the Failure results wrapper with optional [customMessage], [path] and [location]
*/
@@ -223,6 +237,17 @@ fun Throwable.asDiagnostics(
): ScriptDiagnostic =
ScriptDiagnostic(code, customMessage ?: message ?: "$this", severity, path, location, this)
/**
* Converts the receiver Throwable to the Failure results wrapper with optional [customMessage], [locationWithId]
*/
fun Throwable.asDiagnostics(
code: Int = ScriptDiagnostic.unspecifiedException,
customMessage: String? = null,
locationWithId: SourceCode.LocationWithId?,
severity: ScriptDiagnostic.Severity = ScriptDiagnostic.Severity.ERROR
): ScriptDiagnostic =
ScriptDiagnostic(code, customMessage ?: message ?: "$this", severity, locationWithId, this)
/**
* Converts the receiver String to error diagnostic report with optional [path] and [location]
*/
@@ -233,6 +258,15 @@ fun String.asErrorDiagnostics(
): ScriptDiagnostic =
ScriptDiagnostic(code, this, ScriptDiagnostic.Severity.ERROR, path, location)
/**
* Converts the receiver String to error diagnostic report with optional [locationWithId]
*/
fun String.asErrorDiagnostics(
code: Int = ScriptDiagnostic.unspecifiedError,
locationWithId: SourceCode.LocationWithId?
): ScriptDiagnostic =
ScriptDiagnostic(code, this, ScriptDiagnostic.Severity.ERROR, locationWithId)
/**
* Extracts the result value from the receiver wrapper or null if receiver represents a Failure
*/
@@ -51,8 +51,30 @@ interface SourceCode {
* @param end optional range location end position (after the last char)
*/
data class Location(val start: Position, val end: Position? = null) : Serializable
/**
* The source code location including the path to the file
* @param codeLocationId the file path or other script location identifier (see [SourceCode.locationId])
* @param locationInText concrete location of the source code in file
*/
data class LocationWithId(val codeLocationId: String, val locationInText: Location) : Serializable
}
/**
* Annotation found during script source parsing along with its location
*/
data class ScriptSourceAnnotation<out A : Annotation>(
/**
* Annotation found during script source parsing
*/
val annotation: A,
/**
* Location of annotation is script
*/
val location: SourceCode.LocationWithId?
)
/**
* The interface for the source code located externally
*/
@@ -91,6 +113,13 @@ class ScriptCollectedData(properties: Map<PropertiesCollection.Key<*>, Any>) : P
*/
val ScriptCollectedDataKeys.foundAnnotations by PropertiesCollection.key<List<Annotation>>()
/**
* The script file-level annotations and their locations found during script source parsing
*/
val ScriptCollectedDataKeys.collectedAnnotations by PropertiesCollection.key<List<ScriptSourceAnnotation<*>>>(getDefaultValue = {
get(ScriptCollectedData.foundAnnotations)?.map { ScriptSourceAnnotation(it, null) }
})
/**
* The facade to the script data for compilation configuration refinement callbacks
*/
@@ -150,4 +179,4 @@ data class ScriptEvaluationConfigurationRefinementContext(
val compiledScript: CompiledScript,
val evaluationConfiguration: ScriptEvaluationConfiguration,
val contextData: ScriptEvaluationContextData? = null
)
)