From 52973a310d061626cd447660ee1980cfed71e183 Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 13 Sep 2024 02:46:08 +0800 Subject: [PATCH 1/2] Manage the plugin id by bundle message --- build.gradle.kts | 1 + src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt | 7 ++----- src/main/resources/messages/DevChatBundle.properties | 1 + 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 738b03f..39fdcfa 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -148,6 +148,7 @@ tasks { } filesMatching("messages/DevChatBundle.properties") { expand( + "PLUGIN_ID" to (pluginID?.takeIf { it.isNotBlank() } ?: "ai.devchat.plugin"), "ASSISTANT_NAME_ZH" to (assistantNameZH?.takeIf { it.isNotBlank() } ?: "DevChat"), "ASSISTANT_NAME_EN" to (assistantNameEN?.takeIf { it.isNotBlank() } ?: "DevChat"), "default" to "DevChat" diff --git a/src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt b/src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt index eb1dafb..048be1b 100644 --- a/src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt +++ b/src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt @@ -1,10 +1,7 @@ package ai.devchat.installer +import ai.devchat.common.* import ai.devchat.common.Constants.ASSISTANT_NAME_EN -import ai.devchat.common.Log -import ai.devchat.common.Notifier -import ai.devchat.common.OSInfo -import ai.devchat.common.PathUtils import ai.devchat.core.DC_CLIENT import ai.devchat.plugin.DevChatToolWindow import ai.devchat.plugin.browser @@ -20,7 +17,7 @@ class DevChatSetupThread : Thread() { private val minimalPythonVersion: String = "3.8" private val defaultPythonVersion: String = "3.11.4" private val devChatVersion = PluginManagerCore.getPlugin( - PluginId.getId("ai.devchat.plugin") + PluginId.getId(DevChatBundle.message("plugin.id")) )?.version override fun run() { diff --git a/src/main/resources/messages/DevChatBundle.properties b/src/main/resources/messages/DevChatBundle.properties index 77aa50d..e4681bc 100644 --- a/src/main/resources/messages/DevChatBundle.properties +++ b/src/main/resources/messages/DevChatBundle.properties @@ -16,3 +16,4 @@ action.explainCode.text.zh=\u4EE3\u7801\u89E3\u91CA action.fix.text.zh=\u4EE3\u7801\u7EA0\u9519 assistant.name.zh=${ASSISTANT_NAME_ZH} assistant.name.en=${ASSISTANT_NAME_EN} +plugin.id=${PLUGIN_ID} From a557480776c6df822eb08d56922545a9be33755c Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Fri, 13 Sep 2024 02:48:12 +0800 Subject: [PATCH 2/2] Fix local service lifecycle management --- .../ai/devchat/plugin/DevChatToolWindow.kt | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/ai/devchat/plugin/DevChatToolWindow.kt b/src/main/kotlin/ai/devchat/plugin/DevChatToolWindow.kt index 2bd0a7e..f0bee32 100644 --- a/src/main/kotlin/ai/devchat/plugin/DevChatToolWindow.kt +++ b/src/main/kotlin/ai/devchat/plugin/DevChatToolWindow.kt @@ -21,7 +21,7 @@ import javax.swing.SwingConstants class DevChatToolWindow : ToolWindowFactory, DumbAware, Disposable { private var ideService: IDEServer? = null private var localService: LocalService? = null - private val coroutineScope = CoroutineScope(Dispatchers.Default) + private var coroutineScope: CoroutineScope? = null override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) { @@ -40,17 +40,28 @@ class DevChatToolWindow : ToolWindowFactory, DumbAware, Disposable { toolWindow.contentManager.addContent(content) DevChatSetupThread().start() ideService = IDEServer(project).start() - coroutineScope.launch { - while (!pythonReady) { delay(100) } - localService = LocalService().start() + val coroutineExceptionHandler = CoroutineExceptionHandler { _, exception -> + Log.error("Failed to start local service: ${exception.message}") + } + coroutineScope = CoroutineScope(Dispatchers.Default) + coroutineScope!!.launch(coroutineExceptionHandler) { + try { + while (!pythonReady) { + delay(100) + ensureActive() + } + localService = LocalService().start() + awaitCancellation() + } finally { + localService?.stop() + } } } override fun dispose() { DevChatWrapper.activeChannel?.close() - coroutineScope.cancel() + coroutineScope?.cancel() ideService?.stop() - localService?.stop() } companion object {