Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initialization thread #205

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
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
Expand All @@ -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() {
Expand Down
23 changes: 17 additions & 6 deletions src/main/kotlin/ai/devchat/plugin/DevChatToolWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages/DevChatBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Loading