Skip to content

Commit

Permalink
Actions: Add EF Core actions for Solution context menu (#189)
Browse files Browse the repository at this point in the history
+ rework actions visibility in general
  • Loading branch information
seclerp committed Sep 5, 2023
1 parent b8f92a9 commit 583c809
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,53 @@ package com.jetbrains.rider.plugins.efcore.features.shared
import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.platform.backend.workspace.WorkspaceModel
import com.jetbrains.rider.model.RdProjectDescriptor
import com.jetbrains.rider.model.RdUnloadProjectDescriptor
import com.jetbrains.rider.projectView.workspace.ProjectModelEntity
import com.jetbrains.rider.projectView.workspace.getProjectModelEntities
import com.jetbrains.rider.projectView.workspace.isProject
import org.jetbrains.annotations.NonNls
import java.util.*

fun AnActionEvent.isEfCoreActionContext(): Boolean {
// Solution not loaded, hide action
if (project == null) return false

// Tools section
if (place == ActionPlaces.MAIN_MENU) return true

// Search
if (place == ActionPlaces.ACTION_SEARCH) return true

// Other potential popups, except project view popup (project context menu)
if (ActionPlaces.isPopupPlace(place) && place != ActionPlaces.PROJECT_VIEW_POPUP) return true

// If we're trying to show action from context menu, but not under project context menu, hide
val actionFile = getData(PlatformDataKeys.VIRTUAL_FILE) ?: return false
if (!isSupportedProjectExtension(actionFile.extension ?: ""))
return false

// Lastly we check that the supported project file is loaded by MSBuild and has correct descriptor
getFileProject(project!!, actionFile) ?: return false
fun AnActionEvent.isEfCoreActionContext() = when {
isProjectsModeContext() -> true
isSolutionModeContext() -> true
else -> false
}

fun AnActionEvent.isProjectsModeContext(): Boolean {
// Check if action is executing through Solution view, otherwise VIRTUAL_FILE from below may point to wrong file from currently opened editor.
if (place != ActionPlaces.PROJECT_VIEW_POPUP) return false
// Check if solution is loaded
if (project == null) return false
// Fast check if action file extension is supported (F# or C# project)
val extension = getData(PlatformDataKeys.VIRTUAL_FILE)?.extension ?: return false
if (!isSupportedProjectExtension(extension)) return false
// Check that currently opened project is loaded and known by backend
if (actionDotnetProjectFile == null) return false
return true
}

fun AnActionEvent.getDotnetProjectId(): UUID? {
fun AnActionEvent.isSolutionModeContext() = when {
ActionPlaces.isMainMenuOrActionSearch(place) -> true
ActionPlaces.isMainToolbar(place) -> true
ActionPlaces.isPopupPlace(place) -> true
else -> false
}

val AnActionEvent.actionDotnetProjectFile: ProjectModelEntity? get() {
if (place != ActionPlaces.PROJECT_VIEW_POPUP) return null
val intellijProject = project ?: return null
val actionFile = getData(PlatformDataKeys.VIRTUAL_FILE) ?: return null

return getFileProject(project!!, actionFile)?.descriptor?.let {
(it as RdProjectDescriptor).originalGuid
}
return WorkspaceModel
.getInstance(intellijProject)
.getProjectModelEntities(actionFile, intellijProject)
.firstOrNull { it.isProject() }
}

@Suppress("UnstableApiUsage")
private fun getFileProject(intellijProject: Project, virtualFile: VirtualFile): ProjectModelEntity? =
WorkspaceModel
.getInstance(intellijProject)
.getProjectModelEntities(virtualFile, intellijProject)
.firstOrNull {
it.descriptor is RdProjectDescriptor
&& it.descriptor !is RdUnloadProjectDescriptor
}
val AnActionEvent.actionDotnetProjectId: UUID? get() =
actionDotnetProjectFile?.descriptor?.let { it as? RdProjectDescriptor }?.originalGuid

@NonNls
private fun isSupportedProjectExtension(projectFileExtension: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ abstract class BaseCommandAction(
currentDotnetProjectId: UUID?): BaseDialogWrapper

private fun openDialog(actionEvent: AnActionEvent, efCoreVersion: DotnetEfVersion) {
val intellijProject = actionEvent.project!!
val model = getEfCoreRiderModel(actionEvent)
val currentDotnetProjectName = actionEvent.getDotnetProjectId()
val intellijProject = actionEvent.project ?: return
val model = actionEvent.project?.solution?.riderEfCoreModel ?: return
val currentDotnetProjectName = actionEvent.actionDotnetProjectId
val dialog = createDialog(intellijProject, efCoreVersion, model, currentDotnetProjectName)

if (dialog.showAndGet()) {
Expand All @@ -82,12 +82,6 @@ abstract class BaseCommandAction(
}
}

private fun getEfCoreRiderModel(actionEvent: AnActionEvent): RiderEfCoreModel {
// TODO: Validate

return actionEvent.project?.solution?.riderEfCoreModel!!
}

private fun notifyEfIsNotInstalled(intellijProject: Project) {
NotificationGroupManager.getInstance().getNotificationGroup(KnownNotificationGroups.efCore)
.createNotification(EfCoreUiBundle.message("notification.content.ef.core.tools.are.required.to.execute.this.action"), NotificationType.ERROR)
Expand Down

0 comments on commit 583c809

Please sign in to comment.