Skip to content

Commit dab20c0

Browse files
committed
Fixed executing a single selected query (#418)
1 parent aca141d commit dab20c0

File tree

3 files changed

+56
-32
lines changed

3 files changed

+56
-32
lines changed

src/main/com/intellij/lang/jsgraphql/endpoint/ide/startup/GraphQLStartupActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.intellij.lang.jsgraphql.endpoint.ide.startup;
99

1010
import com.intellij.lang.jsgraphql.schema.GraphQLSchemaChangeListener;
11+
import com.intellij.lang.jsgraphql.v1.ide.editor.JSGraphQLQueryContextCaretListener;
1112
import com.intellij.lang.jsgraphql.v1.ide.project.JSGraphQLLanguageUIProjectService;
1213
import com.intellij.openapi.application.ApplicationManager;
1314
import com.intellij.openapi.project.DumbAware;
@@ -32,5 +33,7 @@ public void runActivity(@NotNull Project project) {
3233
}
3334
// startup the UI service
3435
JSGraphQLLanguageUIProjectService.getService(project);
36+
37+
JSGraphQLQueryContextCaretListener.getInstance(project).listen();
3538
}
3639
}

src/main/com/intellij/lang/jsgraphql/frameworks/relay/GraphQLRelayModernEnableStartupActivity.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,75 @@
99

1010
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
1111
import com.intellij.lang.jsgraphql.GraphQLSettings;
12+
import com.intellij.lang.jsgraphql.ide.notifications.GraphQLNotificationUtil;
1213
import com.intellij.notification.Notification;
1314
import com.intellij.notification.NotificationType;
1415
import com.intellij.notification.Notifications;
1516
import com.intellij.openapi.application.ApplicationManager;
17+
import com.intellij.openapi.application.ReadAction;
1618
import com.intellij.openapi.diagnostic.Logger;
19+
import com.intellij.openapi.progress.ProgressManager;
1720
import com.intellij.openapi.project.Project;
1821
import com.intellij.openapi.startup.StartupActivity;
1922
import com.intellij.openapi.vfs.VirtualFile;
2023
import com.intellij.psi.search.FilenameIndex;
2124
import com.intellij.psi.search.GlobalSearchScope;
2225
import com.intellij.ui.EditorNotifications;
26+
import com.intellij.util.concurrency.NonUrgentExecutor;
2327
import org.apache.commons.io.IOUtils;
2428
import org.jetbrains.annotations.NotNull;
2529

2630
import java.io.InputStream;
31+
import java.util.concurrent.atomic.AtomicBoolean;
2732

2833
/**
2934
* Detects Relay Modern projects based on package.json and asks user to enable support for the directives.
3035
*/
3136
public class GraphQLRelayModernEnableStartupActivity implements StartupActivity {
3237

33-
private static final Logger log = Logger.getInstance(GraphQLRelayModernEnableStartupActivity.class);
38+
private static final Logger LOG = Logger.getInstance(GraphQLRelayModernEnableStartupActivity.class);
39+
private final AtomicBoolean isDisplayed = new AtomicBoolean();
3440

3541
@Override
3642
public void runActivity(@NotNull Project project) {
37-
final GraphQLSettings settings = GraphQLSettings.getSettings(project);
38-
if (settings.isEnableRelayModernFrameworkSupport()) {
39-
// already enabled Relay Modern
40-
return;
41-
}
42-
try {
43-
final GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
44-
for (VirtualFile virtualFile : FilenameIndex.getVirtualFilesByName(project, "package.json", scope)) {
45-
if (!virtualFile.isDirectory() && virtualFile.isInLocalFileSystem()) {
46-
try (InputStream inputStream = virtualFile.getInputStream()) {
47-
final String packageJson = IOUtils.toString(inputStream, virtualFile.getCharset());
48-
if (packageJson.contains("\"react-relay\"") || packageJson.contains("\"relay-compiler\"")) {
49-
final Notification enableRelayModern = new Notification("GraphQL", "Relay Modern project detected", "<a href=\"enable\">Enable Relay Modern</a> GraphQL tooling", NotificationType.INFORMATION, (notification, event) -> {
50-
settings.setEnableRelayModernFrameworkSupport(true);
51-
ApplicationManager.getApplication().saveSettings();
52-
notification.expire();
53-
DaemonCodeAnalyzer.getInstance(project).restart();
54-
EditorNotifications.getInstance(project).updateAllNotifications();
55-
});
56-
enableRelayModern.setImportant(true);
57-
Notifications.Bus.notify(enableRelayModern);
58-
break;
43+
ReadAction.nonBlocking(() -> {
44+
final GraphQLSettings settings = GraphQLSettings.getSettings(project);
45+
if (isDisplayed.get() || settings.isEnableRelayModernFrameworkSupport()) {
46+
// already enabled Relay Modern
47+
return;
48+
}
49+
try {
50+
final GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
51+
for (VirtualFile virtualFile : FilenameIndex.getVirtualFilesByName(project, "package.json", scope)) {
52+
ProgressManager.checkCanceled();
53+
if (!virtualFile.isDirectory() && virtualFile.isInLocalFileSystem()) {
54+
try (InputStream inputStream = virtualFile.getInputStream()) {
55+
final String packageJson = IOUtils.toString(inputStream, virtualFile.getCharset());
56+
if (packageJson.contains("\"react-relay\"") || packageJson.contains("\"relay-compiler\"")) {
57+
final Notification enableRelayModern = new Notification(
58+
GraphQLNotificationUtil.NOTIFICATION_GROUP_ID,
59+
"Relay Modern project detected",
60+
"<a href=\"enable\">Enable Relay Modern</a> GraphQL tooling",
61+
NotificationType.INFORMATION,
62+
(notification, event) -> {
63+
settings.setEnableRelayModernFrameworkSupport(true);
64+
ApplicationManager.getApplication().saveSettings();
65+
notification.expire();
66+
DaemonCodeAnalyzer.getInstance(project).restart();
67+
EditorNotifications.getInstance(project).updateAllNotifications();
68+
});
69+
enableRelayModern.setImportant(true);
70+
if (isDisplayed.compareAndSet(false, true)) {
71+
Notifications.Bus.notify(enableRelayModern);
72+
}
73+
break;
74+
}
5975
}
6076
}
6177
}
78+
} catch (Exception e) {
79+
LOG.error("Unable to detect Relay Modern", e);
6280
}
63-
} catch (Exception e) {
64-
log.error("Unable to detect Relay Modern", e);
65-
}
81+
}).inSmartMode(project).submit(NonUrgentExecutor.getInstance());
6682
}
6783
}

src/main/com/intellij/lang/jsgraphql/v1/ide/editor/JSGraphQLQueryContextCaretListener.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
import com.intellij.lang.jsgraphql.psi.GraphQLFile;
1111
import com.intellij.openapi.Disposable;
1212
import com.intellij.openapi.application.ApplicationManager;
13+
import com.intellij.openapi.components.ServiceManager;
1314
import com.intellij.openapi.editor.EditorFactory;
1415
import com.intellij.openapi.editor.event.CaretEvent;
1516
import com.intellij.openapi.editor.event.CaretListener;
1617
import com.intellij.openapi.editor.event.EditorEventMulticaster;
17-
import com.intellij.openapi.project.DumbAware;
1818
import com.intellij.openapi.project.Project;
19-
import com.intellij.openapi.startup.StartupActivity;
2019
import com.intellij.openapi.util.Key;
2120
import com.intellij.psi.PsiDocumentManager;
2221
import com.intellij.psi.PsiFile;
@@ -29,21 +28,27 @@ public class JSGraphQLQueryContextCaretListener implements Disposable {
2928

3029
static final Key<Integer> CARET_OFFSET = Key.create("JSGraphQL.QueryContext.CaretOffset");
3130

31+
private final Project myProject;
32+
3233
public JSGraphQLQueryContextCaretListener(@NotNull Project project) {
33-
listen(project);
34+
myProject = project;
35+
}
36+
37+
public static JSGraphQLQueryContextCaretListener getInstance(@NotNull Project project) {
38+
return ServiceManager.getService(project, JSGraphQLQueryContextCaretListener.class);
3439
}
3540

36-
private void listen(@NotNull Project project) {
41+
public void listen() {
3742
if (ApplicationManager.getApplication().isHeadlessEnvironment()) {
3843
return;
3944
}
4045

4146
final EditorEventMulticaster eventMulticaster = EditorFactory.getInstance().getEventMulticaster();
42-
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
47+
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject);
4348
eventMulticaster.addCaretListener(new CaretListener() {
4449
@Override
4550
public void caretPositionChanged(@NotNull CaretEvent e) {
46-
if (project.isDisposed() || project != e.getEditor().getProject()) {
51+
if (myProject.isDisposed() || myProject != e.getEditor().getProject()) {
4752
return;
4853
}
4954

0 commit comments

Comments
 (0)