@@ -74,6 +74,40 @@ public static void initialize() {
74
74
BuiltInPaths .put ("CupertinoIcons" , CupertinoRelativeIconsPath );
75
75
}
76
76
77
+ @ Nullable
78
+ public static Icon getCupertinoIconByName (@ Nullable Project project , @ NotNull String iconName ) {
79
+ if (project == null ) return null ;
80
+ final FlutterSdk sdk = FlutterSdk .getFlutterSdk (project );
81
+ if (sdk == null ) return null ;
82
+ final IconInfo iconDef =
83
+ findStandardDefinition ("CupertinoIcons" , iconName , project , sdk .getHomePath () + BuiltInPaths .get ("CupertinoIcons" ), sdk );
84
+ if (iconDef == null ) return null ;
85
+ final String path = FlutterSdkUtil .getPathToCupertinoIconsPackage (project );
86
+ // <pub_cache>/hosted/pub.dartlang.org/cupertino_icons-v.m.n/assets/CupertinoIcons.ttf
87
+ return findStandardIconFromDef (iconName , iconDef , path + CupertinoRelativeAssetPath );
88
+ }
89
+
90
+ @ Nullable
91
+ public static Icon getMaterialIconByName (@ Nullable Project project , @ NotNull String iconName ) {
92
+ if (project == null ) return null ;
93
+ final FlutterSdk sdk = FlutterSdk .getFlutterSdk (project );
94
+ if (sdk == null ) return null ;
95
+ final IconInfo iconDef =
96
+ findStandardDefinition ("Icons" , iconName , project , sdk .getHomePath () + BuiltInPaths .get ("Icons" ), sdk );
97
+ if (iconDef == null ) return null ;
98
+ // <flutter-sdk>/bin/cache/artifacts/material_fonts/MaterialIcons-Regular.otf
99
+ return findStandardIconFromDef (iconName , iconDef , sdk .getHomePath () + MaterialRelativeAssetPath );
100
+ }
101
+
102
+ @ Nullable
103
+ public static Icon getMaterialIconFromCodepoint (@ Nullable Project project , int codepoint ) {
104
+ if (project == null ) return null ;
105
+ final FlutterSdk sdk = FlutterSdk .getFlutterSdk (project );
106
+ if (sdk == null ) return null ;
107
+ final IconPreviewGenerator generator = new IconPreviewGenerator (sdk .getHomePath () + MaterialRelativeAssetPath );
108
+ return generator .convert (codepoint );
109
+ }
110
+
77
111
@ Nullable ("null means disabled" )
78
112
@ Override
79
113
public @ GutterName String getName () {
@@ -87,9 +121,10 @@ public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
87
121
return sdk == null ? null : getLineMarkerInfo (element , sdk );
88
122
}
89
123
124
+ @ SuppressWarnings ("MissingRecentApi" )
90
125
@ VisibleForTesting
91
126
@ Nullable
92
- LineMarkerInfo <?> getLineMarkerInfo (@ NotNull PsiElement element , @ NotNull FlutterSdk sdk ) {
127
+ static LineMarkerInfo <?> getLineMarkerInfo (@ NotNull PsiElement element , @ NotNull FlutterSdk sdk ) {
93
128
if ((element .getNode () != null ? element .getNode ().getElementType () : null ) != DartTokenTypes .IDENTIFIER ) return null ;
94
129
95
130
final String name = element .getText ();
@@ -146,30 +181,12 @@ LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element, @NotNull Flutte
146
181
assert parentNode != null ;
147
182
if (parentNode .getElementType () == DartTokenTypes .CALL_EXPRESSION ) {
148
183
// Check font family and package
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
- }
184
+ return getIconFromArguments (DartPsiImplUtil .getArguments ((DartCallExpression )parent ), element , sdk );
159
185
}
160
186
else if (parentNode .getElementType () == DartTokenTypes .SIMPLE_TYPE ) {
161
187
parent = getNewExprFromType (parent );
162
188
if (parent == null ) return null ;
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
- }
189
+ return getIconFromArguments (DartPsiImplUtil .getArguments ((DartNewExpression )parent ), element , sdk );
173
190
}
174
191
else {
175
192
final PsiElement idNode = refExpr .getFirstChild ();
@@ -181,17 +198,10 @@ else if (parentNode.getElementType() == DartTokenTypes.SIMPLE_TYPE) {
181
198
final String selector = AstBufferUtil .getTextSkippingWhitespaceComments (selectorNode .getNode ());
182
199
final Icon icon ;
183
200
if (name .equals ("Icons" )) {
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 );
201
+ icon = getMaterialIconByName (element .getProject (), selector );
188
202
}
189
203
else if (name .equals ("CupertinoIcons" )) {
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 );
204
+ icon = getCupertinoIconByName (element .getProject (), selector );
195
205
}
196
206
else {
197
207
// Note: I want to keep this code until I'm sure we won't use pubspec.yaml.
@@ -227,7 +237,24 @@ else if (name.equals("CupertinoIcons")) {
227
237
}
228
238
229
239
@ Nullable
230
- private IconInfo findStandardDefinition (@ NotNull String className , @ NotNull String iconName , @ NotNull Project project , @ Nullable String path , @ NotNull FlutterSdk sdk ) {
240
+ private static LineMarkerInfo <PsiElement > getIconFromArguments (@ Nullable DartArguments arguments ,
241
+ @ NotNull PsiElement element ,
242
+ @ NotNull FlutterSdk sdk ) {
243
+ if (arguments == null ) return null ;
244
+ final String family = getValueOfNamedArgument (arguments , "fontFamily" );
245
+ final PsiElement fontPackage = getNamedArgumentExpression (arguments , "fontPackage" );
246
+ final String argument = getValueOfPositionalArgument (arguments , 0 );
247
+ if (argument == null ) return null ;
248
+ final Icon icon = getIconFromPackage (fontPackage , family , argument , element .getProject (), sdk );
249
+ return icon == null ? null : createLineMarker (element , icon );
250
+ }
251
+
252
+ @ Nullable
253
+ private static IconInfo findStandardDefinition (@ NotNull String className ,
254
+ @ NotNull String iconName ,
255
+ @ NotNull Project project ,
256
+ @ Nullable String path ,
257
+ @ NotNull FlutterSdk sdk ) {
231
258
if (path != null ) {
232
259
return findDefinition (className , iconName , project , path );
233
260
}
@@ -236,7 +263,7 @@ private IconInfo findStandardDefinition(@NotNull String className, @NotNull Stri
236
263
}
237
264
238
265
@ Nullable
239
- private Icon findStandardIconFromDef (@ NotNull String name , @ NotNull IconInfo iconDef , @ NotNull String path ) {
266
+ private static Icon findStandardIconFromDef (@ NotNull String name , @ NotNull IconInfo iconDef , @ NotNull String path ) {
240
267
assert LocalFileSystem .getInstance () != null ;
241
268
final VirtualFile virtualFile = LocalFileSystem .getInstance ().findFileByPath (path );
242
269
if (virtualFile == null ) return null ;
@@ -246,7 +273,11 @@ private Icon findStandardIconFromDef(@NotNull String name, @NotNull IconInfo ico
246
273
247
274
// Note: package flutter_icons is not currently supported because it takes forever to analyze it.
248
275
@ Nullable
249
- private Icon getIconFromPackage (@ Nullable PsiElement aPackage , @ Nullable String family , @ NotNull String argument , @ NotNull Project project , @ NotNull FlutterSdk sdk ) {
276
+ private static Icon getIconFromPackage (@ Nullable PsiElement aPackage ,
277
+ @ Nullable String family ,
278
+ @ NotNull String argument ,
279
+ @ NotNull Project project ,
280
+ @ NotNull FlutterSdk sdk ) {
250
281
final int code ;
251
282
try {
252
283
code = parseLiteralNumber (argument );
@@ -257,16 +288,17 @@ private Icon getIconFromPackage(@Nullable PsiElement aPackage, @Nullable String
257
288
family = family == null ? "MaterialIcons" : family ;
258
289
if (aPackage == null ) {
259
290
// Looking for IconData with no package -- package specification not currently supported.
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 );
291
+ final String assetPath = family .equals ("MaterialIcons" )
292
+ ? sdk .getHomePath () + MaterialRelativeAssetPath
293
+ : FlutterSdkUtil .getPathToCupertinoIconsPackage (project ) + CupertinoRelativeAssetPath ;
294
+ final IconPreviewGenerator generator = new IconPreviewGenerator (assetPath );
263
295
return generator .convert (code );
264
296
}
265
297
return null ;
266
298
}
267
299
268
300
@ Nullable
269
- private LineMarkerInfo <PsiElement > createLineMarker (@ Nullable PsiElement element , @ NotNull Icon icon ) {
301
+ private static LineMarkerInfo <PsiElement > createLineMarker (@ Nullable PsiElement element , @ NotNull Icon icon ) {
270
302
if (element == null ) return null ;
271
303
assert element .getTextRange () != null ;
272
304
//noinspection MissingRecentApi
@@ -275,7 +307,10 @@ private LineMarkerInfo<PsiElement> createLineMarker(@Nullable PsiElement element
275
307
}
276
308
277
309
@ Nullable
278
- private IconInfo findDefinition (@ NotNull String className , @ NotNull String iconName , @ NotNull Project project , @ NotNull String path ) {
310
+ private static IconInfo findDefinition (@ NotNull String className ,
311
+ @ NotNull String iconName ,
312
+ @ NotNull Project project ,
313
+ @ NotNull String path ) {
279
314
assert LocalFileSystem .getInstance () != null ;
280
315
final VirtualFile virtualFile = LocalFileSystem .getInstance ().findFileByPath (path );
281
316
if (virtualFile == null ) return null ;
@@ -289,7 +324,7 @@ private IconInfo findDefinition(@NotNull String className, @NotNull String iconN
289
324
}
290
325
291
326
@ Nullable
292
- private Icon findIconFromDef (@ NotNull String iconClassName , @ NotNull IconInfo iconDef , @ NotNull String path ) {
327
+ private static Icon findIconFromDef (@ NotNull String iconClassName , @ NotNull IconInfo iconDef , @ NotNull String path ) {
293
328
assert LocalFileSystem .getInstance () != null ;
294
329
final VirtualFile virtualFile = LocalFileSystem .getInstance ().findFileByPath (path );
295
330
if (virtualFile == null ) return null ;
@@ -342,7 +377,7 @@ public boolean visitFile(@NotNull VirtualFile file) {
342
377
return null ;
343
378
}
344
379
345
- public double findPattern (@ NotNull String t , @ NotNull String p ) {
380
+ public static double findPattern (@ NotNull String t , @ NotNull String p ) {
346
381
// This is from https://github.com/tdebatty/java-string-similarity
347
382
// It's MIT license file is: https://github.com/tdebatty/java-string-similarity/blob/master/LICENSE.md
348
383
final JaroWinkler jw = new JaroWinkler ();
0 commit comments