@@ -4,19 +4,19 @@ import cc.unitmesh.devti.AutoDevBundle
4
4
import cc.unitmesh.devti.AutoDevIcons
5
5
import cc.unitmesh.devti.AutoDevNotifications
6
6
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
9
8
import cc.unitmesh.devti.provider.BuildSystemProvider
10
9
import cc.unitmesh.devti.provider.RunService
10
+ import cc.unitmesh.devti.sketch.AutoSketchMode
11
11
import cc.unitmesh.devti.sketch.ui.LangSketch
12
12
import cc.unitmesh.devti.util.parser.CodeFence
13
13
import com.intellij.icons.AllIcons
14
14
import com.intellij.ide.scratch.ScratchRootType
15
15
import com.intellij.lang.Language
16
16
import com.intellij.openapi.Disposable
17
- import com.intellij.openapi.actionSystem.ActionManager
18
17
import com.intellij.openapi.actionSystem.ActionToolbar
19
18
import com.intellij.openapi.actionSystem.DataProvider
19
+ import com.intellij.openapi.actionSystem.Presentation
20
20
import com.intellij.openapi.actionSystem.impl.ActionButton
21
21
import com.intellij.openapi.application.ReadAction
22
22
import com.intellij.openapi.command.WriteCommandAction
@@ -27,6 +27,7 @@ import com.intellij.openapi.editor.ex.EditorEx
27
27
import com.intellij.openapi.fileEditor.FileDocumentManager
28
28
import com.intellij.openapi.fileEditor.FileEditor
29
29
import com.intellij.openapi.fileTypes.PlainTextLanguage
30
+ import com.intellij.openapi.project.DumbAwareAction
30
31
import com.intellij.openapi.project.Project
31
32
import com.intellij.openapi.util.Disposer
32
33
import com.intellij.openapi.util.text.StringUtil
@@ -88,7 +89,7 @@ open class CodeHighlightSketch(
88
89
fun initEditor (text : String , fileName : String? = null) {
89
90
if (hasSetupAction) return
90
91
hasSetupAction = true
91
-
92
+
92
93
if (isUser) {
93
94
setupSimpleEditor(text, fileName)
94
95
return
@@ -117,53 +118,55 @@ open class CodeHighlightSketch(
117
118
118
119
private fun setupSimpleEditor (text : String , fileName : String? ) {
119
120
val editor = EditorUtil .createCodeViewerEditor(project, text, ideaLanguage, fileName, this )
120
-
121
+
121
122
border = if (withLeftRightBorder) {
122
123
JBEmptyBorder (4 , 4 , 4 , 4 )
123
124
} else {
124
125
JBEmptyBorder (4 , 0 , 0 , 0 )
125
126
}
126
-
127
+
127
128
editor.component.isOpaque = true
128
129
editorFragment = EditorFragment (editor, editorLineThreshold, previewEditor)
129
130
add(editorFragment!! .getContent())
130
-
131
+
131
132
setupToolbarAndStyling(fileName, editor)
132
133
}
133
-
134
+
134
135
private fun setupRegularEditor (editor : EditorEx ) {
135
136
editorFragment = EditorFragment (editor, editorLineThreshold, previewEditor)
136
137
add(editorFragment!! .getContent())
137
138
}
138
-
139
+
139
140
private fun setupToolbarAndStyling (fileName : String? , editor : EditorEx ) {
140
141
val isDeclarePackageFile = BuildSystemProvider .isDeclarePackageFile(fileName)
141
142
val lowercase = textLanguage?.lowercase()
142
-
143
+
143
144
if (textLanguage != null && lowercase != " markdown" && lowercase != " plain text" ) {
144
145
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
+ )
146
152
}
147
153
} else {
148
154
editorFragment?.editor?.backgroundColor = JBColor .PanelBackground
149
155
}
150
156
151
157
when (lowercase) {
152
158
" devin" -> editorFragment?.editor?.setBorder(JBEmptyBorder (1 , 1 , 0 , 1 ))
153
- " markdown" -> { /* no border changes needed */ }
159
+ " markdown" -> { /* no border changes needed */
160
+ }
161
+
154
162
else -> editorFragment?.editor?.setBorder(JBEmptyBorder (1 , 0 , 0 , 0 ))
155
163
}
156
164
}
157
165
158
166
private fun setupDevInsView (text : String ) {
159
167
devInsCollapsedPanel = JPanel (BorderLayout ()).apply {
160
168
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)
167
170
168
171
val firstLine = text.lines().firstOrNull() ? : " "
169
172
val previewLabel = JBLabel (firstLine).apply {
@@ -210,10 +213,19 @@ open class CodeHighlightSketch(
210
213
updateRunButtonIcon()
211
214
}
212
215
213
- private fun createRunButton (runAction : AutoDevInsertCodeAction ): ActionButton {
216
+ var devinRunButtonPresentation = Presentation ()
217
+ private fun createRunButton (newText : String ): ActionButton {
218
+ devinRunButtonPresentation.icon = AutoDevIcons .RUN
214
219
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,
217
229
" AutoDevToolbar" ,
218
230
JBUI .size(24 , 24 )
219
231
)
@@ -222,7 +234,7 @@ open class CodeHighlightSketch(
222
234
private fun updateRunButtonIcon () {
223
235
runButton?.let { button: ActionButton ->
224
236
val icon = if (isComplete) AutoDevIcons .RUN else AutoDevIcons .LOADING
225
- button .setIcon(icon)
237
+ devinRunButtonPresentation .setIcon(icon)
226
238
button.repaint()
227
239
}
228
240
}
@@ -243,7 +255,7 @@ open class CodeHighlightSketch(
243
255
}
244
256
245
257
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 {
247
259
border = JBUI .Borders .empty(4 , 8 )
248
260
cursor = Cursor .getPredefinedCursor(Cursor .HAND_CURSOR )
249
261
isOpaque = true
@@ -314,7 +326,7 @@ open class CodeHighlightSketch(
314
326
if (lineCount > editorLineThreshold) {
315
327
editorFragment?.updateExpandCollapseLabel()
316
328
}
317
-
329
+
318
330
// Auto-collapse DevIns view when complete
319
331
if (complete && isDevIns && ! isCollapsed) {
320
332
toggleEditorVisibility()
0 commit comments