42
42
import org .jetbrains .yaml .psi .YamlRecursivePsiElementVisitor ;
43
43
44
44
import javax .swing .*;
45
- import java .io .FileNotFoundException ;
46
45
import java .util .*;
47
46
48
47
import static io .flutter .dart .DartPsiUtil .*;
@@ -75,36 +74,6 @@ public static void initialize() {
75
74
BuiltInPaths .put ("CupertinoIcons" , CupertinoRelativeIconsPath );
76
75
}
77
76
78
- @ Nullable
79
- public static Icon getCupertinoIconByName (@ Nullable Project project , @ NotNull String sdkHomePath , @ NotNull String iconName ) {
80
- if (project == null ) return null ;
81
- final IconInfo iconDef =
82
- findStandardDefinition ("CupertinoIcons" , iconName , project , sdkHomePath + BuiltInPaths .get ("CupertinoIcons" ), sdkHomePath );
83
- if (iconDef == null ) return null ;
84
- final String path = FlutterSdkUtil .getPathToCupertinoIconsPackage (project );
85
- // <pub_cache>/hosted/pub.dartlang.org/cupertino_icons-v.m.n/assets/CupertinoIcons.ttf
86
- return findStandardIconFromDef (iconName , iconDef , path + CupertinoRelativeAssetPath );
87
- }
88
-
89
- @ Nullable
90
- public static Icon getMaterialIconByName (@ Nullable Project project , @ NotNull String sdkHomePath , @ NotNull String iconName ) {
91
- if (project == null ) return null ;
92
- final IconInfo iconDef =
93
- findStandardDefinition ("Icons" , iconName , project , sdkHomePath + BuiltInPaths .get ("Icons" ), sdkHomePath );
94
- if (iconDef == null ) return null ;
95
- // <flutter-sdk>/bin/cache/artifacts/material_fonts/MaterialIcons-Regular.otf
96
- return findStandardIconFromDef (iconName , iconDef , sdkHomePath + MaterialRelativeAssetPath );
97
- }
98
-
99
- @ Nullable
100
- public static Icon getMaterialIconFromCodepoint (@ Nullable Project project , int codepoint ) {
101
- if (project == null ) return null ;
102
- final FlutterSdk sdk = FlutterSdk .getFlutterSdk (project );
103
- if (sdk == null ) return null ;
104
- final IconPreviewGenerator generator = new IconPreviewGenerator (sdk .getHomePath () + MaterialRelativeAssetPath );
105
- return generator .convert (codepoint );
106
- }
107
-
108
77
@ Nullable ("null means disabled" )
109
78
@ Override
110
79
public @ GutterName String getName () {
@@ -118,10 +87,9 @@ public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
118
87
return sdk == null ? null : getLineMarkerInfo (element , sdk );
119
88
}
120
89
121
- @ SuppressWarnings ("MissingRecentApi" )
122
90
@ VisibleForTesting
123
91
@ Nullable
124
- static LineMarkerInfo <?> getLineMarkerInfo (@ NotNull PsiElement element , @ NotNull FlutterSdk sdk ) {
92
+ LineMarkerInfo <?> getLineMarkerInfo (@ NotNull PsiElement element , @ NotNull FlutterSdk sdk ) {
125
93
if ((element .getNode () != null ? element .getNode ().getElementType () : null ) != DartTokenTypes .IDENTIFIER ) return null ;
126
94
127
95
final String name = element .getText ();
@@ -178,12 +146,30 @@ static LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element, @NotNull
178
146
assert parentNode != null ;
179
147
if (parentNode .getElementType () == DartTokenTypes .CALL_EXPRESSION ) {
180
148
// Check font family and package
181
- return getIconFromArguments (DartPsiImplUtil .getArguments ((DartCallExpression )parent ), element , sdk );
149
+ final DartArguments arguments = DartPsiImplUtil .getArguments ((DartCallExpression )parent );
150
+ if (arguments == null ) return null ;
151
+ final String family = getValueOfNamedArgument (arguments , "fontFamily" );
152
+ final PsiElement fontPackage = getNamedArgumentExpression (arguments , "fontPackage" );
153
+ final String argument = getValueOfPositionalArgument (arguments , 0 );
154
+ if (argument == null ) return null ;
155
+ final Icon icon = getIconFromPackage (fontPackage , family , argument , element .getProject (), sdk );
156
+ if (icon != null ) {
157
+ return createLineMarker (element , icon );
158
+ }
182
159
}
183
160
else if (parentNode .getElementType () == DartTokenTypes .SIMPLE_TYPE ) {
184
161
parent = getNewExprFromType (parent );
185
162
if (parent == null ) return null ;
186
- return getIconFromArguments (DartPsiImplUtil .getArguments ((DartNewExpression )parent ), element , sdk );
163
+ final DartArguments arguments = DartPsiImplUtil .getArguments ((DartNewExpression )parent );
164
+ if (arguments == null ) return null ;
165
+ final String family = getValueOfNamedArgument (arguments , "fontFamily" );
166
+ final PsiElement fontPackage = getNamedArgumentExpression (arguments , "fontPackage" );
167
+ final String argument = getValueOfPositionalArgument (arguments , 0 );
168
+ if (argument == null ) return null ;
169
+ final Icon icon = getIconFromPackage (fontPackage , family , argument , element .getProject (), sdk );
170
+ if (icon != null ) {
171
+ return createLineMarker (element , icon );
172
+ }
187
173
}
188
174
else {
189
175
final PsiElement idNode = refExpr .getFirstChild ();
@@ -195,10 +181,17 @@ else if (parentNode.getElementType() == DartTokenTypes.SIMPLE_TYPE) {
195
181
final String selector = AstBufferUtil .getTextSkippingWhitespaceComments (selectorNode .getNode ());
196
182
final Icon icon ;
197
183
if (name .equals ("Icons" )) {
198
- icon = getMaterialIconByName (element .getProject (), sdk .getHomePath (), selector );
184
+ final IconInfo iconDef = findStandardDefinition (name , selector , element .getProject (), knownPath , sdk );
185
+ if (iconDef == null ) return null ;
186
+ // <flutter-sdk>/bin/cache/artifacts/material_fonts/MaterialIcons-Regular.otf
187
+ icon = findStandardIconFromDef (name , iconDef , sdk .getHomePath () + MaterialRelativeAssetPath );
199
188
}
200
189
else if (name .equals ("CupertinoIcons" )) {
201
- icon = getCupertinoIconByName (element .getProject (), sdk .getHomePath (), selector );
190
+ final IconInfo iconDef = findStandardDefinition (name , selector , element .getProject (), knownPath , sdk );
191
+ if (iconDef == null ) return null ;
192
+ final String path = FlutterSdkUtil .getPathToCupertinoIconsPackage (element .getProject ());
193
+ // <pub_cache>/hosted/pub.dartlang.org/cupertino_icons-v.m.n/assets/CupertinoIcons.ttf
194
+ icon = findStandardIconFromDef (name , iconDef , path + CupertinoRelativeAssetPath );
202
195
}
203
196
else {
204
197
// Note: I want to keep this code until I'm sure we won't use pubspec.yaml.
@@ -234,33 +227,16 @@ else if (name.equals("CupertinoIcons")) {
234
227
}
235
228
236
229
@ Nullable
237
- private static LineMarkerInfo <PsiElement > getIconFromArguments (@ Nullable DartArguments arguments ,
238
- @ NotNull PsiElement element ,
239
- @ NotNull FlutterSdk sdk ) {
240
- if (arguments == null ) return null ;
241
- final String family = getValueOfNamedArgument (arguments , "fontFamily" );
242
- final PsiElement fontPackage = getNamedArgumentExpression (arguments , "fontPackage" );
243
- final String argument = getValueOfPositionalArgument (arguments , 0 );
244
- if (argument == null ) return null ;
245
- final Icon icon = getIconFromPackage (fontPackage , family , argument , element .getProject (), sdk );
246
- return icon == null ? null : createLineMarker (element , icon );
247
- }
248
-
249
- @ Nullable
250
- private static IconInfo findStandardDefinition (@ NotNull String className ,
251
- @ NotNull String iconName ,
252
- @ NotNull Project project ,
253
- @ Nullable String path ,
254
- @ NotNull String sdkHomePath ) {
230
+ private IconInfo findStandardDefinition (@ NotNull String className , @ NotNull String iconName , @ NotNull Project project , @ Nullable String path , @ NotNull FlutterSdk sdk ) {
255
231
if (path != null ) {
256
232
return findDefinition (className , iconName , project , path );
257
233
}
258
234
assert Objects .requireNonNull (ApplicationManager .getApplication ()).isUnitTestMode ();
259
- return findDefinition (className , iconName , project , sdkHomePath + BuiltInPaths .get (className ));
235
+ return findDefinition (className , iconName , project , sdk . getHomePath () + BuiltInPaths .get (className ));
260
236
}
261
237
262
238
@ Nullable
263
- private static Icon findStandardIconFromDef (@ NotNull String name , @ NotNull IconInfo iconDef , @ NotNull String path ) {
239
+ private Icon findStandardIconFromDef (@ NotNull String name , @ NotNull IconInfo iconDef , @ NotNull String path ) {
264
240
assert LocalFileSystem .getInstance () != null ;
265
241
final VirtualFile virtualFile = LocalFileSystem .getInstance ().findFileByPath (path );
266
242
if (virtualFile == null ) return null ;
@@ -270,11 +246,7 @@ private static Icon findStandardIconFromDef(@NotNull String name, @NotNull IconI
270
246
271
247
// Note: package flutter_icons is not currently supported because it takes forever to analyze it.
272
248
@ Nullable
273
- private static Icon getIconFromPackage (@ Nullable PsiElement aPackage ,
274
- @ Nullable String family ,
275
- @ NotNull String argument ,
276
- @ NotNull Project project ,
277
- @ NotNull FlutterSdk sdk ) {
249
+ private Icon getIconFromPackage (@ Nullable PsiElement aPackage , @ Nullable String family , @ NotNull String argument , @ NotNull Project project , @ NotNull FlutterSdk sdk ) {
278
250
final int code ;
279
251
try {
280
252
code = parseLiteralNumber (argument );
@@ -285,17 +257,16 @@ private static Icon getIconFromPackage(@Nullable PsiElement aPackage,
285
257
family = family == null ? "MaterialIcons" : family ;
286
258
if (aPackage == null ) {
287
259
// Looking for IconData with no package -- package specification not currently supported.
288
- final String assetPath = family .equals ("MaterialIcons" )
289
- ? sdk .getHomePath () + MaterialRelativeAssetPath
290
- : FlutterSdkUtil .getPathToCupertinoIconsPackage (project ) + CupertinoRelativeAssetPath ;
291
- final IconPreviewGenerator generator = new IconPreviewGenerator (assetPath );
260
+ final String relativeAssetPath = family .equals ("MaterialIcons" ) ? MaterialRelativeAssetPath : CupertinoRelativeAssetPath ;
261
+ // TODO Base path is wrong for cupertino -- is there a test for this branch (IconData with cupertino family)?
262
+ final IconPreviewGenerator generator = new IconPreviewGenerator (sdk .getHomePath () + relativeAssetPath );
292
263
return generator .convert (code );
293
264
}
294
265
return null ;
295
266
}
296
267
297
268
@ Nullable
298
- private static LineMarkerInfo <PsiElement > createLineMarker (@ Nullable PsiElement element , @ NotNull Icon icon ) {
269
+ private LineMarkerInfo <PsiElement > createLineMarker (@ Nullable PsiElement element , @ NotNull Icon icon ) {
299
270
if (element == null ) return null ;
300
271
assert element .getTextRange () != null ;
301
272
//noinspection MissingRecentApi
@@ -304,27 +275,24 @@ private static LineMarkerInfo<PsiElement> createLineMarker(@Nullable PsiElement
304
275
}
305
276
306
277
@ Nullable
307
- private static IconInfo findDefinition (@ NotNull String className ,
308
- @ NotNull String iconName ,
309
- @ NotNull Project project ,
310
- @ NotNull String path ) {
278
+ private IconInfo findDefinition (@ NotNull String className , @ NotNull String iconName , @ NotNull Project project , @ NotNull String path ) {
311
279
assert LocalFileSystem .getInstance () != null ;
312
280
final VirtualFile virtualFile = LocalFileSystem .getInstance ().findFileByPath (path );
313
- if (virtualFile == null ) throw new Error ( "FILE NOT FOUND: " + path ); // return null;
281
+ if (virtualFile == null ) return null ;
314
282
final PsiFile psiFile = PsiManager .getInstance (project ).findFile (virtualFile );
315
283
if (psiFile == null ) {
316
- throw new Error ( "CANNOT CREATE PSI FILE" ); // return null;
284
+ return null ;
317
285
}
318
286
final IconInfoVisitor visitor = new IconInfoVisitor (iconName );
319
287
psiFile .accept (visitor );
320
288
return visitor .info ;
321
289
}
322
290
323
291
@ Nullable
324
- private static Icon findIconFromDef (@ NotNull String iconClassName , @ NotNull IconInfo iconDef , @ NotNull String path ) {
292
+ private Icon findIconFromDef (@ NotNull String iconClassName , @ NotNull IconInfo iconDef , @ NotNull String path ) {
325
293
assert LocalFileSystem .getInstance () != null ;
326
294
final VirtualFile virtualFile = LocalFileSystem .getInstance ().findFileByPath (path );
327
- if (virtualFile == null ) throw new Error ( "FILE NOT FOUND: " + path ); // return null;
295
+ if (virtualFile == null ) return null ;
328
296
VirtualFile parent = virtualFile ;
329
297
while (parent != null && !parent .getName ().equals ("lib" )) {
330
298
parent = parent .getParent ();
@@ -374,7 +342,7 @@ public boolean visitFile(@NotNull VirtualFile file) {
374
342
return null ;
375
343
}
376
344
377
- public static double findPattern (@ NotNull String t , @ NotNull String p ) {
345
+ public double findPattern (@ NotNull String t , @ NotNull String p ) {
378
346
// This is from https://github.com/tdebatty/java-string-similarity
379
347
// It's MIT license file is: https://github.com/tdebatty/java-string-similarity/blob/master/LICENSE.md
380
348
final JaroWinkler jw = new JaroWinkler ();
0 commit comments