Skip to content

Commit

Permalink
Fix warning message (#213)
Browse files Browse the repository at this point in the history
Co-authored-by: Aviram Hassan <[email protected]>
  • Loading branch information
Razz4780 and aviramha authored Dec 12, 2023
1 parent 733e72a commit b486129
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
1 change: 1 addition & 0 deletions changelog.d/211.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed warning notification when the plugin cannot display the target selection popup.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<InvalidProjectException> {
val newConfig = service.configApi.createDefaultConfig()
FileEditorManager.getInstance(service.project).openFile(newConfig, true)
}
}
}
}
}
.withLink("Config doc", "https://mirrord.dev/docs/overview/configuration/#root-target")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,46 +27,55 @@ 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)
}
}
})

return this
}

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 {
Expand Down

0 comments on commit b486129

Please sign in to comment.