generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(terminal): add AI-generated shell script support #135
This commit introduces a new feature to the terminal extension, allowing users to generate shell scripts using AI. It modifies the `GenShellAction` class to include a popup window for input and output, and adds necessary imports and functions to handle the AI-generated script. The commit also includes copyright and license information for the new code.
- Loading branch information
Showing
2 changed files
with
90 additions
and
7 deletions.
There are no files selected for viewing
93 changes: 89 additions & 4 deletions
93
exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/GenShellAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,98 @@ | ||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
package cc.unitmesh.terminal | ||
|
||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.actionSystem.CommonDataKeys | ||
import com.intellij.openapi.editor.ex.EditorEx | ||
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.popup.Balloon | ||
import com.intellij.openapi.ui.popup.JBPopupFactory | ||
import com.intellij.openapi.ui.popup.JBPopupListener | ||
import com.intellij.openapi.ui.popup.LightweightWindowEvent | ||
import com.intellij.openapi.wm.IdeFocusManager | ||
import com.intellij.ui.DocumentAdapter | ||
import com.intellij.ui.awt.RelativePoint | ||
import com.intellij.ui.components.JBLabel | ||
import com.intellij.util.ui.JBUI | ||
import com.intellij.util.ui.StartupUiUtil | ||
import com.intellij.util.ui.SwingHelper | ||
import org.jetbrains.plugins.terminal.TerminalToolWindowManager | ||
import java.awt.Component | ||
import java.awt.Font | ||
import java.awt.Point | ||
import java.awt.event.FocusAdapter | ||
import java.awt.event.FocusEvent | ||
import java.awt.event.KeyAdapter | ||
import java.awt.event.KeyEvent | ||
import javax.swing.Box | ||
import javax.swing.JTextField | ||
import javax.swing.event.DocumentEvent | ||
|
||
class GenShellAction: AnAction() { | ||
private const val OUTLINE_PROPERTY = "JComponent.outline" | ||
private const val ERROR_VALUE = "error" | ||
|
||
|
||
class GenShellAction : AnAction() { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
val editor = e.getData(CommonDataKeys.EDITOR) as? EditorEx ?: return | ||
val project = e.project ?: return | ||
val contextComponent = e.getData(PlatformCoreDataKeys.CONTEXT_COMPONENT) ?: return | ||
TerminalToolWindowManager.getInstance(project).toolWindow | ||
showContentRenamePopup(project, contextComponent) | ||
} | ||
|
||
fun showContentRenamePopup(project: Project, contextComponent: Component) { | ||
val textField = JTextField() | ||
textField.selectAll() | ||
|
||
val label = JBLabel() | ||
label.font = StartupUiUtil.labelFont.deriveFont(Font.BOLD) | ||
|
||
val panel = SwingHelper.newLeftAlignedVerticalPanel(label, Box.createVerticalStrut(JBUI.scale(2)), textField) | ||
panel.addFocusListener(object : FocusAdapter() { | ||
override fun focusGained(e: FocusEvent?) { | ||
IdeFocusManager.findInstance().requestFocus(textField, false) | ||
} | ||
}) | ||
|
||
val balloon = JBPopupFactory.getInstance().createDialogBalloonBuilder(panel, null) | ||
.setShowCallout(true) | ||
.setCloseButtonEnabled(false) | ||
.setAnimationCycle(0) | ||
.setHideOnKeyOutside(true) | ||
.setHideOnClickOutside(true) | ||
.setRequestFocus(true) | ||
.setBlockClicksThroughBalloon(true) | ||
.createBalloon() | ||
|
||
textField.addKeyListener(object : KeyAdapter() { | ||
override fun keyPressed(e: KeyEvent?) { | ||
if (e != null && e.keyCode == KeyEvent.VK_ENTER) { | ||
if (textField.text.isEmpty()) { | ||
textField.putClientProperty(OUTLINE_PROPERTY, ERROR_VALUE) | ||
textField.repaint() | ||
return | ||
} | ||
// applyScript(content, project, textField.text) | ||
balloon.hide() | ||
} | ||
} | ||
}) | ||
|
||
textField.document.addDocumentListener(object : DocumentAdapter() { | ||
override fun textChanged(e: DocumentEvent) { | ||
val outlineValue = textField.getClientProperty(OUTLINE_PROPERTY) | ||
if (outlineValue == ERROR_VALUE) { | ||
textField.putClientProperty(OUTLINE_PROPERTY, null) | ||
textField.repaint() | ||
} | ||
} | ||
}) | ||
|
||
balloon.show(RelativePoint(contextComponent, Point(400, 0)), Balloon.Position.above) | ||
balloon.addListener(object : JBPopupListener { | ||
override fun onClosed(event: LightweightWindowEvent) { | ||
IdeFocusManager.findInstance().requestFocus(contextComponent, false) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters