Skip to content

Commit

Permalink
feat(intention): add Shire Assistant with AI AutoAction
Browse files Browse the repository at this point in the history
Add Shire Assistant with AI AutoAction for AutoDev category.
  • Loading branch information
phodal committed Jun 2, 2024
1 parent e833455 commit 0b23d35
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ shire.patch.cannot.read.patch=Cannot read a patch

inspection.group.name=Shire language
inspection.duplicate.agent=Duplicate agent calls detected. It is recommended to make only one call per agent. Please remove any duplicate agent calls.
shire.intention.name=Shire Assistant
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.phodal.shire.action.intention

import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile

object IntentionHelperUtil {
fun getAiAssistantIntentions(project: Project, editor: Editor, file: PsiFile): List<IntentionAction> {
return emptyList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.phodal.shire.action.intention

import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.lang.injection.InjectedLanguageManager
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.util.Iconable
import com.intellij.psi.PsiFile
import javax.swing.Icon
import com.phodal.shire.ShireMainBundle
import com.phodal.shirelang.ShireIcons
import com.phodal.shire.action.intention.ui.CustomPopupStep

class ShireIntentionHelper : IntentionAction, Iconable {

Check warning on line 15 in src/main/kotlin/com/phodal/shire/action/intention/ShireIntentionHelper.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Class "ShireIntentionHelper" is never used
override fun startInWriteAction(): Boolean = false
override fun getText(): String = ShireMainBundle.message("intentions.assistant.name")

Check warning on line 17 in src/main/kotlin/com/phodal/shire/action/intention/ShireIntentionHelper.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'Shire Intention Action' is not properly capitalized. It should have sentence capitalization
override fun getFamilyName(): String = ShireMainBundle.message("intentions.assistant.name")

Check warning on line 18 in src/main/kotlin/com/phodal/shire/action/intention/ShireIntentionHelper.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'Shire Intention Action' is not properly capitalized. It should have sentence capitalization
override fun getIcon(flags: Int): Icon = ShireIcons.DEFAULT
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
if (file == null) return false

val instance = InjectedLanguageManager.getInstance(project)
return instance.getTopLevelFile(file)?.virtualFile != null
}

override fun invoke(project: Project, editor: Editor, file: PsiFile) {
val intentions = IntentionHelperUtil.getAiAssistantIntentions(project, editor, file)

val title = ShireMainBundle.message("intentions.assistant.popup.title")
val popupStep = CustomPopupStep(intentions, project, editor, file, title)
val popup = JBPopupFactory.getInstance().createListPopup(popupStep)

popup.showInBestPositionFor(editor)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.phodal.shire.action.intention

import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.actionSystem.ActionGroup
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.phodal.shirelang.ShireBundle

class ShireIntentionsActionGroup : ActionGroup(ShireBundle.message("shire.intention.name"), true), DumbAware {
override fun getChildren(e: AnActionEvent?): Array<AnAction> {
val project: Project = e?.project ?: return emptyArray()
val editor: Editor = e.getData(CommonDataKeys.EDITOR) ?: return emptyArray()
val file: PsiFile = e.getData(CommonDataKeys.PSI_FILE) ?: return emptyArray()

val intentions: List<IntentionAction> = IntentionHelperUtil.getAiAssistantIntentions(project, editor, file)

return intentions.map { action ->
DumbAwareAction.create(action.text) {

Check warning on line 24 in src/main/kotlin/com/phodal/shire/action/intention/ShireIntentionsActionGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

The sentence capitalization is provided where title capitalization is required

Check warning on line 24 in src/main/kotlin/com/phodal/shire/action/intention/ShireIntentionsActionGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

The sentence capitalization is provided where title capitalization is required
action.invoke(project, editor, file)
}
}.toTypedArray()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.phodal.shire.action.intention.ui

import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.ListSeparator
import com.intellij.openapi.ui.popup.PopupStep
import com.intellij.openapi.ui.popup.util.BaseListPopupStep
import com.intellij.psi.PsiFile

class CustomPopupStep(
private val intentionAction: List<IntentionAction>,
private val project: Project,
private val editor: Editor,
private val psiFile: PsiFile,
private val popupTitle: String,

Check warning on line 16 in src/main/kotlin/com/phodal/shire/action/intention/ui/CustomPopupStep.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Constructor parameter is never used as a property

Constructor parameter is never used as a property
) : BaseListPopupStep<IntentionAction>(popupTitle, intentionAction) {
override fun getTextFor(value: IntentionAction): String = value.text

override fun getSeparatorAbove(value: IntentionAction?): ListSeparator? =
if (value != null && value == intentionAction) ListSeparator() else null

override fun onChosen(selectedValue: IntentionAction?, finalChoice: Boolean): PopupStep<*>? {
return doFinalStep {
selectedValue!!.invoke(project, editor, psiFile)
}
}
}
9 changes: 9 additions & 0 deletions src/main/resources/META-INF/shire-main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@
id="shireLlmSettingsConfigurable"
displayName="Shire"/>
</extensions>

<actions>
<group id="ShireIntentionsActionGroup" class="com.phodal.shire.action.intention.ShireIntentionsActionGroup"
icon="com.phodal.shirelang.ShireIcons.DEFAULT" searchable="false">

<add-to-group group-id="ShowIntentionsGroup" relative-to-action="ShowIntentionActions" anchor="after"/>
<add-to-group group-id="Floating.CodeToolbar" anchor="first"/>
</group>
</actions>
</idea-plugin>
7 changes: 4 additions & 3 deletions src/main/resources/messages/ShireMainBundle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
projectService=Project service: {0}
randomLabel=The random number is: {0}
shuffle=Shuffle
intention.category.llm=AutoDev
intentions.write.action=Generate code
intentions.assistant.name=Shire Intention Action
intentions.assistant.popup.title=Shire Intention Action

0 comments on commit 0b23d35

Please sign in to comment.