Pass ResolveResult to ScriptNotificationPanel

This commit is contained in:
Natalia Selezneva
2018-11-02 12:58:41 +03:00
parent ed2b9172da
commit 18c9d968f9
2 changed files with 19 additions and 20 deletions
@@ -15,7 +15,7 @@ import com.intellij.ui.EditorNotificationPanel
import com.intellij.ui.HyperlinkLabel import com.intellij.ui.HyperlinkLabel
import org.jetbrains.kotlin.idea.core.script.settings.KotlinScriptingSettings import org.jetbrains.kotlin.idea.core.script.settings.KotlinScriptingSettings
import org.jetbrains.kotlin.psi.UserDataProperty import org.jetbrains.kotlin.psi.UserDataProperty
import kotlin.script.experimental.dependencies.ScriptDependencies import kotlin.script.experimental.dependencies.DependenciesResolver.ResolveResult
fun VirtualFile.removeScriptDependenciesNotificationPanel(project: Project) { fun VirtualFile.removeScriptDependenciesNotificationPanel(project: Project) {
val editor = FileEditorManager.getInstance(project).getSelectedEditor(this) ?: return val editor = FileEditorManager.getInstance(project).getSelectedEditor(this) ?: return
@@ -24,20 +24,20 @@ fun VirtualFile.removeScriptDependenciesNotificationPanel(project: Project) {
} }
fun VirtualFile.addScriptDependenciesNotificationPanel( fun VirtualFile.addScriptDependenciesNotificationPanel(
dependencies: ScriptDependencies, resolveResult: ResolveResult,
project: Project, project: Project,
onClick: (ScriptDependencies) -> Unit onClick: (ResolveResult) -> Unit
) { ) {
val editor = FileEditorManager.getInstance(project).getSelectedEditor(this) ?: return val editor = FileEditorManager.getInstance(project).getSelectedEditor(this) ?: return
val existingPanel = editor.notificationPanel val existingPanel = editor.notificationPanel
if (existingPanel != null) { if (existingPanel != null) {
if (existingPanel.dependencies == dependencies) return if (existingPanel.resolveResult.dependencies == resolveResult.dependencies) return
editor.notificationPanel?.let { editor.notificationPanel?.let {
FileEditorManager.getInstance(project).removeTopComponent(editor, it) FileEditorManager.getInstance(project).removeTopComponent(editor, it)
} }
} }
val panel = NewScriptDependenciesNotificationPanel(onClick, dependencies, project) val panel = NewScriptDependenciesNotificationPanel(onClick, resolveResult, project)
editor.notificationPanel = panel editor.notificationPanel = panel
FileEditorManager.getInstance(project).addTopComponent(editor, 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 var FileEditor.notificationPanel: NewScriptDependenciesNotificationPanel? by UserDataProperty<FileEditor, NewScriptDependenciesNotificationPanel>(Key.create("script.dependencies.panel"))
private class NewScriptDependenciesNotificationPanel( private class NewScriptDependenciesNotificationPanel(
onClick: (ScriptDependencies) -> Unit, onClick: (ResolveResult) -> Unit,
val dependencies: ScriptDependencies, val resolveResult: ResolveResult,
project: Project project: Project
) : EditorNotificationPanel() { ) : EditorNotificationPanel() {
init { init {
setText("There are new script dependencies available.") setText("There are new script dependencies available.")
createComponentActionLabel("Apply dependencies") { createComponentActionLabel("Apply dependencies") {
onClick(dependencies) onClick(resolveResult)
} }
createComponentActionLabel("Enable auto-reload") { createComponentActionLabel("Enable auto-reload") {
onClick(dependencies) onClick(resolveResult)
KotlinScriptingSettings.getInstance(project).isAutoReloadEnabled = true KotlinScriptingSettings.getInstance(project).isAutoReloadEnabled = true
} }
} }
@@ -21,8 +21,6 @@ import org.jetbrains.kotlin.script.*
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import kotlin.script.experimental.dependencies.AsyncDependenciesResolver import kotlin.script.experimental.dependencies.AsyncDependenciesResolver
import kotlin.script.experimental.dependencies.DependenciesResolver import kotlin.script.experimental.dependencies.DependenciesResolver
import kotlin.script.experimental.dependencies.ScriptDependencies
import kotlin.script.experimental.dependencies.ScriptReport
abstract class ScriptDependenciesLoader( abstract class ScriptDependenciesLoader(
protected val file: VirtualFile, protected val file: VirtualFile,
@@ -75,16 +73,16 @@ abstract class ScriptDependenciesLoader(
val newDependencies = result.dependencies?.adjustByDefinition(scriptDef) ?: return val newDependencies = result.dependencies?.adjustByDefinition(scriptDef) ?: return
if (cache[file] != newDependencies) { if (cache[file] != newDependencies) {
if (shouldShowNotification() && cache[file] != null && !ApplicationManager.getApplication().isUnitTestMode) { if (shouldShowNotification() && cache[file] != null && !ApplicationManager.getApplication().isUnitTestMode) {
file.addScriptDependenciesNotificationPanel(newDependencies, project) { file.addScriptDependenciesNotificationPanel(result, project) {
saveDependencies(newDependencies) saveDependencies(it)
attachReports(result.reports) attachReports(it)
} }
} else { } else {
saveDependencies(newDependencies) saveDependencies(result)
attachReports(result.reports) attachReports(result)
} }
} else { } else {
attachReports(result.reports) attachReports(result)
if (shouldShowNotification()) { if (shouldShowNotification()) {
file.removeScriptDependenciesNotificationPanel(project) file.removeScriptDependenciesNotificationPanel(project)
@@ -92,15 +90,16 @@ abstract class ScriptDependenciesLoader(
} }
} }
private fun attachReports(reports: List<ScriptReport>) { private fun attachReports(result: DependenciesResolver.ResolveResult) {
ServiceManager.getService(project, ScriptReportSink::class.java)?.attachReports(file, reports) ServiceManager.getService(project, ScriptReportSink::class.java)?.attachReports(file, result.reports)
} }
private fun saveDependencies(dependencies: ScriptDependencies) { private fun saveDependencies(result: DependenciesResolver.ResolveResult) {
if (shouldShowNotification()) { if (shouldShowNotification()) {
file.removeScriptDependenciesNotificationPanel(project) file.removeScriptDependenciesNotificationPanel(project)
} }
val dependencies = result.dependencies?.adjustByDefinition(scriptDef) ?: return
val rootsChanged = cache.hasNotCachedRoots(dependencies) val rootsChanged = cache.hasNotCachedRoots(dependencies)
if (cache.save(file, dependencies)) { if (cache.save(file, dependencies)) {
file.scriptDependencies = dependencies file.scriptDependencies = dependencies