Skip to content

Commit

Permalink
Migrate the FlutterPubspecNotificationProvider to the new EditorNotif…
Browse files Browse the repository at this point in the history
…icationProvider API (#7831)

This is progress on
#7830
  • Loading branch information
jwren authored Dec 2, 2024
1 parent d5bda7f commit 1db2d38
Showing 1 changed file with 21 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,24 +21,13 @@
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.function.Function;

public class FlutterPubspecNotificationProvider extends EditorNotifications.Provider<EditorNotificationPanel> implements DumbAware {
private static final Key<EditorNotificationPanel> KEY = Key.create("flutter.pubspec");

public FlutterPubspecNotificationProvider(@NotNull Project project) {
}

@NotNull
@Override
public Key<EditorNotificationPanel> 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<? super @NotNull FileEditor, ? extends @Nullable JComponent> 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;
Expand All @@ -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");
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 1db2d38

Please sign in to comment.