Skip to content

Commit ab9e24a

Browse files
committed
fix(sketch): improve run button for DevIns code execution
Replace AutoDevInsertCodeAction with direct DumbAwareAction to handle text insertion. Update UI elements including borders, spacing, and the "Fewer lines" label to "Hidden". Fix presentation handling for the run button and simplify EditorFragment implementation.
1 parent 25aaff5 commit ab9e24a

File tree

3 files changed

+54
-56
lines changed

3 files changed

+54
-56
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
1616
import com.intellij.openapi.actionSystem.DefaultActionGroup
1717
import com.intellij.openapi.application.ApplicationManager
1818
import com.intellij.openapi.application.runInEdt
19+
import com.intellij.openapi.editor.colors.EditorColorsManager
1920
import com.intellij.openapi.fileTypes.PlainTextLanguage
2021
import com.intellij.openapi.project.Project
2122
import com.intellij.openapi.ui.DialogPanel

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

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.AutoDevIcons
55
import cc.unitmesh.devti.AutoDevNotifications
66
import cc.unitmesh.devti.command.dataprovider.BuiltinCommand
7-
import cc.unitmesh.devti.gui.snippet.AutoDevInsertCodeAction
8-
import cc.unitmesh.devti.gui.snippet.AutoDevRunAction
7+
import cc.unitmesh.devti.gui.chat.ui.AutoInputService
98
import cc.unitmesh.devti.provider.BuildSystemProvider
109
import cc.unitmesh.devti.provider.RunService
10+
import cc.unitmesh.devti.sketch.AutoSketchMode
1111
import cc.unitmesh.devti.sketch.ui.LangSketch
1212
import cc.unitmesh.devti.util.parser.CodeFence
1313
import com.intellij.icons.AllIcons
1414
import com.intellij.ide.scratch.ScratchRootType
1515
import com.intellij.lang.Language
1616
import com.intellij.openapi.Disposable
17-
import com.intellij.openapi.actionSystem.ActionManager
1817
import com.intellij.openapi.actionSystem.ActionToolbar
1918
import com.intellij.openapi.actionSystem.DataProvider
19+
import com.intellij.openapi.actionSystem.Presentation
2020
import com.intellij.openapi.actionSystem.impl.ActionButton
2121
import com.intellij.openapi.application.ReadAction
2222
import com.intellij.openapi.command.WriteCommandAction
@@ -27,6 +27,7 @@ import com.intellij.openapi.editor.ex.EditorEx
2727
import com.intellij.openapi.fileEditor.FileDocumentManager
2828
import com.intellij.openapi.fileEditor.FileEditor
2929
import com.intellij.openapi.fileTypes.PlainTextLanguage
30+
import com.intellij.openapi.project.DumbAwareAction
3031
import com.intellij.openapi.project.Project
3132
import com.intellij.openapi.util.Disposer
3233
import com.intellij.openapi.util.text.StringUtil
@@ -88,7 +89,7 @@ open class CodeHighlightSketch(
8889
fun initEditor(text: String, fileName: String? = null) {
8990
if (hasSetupAction) return
9091
hasSetupAction = true
91-
92+
9293
if (isUser) {
9394
setupSimpleEditor(text, fileName)
9495
return
@@ -117,53 +118,55 @@ open class CodeHighlightSketch(
117118

118119
private fun setupSimpleEditor(text: String, fileName: String?) {
119120
val editor = EditorUtil.createCodeViewerEditor(project, text, ideaLanguage, fileName, this)
120-
121+
121122
border = if (withLeftRightBorder) {
122123
JBEmptyBorder(4, 4, 4, 4)
123124
} else {
124125
JBEmptyBorder(4, 0, 0, 0)
125126
}
126-
127+
127128
editor.component.isOpaque = true
128129
editorFragment = EditorFragment(editor, editorLineThreshold, previewEditor)
129130
add(editorFragment!!.getContent())
130-
131+
131132
setupToolbarAndStyling(fileName, editor)
132133
}
133-
134+
134135
private fun setupRegularEditor(editor: EditorEx) {
135136
editorFragment = EditorFragment(editor, editorLineThreshold, previewEditor)
136137
add(editorFragment!!.getContent())
137138
}
138-
139+
139140
private fun setupToolbarAndStyling(fileName: String?, editor: EditorEx) {
140141
val isDeclarePackageFile = BuildSystemProvider.isDeclarePackageFile(fileName)
141142
val lowercase = textLanguage?.lowercase()
142-
143+
143144
if (textLanguage != null && lowercase != "markdown" && lowercase != "plain text") {
144145
if (showToolbar && lowercase != "devin") {
145-
toolbar = setupActionBar(project, editor, isDeclarePackageFile, showBottomBorder = devInsCollapsedPanel != null)
146+
toolbar = setupActionBar(
147+
project,
148+
editor,
149+
isDeclarePackageFile,
150+
showBottomBorder = devInsCollapsedPanel != null
151+
)
146152
}
147153
} else {
148154
editorFragment?.editor?.backgroundColor = JBColor.PanelBackground
149155
}
150156

151157
when (lowercase) {
152158
"devin" -> editorFragment?.editor?.setBorder(JBEmptyBorder(1, 1, 0, 1))
153-
"markdown" -> { /* no border changes needed */ }
159+
"markdown" -> { /* no border changes needed */
160+
}
161+
154162
else -> editorFragment?.editor?.setBorder(JBEmptyBorder(1, 0, 0, 0))
155163
}
156164
}
157165

158166
private fun setupDevInsView(text: String) {
159167
devInsCollapsedPanel = JPanel(BorderLayout()).apply {
160168
border = JBUI.Borders.empty(2)
161-
162-
val runAction =
163-
ActionManager.getInstance().getAction("AutoDev.ToolWindow.Snippet.InsertCode") as? AutoDevInsertCodeAction
164-
?: AutoDevInsertCodeAction()
165-
166-
runButton = createRunButton(runAction)
169+
runButton = createRunButton(text)
167170

168171
val firstLine = text.lines().firstOrNull() ?: ""
169172
val previewLabel = JBLabel(firstLine).apply {
@@ -210,10 +213,19 @@ open class CodeHighlightSketch(
210213
updateRunButtonIcon()
211214
}
212215

213-
private fun createRunButton(runAction: AutoDevInsertCodeAction): ActionButton {
216+
var devinRunButtonPresentation = Presentation()
217+
private fun createRunButton(newText: String): ActionButton {
218+
devinRunButtonPresentation.icon = AutoDevIcons.RUN
214219
return ActionButton(
215-
runAction,
216-
runAction.templatePresentation.clone(),
220+
DumbAwareAction.create {
221+
val sketchService = project.getService(AutoSketchMode::class.java)
222+
if (sketchService.listener == null) {
223+
AutoInputService.getInstance(project).putText(newText)
224+
} else {
225+
sketchService.send(newText)
226+
}
227+
},
228+
devinRunButtonPresentation,
217229
"AutoDevToolbar",
218230
JBUI.size(24, 24)
219231
)
@@ -222,7 +234,7 @@ open class CodeHighlightSketch(
222234
private fun updateRunButtonIcon() {
223235
runButton?.let { button: ActionButton ->
224236
val icon = if (isComplete) AutoDevIcons.RUN else AutoDevIcons.LOADING
225-
button.setIcon(icon)
237+
devinRunButtonPresentation.setIcon(icon)
226238
button.repaint()
227239
}
228240
}
@@ -243,7 +255,7 @@ open class CodeHighlightSketch(
243255
}
244256

245257
private fun createFewerLinesLabel(): JBLabel {
246-
return JBLabel("Fewer lines", AllIcons.General.ChevronUp, JBLabel.LEFT).apply {
258+
return JBLabel("Hidden", AllIcons.General.ChevronUp, JBLabel.LEFT).apply {
247259
border = JBUI.Borders.empty(4, 8)
248260
cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
249261
isOpaque = true
@@ -314,7 +326,7 @@ open class CodeHighlightSketch(
314326
if (lineCount > editorLineThreshold) {
315327
editorFragment?.updateExpandCollapseLabel()
316328
}
317-
329+
318330
// Auto-collapse DevIns view when complete
319331
if (complete && isDevIns && !isCollapsed) {
320332
toggleEditorVisibility()

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

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
package cc.unitmesh.devti.sketch.ui.code
22

33
import com.intellij.icons.AllIcons
4-
import com.intellij.openapi.editor.Editor
5-
import com.intellij.openapi.editor.event.CaretEvent
6-
import com.intellij.openapi.editor.event.CaretListener
74
import com.intellij.openapi.editor.ex.EditorEx
85
import com.intellij.openapi.fileEditor.FileEditor
9-
import com.intellij.ui.JBColor
106
import com.intellij.ui.components.JBLabel
117
import com.intellij.util.ui.JBUI
128
import com.intellij.util.ui.components.BorderLayoutPanel
13-
import java.awt.Color
149
import java.awt.Dimension
1510
import java.awt.Insets
1611
import java.awt.event.MouseAdapter
1712
import java.awt.event.MouseEvent
18-
import javax.swing.Box
1913
import javax.swing.JComponent
2014

21-
class EditorPadding(private val editor: Editor, pad: Int) :
22-
Box.Filler(Dimension(pad, pad), Dimension(pad, pad), Dimension(pad, pad)) {
23-
init {
24-
setOpaque(true)
25-
editor.caretModel.addCaretListener(object : CaretListener {
26-
override fun caretPositionChanged(event: CaretEvent) {
27-
this@EditorPadding.repaint()
28-
}
29-
})
30-
}
31-
32-
override fun getBackground(): Color = editor.contentComponent.getBackground()
33-
}
34-
35-
3615
class EditorFragment(
3716
var editor: EditorEx,
3817
private val editorLineThreshold: Int = EDITOR_LINE_THRESHOLD,
@@ -41,22 +20,36 @@ class EditorFragment(
4120
private val expandCollapseTextLabel: JBLabel = JBLabel("", 0).apply {
4221
isOpaque = true
4322
isVisible = false
44-
border = JBUI.Borders.customLine(JBColor.border(), 0, 1, 1, 1)
23+
border = JBUI.Borders.empty()
4524
}
4625

47-
private val content: BorderLayoutPanel = createContentPanel().also {
48-
it.border = JBUI.Borders.empty()
26+
private val content: BorderLayoutPanel = createContentPanel().apply {
27+
this.border = JBUI.Borders.empty()
4928
}
5029

5130
private var collapsed = false
5231

32+
init {
33+
expandCollapseTextLabel.addMouseListener(object : MouseAdapter() {
34+
override fun mouseClicked(e: MouseEvent?) {
35+
toggleCollapsedState()
36+
}
37+
})
38+
39+
editor.component.border = JBUI.Borders.empty()
40+
editor.contentComponent.border = JBUI.Borders.empty()
41+
editor.setBorder(JBUI.Borders.empty())
42+
editor.headerComponent?.border = JBUI.Borders.empty()
43+
}
44+
5345
private fun createContentPanel(): BorderLayoutPanel {
5446
return object : BorderLayoutPanel() {
5547
override fun getPreferredSize(): Dimension {
5648
val preferredSize = super.getPreferredSize()
5749
if (editor.document.lineCount > editorLineThreshold && collapsed) {
5850
return calculatePreferredSize(preferredSize, this@EditorFragment.editorLineThreshold)
5951
}
52+
6053
return preferredSize
6154
}
6255
}.apply {
@@ -84,14 +77,6 @@ class EditorFragment(
8477
return height + headerHeight + labelHeight + insets.height
8578
}
8679

87-
init {
88-
expandCollapseTextLabel.addMouseListener(object : MouseAdapter() {
89-
override fun mouseClicked(e: MouseEvent?) {
90-
toggleCollapsedState()
91-
}
92-
})
93-
}
94-
9580
fun getContent(): JComponent = content
9681

9782
fun isCollapsed(): Boolean = collapsed

0 commit comments

Comments
 (0)