Skip to content

Commit

Permalink
[IJ/AS Plugin] Add fragment usages when going to fragment declaration (
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD authored Aug 17, 2023
1 parent 5ab77cb commit 7f6f4ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
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

0 comments on commit 7f6f4ad

Please sign in to comment.