Skip to content

Commit

Permalink
feat(actions): refactor action groups and context menu action
Browse files Browse the repository at this point in the history
Refactored ShireVcsActionGroup and ShireContextMenuActionGroup to inherit from a new base class, ShireActionGroup. Moved ShireContextMenuAction to a new base package. This change simplifies the code structure and reduces redundancy.
  • Loading branch information
phodal committed Jun 30, 2024
1 parent 902bc2a commit 900d613
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.phodal.shirelang.actions.base

import com.intellij.openapi.actionSystem.ActionGroup
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.phodal.shirecore.action.ShireActionLocation
import com.phodal.shirelang.actions.dynamic.DynamicShireActionConfig
import com.phodal.shirelang.actions.dynamic.DynamicShireActionService

abstract class ShireActionGroup : ActionGroup() {
override fun getActionUpdateThread() = ActionUpdateThread.BGT

override fun update(e: AnActionEvent) {
e.presentation.isPopupGroup = DynamicShireActionService.getInstance().getAllActions().size > 1
}

fun getActionsByType(e: AnActionEvent?, shireActionLocation: ShireActionLocation): Array<AnAction> {
val configs: List<DynamicShireActionConfig> =
DynamicShireActionService.getInstance().getAction(shireActionLocation)
return configs.map { actionConfig ->
ShireContextMenuAction(actionConfig)
}.toTypedArray()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.phodal.shirelang.actions.base

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.DumbAwareAction
import com.phodal.shirecore.middleware.PostCodeHandleContext
import com.phodal.shirelang.ShireIcons
import com.phodal.shirelang.actions.ShireRunFileAction
import com.phodal.shirelang.actions.dynamic.DynamicShireActionConfig
import com.phodal.shirelang.actions.validator.WhenConditionValidator

class ShireContextMenuAction(private val config: DynamicShireActionConfig) :
DumbAwareAction(config.name, config.hole?.description, ShireIcons.DEFAULT) {

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}

override fun update(e: AnActionEvent) {
val conditions = config.hole?.when_ ?: return
val psiFile = e.getData(CommonDataKeys.PSI_FILE) ?: return

WhenConditionValidator.isAvailable(conditions, psiFile).let {
e.presentation.isEnabled = it
e.presentation.isVisible = it
}
}

override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val file = e.getData(CommonDataKeys.PSI_FILE)
val pickupElement = config.hole?.pickupElement(project, editor)

val context = PostCodeHandleContext.create(file, pickupElement)
config.hole?.setupStreamingEndProcessor(project, context)

PostCodeHandleContext.putData(context)
ShireRunFileAction.executeShireFile(project, config, ShireRunFileAction.createRunConfig(e))
}
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,18 @@
package com.phodal.shirelang.actions.context

import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.phodal.shirecore.action.ShireActionLocation
import com.phodal.shirecore.middleware.PostCodeHandleContext
import com.phodal.shirelang.ShireIcons
import com.phodal.shirelang.actions.ShireRunFileAction
import com.phodal.shirelang.actions.dynamic.DynamicShireActionConfig
import com.phodal.shirelang.actions.base.ShireActionGroup
import com.phodal.shirelang.actions.dynamic.DynamicShireActionService
import com.phodal.shirelang.actions.validator.WhenConditionValidator

class ShireContextMenuActionGroup : ActionGroup() {
override fun getActionUpdateThread() = ActionUpdateThread.BGT

class ShireContextMenuActionGroup : ShireActionGroup() {
override fun update(e: AnActionEvent) {
e.presentation.isPopupGroup = DynamicShireActionService.getInstance().getAllActions().size > 1
}

override fun getChildren(e: AnActionEvent?): Array<AnAction> {
val configs: List<DynamicShireActionConfig> =
DynamicShireActionService.getInstance().getAction(ShireActionLocation.CONTEXT_MENU)
return configs.map { actionConfig ->
ShireContextMenuAction(actionConfig)
}.toTypedArray()
return getActionsByType(e, ShireActionLocation.CONTEXT_MENU)
}
}

class ShireContextMenuAction(private val config: DynamicShireActionConfig) :
DumbAwareAction(config.name, config.hole?.description, ShireIcons.DEFAULT) {

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}

override fun update(e: AnActionEvent) {
val conditions = config.hole?.when_ ?: return
val psiFile = e.getData(CommonDataKeys.PSI_FILE) ?: return

WhenConditionValidator.isAvailable(conditions, psiFile).let {
e.presentation.isEnabled = it
e.presentation.isVisible = it
}
}

override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val file = e.getData(CommonDataKeys.PSI_FILE)
val pickupElement = config.hole?.pickupElement(project, editor)

val context = PostCodeHandleContext.create(file, pickupElement)
config.hole?.setupStreamingEndProcessor(project, context)

PostCodeHandleContext.putData(context)
ShireRunFileAction.executeShireFile(project, config, ShireRunFileAction.createRunConfig(e))
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package com.phodal.shirelang.actions.vcs

import com.intellij.openapi.actionSystem.ActionGroup
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.phodal.shirecore.action.ShireActionLocation
import com.phodal.shirelang.actions.context.ShireContextMenuAction
import com.phodal.shirelang.actions.dynamic.DynamicShireActionConfig
import com.phodal.shirelang.actions.dynamic.DynamicShireActionService
import com.phodal.shirelang.actions.base.ShireActionGroup

class ShireVcsActionGroup : ActionGroup() {
class ShireVcsActionGroup : ShireActionGroup() {
override fun getActionUpdateThread() = ActionUpdateThread.BGT

override fun update(e: AnActionEvent) {
e.presentation.isPopupGroup = DynamicShireActionService.getInstance().getAllActions().size > 1
e.presentation.isPopupGroup = getActionsByType(e, ShireActionLocation.COMMIT_MENU).size > 1
}

/// todo: spike for logic in commit menu
override fun getChildren(e: AnActionEvent?): Array<AnAction> {
val configs: List<DynamicShireActionConfig> =
DynamicShireActionService.getInstance().getAction(ShireActionLocation.CONTEXT_MENU)
return configs.map { actionConfig ->
ShireContextMenuAction(actionConfig)
}.toTypedArray()
return getActionsByType(e, ShireActionLocation.COMMIT_MENU)
}
}

0 comments on commit 900d613

Please sign in to comment.