Refactoring: move logic that script shouldn't be highlighted if there are any FATAL error to KotlinHighlightingUtil

This commit is contained in:
Natalia Selezneva
2018-05-03 13:08:37 +03:00
parent 3cc3ca97ac
commit 61c31ce030
3 changed files with 19 additions and 23 deletions
@@ -22,12 +22,14 @@ import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.caches.project.NotUnderContentRootModuleInfo
import org.jetbrains.kotlin.idea.caches.project.getModuleInfo
import org.jetbrains.kotlin.idea.core.script.IdeScriptReportSink
import org.jetbrains.kotlin.idea.core.script.scriptDependencies
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil.isInContent
import org.jetbrains.kotlin.psi.KtCodeFragment
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.script.getScriptDefinition
import kotlin.script.experimental.dependencies.ScriptReport
import kotlin.script.experimental.location.ScriptExpectedLocation
object KotlinHighlightingUtil {
@@ -66,6 +68,7 @@ object KotlinHighlightingUtil {
private fun shouldHighlightScript(ktFile: KtFile): Boolean {
if (ScratchFileService.isInScratchRoot(ktFile.virtualFile)) return true
if (ktFile.virtualFile.scriptDependencies == null) return false
if (ktFile.getUserData(IdeScriptReportSink.Reports)?.any { it.severity == ScriptReport.Severity.FATAL } == true) return false
val scriptScope = getScriptDefinition(ktFile)?.scriptExpectedLocations ?: return false
return when {
@@ -15,7 +15,7 @@ import com.intellij.ui.EditorNotificationPanel
import com.intellij.ui.HyperlinkLabel
import org.jetbrains.kotlin.idea.core.script.settings.KotlinScriptingSettings
import org.jetbrains.kotlin.psi.UserDataProperty
import kotlin.script.experimental.dependencies.DependenciesResolver
import kotlin.script.experimental.dependencies.ScriptDependencies
fun VirtualFile.removeScriptDependenciesNotificationPanel(project: Project) {
val editor = FileEditorManager.getInstance(project).getSelectedEditor(this) ?: return
@@ -24,20 +24,20 @@ fun VirtualFile.removeScriptDependenciesNotificationPanel(project: Project) {
}
fun VirtualFile.addScriptDependenciesNotificationPanel(
result: DependenciesResolver.ResolveResult,
dependencies: ScriptDependencies,
project: Project,
onClick: (DependenciesResolver.ResolveResult) -> Unit
onClick: (ScriptDependencies) -> Unit
) {
val editor = FileEditorManager.getInstance(project).getSelectedEditor(this) ?: return
val existingPanel = editor.notificationPanel
if (existingPanel != null) {
if (existingPanel.result == result) return
if (existingPanel.dependencies == dependencies) return
editor.notificationPanel?.let {
FileEditorManager.getInstance(project).removeTopComponent(editor, it)
}
}
val panel = NewScriptDependenciesNotificationPanel(onClick, result, project)
val panel = NewScriptDependenciesNotificationPanel(onClick, dependencies, project)
editor.notificationPanel = panel
FileEditorManager.getInstance(project).addTopComponent(editor, panel)
}
@@ -45,19 +45,19 @@ fun VirtualFile.addScriptDependenciesNotificationPanel(
private var FileEditor.notificationPanel: NewScriptDependenciesNotificationPanel? by UserDataProperty<FileEditor, NewScriptDependenciesNotificationPanel>(Key.create("script.dependencies.panel"))
private class NewScriptDependenciesNotificationPanel(
onClick: (DependenciesResolver.ResolveResult) -> Unit,
val result: DependenciesResolver.ResolveResult,
onClick: (ScriptDependencies) -> Unit,
val dependencies: ScriptDependencies,
project: Project
) : EditorNotificationPanel() {
init {
setText("There are new script dependencies available.")
createComponentActionLabel("Reload dependencies") {
onClick(result)
onClick(dependencies)
}
createComponentActionLabel("Enable auto-reload") {
onClick(result)
onClick(dependencies)
KotlinScriptingSettings.getInstance(project).isAutoReloadEnabled = true
}
}
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.script.*
import kotlin.script.experimental.dependencies.AsyncDependenciesResolver
import kotlin.script.experimental.dependencies.DependenciesResolver
import kotlin.script.experimental.dependencies.ScriptDependencies
import kotlin.script.experimental.dependencies.ScriptReport
abstract class ScriptDependenciesLoader(
protected val file: VirtualFile,
@@ -77,11 +76,11 @@ abstract class ScriptDependenciesLoader(
val newDependencies = result.dependencies?.adjustByDefinition(scriptDef) ?: return
if (cache[file] != newDependencies) {
if (shouldShowNotification() && cache[file] != null && !ApplicationManager.getApplication().isUnitTestMode) {
file.addScriptDependenciesNotificationPanel(result, project) {
saveDependencies(result)
file.addScriptDependenciesNotificationPanel(newDependencies, project) {
saveDependencies(newDependencies)
}
} else {
saveDependencies(result)
saveDependencies(newDependencies)
}
} else {
if (shouldShowNotification()) {
@@ -92,20 +91,14 @@ abstract class ScriptDependenciesLoader(
loaders.remove(file)
}
private fun saveDependencies(result: DependenciesResolver.ResolveResult) {
private fun saveDependencies(dependencies: ScriptDependencies) {
if (shouldShowNotification()) {
file.removeScriptDependenciesNotificationPanel(project)
}
val newDependencies = result.dependencies?.adjustByDefinition(scriptDef) ?: ScriptDependencies.Empty
val rootsChanged = cache.hasNotCachedRoots(newDependencies)
if (cache.save(file, newDependencies)) {
if (result.reports.any { it.severity == ScriptReport.Severity.FATAL }) {
file.scriptDependencies = null
} else {
file.scriptDependencies = newDependencies
}
val rootsChanged = cache.hasNotCachedRoots(dependencies)
if (cache.save(file, dependencies)) {
file.scriptDependencies = dependencies
}
if (rootsChanged) {