Skip to content

Commit d4a4bd2

Browse files
committed
Code cleanup GraphQLConfigManager
1 parent 357c1e4 commit d4a4bd2

File tree

10 files changed

+215
-435
lines changed

10 files changed

+215
-435
lines changed

resources/messages/GraphQLMessages.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ graphql.notification.unable.to.create.file=Unable to create file '{0}' in direct
1212
graphql.notification.invalid.config.file=Invalid config file
1313
graphql.notification.empty.schema.path=Please set a non-empty `schemaPath` field in the config file.
1414
graphql.notification.empty.endpoint.url=Please set a non-empty endpoint `url` field in the config file.
15+
graphql.notification.unable.to.parse.file=Unable to parse {0}
16+
graphql.notification.load.schema.from.endpoint.title=Get GraphQL schema from endpoint now?
17+
graphql.notification.load.schema.from.endpoint.body=Introspect '{0}' to update the local schema file.
18+
graphql.notification.load.schema.from.endpoint.action=Introspect '{0}'
1519

1620
# Introspection
1721
graphql.introspection.missing.data=Expected `data` key to be present in query result.
@@ -20,6 +24,7 @@ graphql.introspection.errors=Introspection query returned errors: {0}
2024

2125
# Progress
2226
graphql.progress.executing.introspection.query=Executing GraphQL introspection query
27+
graphql.progress.configuration.scan=GraphQL configuration scan
2328

2429
# Editor
2530
graphql.line.marker.generate.schema.file=Generate GraphQL SDL schema file

src/main/com/intellij/lang/jsgraphql/GraphQLFileType.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Icon getIcon() {
5656
*
5757
* @return true if the scratch file contains a GraphQL PsiFile
5858
*/
59-
public static boolean isGraphQLScratchFile(Project project, VirtualFile file) {
59+
public static boolean isGraphQLScratchFile(@NotNull Project project, @NotNull VirtualFile file) {
6060
if (ScratchUtil.isScratch(file)) {
6161
final PsiManager psiManager = PsiManager.getInstance(project);
6262
try {
@@ -71,4 +71,12 @@ public static boolean isGraphQLScratchFile(Project project, VirtualFile file) {
7171
return false;
7272
}
7373

74+
public static boolean isGraphQLFile(@NotNull Project project, @Nullable VirtualFile virtualFile) {
75+
if (virtualFile == null) {
76+
return false;
77+
}
78+
79+
return virtualFile.getFileType() == GraphQLFileType.INSTANCE || GraphQLFileType.isGraphQLScratchFile(project, virtualFile);
80+
}
81+
7482
}

src/main/com/intellij/lang/jsgraphql/ide/actions/GraphQLEditConfigAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void actionPerformed(AnActionEvent e) {
7070
// no config associated, ask to create one
7171
String message = "Searched current and parent directories.<br><a href=\"create\">Create .graphqlconfig file</a>";
7272
Notifications.Bus.notify(new Notification("GraphQL", "No .graphqlconfig file found", message, NotificationType.INFORMATION, (notification, event) -> {
73-
final Set<VirtualFile> contentRoots = Optional.ofNullable(configManager.getContentRoots(virtualFile)).orElse(Collections.emptySet());
73+
final Set<VirtualFile> contentRoots = configManager.getContentRoots(virtualFile);
7474
VirtualFile directory = virtualFile.getParent();
7575
assert directory != null;
7676
final List<VirtualFile> configDirectoryCandidates = Lists.newArrayList(directory);

src/main/com/intellij/lang/jsgraphql/ide/project/GraphQLPsiSearchHelper.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.intellij.notification.Notification;
2929
import com.intellij.notification.NotificationType;
3030
import com.intellij.notification.Notifications;
31+
import com.intellij.openapi.Disposable;
3132
import com.intellij.openapi.components.ServiceManager;
3233
import com.intellij.openapi.diagnostic.Logger;
3334
import com.intellij.openapi.extensions.PluginDescriptor;
@@ -62,7 +63,7 @@
6263
/**
6364
* Enables cross-file searches for PSI references
6465
*/
65-
public class GraphQLPsiSearchHelper {
66+
public class GraphQLPsiSearchHelper implements Disposable {
6667

6768
private static final Key<PsiFile> GRAPHQL_BUILT_IN_SCHEMA_PSI_FILE = Key.create("JSGraphQL.built-in.schema.psi-file");
6869
private static final Key<PsiFile> RELAY_MODERN_DIRECTIVES_SCHEMA_PSI_FILE = Key.create("JSGraphQL.relay.modern.directives.schema.psi-file");
@@ -72,7 +73,7 @@ public class GraphQLPsiSearchHelper {
7273
private final Project myProject;
7374
private final Map<String, GlobalSearchScope> fileNameToSchemaScope = Maps.newConcurrentMap();
7475
private final PluginDescriptor pluginDescriptor;
75-
private final GlobalSearchScope myGlobalSearchScope;
76+
private final GlobalSearchScope myGlobalScope;
7677
private final GlobalSearchScope allBuiltInSchemaScopes;
7778
private final GraphQLConfigManager graphQLConfigManager;
7879

@@ -106,8 +107,11 @@ public GraphQLPsiSearchHelper(@NotNull final Project project) {
106107
.union(defaultProjectFileScope);
107108

108109
final FileType[] searchScopeFileTypes = GraphQLFindUsagesUtil.getService().getIncludedFileTypes().toArray(FileType.EMPTY_ARRAY);
109-
myGlobalSearchScope = GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(myProject), searchScopeFileTypes).union(allBuiltInSchemaScopes);
110-
project.getMessageBus().connect().subscribe(PsiManagerImpl.ANY_PSI_CHANGE_TOPIC, new AnyPsiChangeListener() {
110+
myGlobalScope = GlobalSearchScope
111+
.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(myProject), searchScopeFileTypes)
112+
.union(allBuiltInSchemaScopes);
113+
114+
project.getMessageBus().connect(this).subscribe(PsiManagerImpl.ANY_PSI_CHANGE_TOPIC, new AnyPsiChangeListener() {
111115
@Override
112116
public void beforePsiChanged(boolean isPhysical) {
113117
// clear the cache on each PSI change
@@ -135,11 +139,11 @@ public GlobalSearchScope getSchemaScope(@NotNull PsiElement element) {
135139
final NamedScope schemaScope = graphQLConfigManager.getSchemaScope(virtualFile);
136140
if (schemaScope != null) {
137141
final GlobalSearchScope filterSearchScope = GlobalSearchScopesCore.filterScope(myProject, schemaScope);
138-
return myGlobalSearchScope.intersectWith(filterSearchScope.union(allBuiltInSchemaScopes));
142+
return myGlobalScope.intersectWith(filterSearchScope.union(allBuiltInSchemaScopes));
139143
}
140144

141145
// default is entire project limited by relevant file types
142-
return myGlobalSearchScope;
146+
return myGlobalScope;
143147
});
144148
}
145149

@@ -152,7 +156,7 @@ public GlobalSearchScope getUseScope(@NotNull PsiElement element) {
152156
if (element.getContainingFile().getVirtualFile() != null) {
153157
return getSchemaScope(element);
154158
} else {
155-
return myGlobalSearchScope;
159+
return myGlobalScope;
156160
}
157161
}
158162

@@ -424,4 +428,8 @@ public void processAdditionalBuiltInPsiFiles(@NotNull GlobalSearchScope schemaSc
424428
consumer.accept(relayModernDirectivesSchema);
425429
}
426430
}
431+
432+
@Override
433+
public void dispose() {
434+
}
427435
}

0 commit comments

Comments
 (0)