From 06291b33c790471559b132a29d4911b05df1b458 Mon Sep 17 00:00:00 2001 From: Jaime Wren Date: Wed, 27 Nov 2024 11:43:52 -0800 Subject: [PATCH] Migrate the FlutterPubspecNotificationProvider to the new EditorNotificationProvider API This is progress on https://github.com/flutter/flutter-intellij/issues/7830 --- .../FlutterPubspecNotificationProvider.java | 61 +++++++------------ 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/flutter-idea/src/io/flutter/editor/FlutterPubspecNotificationProvider.java b/flutter-idea/src/io/flutter/editor/FlutterPubspecNotificationProvider.java index da38a6f7ae..38ab324439 100644 --- a/flutter-idea/src/io/flutter/editor/FlutterPubspecNotificationProvider.java +++ b/flutter-idea/src/io/flutter/editor/FlutterPubspecNotificationProvider.java @@ -6,12 +6,10 @@ package io.flutter.editor; import com.intellij.openapi.fileEditor.FileEditor; -import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Key; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.ui.EditorNotificationPanel; -import com.intellij.ui.EditorNotifications; +import com.intellij.ui.EditorNotificationProvider; import com.intellij.ui.HyperlinkLabel; import icons.FlutterIcons; import io.flutter.FlutterUtils; @@ -23,24 +21,13 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.util.function.Function; -public class FlutterPubspecNotificationProvider extends EditorNotifications.Provider implements DumbAware { - private static final Key KEY = Key.create("flutter.pubspec"); - - public FlutterPubspecNotificationProvider(@NotNull Project project) { - } - - @NotNull - @Override - public Key getKey() { - return KEY; - } - +public final class FlutterPubspecNotificationProvider implements EditorNotificationProvider { @Nullable @Override - public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, - @NotNull FileEditor fileEditor, - @NotNull Project project) { + public Function collectNotificationData(@NotNull Project project, + @NotNull VirtualFile file) { // We only show this notification inside of local pubspec files. if (!PubRoot.isPubspec(file) || !file.isInLocalFileSystem()) { return null; @@ -57,22 +44,25 @@ public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file return null; } - if (FlutterSdk.getFlutterSdk(project) == null) { + final FlutterSdk flutterSdk = FlutterSdk.getFlutterSdk(project); + if (flutterSdk == null) { return null; } - return new FlutterPubspecActionsPanel(project, file); + return fileEditor -> new FlutterPubspecActionsPanel(fileEditor, project, flutterSdk); } static class FlutterPubspecActionsPanel extends EditorNotificationPanel { - @NotNull final Project project; @NotNull final VirtualFile myFile; + @NotNull final Project myProject; + @NotNull final FlutterSdk myFlutterSdk; - FlutterPubspecActionsPanel(@NotNull Project project, @NotNull VirtualFile file) { + FlutterPubspecActionsPanel(@NotNull FileEditor fileEditor, @NotNull Project project, @NotNull FlutterSdk flutterSdk) { super(UIUtils.getEditorNotificationBackgroundColor()); - this.project = project; - myFile = file; + myFile = fileEditor.getFile(); + myProject = project; + myFlutterSdk = flutterSdk; icon(FlutterIcons.Flutter); text("Flutter commands"); @@ -86,44 +76,35 @@ static class FlutterPubspecActionsPanel extends EditorNotificationPanel { label.setToolTipText("Upgrade referenced packages to the latest versions"); // If the SDK is the right version, add a 'flutter pub outdated' command. - final FlutterSdk sdk = FlutterSdk.getFlutterSdk(project); - if (sdk != null && sdk.getVersion().isPubOutdatedSupported()) { + if (myFlutterSdk.getVersion().isPubOutdatedSupported()) { // "flutter.pub.outdated" label = createActionLabel("Pub outdated", this::runPubOutdated); label.setToolTipText("Analyze packages to determine which ones can be upgraded"); } - myLinksPanel.add(new JSeparator(SwingConstants.VERTICAL)); + if (myLinksPanel != null) { + myLinksPanel.add(new JSeparator(SwingConstants.VERTICAL)); + } label = createActionLabel("Flutter doctor", "flutter.doctor"); label.setToolTipText("Validate installed tools and their versions"); } private void runPubGet(boolean upgrade) { - final FlutterSdk sdk = FlutterSdk.getFlutterSdk(project); - if (sdk == null) { - return; - } - final PubRoot root = PubRoot.forDirectory(myFile.getParent()); if (root != null) { if (!upgrade) { - sdk.startPubGet(root, project); + myFlutterSdk.startPubGet(root, myProject); } else { - sdk.startPubUpgrade(root, project); + myFlutterSdk.startPubUpgrade(root, myProject); } } } private void runPubOutdated() { - final FlutterSdk sdk = FlutterSdk.getFlutterSdk(project); - if (sdk == null) { - return; - } - final PubRoot root = PubRoot.forDirectory(myFile.getParent()); if (root != null) { - sdk.startPubOutdated(root, project); + myFlutterSdk.startPubOutdated(root, myProject); } } }