@@ -4,19 +4,19 @@ import cc.unitmesh.devti.AutoDevBundle
44import cc.unitmesh.devti.AutoDevIcons
55import cc.unitmesh.devti.AutoDevNotifications
66import 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
98import cc.unitmesh.devti.provider.BuildSystemProvider
109import cc.unitmesh.devti.provider.RunService
10+ import cc.unitmesh.devti.sketch.AutoSketchMode
1111import cc.unitmesh.devti.sketch.ui.LangSketch
1212import cc.unitmesh.devti.util.parser.CodeFence
1313import com.intellij.icons.AllIcons
1414import com.intellij.ide.scratch.ScratchRootType
1515import com.intellij.lang.Language
1616import com.intellij.openapi.Disposable
17- import com.intellij.openapi.actionSystem.ActionManager
1817import com.intellij.openapi.actionSystem.ActionToolbar
1918import com.intellij.openapi.actionSystem.DataProvider
19+ import com.intellij.openapi.actionSystem.Presentation
2020import com.intellij.openapi.actionSystem.impl.ActionButton
2121import com.intellij.openapi.application.ReadAction
2222import com.intellij.openapi.command.WriteCommandAction
@@ -27,6 +27,7 @@ import com.intellij.openapi.editor.ex.EditorEx
2727import com.intellij.openapi.fileEditor.FileDocumentManager
2828import com.intellij.openapi.fileEditor.FileEditor
2929import com.intellij.openapi.fileTypes.PlainTextLanguage
30+ import com.intellij.openapi.project.DumbAwareAction
3031import com.intellij.openapi.project.Project
3132import com.intellij.openapi.util.Disposer
3233import 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()
0 commit comments