From b486129896252e8b593b39dacdd089ee1e486dd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Smolarek?= <34063647+Razz4780@users.noreply.github.com> Date: Tue, 12 Dec 2023 17:35:07 +0100 Subject: [PATCH] Fix warning message (#213) Co-authored-by: Aviram Hassan --- changelog.d/211.fixed.md | 1 + .../metalbear/mirrord/MirrordExecManager.kt | 27 +++++---- .../com/metalbear/mirrord/MirrordNotifier.kt | 57 +++++++++++-------- 3 files changed, 48 insertions(+), 37 deletions(-) create mode 100644 changelog.d/211.fixed.md diff --git a/changelog.d/211.fixed.md b/changelog.d/211.fixed.md new file mode 100644 index 00000000..9538435d --- /dev/null +++ b/changelog.d/211.fixed.md @@ -0,0 +1 @@ +Fixed warning notification when the plugin cannot display the target selection popup. \ No newline at end of file diff --git a/modules/core/src/main/kotlin/com/metalbear/mirrord/MirrordExecManager.kt b/modules/core/src/main/kotlin/com/metalbear/mirrord/MirrordExecManager.kt index b9731d2b..debd73d4 100644 --- a/modules/core/src/main/kotlin/com/metalbear/mirrord/MirrordExecManager.kt +++ b/modules/core/src/main/kotlin/com/metalbear/mirrord/MirrordExecManager.kt @@ -3,12 +3,12 @@ package com.metalbear.mirrord import com.intellij.execution.wsl.WSLDistribution import com.intellij.notification.NotificationType import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.WriteAction import com.intellij.openapi.components.service +import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.progress.ProcessCanceledException import com.intellij.openapi.util.SystemInfo -import com.intellij.openapi.vfs.VirtualFileManager import com.intellij.util.alsoIfNull -import java.nio.file.Path /** * Functions to be called when one of our entry points to the program is called - when process is @@ -55,21 +55,20 @@ class MirrordExecManager(private val service: MirrordProjectService) { service .notifier .notification( - "mirrord plugin was unable to display the target selection dialog. " + - "You can set it manually in the configuration file $config.", + "mirrord plugin was unable to display the target selection dialog. You can set it manually in the configuration file.", NotificationType.WARNING ) .apply { - val configFile = try { - val path = Path.of(config) - VirtualFileManager.getInstance().findFileByNioPath(path) - } catch (e: Exception) { - MirrordLogger.logger.debug("failed to find config under path $config", e) - null - } - - configFile?.let { - withOpenFile(it) + config.let { + when { + it != null -> withOpenPath(it) + else -> withAction("Create") { _, _ -> + WriteAction.run { + val newConfig = service.configApi.createDefaultConfig() + FileEditorManager.getInstance(service.project).openFile(newConfig, true) + } + } + } } } .withLink("Config doc", "https://mirrord.dev/docs/overview/configuration/#root-target") diff --git a/modules/core/src/main/kotlin/com/metalbear/mirrord/MirrordNotifier.kt b/modules/core/src/main/kotlin/com/metalbear/mirrord/MirrordNotifier.kt index 27a3c41b..fb32a4f9 100644 --- a/modules/core/src/main/kotlin/com/metalbear/mirrord/MirrordNotifier.kt +++ b/modules/core/src/main/kotlin/com/metalbear/mirrord/MirrordNotifier.kt @@ -9,6 +9,8 @@ import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.VirtualFileManager +import java.nio.file.Path /** * Mirrord notification handler. @@ -25,10 +27,25 @@ class MirrordNotifier(private val service: MirrordProjectService) { class MirrordNotification(private val inner: Notification, private val project: Project) { private var id: MirrordSettingsState.NotificationId? = null + /** + * Adds a new action to this notification. + * Exceptions thrown in the action are caught, converted to `MirrordError` and displayed to the user. + * + * @see MirrordError + * + * @param name name of the action, visible in the notification + * @param handler function to call when this action is dispatched + */ fun withAction(name: String, handler: (e: AnActionEvent, notification: Notification) -> Unit): MirrordNotification { inner.addAction(object : NotificationAction(name) { override fun actionPerformed(e: AnActionEvent, notification: Notification) { - handler(e, notification) + try { + handler(e, notification) + } catch (error: MirrordError) { + error.showHelp(project) + } catch (error: Throwable) { + MirrordError(error.message ?: "An error occurred", error).showHelp(project) + } } }) @@ -36,35 +53,29 @@ class MirrordNotifier(private val service: MirrordProjectService) { } fun withOpenFile(file: VirtualFile): MirrordNotification { - inner.addAction(object : NotificationAction("Open") { - override fun actionPerformed(e: AnActionEvent, notification: Notification) { - FileEditorManager.getInstance(project).openFile(file, true) - } - }) + return withAction("Open") { _, _ -> + FileEditorManager.getInstance(project).openFile(file, true) + } + } - return this + fun withOpenPath(rawPath: String): MirrordNotification { + return withAction("Open") { _, _ -> + val path = Path.of(rawPath) + val file = VirtualFileManager.getInstance().findFileByNioPath(path) ?: throw Exception("file $rawPath not found") + FileEditorManager.getInstance(project).openFile(file, true) + } } fun withDontShowAgain(id: MirrordSettingsState.NotificationId): MirrordNotification { this.id = id - inner.addAction(object : NotificationAction("Don't show again") { - override fun actionPerformed(e: AnActionEvent, notification: Notification) { - MirrordSettingsState.instance.mirrordState.disableNotification(id) - notification.expire() - } - }) - - return this + return withAction("Don't show again") { _, n -> + MirrordSettingsState.instance.mirrordState.disableNotification(id) + n.expire() + } } - fun withLink(action: String, url: String): MirrordNotification { - inner.addAction(object : NotificationAction(action) { - override fun actionPerformed(e: AnActionEvent, notification: Notification) { - BrowserUtil.browse(url) - } - }) - - return this + fun withLink(name: String, url: String): MirrordNotification { + return withAction(name) { _, _ -> BrowserUtil.browse(url) } } fun withCollapseDirection(direction: Notification.CollapseActionsDirection): MirrordNotification {