66package io .flutter .editor ;
77
88import com .intellij .openapi .fileEditor .FileEditor ;
9- import com .intellij .openapi .project .DumbAware ;
109import com .intellij .openapi .project .Project ;
11- import com .intellij .openapi .util .Key ;
1210import com .intellij .openapi .vfs .VirtualFile ;
1311import com .intellij .ui .EditorNotificationPanel ;
14- import com .intellij .ui .EditorNotifications ;
12+ import com .intellij .ui .EditorNotificationProvider ;
1513import com .intellij .ui .HyperlinkLabel ;
1614import icons .FlutterIcons ;
1715import io .flutter .FlutterUtils ;
2321import org .jetbrains .annotations .Nullable ;
2422
2523import javax .swing .*;
24+ import java .util .function .Function ;
2625
27- public class FlutterPubspecNotificationProvider extends EditorNotifications .Provider <EditorNotificationPanel > implements DumbAware {
28- private static final Key <EditorNotificationPanel > KEY = Key .create ("flutter.pubspec" );
29-
30- public FlutterPubspecNotificationProvider (@ NotNull Project project ) {
31- }
32-
33- @ NotNull
34- @ Override
35- public Key <EditorNotificationPanel > getKey () {
36- return KEY ;
37- }
38-
26+ public final class FlutterPubspecNotificationProvider implements EditorNotificationProvider {
3927 @ Nullable
4028 @ Override
41- public EditorNotificationPanel createNotificationPanel (@ NotNull VirtualFile file ,
42- @ NotNull FileEditor fileEditor ,
43- @ NotNull Project project ) {
29+ public Function <? super @ NotNull FileEditor , ? extends @ Nullable JComponent > collectNotificationData (@ NotNull Project project ,
30+ @ NotNull VirtualFile file ) {
4431 // We only show this notification inside of local pubspec files.
4532 if (!PubRoot .isPubspec (file ) || !file .isInLocalFileSystem ()) {
4633 return null ;
@@ -57,22 +44,25 @@ public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file
5744 return null ;
5845 }
5946
60- if (FlutterSdk .getFlutterSdk (project ) == null ) {
47+ final FlutterSdk flutterSdk = FlutterSdk .getFlutterSdk (project );
48+ if (flutterSdk == null ) {
6149 return null ;
6250 }
6351
64- return new FlutterPubspecActionsPanel (project , file );
52+ return fileEditor -> new FlutterPubspecActionsPanel (fileEditor , project , flutterSdk );
6553 }
6654
6755 static class FlutterPubspecActionsPanel extends EditorNotificationPanel {
68- @ NotNull final Project project ;
6956 @ NotNull final VirtualFile myFile ;
57+ @ NotNull final Project myProject ;
58+ @ NotNull final FlutterSdk myFlutterSdk ;
7059
71- FlutterPubspecActionsPanel (@ NotNull Project project , @ NotNull VirtualFile file ) {
60+ FlutterPubspecActionsPanel (@ NotNull FileEditor fileEditor , @ NotNull Project project , @ NotNull FlutterSdk flutterSdk ) {
7261 super (UIUtils .getEditorNotificationBackgroundColor ());
7362
74- this .project = project ;
75- myFile = file ;
63+ myFile = fileEditor .getFile ();
64+ myProject = project ;
65+ myFlutterSdk = flutterSdk ;
7666
7767 icon (FlutterIcons .Flutter );
7868 text ("Flutter commands" );
@@ -86,44 +76,35 @@ static class FlutterPubspecActionsPanel extends EditorNotificationPanel {
8676 label .setToolTipText ("Upgrade referenced packages to the latest versions" );
8777
8878 // If the SDK is the right version, add a 'flutter pub outdated' command.
89- final FlutterSdk sdk = FlutterSdk .getFlutterSdk (project );
90- if (sdk != null && sdk .getVersion ().isPubOutdatedSupported ()) {
79+ if (myFlutterSdk .getVersion ().isPubOutdatedSupported ()) {
9180 // "flutter.pub.outdated"
9281 label = createActionLabel ("Pub outdated" , this ::runPubOutdated );
9382 label .setToolTipText ("Analyze packages to determine which ones can be upgraded" );
9483 }
9584
96- myLinksPanel .add (new JSeparator (SwingConstants .VERTICAL ));
85+ if (myLinksPanel != null ) {
86+ myLinksPanel .add (new JSeparator (SwingConstants .VERTICAL ));
87+ }
9788 label = createActionLabel ("Flutter doctor" , "flutter.doctor" );
9889 label .setToolTipText ("Validate installed tools and their versions" );
9990 }
10091
10192 private void runPubGet (boolean upgrade ) {
102- final FlutterSdk sdk = FlutterSdk .getFlutterSdk (project );
103- if (sdk == null ) {
104- return ;
105- }
106-
10793 final PubRoot root = PubRoot .forDirectory (myFile .getParent ());
10894 if (root != null ) {
10995 if (!upgrade ) {
110- sdk .startPubGet (root , project );
96+ myFlutterSdk .startPubGet (root , myProject );
11197 }
11298 else {
113- sdk .startPubUpgrade (root , project );
99+ myFlutterSdk .startPubUpgrade (root , myProject );
114100 }
115101 }
116102 }
117103
118104 private void runPubOutdated () {
119- final FlutterSdk sdk = FlutterSdk .getFlutterSdk (project );
120- if (sdk == null ) {
121- return ;
122- }
123-
124105 final PubRoot root = PubRoot .forDirectory (myFile .getParent ());
125106 if (root != null ) {
126- sdk .startPubOutdated (root , project );
107+ myFlutterSdk .startPubOutdated (root , myProject );
127108 }
128109 }
129110 }
0 commit comments