Changesets · Branches · Tags · Compare
| Changeset | 796feb88992ea6d4353d126c1ba4cc9b9994f7d0b70f7a3d5378d51cea28b6cd |
|---|---|
| Author | Jakub Błach <jakubblach@gmail.com> · 2025-07-21 23:37 +02:00 |
| Committer | GitHub <noreply@github.com> · 2025-07-21 14:37 -07:00 |
| Parents | 019609462ca8 |
1 file changed
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/CoreMessenger.kt +17 −4
| @@ -7,9 +7,11 @@ | ||
| 7 | 7 | import com.github.continuedev.continueintellijextension.services.ContinuePluginService |
| 8 | 8 | import com.github.continuedev.continueintellijextension.utils.uuid |
| 9 | 9 | import com.google.gson.Gson |
| 10 | +import com.google.gson.JsonSyntaxException | |
| 10 | 11 | import com.intellij.openapi.components.service |
| 12 | +import com.intellij.openapi.diagnostic.Logger | |
| 11 | 13 | import com.intellij.openapi.project.Project |
| 12 | -import kotlinx.coroutines.* | |
| 14 | +import kotlinx.coroutines.CoroutineScope | |
| 13 | 15 | |
| 14 | 16 | class CoreMessenger( |
| 15 | 17 | private val project: Project, |
| @@ -20,6 +22,7 @@ | ||
| 20 | 22 | private val gson = Gson() |
| 21 | 23 | private val responseListeners = mutableMapOf<String, (Any?) -> Unit>() |
| 22 | 24 | private val process = startContinueProcess() |
| 25 | + private val log = Logger.getInstance(CoreMessenger::class.java) | |
| 23 | 26 | |
| 24 | 27 | fun request(messageType: String, data: Any?, messageId: String?, onResponse: (Any?) -> Unit) { |
| 25 | 28 | val id = messageId ?: uuid() |
| @@ -38,13 +41,13 @@ | ||
| 38 | 41 | } |
| 39 | 42 | |
| 40 | 43 | private fun handleMessage(json: String) { |
| 41 | - val responseMap = gson.fromJson(json, Map::class.java) | |
| 44 | + val responseMap = tryToParse(json) ?: return | |
| 42 | 45 | val messageId = responseMap["messageId"].toString() |
| 43 | 46 | val messageType = responseMap["messageType"].toString() |
| 44 | 47 | val data = responseMap["data"] |
| 45 | 48 | |
| 46 | 49 | // IDE listeners |
| 47 | - if (MessageTypes.IDE_MESSAGE_TYPES.contains(messageType)) { | |
| 50 | + if (messageType in MessageTypes.IDE_MESSAGE_TYPES) { | |
| 48 | 51 | ideProtocolClient.handleMessage(json) { data -> |
| 49 | 52 | val message = gson.toJson(mapOf("messageId" to messageId, "messageType" to messageType, "data" to data)) |
| 50 | 53 | process.write(message) |
| @@ -52,8 +55,9 @@ | ||
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | // Forward to webview |
| 55 | - if (MessageTypes.PASS_THROUGH_TO_WEBVIEW.contains(messageType)) { | |
| 58 | + if (messageType in MessageTypes.PASS_THROUGH_TO_WEBVIEW) { | |
| 56 | 59 | val continuePluginService = project.service<ContinuePluginService>() |
| 60 | + // todo: is this a bug below (messageType = ID)? verify | |
| 57 | 61 | continuePluginService.sendToWebview(messageType, responseMap["data"], messageType) |
| 58 | 62 | } |
| 59 | 63 | |
| @@ -68,6 +72,15 @@ | ||
| 68 | 72 | } |
| 69 | 73 | } |
| 70 | 74 | |
| 75 | + // todo: map<*, *> = code smell | |
| 76 | + private fun tryToParse(json: String): Map<*, *>? = | |
| 77 | + try { | |
| 78 | + gson.fromJson(json, Map::class.java) | |
| 79 | + } catch (_: JsonSyntaxException) { | |
| 80 | + log.warn("Invalid message JSON: $json") // example: NODE_ENV undefined | |
| 81 | + null | |
| 82 | + } | |
| 83 | + | |
| 71 | 84 | fun killSubProcess() { |
| 72 | 85 | process.close() |
| 73 | 86 | } |