Refactor script reports: introduce getter, check that report are changed inside attach method
This commit is contained in:
+1
-1
@@ -71,7 +71,7 @@ object KotlinHighlightingUtil {
|
||||
private fun shouldHighlightScript(ktFile: KtFile): Boolean {
|
||||
if (isRunningInCidrIde) return false // There is no Java support in CIDR. So do not highlight errors in KTS if running in CIDR.
|
||||
if (!ScriptsCompilationConfigurationUpdater.areDependenciesCached(ktFile)) return false
|
||||
if (ktFile.virtualFile.getUserData(IdeScriptReportSink.Reports)?.any { it.severity == ScriptReport.Severity.FATAL } == true) {
|
||||
if (IdeScriptReportSink.getReports(ktFile.virtualFile).any { it.severity == ScriptReport.Severity.FATAL }) {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ class ScriptExternalHighlightingPass(
|
||||
|
||||
if (!file.isScript()) return
|
||||
|
||||
val reports = file.virtualFile.getUserData(IdeScriptReportSink.Reports) ?: return
|
||||
val reports = IdeScriptReportSink.getReports(file.virtualFile)
|
||||
|
||||
val annotations = reports.mapNotNull { (message, severity, position) ->
|
||||
val (startOffset, endOffset) = position?.let { computeOffsets(document, position) } ?: 0 to 0
|
||||
|
||||
+1
-1
@@ -72,7 +72,7 @@ class ScriptExternalHighlightingPass(
|
||||
}
|
||||
}
|
||||
|
||||
val reports = file.virtualFile.getUserData(IdeScriptReportSink.Reports) ?: return
|
||||
val reports = IdeScriptReportSink.getReports(file.virtualFile)
|
||||
|
||||
val annotations = reports.mapNotNull { (message, severity, position) ->
|
||||
val (startOffset, endOffset) = position?.let { computeOffsets(document, position) } ?: 0 to 0
|
||||
|
||||
@@ -23,13 +23,16 @@ import com.intellij.openapi.util.Key
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiManager
|
||||
import com.intellij.ui.EditorNotifications
|
||||
import org.jetbrains.kotlin.psi.UserDataProperty
|
||||
import org.jetbrains.kotlin.scripting.resolve.ScriptReportSink
|
||||
import kotlin.script.experimental.dependencies.ScriptReport
|
||||
|
||||
class IdeScriptReportSink(val project: Project) : ScriptReportSink {
|
||||
override fun attachReports(scriptFile: VirtualFile, reports: List<ScriptReport>) {
|
||||
if (getReports(scriptFile) == reports) return
|
||||
|
||||
// TODO: persist errors between launches?
|
||||
scriptFile.putUserData(Reports, reports)
|
||||
scriptFile.scriptReports = reports
|
||||
|
||||
ApplicationManager.getApplication().invokeLater {
|
||||
if (scriptFile.isValid && !project.isDisposed) {
|
||||
@@ -41,5 +44,11 @@ class IdeScriptReportSink(val project: Project) : ScriptReportSink {
|
||||
}
|
||||
}
|
||||
|
||||
object Reports : Key<List<ScriptReport>>("KOTLIN_SCRIPT_REPORTS")
|
||||
companion object {
|
||||
fun getReports(file: VirtualFile): List<ScriptReport> {
|
||||
return file.scriptReports ?: emptyList()
|
||||
}
|
||||
|
||||
private var VirtualFile.scriptReports: List<ScriptReport>? by UserDataProperty(Key.create("KOTLIN_SCRIPT_REPORTS"))
|
||||
}
|
||||
}
|
||||
+1
-3
@@ -77,9 +77,7 @@ abstract class ScriptDependenciesLoader(protected val project: Project) {
|
||||
}
|
||||
|
||||
private fun attachReportsIfChanged(result: ResultWithDiagnostics<*>, file: VirtualFile) {
|
||||
if (file.getUserData(IdeScriptReportSink.Reports) != result.reports.takeIf { it.isNotEmpty() }) {
|
||||
reporter.attachReports(file, result.reports.mapToLegacyReports())
|
||||
}
|
||||
reporter.attachReports(file, result.reports.mapToLegacyReports())
|
||||
}
|
||||
|
||||
private fun save(compilationConfigurationResult: ScriptCompilationConfigurationResult?, file: VirtualFile) {
|
||||
|
||||
@@ -351,7 +351,7 @@ abstract class AbstractScriptConfigurationTest : KotlinCompletionTestCase() {
|
||||
// This is needed because updateScriptDependencies invalidates psiFile that was stored in myFile field
|
||||
myFile = psiManager.findFile(script)
|
||||
|
||||
val isFatalErrorPresent = myFile.virtualFile.getUserData(IdeScriptReportSink.Reports)?.any { it.severity == ScriptReport.Severity.FATAL } == true
|
||||
val isFatalErrorPresent = IdeScriptReportSink.getReports(script).any { it.severity == ScriptReport.Severity.FATAL }
|
||||
assert(isFatalErrorPresent || KotlinHighlightingUtil.shouldHighlight(myFile)) {
|
||||
"Highlighting is switched off for ${myFile.virtualFile.path}"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user