Skip to content

Commit da70cfa

Browse files
authored
[PE] fix read context EDT juggling in FlutterInitializer (flutter#8216)
The move to project activities has some nuance. In the long run we'll be able to do some real performance tuning but in the short-run some of our assumptions about being in a read context need to be rethunk. Here's one. The rub is `autoCreateRunConfig` needs to be in a read context but the remaining initialization needs to be on the EDT. Fixes flutter#8215 See also: flutter#8100 --- - [x] I’ve reviewed the contributor guide and applied the relevant portions to this PR. <details> <summary>Contribution guidelines:</summary><br> - See our [contributor guide]([https://github.com/dart-lang/sdk/blob/main/CONTRIBUTING.md](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview) for general expectations for PRs. - Larger or significant changes should be discussed in an issue before creating a PR. - Dart contributions to our repos should follow the [Dart style guide](https://dart.dev/guides/language/effective-dart) and use `dart format`. - Java and Kotlin contributions should strive to follow Java and Kotlin best practices ([discussion](flutter#8098)). </details>
1 parent d15559b commit da70cfa

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

flutter-idea/src/io/flutter/FlutterInitializer.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import com.intellij.openapi.actionSystem.AnAction;
1717
import com.intellij.openapi.actionSystem.AnActionEvent;
1818
import com.intellij.openapi.application.ApplicationManager;
19+
import com.intellij.openapi.application.ModalityState;
20+
import com.intellij.openapi.application.ReadAction;
1921
import com.intellij.openapi.diagnostic.Logger;
2022
import com.intellij.openapi.editor.colors.EditorColorsListener;
2123
import com.intellij.openapi.editor.colors.EditorColorsManager;
@@ -26,12 +28,14 @@
2628
import com.intellij.openapi.project.Project;
2729
import com.intellij.openapi.roots.ProjectRootManager;
2830
import com.intellij.openapi.vfs.VirtualFile;
31+
import com.intellij.util.concurrency.AppExecutorUtil;
2932
import com.intellij.util.messages.MessageBusConnection;
3033
import com.jetbrains.lang.dart.ide.toolingDaemon.DartToolingDaemonService;
3134
import de.roderick.weberknecht.WebSocketException;
3235
import io.flutter.android.IntelliJAndroidSdk;
3336
import io.flutter.bazel.WorkspaceCache;
3437
import io.flutter.dart.DtdUtils;
38+
import io.flutter.dart.FlutterDartAnalysisServer;
3539
import io.flutter.devtools.DevToolsExtensionsViewFactory;
3640
import io.flutter.devtools.DevToolsUtils;
3741
import io.flutter.devtools.RemainingDevToolsViewFactory;
@@ -54,6 +58,7 @@
5458
import io.flutter.view.InspectorViewFactory;
5559
import org.jetbrains.annotations.NotNull;
5660

61+
import java.util.ArrayList;
5762
import java.util.List;
5863
import java.util.concurrent.ExecutionException;
5964
import java.util.concurrent.Executors;
@@ -90,6 +95,9 @@ public void executeProjectStartup(@NotNull Project project) {
9095
// If the project declares a Flutter dependency, do some extra initialization.
9196
boolean hasFlutterModule = false;
9297

98+
99+
var roots = new ArrayList<PubRoot>();
100+
93101
for (Module module : OpenApiUtils.getModules(project)) {
94102
final boolean declaresFlutter = FlutterModuleUtils.declaresFlutter(module);
95103

@@ -108,8 +116,8 @@ public void executeProjectStartup(@NotNull Project project) {
108116
ensureAndroidSdk(project);
109117
}
110118

111-
// Set up a default run configuration for 'main.dart' (if it's not there already and the file exists).
112-
FlutterModuleUtils.autoCreateRunConfig(project, root);
119+
// Collect the root for later processing in a read context.
120+
roots.add(root);
113121

114122
// If there are no open editors, show main.
115123
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
@@ -119,6 +127,27 @@ public void executeProjectStartup(@NotNull Project project) {
119127
}
120128
}
121129

130+
131+
// Lambdas need final vars.
132+
boolean finalHasFlutterModule = hasFlutterModule;
133+
ReadAction.nonBlocking(() -> {
134+
for (var root : roots) {
135+
// Set up a default run configuration for 'main.dart' (if it's not there already and the file exists).
136+
FlutterModuleUtils.autoCreateRunConfig(project, root);
137+
}
138+
return null;
139+
})
140+
.expireWith(FlutterDartAnalysisServer.getInstance(project))
141+
.finishOnUiThread(ModalityState.defaultModalityState(), result -> {
142+
edtInitialization(finalHasFlutterModule, project);
143+
})
144+
.submit(AppExecutorUtil.getAppExecutorService());
145+
}
146+
147+
/***
148+
* Initialization that needs to complete on the EDT thread.
149+
*/
150+
private void edtInitialization(boolean hasFlutterModule, @NotNull Project project) {
122151
if (hasFlutterModule || WorkspaceCache.getInstance(project).isBazel()) {
123152
initializeToolWindows(project);
124153
}

0 commit comments

Comments
 (0)