Skip to content

Commit

Permalink
Include the current scratch file while resolving fragment references (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jimkyndemeyer committed Mar 13, 2019
1 parent 4886cf0 commit 126f50f
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.intellij.ide.plugins.PluginManager;
import com.intellij.injected.editor.VirtualFileWindow;
import com.intellij.json.psi.JsonStringLiteral;
import com.intellij.lang.jsgraphql.GraphQLFileType;
import com.intellij.lang.jsgraphql.GraphQLLanguage;
import com.intellij.lang.jsgraphql.GraphQLSettings;
import com.intellij.lang.jsgraphql.ide.project.graphqlconfig.GraphQLConfigManager;
Expand Down Expand Up @@ -159,14 +160,19 @@ public GlobalSearchScope getUseScope(PsiElement element) {
}

/**
* Finds all fragment definition across files in the project
* Finds all fragment definitions inside the scope of the specified element
*
* @param scopedElement the starting point for finding known fragment definitions
* @return a list of known fragment definitions, or an empty list if the index is not yet ready
*/
public List<GraphQLFragmentDefinition> getKnownFragmentDefinitions(PsiElement scopedElement) {
try {
final List<GraphQLFragmentDefinition> fragmentDefinitions = Lists.newArrayList();
GlobalSearchScope schemaScope = getSchemaScope(scopedElement);
if (GraphQLFileType.isGraphQLScratchFile(myProject, getVirtualFile(scopedElement.getContainingFile()))) {
// include the fragments in the currently edited scratch file
schemaScope = schemaScope.union(GlobalSearchScope.fileScope(scopedElement.getContainingFile()));
}
PsiSearchHelper.SERVICE.getInstance(myProject).processElementsWithWord((psiElement, offsetInElement) -> {
if (psiElement.getNode().getElementType() == GraphQLElementTypes.FRAGMENT_KEYWORD) {
final GraphQLFragmentDefinition fragmentDefinition = PsiTreeUtil.getParentOfType(psiElement, GraphQLFragmentDefinition.class);
Expand All @@ -175,7 +181,7 @@ public List<GraphQLFragmentDefinition> getKnownFragmentDefinitions(PsiElement sc
}
}
return true;
}, getSchemaScope(scopedElement), "fragment", UsageSearchContext.IN_CODE, true, true);
}, schemaScope, "fragment", UsageSearchContext.IN_CODE, true, true);
return fragmentDefinitions;
} catch (IndexNotReadyException e) {
// can't search yet (e.g. during project startup)
Expand Down Expand Up @@ -247,6 +253,11 @@ public void visitElement(PsiElement element) {
relayModernDirectivesSchema.accept(builtInFileVisitor);
}

// finally, look in the current scratch file
if (GraphQLFileType.isGraphQLScratchFile(myProject, getVirtualFile(scopedElement.getContainingFile()))) {
scopedElement.getContainingFile().accept(builtInFileVisitor);
}

} catch (IndexNotReadyException e) {
// can't search yet (e.g. during project startup)
}
Expand Down

0 comments on commit 126f50f

Please sign in to comment.