Skip to content

Commit

Permalink
refactor(terminal): simplify terminal popup and script execution logic
Browse files Browse the repository at this point in the history
…#257

- Replace `RunService` with direct process handling using `ShellUtil`.
- Simplify `executePopup` logic and remove redundant panel handling.
- Update button styling and border spacing in `SketchToolWindow`.
- Fix color property in `SingleFileDiffView` from `background` to `foreground
  • Loading branch information
phodal committed Jan 16, 2025
1 parent 0f67562 commit da5b54d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"

Expand All @@ -46,14 +51,15 @@ 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())
buttonPanel.add(JButton(AllIcons.Toolwindows.ToolWindowRun).apply {
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)
Expand All @@ -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) {
Expand All @@ -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
}
}

0 comments on commit da5b54d

Please sign in to comment.