Skip to content

Do hot reload on auto-save #5774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/io/flutter/run/FlutterReloadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package io.flutter.run;

import com.intellij.AppTopics;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.hint.HintManagerImpl;
import com.intellij.codeInsight.hint.HintUtil;
Expand All @@ -22,11 +23,16 @@
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.ex.AnActionListener;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileDocumentManagerListener;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.util.Computable;
Expand Down Expand Up @@ -54,6 +60,7 @@
import io.flutter.run.common.RunMode;
import io.flutter.run.daemon.FlutterApp;
import io.flutter.settings.FlutterSettings;
import io.flutter.utils.FlutterModuleUtils;
import io.flutter.utils.MostlySilentColoredProcessHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -85,7 +92,6 @@ private static NotificationGroup getNotificationGroup(String toolWindowId) {
}

private final @NotNull Project myProject;
private final FlutterSettings mySettings;

private Notification lastNotification;

Expand All @@ -103,7 +109,6 @@ public static FlutterReloadManager getInstance(@NotNull Project project) {

private FlutterReloadManager(@NotNull Project project) {
this.myProject = project;
this.mySettings = FlutterSettings.getInstance();

final MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(project);
connection.subscribe(AnActionListener.TOPIC, new AnActionListener() {
Expand Down Expand Up @@ -146,10 +151,48 @@ public void afterActionPerformed(@NotNull AnAction action, @NotNull DataContext
}
}
});
connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerListener() {
@Override
public void beforeAllDocumentsSaving() {
if (!FlutterSettings.getInstance().isReloadOnSave()) return;
if (myProject.isDisposed()) return;
if (!FlutterModuleUtils.hasFlutterModule(myProject)) return;
// The "Save files if the IDE is idle ..." option runs whether there are any changes or not.
boolean isModified = false;
for (FileEditor fileEditor : FileEditorManager.getInstance(myProject).getAllEditors()) {
if (fileEditor.isModified()) {
isModified = true;
break;
}
}
if (!isModified) return;

ApplicationManager.getApplication().invokeLater(() -> {
// Find a Dart editor to trigger the reload.
final Editor anEditor = ApplicationManager.getApplication().runReadAction((Computable<Editor>)() -> {
Editor someEditor = null;
for (Editor editor : EditorFactory.getInstance().getAllEditors()) {
if (editor.isDisposed()) continue;
if (editor.getProject() != myProject) continue;
final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(editor.getDocument());
if (psiFile instanceof DartFile && someEditor == null) {
someEditor = editor;
}
if (null != PsiTreeUtil.findChildOfType(psiFile, PsiErrorElement.class, false)) {
// If there are analysis errors we want to silently exit, without showing a notification.
return null;
}
}
return someEditor;
});
handleSaveAllNotification(anEditor);
}, ModalityState.any());
}
});
}

private void handleSaveAllNotification(@Nullable Editor editor) {
if (!mySettings.isReloadOnSave() || editor == null) {
if (!FlutterSettings.getInstance().isReloadOnSave() || editor == null) {
return;
}

Expand Down