Skip to content

Commit 389f7c6

Browse files
authored
Merge pull request #52 from devchat-ai/handle-add-to-devchat-before-initialization
Handle add to devchat before initialization
2 parents 68dbee8 + a9d20a3 commit 389f7c6

8 files changed

Lines changed: 36 additions & 9 deletions

File tree

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/kotlin/ai/devchat/common/PathUtils.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ object PathUtils {
1313
fun copyResourceDirToPath(resourceDir: String, outputPath: String) {
1414
val uri = javaClass.getResource(resourceDir)!!.toURI()
1515
val path = if (uri.scheme == "jar") {
16-
val fileSystem = FileSystems.newFileSystem(uri, emptyMap<String, Any>())
16+
val fileSystem = try {
17+
FileSystems.newFileSystem(uri, emptyMap<String, Any>())
18+
} catch (e: FileSystemAlreadyExistsException) {
19+
FileSystems.getFileSystem(uri)
20+
}
1721
fileSystem.getPath("/$resourceDir")
1822
} else {
1923
Paths.get(uri)

src/main/kotlin/ai/devchat/idea/DevChatSetupThread.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DevChatSetupThread : Thread() {
2929
ProjectUtils.executeJS("onInitializationFinish")
3030
DevChatNotifier.info("DevChat initialization has completed successfully.")
3131
} catch (e: Exception) {
32-
Log.error("Failed to install DevChat CLI: " + e.message)
32+
Log.error("Failed to install DevChat CLI: $e\n" + e.stackTrace.joinToString("\n"))
3333
DevChatNotifier.error("DevChat initialization has failed. Please check the logs for more details.")
3434
}
3535
}

src/main/kotlin/ai/devchat/idea/DevChatToolWindow.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class DevChatToolWindow : ToolWindowFactory, DumbAware {
3232
DevChatSetupThread().start()
3333
// LanguageServer(project).start()
3434
}
35+
36+
companion object {
37+
var loaded: Boolean = false
38+
}
3539
}
3640

3741
internal class DevChatToolWindowContent(project: Project) {

src/main/kotlin/ai/devchat/idea/JSJavaBridge.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package ai.devchat.idea
22

33
import ai.devchat.common.Log.info
44
import ai.devchat.devchat.ActionHandlerFactory
5+
import ai.devchat.devchat.handler.AddContextNotifyHandler
6+
import ai.devchat.idea.action.AddToDevChatAction
57
import com.alibaba.fastjson.JSON
68
import com.intellij.openapi.application.ApplicationManager
79
import com.intellij.ui.jcef.JBCefBrowser
@@ -55,6 +57,14 @@ class JSJavaBridge(private val jbCefBrowser: JBCefBrowser) {
5557
"", 0
5658
)
5759
}
60+
61+
override fun onLoadEnd(browser: CefBrowser?, frame: CefFrame?, httpStatusCode: Int) {
62+
if (AddToDevChatAction.cache != null) {
63+
AddContextNotifyHandler(null, AddToDevChatAction.cache).executeAction()
64+
AddToDevChatAction.cache = null
65+
}
66+
DevChatToolWindow.loaded = true
67+
}
5868
}, jbCefBrowser.cefBrowser)
5969
}
6070
}

src/main/kotlin/ai/devchat/idea/action/AddToDevChatAction.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ai.devchat.devchat.handler.AddContextNotifyHandler
55
import com.alibaba.fastjson.JSONObject
66

77
class AddToDevChatAction {
8-
fun execute(filePath: String, fileContent: String, language: String, startLine: Int) {
8+
fun execute(filePath: String, fileContent: String, language: String, startLine: Int, invokeLater: Boolean = false) {
99
info(
1010
"Add to DevChat -> path: " + filePath +
1111
" content: " + fileContent +
@@ -17,7 +17,14 @@ class AddToDevChatAction {
1717
payload["content"] = fileContent
1818
payload["languageId"] = language
1919
payload["startLine"] = startLine
20-
val addContextNotifyHandler = AddContextNotifyHandler(null, payload)
21-
addContextNotifyHandler.executeAction()
20+
if (invokeLater) {
21+
cache = payload
22+
} else {
23+
AddContextNotifyHandler(null, payload).executeAction()
24+
}
25+
}
26+
27+
companion object {
28+
var cache: JSONObject? = null
2229
}
2330
}

src/main/kotlin/ai/devchat/idea/action/AddToDevChatEditorAction.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ai.devchat.idea.action
22

3+
import ai.devchat.idea.DevChatToolWindow
34
import com.intellij.openapi.actionSystem.ActionUpdateThread
45
import com.intellij.openapi.actionSystem.AnAction
56
import com.intellij.openapi.actionSystem.AnActionEvent
@@ -26,7 +27,7 @@ class AddToDevChatEditorAction : AnAction() {
2627
val fileType = virtualFile.fileType
2728
val language = fileType.name
2829
if (editor != null) {
29-
ToolWindowManager.getInstance(project).getToolWindow("DevChat")?.show() {
30+
ToolWindowManager.getInstance(project).getToolWindow("DevChat")?.show {
3031
val selectionModel = editor.selectionModel
3132
var selectedText = selectionModel.selectedText
3233
if (selectedText.isNullOrEmpty()) {
@@ -36,7 +37,7 @@ class AddToDevChatEditorAction : AnAction() {
3637
val startOffset = selectionModel.selectionStart
3738
val document = editor.document
3839
val startLine = document.getLineNumber(startOffset) + 1
39-
addToDevChatAction.execute(relativePath, selectedText, language, startLine)
40+
addToDevChatAction.execute(relativePath, selectedText, language, startLine, !DevChatToolWindow.loaded)
4041
}
4142
}
4243
}

src/main/kotlin/ai/devchat/idea/action/AddToDevChatFileAction.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ai.devchat.idea.action
22

3+
import ai.devchat.idea.DevChatToolWindow
34
import com.intellij.openapi.actionSystem.ActionUpdateThread
45
import com.intellij.openapi.actionSystem.AnAction
56
import com.intellij.openapi.actionSystem.AnActionEvent
@@ -35,7 +36,7 @@ class AddToDevChatFileAction : AnAction() {
3536
ToolWindowManager.getInstance(project).getToolWindow("DevChat")?.show {
3637
val bytes = virtualFile.contentsToByteArray()
3738
val content = String(bytes, StandardCharsets.UTF_8)
38-
addToDevChatAction.execute(relativePath, content, language, 0)
39+
addToDevChatAction.execute(relativePath, content, language, 0, !DevChatToolWindow.loaded)
3940
}
4041
} catch (ex: IOException) {
4142
ex.printStackTrace()

0 commit comments

Comments
 (0)