Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IJ/AS Plugin] Add fragment usages when going to fragment declaration #5189

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class GraphQLGotoDeclarationHandler : GotoDeclarationHandler {
val resolvedElement = sourceElement.parent?.reference?.resolve()
if (resolvedElement != null) {
add(resolvedElement)
} else {
// Special case for Fragment declaration: we switch to the Fragment's usages
if (gqlElement is GraphQLFragmentDefinition) {
addAll(findFragmentSpreads(gqlElement.project) { it.nameIdentifier.reference?.resolve() == gqlElement.nameIdentifier })
}
}

// Add Kotlin definition(s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.intellij.lang.jsgraphql.psi.GraphQLInlineFragment
import com.intellij.lang.jsgraphql.psi.GraphQLInputObjectTypeDefinition
import com.intellij.lang.jsgraphql.psi.GraphQLInputValueDefinition
import com.intellij.lang.jsgraphql.psi.GraphQLOperationDefinition
import com.intellij.lang.jsgraphql.psi.GraphQLRecursiveVisitor
import com.intellij.lang.jsgraphql.psi.GraphQLSelectionSet
import com.intellij.lang.jsgraphql.psi.GraphQLTypeNameDefinition
import com.intellij.openapi.project.Project
Expand Down Expand Up @@ -357,3 +358,19 @@ private fun String.minusOperationTypeSuffix(): String {
else -> this
}
}

fun findFragmentSpreads(project: Project, predicate: (GraphQLFragmentSpread) -> Boolean): List<GraphQLFragmentSpread> {
return FileTypeIndex.getFiles(GraphQLFileType.INSTANCE, GlobalSearchScope.allScope(project)).flatMap { virtualFile ->
val fragmentSpreads = mutableListOf<GraphQLFragmentSpread>()
val visitor = object : GraphQLRecursiveVisitor() {
override fun visitFragmentSpread(o: GraphQLFragmentSpread) {
super.visitFragmentSpread(o)
if (predicate(o)) {
fragmentSpreads += o
}
}
}
PsiManager.getInstance(project).findFile(virtualFile)?.accept(visitor)
fragmentSpreads
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class GraphQLGotoDeclarationHandlerTest : ApolloTestCase() {
fromElement = { elementAt<PsiElement>("computerFields")!! },
toFile = "build/generated/source/apollo/main/com/example/generated/fragment/ComputerFields.kt",
toElement = { elementAt<KtClass>("class ComputerFields")!! },
multipleTarget = true
)

@Test
Expand Down
Loading