11
11
import com .google .common .collect .Lists ;
12
12
import com .google .common .collect .Maps ;
13
13
import com .intellij .ide .plugins .PluginManager ;
14
- import com .intellij .lang .injection .InjectedLanguageManager ;
15
14
import com .intellij .lang .jsgraphql .GraphQLLanguage ;
16
- import com .intellij .lang .jsgraphql .ide .injection .GraphQLInjectionIndex ;
17
- import com .intellij .lang .jsgraphql .ide .injection .GraphQLLanguageInjectionUtil ;
18
15
import com .intellij .lang .jsgraphql .ide .references .GraphQLFindUsagesUtil ;
19
16
import com .intellij .lang .jsgraphql .psi .GraphQLElementTypes ;
20
17
import com .intellij .lang .jsgraphql .psi .GraphQLFragmentDefinition ;
42
39
import com .intellij .psi .search .scope .packageSet .NamedScope ;
43
40
import com .intellij .psi .search .scope .packageSet .NamedScopesHolder ;
44
41
import com .intellij .psi .util .PsiTreeUtil ;
45
- import com .intellij .util .indexing .FileBasedIndex ;
46
42
import org .apache .commons .compress .utils .IOUtils ;
47
43
import org .jetbrains .annotations .NotNull ;
48
44
@@ -64,8 +60,6 @@ public class GraphQLPsiSearchHelper {
64
60
65
61
private final static Logger log = Logger .getInstance (GraphQLPsiSearchHelper .class );
66
62
67
- private static final FileType [] FILE_TYPES = GraphQLFindUsagesUtil .INCLUDED_FILE_TYPES .toArray (new FileType [GraphQLFindUsagesUtil .INCLUDED_FILE_TYPES .size ()]);
68
-
69
63
private final Project myProject ;
70
64
private PluginDescriptor pluginDescriptor ;
71
65
private final Map <String , GraphQLFragmentDefinition > fragmentDefinitionsByName = Maps .newConcurrentMap ();
@@ -81,7 +75,8 @@ public GraphQLPsiSearchHelper(@NotNull final Project project) {
81
75
myProject = project ;
82
76
pluginDescriptor = PluginManager .getPlugin (PluginId .getId ("com.intellij.lang.jsgraphql" ));
83
77
builtInSchemaScope = GlobalSearchScope .fileScope (project , getBuiltInSchema ().getVirtualFile ());
84
- searchScope = GlobalSearchScope .getScopeRestrictedByFileTypes (GlobalSearchScope .projectScope (myProject ), FILE_TYPES ).union (builtInSchemaScope );
78
+ final FileType [] searchScopeFileTypes = GraphQLFindUsagesUtil .INCLUDED_FILE_TYPES .toArray (new FileType [GraphQLFindUsagesUtil .INCLUDED_FILE_TYPES .size ()]);
79
+ searchScope = GlobalSearchScope .getScopeRestrictedByFileTypes (GlobalSearchScope .projectScope (myProject ), searchScopeFileTypes ).union (builtInSchemaScope );
85
80
project .getMessageBus ().connect ().subscribe (PsiManagerImpl .ANY_PSI_CHANGE_TOPIC , new AnyPsiChangeListener .Adapter () {
86
81
@ Override
87
82
public void beforePsiChanged (boolean isPhysical ) {
@@ -137,6 +132,7 @@ public GlobalSearchScope getUseScope(PsiElement element) {
137
132
* Finds all fragment definition across files in the project
138
133
*
139
134
* @param scopedElement the starting point for finding known fragment definitions
135
+ *
140
136
* @return a list of known fragment definitions, or an empty list if the index is not yet ready
141
137
*/
142
138
public List <GraphQLFragmentDefinition > getKnownFragmentDefinitions (PsiElement scopedElement ) {
@@ -162,6 +158,7 @@ public List<GraphQLFragmentDefinition> getKnownFragmentDefinitions(PsiElement sc
162
158
* Gets a resolved reference or null if no reference or resolved element is found
163
159
*
164
160
* @param psiElement the element to get a resolved reference for
161
+ *
165
162
* @return the resolved reference, or null if non is available
166
163
*/
167
164
public static GraphQLIdentifier getResolvedReference (GraphQLNamedElement psiElement ) {
@@ -232,45 +229,28 @@ public PsiFile getBuiltInSchema() {
232
229
}
233
230
234
231
/**
235
- * Uses the {@link GraphQLInjectionIndex} to process injected GraphQL PsiFiles
232
+ * Process injected GraphQL PsiFiles
236
233
*
237
234
* @param scopedElement the starting point of the enumeration settings the scopedElement of the processing
235
+ * @param schemaScope the search scope to use for limiting the schema definitions
238
236
* @param consumer a consumer that will be invoked for each injected GraphQL PsiFile
239
237
*/
240
- public void processInjectedGraphQLPsiFiles (PsiElement scopedElement , Consumer <PsiFile > consumer ) {
241
- final PsiManager psiManager = PsiManager .getInstance (scopedElement .getProject ());
242
- final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager .getInstance (scopedElement .getProject ());
243
- FileBasedIndex .getInstance ().getFilesWithKey (GraphQLInjectionIndex .NAME , Collections .singleton (GraphQLInjectionIndex .DATA_KEY ), virtualFile -> {
244
- final PsiFile fileWithInjection = psiManager .findFile (virtualFile );
245
- if (fileWithInjection != null ) {
246
- fileWithInjection .accept (new PsiRecursiveElementVisitor () {
247
- @ Override
248
- public void visitElement (PsiElement element ) {
249
- if (GraphQLLanguageInjectionUtil .isJSGraphQLLanguageInjectionTarget (element )) {
250
- injectedLanguageManager .enumerate (element , (injectedPsi , places ) -> {
251
- consumer .accept (injectedPsi );
252
- });
253
- } else {
254
- // visit deeper until injection found
255
- super .visitElement (element );
256
- }
257
- }
258
- });
259
- }
260
- return true ;
261
- }, getSchemaScope (scopedElement ));
262
-
238
+ public void processInjectedGraphQLPsiFiles (PsiElement scopedElement , GlobalSearchScope schemaScope , Consumer <PsiFile > consumer ) {
239
+ GraphQLInjectionSearchHelper graphQLInjectionSearchHelper = ServiceManager .getService (myProject , GraphQLInjectionSearchHelper .class );
240
+ if (graphQLInjectionSearchHelper != null ) {
241
+ graphQLInjectionSearchHelper .processInjectedGraphQLPsiFiles (scopedElement , schemaScope , consumer );
242
+ }
263
243
}
264
244
265
245
/**
266
246
* Gets the virtual file system path of a PSI file
267
247
*/
268
248
public static String getFileName (PsiFile psiFile ) {
269
249
VirtualFile virtualFile = psiFile .getVirtualFile ();
270
- if (virtualFile == null ) {
250
+ if (virtualFile == null ) {
271
251
virtualFile = psiFile .getOriginalFile ().getVirtualFile ();
272
252
}
273
- if (virtualFile != null ) {
253
+ if (virtualFile != null ) {
274
254
return virtualFile .getPath ();
275
255
}
276
256
return psiFile .getName ();
0 commit comments