Skip to content
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

Migrate the FlutterPubspecNotificationProvider to the new EditorNotificationProvider API #7831

Merged
merged 1 commit into from
Dec 2, 2024
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
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