Skip to content

Commit

Permalink
Merge pull request #205 from devchat-ai/fix-initialization-thread
Browse files Browse the repository at this point in the history
Fix initialization thread
pplam authored Sep 12, 2024
2 parents 147d1b4 + a557480 commit e27a12e
Showing 4 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 2 additions & 5 deletions src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt
Original file line number Diff line number Diff line change
@@ -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() {
23 changes: 17 additions & 6 deletions src/main/kotlin/ai/devchat/plugin/DevChatToolWindow.kt
Original file line number Diff line number Diff line change
@@ -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 {
1 change: 1 addition & 0 deletions src/main/resources/messages/DevChatBundle.properties
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit e27a12e

Please sign in to comment.