Skip to content

Commit

Permalink
feat: add error notify
Browse files Browse the repository at this point in the history
  • Loading branch information
reko committed Oct 29, 2022
1 parent ccdc5aa commit d8b8ed2
Showing 1 changed file with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,54 @@ import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.progress.runBackgroundableTask
import com.intellij.openapi.project.Project

private fun errorAlert(project: Project, task: String, message: String?) {
Notifications.Bus.notify(
Notification(
"com.github.suhli.ideagokratosplugin",
"$task failed:\n${message ?: ""}",
NotificationType.ERROR
), project
)
}

private fun runTask(project: Project, taskName: String, t: KratosTask) {
val result = t.runnable()
if (result?.exception != null) {
errorAlert(project, taskName, result.message)
}
}

fun runKratosTaskInBackground(taskName: String, project: Project, tasks: List<KratosTask>) {
if (tasks.isEmpty()) {
return
}
val editor = FileEditorManager.getInstance(project).selectedTextEditor


runBackgroundableTask(taskName, project, true) {
it.isIndeterminate = false
val size = tasks.size
it.fraction = 0.0
for ((i, t) in tasks.withIndex()) {
if (t.needWrite) {
WriteCommandAction.runWriteCommandAction(project, t.runnable)
WriteCommandAction.runWriteCommandAction(project) {
runTask(project, taskName, t)
}
} else {
t.runnable.run()
runTask(project, taskName, t)
}
it.fraction = ((i + 1) / size).toDouble()
}
it.fraction = 1.0
if (editor != null) {
Notifications.Bus.notify(Notification("com.github.suhli.ideagokratosplugin", "$taskName done!", NotificationType.INFORMATION), project)
Notifications.Bus.notify(
Notification(
"com.github.suhli.ideagokratosplugin",
"$taskName done!",
NotificationType.INFORMATION
), project
)
}
project.projectFile?.refresh(false,true)
project.projectFile?.refresh(false, true)
DaemonCodeAnalyzer.getInstance(project).restart();
}
}

0 comments on commit d8b8ed2

Please sign in to comment.