6
6
package io .flutter .editor ;
7
7
8
8
import com .intellij .openapi .fileEditor .FileEditor ;
9
- import com .intellij .openapi .project .DumbAware ;
10
9
import com .intellij .openapi .project .Project ;
11
- import com .intellij .openapi .util .Key ;
12
10
import com .intellij .openapi .vfs .VirtualFile ;
13
11
import com .intellij .ui .EditorNotificationPanel ;
14
- import com .intellij .ui .EditorNotifications ;
12
+ import com .intellij .ui .EditorNotificationProvider ;
15
13
import com .intellij .ui .HyperlinkLabel ;
16
14
import icons .FlutterIcons ;
17
15
import io .flutter .FlutterUtils ;
23
21
import org .jetbrains .annotations .Nullable ;
24
22
25
23
import javax .swing .*;
24
+ import java .util .function .Function ;
26
25
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 {
39
27
@ Nullable
40
28
@ 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 ) {
44
31
// We only show this notification inside of local pubspec files.
45
32
if (!PubRoot .isPubspec (file ) || !file .isInLocalFileSystem ()) {
46
33
return null ;
@@ -57,22 +44,25 @@ public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file
57
44
return null ;
58
45
}
59
46
60
- if (FlutterSdk .getFlutterSdk (project ) == null ) {
47
+ final FlutterSdk flutterSdk = FlutterSdk .getFlutterSdk (project );
48
+ if (flutterSdk == null ) {
61
49
return null ;
62
50
}
63
51
64
- return new FlutterPubspecActionsPanel (project , file );
52
+ return fileEditor -> new FlutterPubspecActionsPanel (fileEditor , project , flutterSdk );
65
53
}
66
54
67
55
static class FlutterPubspecActionsPanel extends EditorNotificationPanel {
68
- @ NotNull final Project project ;
69
56
@ NotNull final VirtualFile myFile ;
57
+ @ NotNull final Project myProject ;
58
+ @ NotNull final FlutterSdk myFlutterSdk ;
70
59
71
- FlutterPubspecActionsPanel (@ NotNull Project project , @ NotNull VirtualFile file ) {
60
+ FlutterPubspecActionsPanel (@ NotNull FileEditor fileEditor , @ NotNull Project project , @ NotNull FlutterSdk flutterSdk ) {
72
61
super (UIUtils .getEditorNotificationBackgroundColor ());
73
62
74
- this .project = project ;
75
- myFile = file ;
63
+ myFile = fileEditor .getFile ();
64
+ myProject = project ;
65
+ myFlutterSdk = flutterSdk ;
76
66
77
67
icon (FlutterIcons .Flutter );
78
68
text ("Flutter commands" );
@@ -86,44 +76,35 @@ static class FlutterPubspecActionsPanel extends EditorNotificationPanel {
86
76
label .setToolTipText ("Upgrade referenced packages to the latest versions" );
87
77
88
78
// 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 ()) {
91
80
// "flutter.pub.outdated"
92
81
label = createActionLabel ("Pub outdated" , this ::runPubOutdated );
93
82
label .setToolTipText ("Analyze packages to determine which ones can be upgraded" );
94
83
}
95
84
96
- myLinksPanel .add (new JSeparator (SwingConstants .VERTICAL ));
85
+ if (myLinksPanel != null ) {
86
+ myLinksPanel .add (new JSeparator (SwingConstants .VERTICAL ));
87
+ }
97
88
label = createActionLabel ("Flutter doctor" , "flutter.doctor" );
98
89
label .setToolTipText ("Validate installed tools and their versions" );
99
90
}
100
91
101
92
private void runPubGet (boolean upgrade ) {
102
- final FlutterSdk sdk = FlutterSdk .getFlutterSdk (project );
103
- if (sdk == null ) {
104
- return ;
105
- }
106
-
107
93
final PubRoot root = PubRoot .forDirectory (myFile .getParent ());
108
94
if (root != null ) {
109
95
if (!upgrade ) {
110
- sdk .startPubGet (root , project );
96
+ myFlutterSdk .startPubGet (root , myProject );
111
97
}
112
98
else {
113
- sdk .startPubUpgrade (root , project );
99
+ myFlutterSdk .startPubUpgrade (root , myProject );
114
100
}
115
101
}
116
102
}
117
103
118
104
private void runPubOutdated () {
119
- final FlutterSdk sdk = FlutterSdk .getFlutterSdk (project );
120
- if (sdk == null ) {
121
- return ;
122
- }
123
-
124
105
final PubRoot root = PubRoot .forDirectory (myFile .getParent ());
125
106
if (root != null ) {
126
- sdk .startPubOutdated (root , project );
107
+ myFlutterSdk .startPubOutdated (root , myProject );
127
108
}
128
109
}
129
110
}
0 commit comments