Skip to content

Commit

Permalink
feat(terminal): add AI-generated shell script support #135
Browse files Browse the repository at this point in the history
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
phodal committed Apr 4, 2024
1 parent a2244d9 commit fc977a5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
@@ -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)
}
})
}
}
Expand Up @@ -5,12 +5,10 @@
</dependencies>

<actions>

<action id="genShell"
class="cc.unitmesh.terminal.GenShellAction"
description="Ask AI to generate shell"
icon="cc.unitmesh.devti.AutoDevIcons.AI_COPILOT"
>
icon="cc.unitmesh.devti.AutoDevIcons.AI_COPILOT">

<add-to-group group-id="TerminalToolwindowActionGroup" anchor="last"/>
</action>
Expand Down

0 comments on commit fc977a5

Please sign in to comment.