Skip to content

Commit 000437a

Browse files
committed
refactor(GUI & parser): improve code structure and error handling
- Refactor `DisplayComponent` to use direct assignment for selection. - Add `SwingUtilities` import for clarity. - Enhance `CodeUtil` with try-catch for extension lookup to handle exceptions.
1 parent 7fc38c6 commit 000437a

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/component/DisplayComponent.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package cc.unitmesh.devti.gui.component
22

33
import com.intellij.util.ui.JBEmptyBorder
44
import com.intellij.util.ui.UIUtil
5-
import javax.accessibility.AccessibleContext
6-
import javax.swing.JEditorPane
75
import org.apache.commons.text.StringEscapeUtils
86
import org.jsoup.Jsoup
7+
import javax.accessibility.AccessibleContext
8+
import javax.swing.JEditorPane
9+
import javax.swing.SwingUtilities
910

1011
class DisplayComponent(question: String) : JEditorPane() {
1112
init {
@@ -32,8 +33,8 @@ class DisplayComponent(question: String) : JEditorPane() {
3233
this.text = content
3334
if (selectionStart != selectionEnd) {
3435
SwingUtilities.invokeLater {
35-
this.setSelectionStart(selectionStart)
36-
this.setSelectionEnd(selectionEnd)
36+
this.selectionStart = selectionStart
37+
this.selectionEnd = selectionEnd
3738
}
3839
}
3940
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ class CodeFence(val language: Language, val text: String, val isComplete: Boolea
3232

3333
val trimmedCode = codeBuilder.trim().toString()
3434
val language = findLanguage(languageId ?: "")
35-
val extension =
35+
val extension = try {
3636
language.associatedFileType?.defaultExtension ?: lookupFileExt(languageId ?: "txt")
37+
} catch (e: Exception) {
38+
lookupFileExt(languageId ?: "txt")
39+
}
3740

3841
return if (trimmedCode.isEmpty()) {
3942
CodeFence(language, "", codeClosed, extension)

core/src/test/kotlin/cc/unitmesh/devti/gui/chat/MessageViewTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ class MessageViewTest {
7777
|}
7878
|```""".trimMargin())
7979
}
80-
}
80+
}

0 commit comments

Comments
 (0)