Skip to content

Commit 9384bc8

Browse files
committed
feat(sketch): refactor CodeHighlightSketch editor initialization
- Extract setupSimpleEditor, setupRegularEditor, and setupToolbarAndStyling methods - Add user-specific editor handling with different border styling - Improve conditional logic for toolbar display and border management - Add clarifying comments for DevIns-specific functionality
1 parent 87c1143 commit 9384bc8

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/code/CodeHighlightSketch.kt

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.intellij.openapi.command.WriteCommandAction
2222
import com.intellij.openapi.diagnostic.logger
2323
import com.intellij.openapi.editor.Document
2424
import com.intellij.openapi.editor.EditorFactory
25+
import com.intellij.openapi.editor.ex.EditorEx
2526
import com.intellij.openapi.fileEditor.FileDocumentManager
2627
import com.intellij.openapi.fileEditor.FileEditor
2728
import com.intellij.openapi.fileTypes.PlainTextLanguage
@@ -86,6 +87,11 @@ open class CodeHighlightSketch(
8687
fun initEditor(text: String, fileName: String? = null) {
8788
if (hasSetupAction) return
8889
hasSetupAction = true
90+
91+
if (isUser) {
92+
setupSimpleEditor(text, fileName)
93+
return
94+
}
8995

9096
val editor = EditorUtil.createCodeViewerEditor(project, text, ideaLanguage, fileName, this)
9197

@@ -102,24 +108,49 @@ open class CodeHighlightSketch(
102108
editorFragment = EditorFragment(editor, minDevinLineThreshold, previewEditor)
103109
setupDevInsView(text)
104110
} else {
105-
editorFragment = EditorFragment(editor, editorLineThreshold, previewEditor)
106-
add(editorFragment!!.getContent())
111+
setupRegularEditor(editor)
107112
}
108113

114+
setupToolbarAndStyling(fileName, editor)
115+
}
116+
117+
private fun setupSimpleEditor(text: String, fileName: String?) {
118+
val editor = EditorUtil.createCodeViewerEditor(project, text, ideaLanguage, fileName, this)
119+
120+
border = if (withLeftRightBorder) {
121+
JBEmptyBorder(4, 4, 4, 4)
122+
} else {
123+
JBEmptyBorder(4, 0, 0, 0)
124+
}
125+
126+
editor.component.isOpaque = true
127+
editorFragment = EditorFragment(editor, editorLineThreshold, previewEditor)
128+
add(editorFragment!!.getContent())
129+
130+
setupToolbarAndStyling(fileName, editor)
131+
}
132+
133+
private fun setupRegularEditor(editor: EditorEx) {
134+
editorFragment = EditorFragment(editor, editorLineThreshold, previewEditor)
135+
add(editorFragment!!.getContent())
136+
}
137+
138+
private fun setupToolbarAndStyling(fileName: String?, editor: EditorEx) {
109139
val isDeclarePackageFile = BuildSystemProvider.isDeclarePackageFile(fileName)
110140
val lowercase = textLanguage?.lowercase()
141+
111142
if (textLanguage != null && lowercase != "markdown" && lowercase != "plain text") {
112-
if (showToolbar && !isDevIns) {
143+
if (showToolbar && lowercase != "devin") {
113144
toolbar = setupActionBar(project, editor, isDeclarePackageFile)
114145
}
115146
} else {
116147
editorFragment?.editor?.backgroundColor = JBColor.PanelBackground
117148
}
118149

119-
if (lowercase == "devin") {
120-
editorFragment?.editor?.setBorder(JBEmptyBorder(1, 1, 0, 1))
121-
} else if (lowercase != "markdown") {
122-
editorFragment?.editor?.setBorder(JBEmptyBorder(1, 0, 0, 0))
150+
when (lowercase) {
151+
"devin" -> editorFragment?.editor?.setBorder(JBEmptyBorder(1, 1, 0, 1))
152+
"markdown" -> { /* no border changes needed */ }
153+
else -> editorFragment?.editor?.setBorder(JBEmptyBorder(1, 0, 0, 0))
123154
}
124155
}
125156

@@ -244,6 +275,7 @@ open class CodeHighlightSketch(
244275
override fun updateViewText(text: String, complete: Boolean) {
245276
isComplete = complete
246277

278+
// Initialize editor if not already done and text is not empty
247279
if (!hasSetupAction && text.trim().isNotEmpty()) {
248280
initEditor(text)
249281
}
@@ -256,6 +288,7 @@ open class CodeHighlightSketch(
256288
try {
257289
document?.replaceString(0, document.textLength, normalizedText)
258290

291+
// Update DevIns collapsed panel preview text if applicable
259292
if (isDevIns && devInsCollapsedPanel != null) {
260293
val firstLine = normalizedText.lines().firstOrNull() ?: ""
261294
val components = devInsCollapsedPanel!!.components
@@ -273,24 +306,24 @@ open class CodeHighlightSketch(
273306
logger<CodeHighlightSketch>().error("Error updating editor text", e)
274307
}
275308

276-
// 更新 runButton 图标状态
309+
// Update run button icon state for DevIns
277310
updateRunButtonIcon()
278311

279312
val lineCount = document?.lineCount ?: 0
280313
if (lineCount > editorLineThreshold) {
281314
editorFragment?.updateExpandCollapseLabel()
282315
}
283-
284-
if (complete && isDevIns) {
285-
if (!isCollapsed) {
286-
toggleEditorVisibility()
287-
}
316+
317+
// Auto-collapse DevIns view when complete
318+
if (complete && isDevIns && !isCollapsed) {
319+
toggleEditorVisibility()
288320
}
289321
}
290322
}
291323

292324
override fun onDoneStream(allText: String) {
293-
if (ideaLanguage?.displayName != "DevIn") return
325+
// Only process DevIns commands for non-user messages
326+
if (isUser || ideaLanguage?.displayName != "DevIn") return
294327

295328
val currentText = getViewText()
296329
if (currentText.startsWith("/" + BuiltinCommand.WRITE.commandName + ":")) {

0 commit comments

Comments
 (0)