|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Jim Kynde Meyer |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +package com.intellij.lang.jsgraphql.ide.project; |
| 9 | + |
| 10 | + |
| 11 | +import com.google.common.collect.Lists; |
| 12 | +import com.google.common.collect.Maps; |
| 13 | +import com.intellij.lang.jsgraphql.JSGraphQLTokenTypes; |
| 14 | +import com.intellij.lang.jsgraphql.ide.findUsages.JSGraphQLFindUsagesUtil; |
| 15 | +import com.intellij.lang.jsgraphql.psi.JSGraphQLFragmentDefinitionPsiElement; |
| 16 | +import com.intellij.lang.jsgraphql.psi.JSGraphQLNamedTypePsiElement; |
| 17 | +import com.intellij.openapi.components.ServiceManager; |
| 18 | +import com.intellij.openapi.fileTypes.FileType; |
| 19 | +import com.intellij.openapi.project.IndexNotReadyException; |
| 20 | +import com.intellij.openapi.project.Project; |
| 21 | +import com.intellij.openapi.util.Ref; |
| 22 | +import com.intellij.psi.impl.AnyPsiChangeListener; |
| 23 | +import com.intellij.psi.impl.PsiManagerImpl; |
| 24 | +import com.intellij.psi.search.GlobalSearchScope; |
| 25 | +import com.intellij.psi.search.PsiSearchHelper; |
| 26 | +import com.intellij.psi.search.UsageSearchContext; |
| 27 | +import org.jetbrains.annotations.NotNull; |
| 28 | + |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +/** |
| 34 | + * Enables cross-file searches for PSI references and fragment completion |
| 35 | + */ |
| 36 | +public class JSGraphQLPsiSearchHelper { |
| 37 | + |
| 38 | + private static final FileType[] FILE_TYPES = JSGraphQLFindUsagesUtil.INCLUDED_FILE_TYPES.toArray(new FileType[JSGraphQLFindUsagesUtil.INCLUDED_FILE_TYPES.size()]); |
| 39 | + |
| 40 | + private final Project myProject; |
| 41 | + private final Map<String, JSGraphQLNamedTypePsiElement> fragmentDefinitionsByName = Maps.newConcurrentMap(); |
| 42 | + private final GlobalSearchScope searchScope; |
| 43 | + |
| 44 | + public static JSGraphQLPsiSearchHelper getService(@NotNull Project project) { |
| 45 | + return ServiceManager.getService(project, JSGraphQLPsiSearchHelper.class); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public JSGraphQLPsiSearchHelper(@NotNull final Project project) { |
| 50 | + myProject = project; |
| 51 | + searchScope = GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(myProject), FILE_TYPES); |
| 52 | + project.getMessageBus().connect().subscribe(PsiManagerImpl.ANY_PSI_CHANGE_TOPIC, new AnyPsiChangeListener.Adapter() { |
| 53 | + @Override |
| 54 | + public void beforePsiChanged(boolean isPhysical) { |
| 55 | + // clear the cache on each PSI change |
| 56 | + fragmentDefinitionsByName.clear(); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets the fragment name element that is the source of a fragment usage by searching across files |
| 63 | + * |
| 64 | + * @param fragmentUsage a specific fragment usage, e.g. '...FragmentName' |
| 65 | + * @return the fragment definition that the usage references, e.g. 'fragment FragmentName' |
| 66 | + */ |
| 67 | + public JSGraphQLNamedTypePsiElement resolveFragmentReference(@NotNull JSGraphQLNamedTypePsiElement fragmentUsage) { |
| 68 | + final String fragmentName = fragmentUsage.getName(); |
| 69 | + if (fragmentName != null) { |
| 70 | + final JSGraphQLNamedTypePsiElement cachedResult = fragmentDefinitionsByName.get(fragmentName); |
| 71 | + if (cachedResult != null) { |
| 72 | + return cachedResult; |
| 73 | + } |
| 74 | + final Ref<JSGraphQLNamedTypePsiElement> fragmentDefinitionRef = new Ref<>(); |
| 75 | + try { |
| 76 | + PsiSearchHelper.SERVICE.getInstance(myProject).processElementsWithWord((element, offsetInElement) -> { |
| 77 | + if (element instanceof JSGraphQLNamedTypePsiElement && element.getParent() instanceof JSGraphQLFragmentDefinitionPsiElement) { |
| 78 | + if (!element.equals(fragmentUsage)) { |
| 79 | + // only consider as a reference if the element is not the usage element |
| 80 | + final JSGraphQLNamedTypePsiElement fragmentDefinition = (JSGraphQLNamedTypePsiElement) element; |
| 81 | + fragmentDefinitionsByName.put(fragmentName, fragmentDefinition); |
| 82 | + fragmentDefinitionRef.set(fragmentDefinition); |
| 83 | + } |
| 84 | + return false; |
| 85 | + } |
| 86 | + return true; |
| 87 | + }, searchScope, fragmentName, UsageSearchContext.IN_CODE, true, true); |
| 88 | + } catch (IndexNotReadyException e) { |
| 89 | + // can't search yet (e.g. during project startup) |
| 90 | + } |
| 91 | + return fragmentDefinitionRef.get(); |
| 92 | + } |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Finds all fragment definition across files in the project |
| 98 | + * |
| 99 | + * @return a list of known fragment definitions, or an empty list if the index is not yet ready |
| 100 | + */ |
| 101 | + public List<JSGraphQLFragmentDefinitionPsiElement> getKnownFragmentDefinitions() { |
| 102 | + try { |
| 103 | + final List<JSGraphQLFragmentDefinitionPsiElement> fragmentDefinitions = Lists.newArrayList(); |
| 104 | + PsiSearchHelper.SERVICE.getInstance(myProject).processElementsWithWord((psiElement, offsetInElement) -> { |
| 105 | + if (psiElement.getNode().getElementType() == JSGraphQLTokenTypes.KEYWORD && psiElement.getParent() instanceof JSGraphQLFragmentDefinitionPsiElement) { |
| 106 | + final JSGraphQLFragmentDefinitionPsiElement fragmentDefinition = (JSGraphQLFragmentDefinitionPsiElement) psiElement.getParent(); |
| 107 | + final String fragmentName = fragmentDefinition.getName(); |
| 108 | + if (fragmentName != null) { |
| 109 | + fragmentDefinitions.add(fragmentDefinition); |
| 110 | + } |
| 111 | + } |
| 112 | + return true; |
| 113 | + }, searchScope, "fragment", UsageSearchContext.IN_CODE, true, true); |
| 114 | + return fragmentDefinitions; |
| 115 | + } catch (IndexNotReadyException e) { |
| 116 | + // can't search yet (e.g. during project startup) |
| 117 | + } |
| 118 | + return Collections.emptyList(); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments