generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(actions): refactor action groups and context menu action
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
Showing
4 changed files
with
78 additions
and
57 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
shirelang/src/main/kotlin/com/phodal/shirelang/actions/base/ShireActionGroup.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 |
---|---|---|
@@ -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() | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
shirelang/src/main/kotlin/com/phodal/shirelang/actions/base/ShireContextMenuAction.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 |
---|---|---|
@@ -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)) | ||
} | ||
} |
51 changes: 5 additions & 46 deletions
51
...elang/src/main/kotlin/com/phodal/shirelang/actions/context/ShireContextMenuActionGroup.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,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)) | ||
} | ||
} |
16 changes: 5 additions & 11 deletions
16
shirelang/src/main/kotlin/com/phodal/shirelang/actions/vcs/ShireVcsActionGroup.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,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) | ||
} | ||
} |