Skip to content

Commit 7f6f4ad

Browse files
authored
[IJ/AS Plugin] Add fragment usages when going to fragment declaration (#5189)
1 parent 5ab77cb commit 7f6f4ad

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/navigation/GraphQLGotoDeclarationHandler.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class GraphQLGotoDeclarationHandler : GotoDeclarationHandler {
6969
val resolvedElement = sourceElement.parent?.reference?.resolve()
7070
if (resolvedElement != null) {
7171
add(resolvedElement)
72+
} else {
73+
// Special case for Fragment declaration: we switch to the Fragment's usages
74+
if (gqlElement is GraphQLFragmentDefinition) {
75+
addAll(findFragmentSpreads(gqlElement.project) { it.nameIdentifier.reference?.resolve() == gqlElement.nameIdentifier })
76+
}
7277
}
7378

7479
// Add Kotlin definition(s)

intellij-plugin/src/main/kotlin/com/apollographql/ijplugin/navigation/GraphQLNavigation.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.intellij.lang.jsgraphql.psi.GraphQLInlineFragment
1717
import com.intellij.lang.jsgraphql.psi.GraphQLInputObjectTypeDefinition
1818
import com.intellij.lang.jsgraphql.psi.GraphQLInputValueDefinition
1919
import com.intellij.lang.jsgraphql.psi.GraphQLOperationDefinition
20+
import com.intellij.lang.jsgraphql.psi.GraphQLRecursiveVisitor
2021
import com.intellij.lang.jsgraphql.psi.GraphQLSelectionSet
2122
import com.intellij.lang.jsgraphql.psi.GraphQLTypeNameDefinition
2223
import com.intellij.openapi.project.Project
@@ -357,3 +358,19 @@ private fun String.minusOperationTypeSuffix(): String {
357358
else -> this
358359
}
359360
}
361+
362+
fun findFragmentSpreads(project: Project, predicate: (GraphQLFragmentSpread) -> Boolean): List<GraphQLFragmentSpread> {
363+
return FileTypeIndex.getFiles(GraphQLFileType.INSTANCE, GlobalSearchScope.allScope(project)).flatMap { virtualFile ->
364+
val fragmentSpreads = mutableListOf<GraphQLFragmentSpread>()
365+
val visitor = object : GraphQLRecursiveVisitor() {
366+
override fun visitFragmentSpread(o: GraphQLFragmentSpread) {
367+
super.visitFragmentSpread(o)
368+
if (predicate(o)) {
369+
fragmentSpreads += o
370+
}
371+
}
372+
}
373+
PsiManager.getInstance(project).findFile(virtualFile)?.accept(visitor)
374+
fragmentSpreads
375+
}
376+
}

intellij-plugin/src/test/kotlin/com/apollographql/ijplugin/navigation/GraphQLGotoDeclarationHandlerTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class GraphQLGotoDeclarationHandlerTest : ApolloTestCase() {
5858
fromElement = { elementAt<PsiElement>("computerFields")!! },
5959
toFile = "build/generated/source/apollo/main/com/example/generated/fragment/ComputerFields.kt",
6060
toElement = { elementAt<KtClass>("class ComputerFields")!! },
61+
multipleTarget = true
6162
)
6263

6364
@Test

0 commit comments

Comments
 (0)