Skip to content

Commit bfa8e55

Browse files
committed
refactor(code-highlight): consolidate panel handling and update shell support
- Refactor `CodeHighlightSketch` to consolidate panel handling logic for different languages. - Update shell script support to include both "bash" and "shell" language types. - Adjust terminal UI layout and improve shell script execution timing.
1 parent 4cf2fe6 commit bfa8e55

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,31 @@ open class CodeHighlightSketch(
121121
/// get the text from the editor
122122
val parse = CodeFence.parse(editorFragment!!.editor.document.text)
123123
/// todo redesign for DevIn blocking
124+
var panel: JComponent? = null
124125
if (parse.originLanguage == "diff" || parse.originLanguage == "patch") {
125126
val langSketch = LanguageSketchProvider.provide("patch")?.create(project, parse.text) ?: return
126-
val panel = langSketch.getComponent()
127-
panel.border = JBEmptyBorder(8)
128-
add(panel, BorderLayout.SOUTH)
129-
130-
editorFragment?.updateExpandCollapseLabel()
131-
132-
revalidate()
133-
repaint()
134-
} else if(parse.originLanguage == "html") {
127+
panel = langSketch.getComponent()
128+
} else if (parse.originLanguage == "html") {
135129
val langSketch = LanguageSketchProvider.provide("html")?.create(project, parse.text) ?: return
136-
val panel = langSketch.getComponent()
130+
panel = langSketch.getComponent()
137131
langSketch.doneUpdateText(text_)
138-
panel.border = JBEmptyBorder(8)
139-
add(panel, BorderLayout.SOUTH)
140-
141-
editorFragment?.updateExpandCollapseLabel()
132+
} else if (parse.originLanguage == "bash") {
133+
val langSketch = LanguageSketchProvider.provide("shell")?.create(project, parse.text) ?: return
134+
panel = langSketch.getComponent()
135+
langSketch.doneUpdateText(text_)
136+
}
142137

143-
revalidate()
144-
repaint()
138+
if (panel == null) {
139+
return
145140
}
141+
142+
panel.border = JBEmptyBorder(8)
143+
add(panel, BorderLayout.SOUTH)
144+
145+
editorFragment?.updateExpandCollapseLabel()
146+
147+
revalidate()
148+
repaint()
146149
}
147150
}
148151

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
执行 shell 脚本,要考虑生成的脚本是非阻塞的,即可以读取标准输入
22
/shell:execute
3-
```
3+
```bash
44
echo "Hello, World!"
55
```

exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalLangSketchProvider.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package cc.unitmesh.terminal.sketch
22

3-
import java.io.File
4-
import java.io.IOException
53
import cc.unitmesh.devti.AutoDevNotifications
64
import cc.unitmesh.devti.sketch.SketchToolWindow
75
import cc.unitmesh.devti.sketch.run.ShellUtil
@@ -23,21 +21,24 @@ import com.intellij.openapi.ui.popup.JBPopupFactory
2321
import com.intellij.openapi.ui.popup.util.MinimizeButton
2422
import com.intellij.openapi.wm.ToolWindowManager
2523
import com.intellij.terminal.JBTerminalWidget
26-
import com.intellij.ui.components.panels.VerticalLayout
24+
import com.intellij.ui.components.panels.HorizontalLayout
2725
import com.intellij.util.ui.JBUI
26+
import com.jediterm.terminal.ui.TerminalWidgetListener
2827
import org.jetbrains.plugins.terminal.LocalTerminalDirectRunner
2928
import java.awt.BorderLayout
3029
import java.awt.Dimension
3130
import java.awt.event.MouseAdapter
3231
import java.awt.event.MouseEvent
32+
import java.io.File
33+
import java.io.IOException
3334
import javax.swing.JButton
3435
import javax.swing.JComponent
3536
import javax.swing.JLabel
3637
import javax.swing.JPanel
3738

3839

3940
class TerminalLangSketchProvider : LanguageSketchProvider {
40-
override fun isSupported(lang: String): Boolean = lang == "bash"
41+
override fun isSupported(lang: String): Boolean = lang == "bash" || lang == "shell"
4142

4243
override fun create(project: Project, content: String): ExtensionLangSketch {
4344
var content = content
@@ -60,7 +61,7 @@ class TerminalLangSketchProvider : LanguageSketchProvider {
6061

6162
add(terminalWidget!!.component, BorderLayout.CENTER)
6263

63-
val buttonPanel = JPanel(VerticalLayout(JBUI.scale(10)))
64+
val buttonPanel = JPanel(HorizontalLayout(JBUI.scale(10)))
6465
val runButton = JButton(AllIcons.Toolwindows.ToolWindowRun)
6566
.apply {
6667
addMouseListener(executeShellScriptOnClick(project, content))
@@ -133,7 +134,7 @@ class TerminalLangSketchProvider : LanguageSketchProvider {
133134

134135
override fun doneUpdateText(allText: String) {
135136
ApplicationManager.getApplication().invokeLater {
136-
Thread.sleep(1000) // todo: change to when terminal ready
137+
Thread.sleep(2000) // todo: change to when terminal ready
137138
terminalWidget!!.terminalStarter?.sendString(content, false)
138139
}
139140
}

0 commit comments

Comments
 (0)