diff --git a/core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchToolWindow.kt b/core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchToolWindow.kt index 00bdf4a8d..fc5ec936c 100644 --- a/core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchToolWindow.kt +++ b/core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchToolWindow.kt @@ -33,6 +33,7 @@ import java.awt.event.KeyAdapter import java.awt.event.KeyEvent import java.awt.event.MouseAdapter import java.awt.event.MouseEvent +import javax.swing.JButton import javax.swing.JPanel import javax.swing.JProgressBar import javax.swing.ScrollPaneConstants @@ -60,8 +61,8 @@ class SketchToolWindow(val project: Project, val editor: Editor?, private val sh this.isOpaque = true } - val header = JBLabel(AllIcons.Actions.Copy).apply { - this.border = JBUI.Borders.empty(10, 0) + val header = JButton(AllIcons.Actions.Copy).apply { + this.border = JBUI.Borders.empty(10, 20) addMouseListener(object : MouseAdapter() { override fun mouseClicked(e: MouseEvent?) { val selection = StringSelection(myText) diff --git a/core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffView.kt b/core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffView.kt index bbf38d9d0..bbeb13709 100644 --- a/core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffView.kt +++ b/core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffView.kt @@ -183,7 +183,7 @@ class SingleFileDiffView( border = BorderFactory.createEmptyBorder() toolTipText = AutoDevBundle.message("sketch.patch.action.repairDiff.tooltip") isEnabled = appliedPatch?.status != ApplyPatchStatus.SUCCESS - background = if (isEnabled) JBColor(0xFF0000, 0xFF0000) else JPanel().background + foreground = if (isEnabled) JBColor(0xFF0000, 0xFF0000) else JPanel().background addActionListener { FileEditorManager.getInstance(myProject).openFile(virtualFile, true) diff --git a/exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalLangSketchProvider.kt b/exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalLangSketchProvider.kt index 58ad10513..83f14d594 100644 --- a/exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalLangSketchProvider.kt +++ b/exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalLangSketchProvider.kt @@ -1,11 +1,15 @@ package cc.unitmesh.terminal.sketch import cc.unitmesh.devti.AutoDevNotifications -import cc.unitmesh.devti.provider.RunService +import cc.unitmesh.devti.sketch.run.ShellUtil import cc.unitmesh.devti.sketch.ui.ExtensionLangSketch import cc.unitmesh.devti.sketch.ui.LanguageSketchProvider +import com.intellij.execution.configurations.GeneralCommandLine +import com.intellij.execution.configurations.PtyCommandLine +import com.intellij.execution.process.KillableProcessHandler +import com.intellij.execution.process.ProcessAdapter +import com.intellij.execution.process.ProcessEvent import com.intellij.icons.AllIcons -import com.intellij.ide.scratch.ScratchRootType import com.intellij.lang.Language import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.project.Project @@ -25,6 +29,7 @@ import javax.swing.JComponent import javax.swing.JLabel import javax.swing.JPanel + class TerminalLangSketchProvider : LanguageSketchProvider { override fun isSupported(lang: String): Boolean = lang == "bash" @@ -46,6 +51,7 @@ class TerminalLangSketchProvider : LanguageSketchProvider { add(JLabel("Terminal").also { it.border = JBUI.Borders.empty(5, 0) }, BorderLayout.NORTH) + add(terminalWidget!!.component, BorderLayout.CENTER) val buttonPanel = JPanel(BorderLayout()) @@ -53,7 +59,7 @@ class TerminalLangSketchProvider : LanguageSketchProvider { addMouseListener(executeShellScriptOnClick(project, content)) }, BorderLayout.WEST) buttonPanel.add(JButton("Pop up Terminal").apply { - addMouseListener(executePopup(terminalWidget, project, panelLayout)) + addMouseListener(executePopup(terminalWidget, project)) }, BorderLayout.EAST) add(buttonPanel, BorderLayout.SOUTH) @@ -65,6 +71,36 @@ class TerminalLangSketchProvider : LanguageSketchProvider { ) } + + private fun executePopup(terminalWidget: JBTerminalWidget?, project: Project): MouseAdapter = + object : MouseAdapter() { + override fun mouseClicked(e: MouseEvent?) { + var popup: JBPopup? = null + popup = JBPopupFactory.getInstance() + .createComponentPopupBuilder(terminalWidget!!.component, null) + .setProject(project) + .setResizable(true) + .setMovable(true) + .setTitle("Terminal") + .setCancelButton(MinimizeButton("Hide")) + .setCancelCallback { + popup?.cancel() + panelLayout!!.remove(terminalWidget.component) + panelLayout!!.add(terminalWidget.component) + + panelLayout!!.revalidate() + panelLayout!!.repaint() + true + } + .setFocusable(true) + .setRequestFocus(true) + .createPopup() + + val editor = FileEditorManager.getInstance(project).selectedTextEditor!! + popup.showInBestPositionFor(editor) + } + } + override fun getExtensionName(): String = "Terminal" override fun getViewText(): String = content override fun updateViewText(text: String) { @@ -88,48 +124,31 @@ class TerminalLangSketchProvider : LanguageSketchProvider { content: String ): MouseAdapter = object : MouseAdapter() { override fun mouseClicked(e: MouseEvent?) { - val tempShellName = "temp" + System.currentTimeMillis() + ".sh" - val language = Language.findLanguageByID("Shell Script")!! - val scratchFile = ScratchRootType.getInstance() - .createScratchFile(project, tempShellName, language, content) - ?: return - - try { - RunService.provider(project, scratchFile) - ?.runFile(project, scratchFile, null, isFromToolAction = true) - ?: AutoDevNotifications.notify(project, "Run Failed, no provider") - } catch (e: Exception) { - AutoDevNotifications.notify(project, "Run Failed: ${e.message}") - } + val commandLine = createCommandLineForScript(project, content) + val processBuilder = commandLine.toProcessBuilder() + val process = processBuilder.start() + val processHandler = KillableProcessHandler(process, commandLine.commandLineString) + processHandler.startNotify() + + processHandler.addProcessListener(object : ProcessAdapter() { + override fun processTerminated(event: ProcessEvent) { + AutoDevNotifications.notify(project, "Process terminated with exit code ${event.exitCode}") + processHandler.destroyProcess() + } + }) } } - private fun executePopup(terminalWidget: JBTerminalWidget?, project: Project, panelLayout: JPanel?): MouseAdapter = - object : MouseAdapter() { - override fun mouseClicked(e: MouseEvent?) { - var popup: JBPopup? = null - popup = JBPopupFactory.getInstance() - .createComponentPopupBuilder(terminalWidget!!.component, null) - .setProject(project) - .setResizable(true) - .setMovable(true) - .setTitle("Terminal") - .setCancelButton(MinimizeButton("Hide")) - .setCancelOnClickOutside(true) - .setCancelOnWindowDeactivation(false) - .setCancelCallback { - panelLayout!!.add("terminal", terminalWidget!!.component) - popup!!.closeOk(null) - panelLayout.revalidate() - panelLayout.repaint() - true - } - .setFocusable(true) - .setRequestFocus(true) - .createPopup() - - val editor = FileEditorManager.getInstance(project).selectedTextEditor!! - popup.showInBestPositionFor(editor) - } - } + private fun createCommandLineForScript(project: Project, scriptText: String): GeneralCommandLine { + val workingDirectory = project.basePath + val commandLine = PtyCommandLine() + commandLine.withConsoleMode(false) + commandLine.withInitialColumns(120) + commandLine.withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE) + commandLine.setWorkDirectory(workingDirectory!!) + commandLine.withExePath(ShellUtil.detectShells().first()) + commandLine.withParameters("-c") + commandLine.withParameters(scriptText) + return commandLine + } }