Skip to content

Commit

Permalink
fix(gui): improve code block rendering and parsing #51
Browse files Browse the repository at this point in the history
The commit addresses several issues related to code block rendering and parsing in the GUI. It fixes a bug where the foreground color of code blocks was not being set correctly and ensures that the code block component is added to the center panel. Additionally, the commit enhances the code parsing utility to correctly handle empty code blocks within markdown content, treating them as markdown instead of code.
  • Loading branch information
phodal committed Mar 6, 2024
1 parent 768e4c0 commit 19a0e97
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
Expand Up @@ -4,6 +4,7 @@ import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.AutoDevIcons
import cc.unitmesh.devti.counit.configurable.customAgentSetting
import cc.unitmesh.devti.counit.model.CustomAgentConfig
import cc.unitmesh.devti.counit.model.CustomAgentState
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
import cc.unitmesh.devti.llms.tokenizer.TokenizerImpl
import cc.unitmesh.devti.settings.AutoDevSettingsState
Expand Down Expand Up @@ -222,6 +223,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
}

fun resetAgent() {
(customRag.selectedItem as CustomAgentConfig).state = CustomAgentState.START
customRag.selectedItem = defaultRag
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/cc/unitmesh/devti/gui/chat/MessageView.kt
Expand Up @@ -98,8 +98,10 @@ class MessageView(private val message: String, val role: ChatRole, private val d
}

blockView.initialize()
blockView.getComponent()?.setForeground(JBUI.CurrentTheme.Label.foreground())
blockView.getComponent()?.let { component -> centerPanel.add(component) }
val component = blockView.getComponent() ?: return@forEach

component.setForeground(JBUI.CurrentTheme.Label.foreground())
centerPanel.add(component)
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/kotlin/cc/unitmesh/devti/util/parser/CodeUtil.kt
Expand Up @@ -6,7 +6,8 @@ class Code(val language: Language, val text: String, val isComplete: Boolean) {
companion object {
fun parse(content: String): Code {
val regex = Regex("```([\\w#+]*)")
val lines = content.lines()
// convert content \\n to \n
val lines = content.replace("\\n", "\n").lines()

var codeStarted = false
var codeClosed = false
Expand Down Expand Up @@ -48,6 +49,12 @@ class Code(val language: Language, val text: String, val isComplete: Boolean) {

val trimmedCode = codeBuilder.substring(startIndex, endIndex + 1).toString()
val language = findLanguage(languageId ?: "")

// if content is not empty, but code is empty, then it's a markdown
if (trimmedCode.isEmpty()) {
return Code(findLanguage("markdown"), content.replace("\\n", "\n"), codeClosed)
}

return Code(language, trimmedCode, codeClosed)
}

Expand Down
Expand Up @@ -38,7 +38,7 @@ import javax.swing.JComponent
class CodeBlockView(
private val block: CodeBlock,
private val project: Project,
private val disposable: Disposable
private val disposable: Disposable,
) :
MessageBlockView {
private var editorInfo: CodePartEditorInfo? = null
Expand Down Expand Up @@ -103,7 +103,7 @@ class CodeBlockView(
project: Project,
file: LightVirtualFile,
document: Document,
disposable: Disposable
disposable: Disposable,
): EditorEx {
val editor: EditorEx = ReadAction.compute<EditorEx, Throwable> {
EditorFactory.getInstance()
Expand Down Expand Up @@ -156,7 +156,7 @@ class CodeBlockView(
graphProperty: GraphProperty<String>,
disposable: Disposable,
language: Language,
message: CompletableMessage
message: CompletableMessage,
): CodePartEditorInfo {
val forceFoldEditorByDefault = message.getRole() === ChatRole.User
val createCodeViewerFile = createCodeViewerFile(language, graphProperty.get())
Expand Down
Expand Up @@ -69,7 +69,7 @@ class CodeBlock(private val msg: CompletableMessage) : AbstractMessageBlock(msg)

init {
val language = Language.ANY
this.code = Code(language, "", false)
this.code = Code(language, msg.text, false)
}

override fun onContentChanged(content: String) {
Expand Down
19 changes: 15 additions & 4 deletions src/test/kotlin/cc/unitmesh/devti/parser/CodeUtilTest.kt
Expand Up @@ -21,13 +21,15 @@ class CodeUtilTest {
val code = Code.parse(markdown)

// assertEquals(code.language.id, "java")
assertEquals(code.text, """
assertEquals(
code.text, """
|public class HelloWorld {
| public static void main(String[] args) {
| System.out.println("Hello, World");
| }
|}
""".trimMargin())
""".trimMargin()
)
assertTrue(code.isComplete)
}

Expand All @@ -41,11 +43,20 @@ class CodeUtilTest {
""".trimMargin()

val code = Code.parse(markdown)
assertEquals(code.text, """
assertEquals(
code.text, """
|public class HelloWorld {
| public static void main(String[] args) {
| System.out.println("Hello, World");
""".trimMargin())
""".trimMargin()
)
assertTrue(!code.isComplete)
}

@Test
fun should_handle_pure_markdown_content() {
val content = "```markdown\\nGET /wp/v2/posts\\n```"
val code = Code.parse(content)
assertEquals(code.text, "GET /wp/v2/posts")
}
}

0 comments on commit 19a0e97

Please sign in to comment.