Skip to content

Commit

Permalink
fix: try to resolve editor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 31, 2023
1 parent ff97dd8 commit 76b698c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
Expand Up @@ -31,7 +31,7 @@ class ChatWithThisAction: ChatBaseAction() {
contentManager?.addContent(content!!)

toolWindowManager?.activate {
contentPanel.setContent("""
contentPanel.setContent("\n" + """
|```$language
|$prefixText
|```""".trimMargin())
Expand Down
Expand Up @@ -2,6 +2,7 @@ package cc.unitmesh.devti.gui.chat

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.provider.ContextPrompter
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.ui.NullableComponent
Expand Down Expand Up @@ -72,6 +73,7 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB
fun addMessage(message: String, isMe: Boolean = false, displayPrompt: String = "") {
val role = if (isMe) ChatRole.User else ChatRole.Assistant
val displayText = displayPrompt.ifEmpty { message }

val messageView = MessageView(message, role, displayText)

myList.add(messageView)
Expand All @@ -91,7 +93,10 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB
}

suspend fun updateMessage(content: Flow<String>): String {
myList.remove(myList.componentCount - 1)
if (myList.componentCount > 0) {
myList.remove(myList.componentCount - 1)
}

val result = updateMessageInUi(content)

progressBar.isIndeterminate = false
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/cc/unitmesh/devti/gui/chat/MessageView.kt
Expand Up @@ -46,7 +46,7 @@ class MessageView(private val message: String, role: ChatRole, displayText: Stri

else -> TextBlockView(it)
}
blockView.initialize();
blockView.initialize()
blockView.getComponent()?.setForeground(JBUI.CurrentTheme.Label.foreground())
centerPanel.add(blockView.getComponent())
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/block/CodeBlockView.kt
Expand Up @@ -4,6 +4,8 @@ import cc.unitmesh.devti.gui.chat.ChatRole
import cc.unitmesh.devti.parser.Code
import com.intellij.lang.Language
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorFactory
Expand All @@ -22,7 +24,7 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.LightVirtualFile
import com.intellij.util.concurrency.annotations.RequiresReadLock
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.components.BorderLayoutPanel
import kotlin.jvm.internal.Ref
import javax.swing.JComponent

class CodeBlockView(private val block: CodeBlock, private val project: Project, private val disposable: Disposable) :
Expand Down Expand Up @@ -51,7 +53,9 @@ class CodeBlockView(private val block: CodeBlock, private val project: Project,

override fun initialize() {
if (editorInfo == null) {
updateOrCreateCodeView()
ApplicationManager.getApplication().invokeLater() {
updateOrCreateCodeView()
}
}
}

Expand Down Expand Up @@ -94,7 +98,6 @@ class CodeBlockView(private val block: CodeBlock, private val project: Project,
document: Document,
disposable: Disposable
): EditorEx {
val language = file.language
val editor: Editor = EditorFactory.getInstance().createViewer(document, project);
(editor as EditorEx).setFile(file)
editor.setCaretEnabled(true)
Expand Down Expand Up @@ -164,5 +167,11 @@ class CodeBlockView(private val block: CodeBlock, private val project: Project,

@RequiresReadLock
fun VirtualFile.findDocument(): Document? {
return FileDocumentManager.getInstance().getDocument(this)
val ref: Ref.ObjectRef<Document?> = Ref.ObjectRef()
runReadAction {
val instance = FileDocumentManager.getInstance()
ref.element = instance.getDocument(this)
}

return ref.element
}

0 comments on commit 76b698c

Please sign in to comment.