|
9 | 9 |
|
10 | 10 | import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; |
11 | 11 | import com.intellij.lang.jsgraphql.GraphQLSettings; |
| 12 | +import com.intellij.lang.jsgraphql.ide.notifications.GraphQLNotificationUtil; |
12 | 13 | import com.intellij.notification.Notification; |
13 | 14 | import com.intellij.notification.NotificationType; |
14 | 15 | import com.intellij.notification.Notifications; |
15 | 16 | import com.intellij.openapi.application.ApplicationManager; |
| 17 | +import com.intellij.openapi.application.ReadAction; |
16 | 18 | import com.intellij.openapi.diagnostic.Logger; |
| 19 | +import com.intellij.openapi.progress.ProgressManager; |
17 | 20 | import com.intellij.openapi.project.Project; |
18 | 21 | import com.intellij.openapi.startup.StartupActivity; |
19 | 22 | import com.intellij.openapi.vfs.VirtualFile; |
20 | 23 | import com.intellij.psi.search.FilenameIndex; |
21 | 24 | import com.intellij.psi.search.GlobalSearchScope; |
22 | 25 | import com.intellij.ui.EditorNotifications; |
| 26 | +import com.intellij.util.concurrency.NonUrgentExecutor; |
23 | 27 | import org.apache.commons.io.IOUtils; |
24 | 28 | import org.jetbrains.annotations.NotNull; |
25 | 29 |
|
26 | 30 | import java.io.InputStream; |
| 31 | +import java.util.concurrent.atomic.AtomicBoolean; |
27 | 32 |
|
28 | 33 | /** |
29 | 34 | * Detects Relay Modern projects based on package.json and asks user to enable support for the directives. |
30 | 35 | */ |
31 | 36 | public class GraphQLRelayModernEnableStartupActivity implements StartupActivity { |
32 | 37 |
|
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(); |
34 | 40 |
|
35 | 41 | @Override |
36 | 42 | 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 | + } |
59 | 75 | } |
60 | 76 | } |
61 | 77 | } |
| 78 | + } catch (Exception e) { |
| 79 | + LOG.error("Unable to detect Relay Modern", e); |
62 | 80 | } |
63 | | - } catch (Exception e) { |
64 | | - log.error("Unable to detect Relay Modern", e); |
65 | | - } |
| 81 | + }).inSmartMode(project).submit(NonUrgentExecutor.getInstance()); |
66 | 82 | } |
67 | 83 | } |
0 commit comments