Skip to content

Commit 19a0e97

Browse files
committed
fix(gui): improve code block rendering and parsing #51
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.
1 parent 768e4c0 commit 19a0e97

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInputSection.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.AutoDevIcons
55
import cc.unitmesh.devti.counit.configurable.customAgentSetting
66
import cc.unitmesh.devti.counit.model.CustomAgentConfig
7+
import cc.unitmesh.devti.counit.model.CustomAgentState
78
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
89
import cc.unitmesh.devti.llms.tokenizer.TokenizerImpl
910
import cc.unitmesh.devti.settings.AutoDevSettingsState
@@ -222,6 +223,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
222223
}
223224

224225
fun resetAgent() {
226+
(customRag.selectedItem as CustomAgentConfig).state = CustomAgentState.START
225227
customRag.selectedItem = defaultRag
226228
}
227229

src/main/kotlin/cc/unitmesh/devti/gui/chat/MessageView.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ class MessageView(private val message: String, val role: ChatRole, private val d
9898
}
9999

100100
blockView.initialize()
101-
blockView.getComponent()?.setForeground(JBUI.CurrentTheme.Label.foreground())
102-
blockView.getComponent()?.let { component -> centerPanel.add(component) }
101+
val component = blockView.getComponent() ?: return@forEach
102+
103+
component.setForeground(JBUI.CurrentTheme.Label.foreground())
104+
centerPanel.add(component)
103105
}
104106
}
105107

src/main/kotlin/cc/unitmesh/devti/util/parser/CodeUtil.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class Code(val language: Language, val text: String, val isComplete: Boolean) {
66
companion object {
77
fun parse(content: String): Code {
88
val regex = Regex("```([\\w#+]*)")
9-
val lines = content.lines()
9+
// convert content \\n to \n
10+
val lines = content.replace("\\n", "\n").lines()
1011

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

4950
val trimmedCode = codeBuilder.substring(startIndex, endIndex + 1).toString()
5051
val language = findLanguage(languageId ?: "")
52+
53+
// if content is not empty, but code is empty, then it's a markdown
54+
if (trimmedCode.isEmpty()) {
55+
return Code(findLanguage("markdown"), content.replace("\\n", "\n"), codeClosed)
56+
}
57+
5158
return Code(language, trimmedCode, codeClosed)
5259
}
5360

src/main/kotlin/com/intellij/temporary/gui/block/CodeBlockView.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import javax.swing.JComponent
3838
class CodeBlockView(
3939
private val block: CodeBlock,
4040
private val project: Project,
41-
private val disposable: Disposable
41+
private val disposable: Disposable,
4242
) :
4343
MessageBlockView {
4444
private var editorInfo: CodePartEditorInfo? = null
@@ -103,7 +103,7 @@ class CodeBlockView(
103103
project: Project,
104104
file: LightVirtualFile,
105105
document: Document,
106-
disposable: Disposable
106+
disposable: Disposable,
107107
): EditorEx {
108108
val editor: EditorEx = ReadAction.compute<EditorEx, Throwable> {
109109
EditorFactory.getInstance()
@@ -156,7 +156,7 @@ class CodeBlockView(
156156
graphProperty: GraphProperty<String>,
157157
disposable: Disposable,
158158
language: Language,
159-
message: CompletableMessage
159+
message: CompletableMessage,
160160
): CodePartEditorInfo {
161161
val forceFoldEditorByDefault = message.getRole() === ChatRole.User
162162
val createCodeViewerFile = createCodeViewerFile(language, graphProperty.get())

src/main/kotlin/com/intellij/temporary/gui/block/MessageBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class CodeBlock(private val msg: CompletableMessage) : AbstractMessageBlock(msg)
6969

7070
init {
7171
val language = Language.ANY
72-
this.code = Code(language, "", false)
72+
this.code = Code(language, msg.text, false)
7373
}
7474

7575
override fun onContentChanged(content: String) {

src/test/kotlin/cc/unitmesh/devti/parser/CodeUtilTest.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ class CodeUtilTest {
2121
val code = Code.parse(markdown)
2222

2323
// assertEquals(code.language.id, "java")
24-
assertEquals(code.text, """
24+
assertEquals(
25+
code.text, """
2526
|public class HelloWorld {
2627
| public static void main(String[] args) {
2728
| System.out.println("Hello, World");
2829
| }
2930
|}
30-
""".trimMargin())
31+
""".trimMargin()
32+
)
3133
assertTrue(code.isComplete)
3234
}
3335

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

4345
val code = Code.parse(markdown)
44-
assertEquals(code.text, """
46+
assertEquals(
47+
code.text, """
4548
|public class HelloWorld {
4649
| public static void main(String[] args) {
4750
| System.out.println("Hello, World");
48-
""".trimMargin())
51+
""".trimMargin()
52+
)
4953
assertTrue(!code.isComplete)
5054
}
55+
56+
@Test
57+
fun should_handle_pure_markdown_content() {
58+
val content = "```markdown\\nGET /wp/v2/posts\\n```"
59+
val code = Code.parse(content)
60+
assertEquals(code.text, "GET /wp/v2/posts")
61+
}
5162
}

0 commit comments

Comments
 (0)