5
5
*/
6
6
package io .flutter .run ;
7
7
8
+ import com .intellij .AppTopics ;
8
9
import com .intellij .codeInsight .hint .HintManager ;
9
10
import com .intellij .codeInsight .hint .HintManagerImpl ;
10
11
import com .intellij .codeInsight .hint .HintUtil ;
22
23
import com .intellij .openapi .actionSystem .DataContext ;
23
24
import com .intellij .openapi .actionSystem .ex .AnActionListener ;
24
25
import com .intellij .openapi .application .ApplicationManager ;
26
+ import com .intellij .openapi .application .ModalityState ;
25
27
import com .intellij .openapi .components .ServiceManager ;
26
28
import com .intellij .openapi .diagnostic .Logger ;
27
29
import com .intellij .openapi .editor .Document ;
28
30
import com .intellij .openapi .editor .Editor ;
31
+ import com .intellij .openapi .editor .EditorFactory ;
29
32
import com .intellij .openapi .fileEditor .FileDocumentManager ;
33
+ import com .intellij .openapi .fileEditor .FileDocumentManagerListener ;
34
+ import com .intellij .openapi .fileEditor .FileEditor ;
35
+ import com .intellij .openapi .fileEditor .FileEditorManager ;
30
36
import com .intellij .openapi .project .Project ;
31
37
import com .intellij .openapi .ui .popup .Balloon ;
32
38
import com .intellij .openapi .util .Computable ;
54
60
import io .flutter .run .common .RunMode ;
55
61
import io .flutter .run .daemon .FlutterApp ;
56
62
import io .flutter .settings .FlutterSettings ;
63
+ import io .flutter .utils .FlutterModuleUtils ;
57
64
import io .flutter .utils .MostlySilentColoredProcessHandler ;
58
65
import org .jetbrains .annotations .NotNull ;
59
66
import org .jetbrains .annotations .Nullable ;
@@ -85,7 +92,6 @@ private static NotificationGroup getNotificationGroup(String toolWindowId) {
85
92
}
86
93
87
94
private final @ NotNull Project myProject ;
88
- private final FlutterSettings mySettings ;
89
95
90
96
private Notification lastNotification ;
91
97
@@ -103,7 +109,6 @@ public static FlutterReloadManager getInstance(@NotNull Project project) {
103
109
104
110
private FlutterReloadManager (@ NotNull Project project ) {
105
111
this .myProject = project ;
106
- this .mySettings = FlutterSettings .getInstance ();
107
112
108
113
final MessageBusConnection connection = ApplicationManager .getApplication ().getMessageBus ().connect (project );
109
114
connection .subscribe (AnActionListener .TOPIC , new AnActionListener () {
@@ -146,10 +151,48 @@ public void afterActionPerformed(@NotNull AnAction action, @NotNull DataContext
146
151
}
147
152
}
148
153
});
154
+ connection .subscribe (AppTopics .FILE_DOCUMENT_SYNC , new FileDocumentManagerListener () {
155
+ @ Override
156
+ public void beforeAllDocumentsSaving () {
157
+ if (!FlutterSettings .getInstance ().isReloadOnSave ()) return ;
158
+ if (myProject .isDisposed ()) return ;
159
+ if (!FlutterModuleUtils .hasFlutterModule (myProject )) return ;
160
+ // The "Save files if the IDE is idle ..." option runs whether there are any changes or not.
161
+ boolean isModified = false ;
162
+ for (FileEditor fileEditor : FileEditorManager .getInstance (myProject ).getAllEditors ()) {
163
+ if (fileEditor .isModified ()) {
164
+ isModified = true ;
165
+ break ;
166
+ }
167
+ }
168
+ if (!isModified ) return ;
169
+
170
+ ApplicationManager .getApplication ().invokeLater (() -> {
171
+ // Find a Dart editor to trigger the reload.
172
+ final Editor anEditor = ApplicationManager .getApplication ().runReadAction ((Computable <Editor >)() -> {
173
+ Editor someEditor = null ;
174
+ for (Editor editor : EditorFactory .getInstance ().getAllEditors ()) {
175
+ if (editor .isDisposed ()) continue ;
176
+ if (editor .getProject () != myProject ) continue ;
177
+ final PsiFile psiFile = PsiDocumentManager .getInstance (myProject ).getPsiFile (editor .getDocument ());
178
+ if (psiFile instanceof DartFile && someEditor == null ) {
179
+ someEditor = editor ;
180
+ }
181
+ if (null != PsiTreeUtil .findChildOfType (psiFile , PsiErrorElement .class , false )) {
182
+ // If there are analysis errors we want to silently exit, without showing a notification.
183
+ return null ;
184
+ }
185
+ }
186
+ return someEditor ;
187
+ });
188
+ handleSaveAllNotification (anEditor );
189
+ }, ModalityState .any ());
190
+ }
191
+ });
149
192
}
150
193
151
194
private void handleSaveAllNotification (@ Nullable Editor editor ) {
152
- if (!mySettings .isReloadOnSave () || editor == null ) {
195
+ if (!FlutterSettings . getInstance () .isReloadOnSave () || editor == null ) {
153
196
return ;
154
197
}
155
198
0 commit comments