41
41
import com .jetbrains .lang .dart .psi .DartType ;
42
42
import com .jetbrains .lang .dart .psi .DartVarAccessDeclaration ;
43
43
import com .jetbrains .lang .dart .psi .DartVarInit ;
44
+ import com .jetbrains .lang .dart .psi .impl .DartCallExpressionImpl ;
44
45
import com .jetbrains .lang .dart .util .DartPsiImplUtil ;
45
46
import com .jetbrains .lang .dart .util .DartResolveUtil ;
46
47
import gnu .trove .THashSet ;
@@ -97,14 +98,21 @@ public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
97
98
final VirtualFile dir = file .getParent ();
98
99
if (dir .isInLocalFileSystem ()) {
99
100
final String path = dir .getPath ();
101
+ String trimmedPath = path ;
102
+ if (!path .endsWith ("lib" )) {
103
+ final int index = path .indexOf ("lib" );
104
+ if (index >= 0 ) {
105
+ trimmedPath = path .substring (0 , index + 3 );
106
+ }
107
+ }
100
108
final Set <String > knownPaths = KnownPaths .get (name );
101
- if (knownPaths .contains (path )) {
102
- knownPath = path ;
109
+ if (knownPaths .contains (path ) || knownPaths . contains ( trimmedPath ) ) {
110
+ knownPath = file . getPath () ;
103
111
break ;
104
112
}
105
113
for (String aPath : knownPaths ) {
106
- if (path .endsWith (aPath ) || aPath .contains (path )) {
107
- knownPath = aPath ;
114
+ if (path .endsWith (aPath ) || aPath .contains (path ) || trimmedPath . endsWith ( aPath ) || aPath . contains ( trimmedPath ) ) {
115
+ knownPath = file . getPath () ;
108
116
break ;
109
117
}
110
118
}
@@ -118,17 +126,24 @@ public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
118
126
final DartArguments arguments = DartPsiImplUtil .getArguments ((DartCallExpression )parent );
119
127
if (arguments == null ) return null ;
120
128
final String family = getValueOfNamedArgument (arguments , "fontFamily" );
121
- if (family != null ) {
122
- // TODO https://github.com/flutter/flutter-intellij/issues/2334
123
- if (!"MaterialIcons" .equals (family )) return null ;
124
- }
129
+ // if (family != null) {
130
+ // // TODO https://github.com/flutter/flutter-intellij/issues/2334
131
+ // if (!"MaterialIcons".equals(family)) return null;
132
+ // }
125
133
final PsiElement fontPackage = getNamedArgumentExpression (arguments , "fontPackage" );
126
- if (fontPackage != null ) return null ; // See previous TODO
134
+ // if (fontPackage != null) return null; // See previous TODO
127
135
final String argument = getValueOfPositionalArgument (arguments , 0 );
128
136
if (argument == null ) return null ;
129
- final Icon icon = getIconFromCode (argument );
130
- if (icon != null ) {
131
- return createLineMarker (element , icon );
137
+ if (family == null || "MaterialIcons" .equals (family )) {
138
+ final Icon icon = getIconFromCode (argument );
139
+ if (icon != null ) {
140
+ return createLineMarker (element , icon );
141
+ }
142
+ } else {
143
+ final Icon icon = getIconFromPackage (fontPackage , family , argument );
144
+ if (icon != null ) {
145
+ return createLineMarker (element , icon );
146
+ }
132
147
}
133
148
}
134
149
else if (parent .getNode ().getElementType () == DartTokenTypes .SIMPLE_TYPE ) {
@@ -188,6 +203,20 @@ else if (name.equals("CupertinoIcons")) {
188
203
return null ;
189
204
}
190
205
206
+ private Icon getIconFromPackage (PsiElement aPackage , String family , String argument ) {
207
+ if (aPackage == null || family == null || argument == null ) {
208
+ return null ;
209
+ }
210
+ final int code ;
211
+ try {
212
+ code = parseLiteralNumber (argument );
213
+ }
214
+ catch (NumberFormatException ignored ) {
215
+ return null ;
216
+ }
217
+ return null ;
218
+ }
219
+
191
220
private Icon getIconFromCode (@ NotNull String value ) {
192
221
final int code ;
193
222
try {
@@ -227,7 +256,14 @@ private IconInfo findDefinition(@NotNull String className, @NotNull String iconN
227
256
private Icon findIconFromDef (@ NotNull String iconClassName , @ NotNull IconInfo iconDef , @ NotNull String path ) {
228
257
final VirtualFile virtualFile = LocalFileSystem .getInstance ().findFileByPath (path );
229
258
if (virtualFile == null ) return null ;
230
- final VirtualFile parent = virtualFile .getParent ();
259
+ VirtualFile parent = virtualFile ;
260
+ while (parent != null && !parent .getName ().equals ("lib" )) {
261
+ parent = parent .getParent ();
262
+ }
263
+ if (parent != null ) parent = parent .getParent (); // We have to search the entire project.
264
+ if (parent == null ) {
265
+ return null ;
266
+ }
231
267
final List <VirtualFile > ttfFiles = new ArrayList <>();
232
268
VfsUtilCore .visitChildrenRecursively (parent , new VirtualFileVisitor <Object >() {
233
269
@ Override
@@ -242,14 +278,15 @@ public boolean visitFile(@NotNull VirtualFile file) {
242
278
}
243
279
});
244
280
double match = -1 ;
245
- String family = iconDef .familyName ;
281
+ final String family = iconDef .familyName ;
246
282
VirtualFile bestFileMatch = null ;
247
- if (family == null ) family = "FontAwesomeSolid" ;
248
- for (VirtualFile file : ttfFiles ) {
249
- final double n = findPattern (file .getNameWithoutExtension (), family );
250
- if (n > match ) {
251
- match = n ;
252
- bestFileMatch = file ;
283
+ if (family != null ) {
284
+ for (VirtualFile file : ttfFiles ) {
285
+ final double n = findPattern (file .getNameWithoutExtension (), family );
286
+ if (n > match ) {
287
+ match = n ;
288
+ bestFileMatch = file ;
289
+ }
253
290
}
254
291
}
255
292
if (bestFileMatch != null ) {
@@ -294,16 +331,16 @@ static class IconInfoVisitor extends DartRecursiveVisitor {
294
331
this .iconName = iconName ;
295
332
}
296
333
297
- private String findFamilyName (@ Nullable PsiElement expression , @ NotNull DartType type ) {
298
- if (expression == null ) {
334
+ private String findFamilyName (@ Nullable PsiElement expression , @ Nullable DartType type ) {
335
+ if (expression == null && type != null ) {
299
336
LOG .info ("Check superclass constructor for font family: " + type .getName ());
300
337
return null ; // TODO Check superclass of <type> for a constructor that includes the family.
301
338
}
302
339
else if (expression instanceof DartStringLiteralExpression ) {
303
340
final Pair <String , TextRange > pair = DartPsiImplUtil .getUnquotedDartStringAndItsRange (expression .getText ().trim ());
304
341
return pair .first ;
305
342
}
306
- else if (expression .getNode ().getElementType () == DartTokenTypes .REFERENCE_EXPRESSION ) {
343
+ else if (expression != null && expression .getNode ().getElementType () == DartTokenTypes .REFERENCE_EXPRESSION ) {
307
344
final Pair <String , TextRange > pair = DartPsiImplUtil .getUnquotedDartStringAndItsRange (expression .getText ().trim ());
308
345
final String varName = pair .first ;
309
346
return staticVars .get (varName );
@@ -316,24 +353,31 @@ public void visitVarAccessDeclaration(@NotNull DartVarAccessDeclaration o) {
316
353
if (o .getComponentName ().getText ().trim ().equals (iconName )) {
317
354
final DartVarInit init = (DartVarInit )o .getParent ().getLastChild ();
318
355
final DartExpression expression = init .getExpression ();
356
+ String className = null ;
357
+ DartArguments arguments = null ;
358
+ DartType type = null ;
319
359
if (expression instanceof DartNewExpression ) {
320
360
final DartNewExpression newExpr = (DartNewExpression )expression ;
321
- final DartType type = newExpr .getType ();
361
+ type = newExpr .getType ();
322
362
if (type != null ) {
323
- final String className = type .getText ();
324
- if (KnownPaths .containsKey (className )) {
325
- final DartArguments arguments = newExpr .getArguments ();
326
- if (arguments != null ) {
327
- final DartArgumentList argumentList = arguments .getArgumentList ();
328
- if (argumentList != null ) {
329
- final List <DartExpression > list = argumentList .getExpressionList ();
330
- if (!list .isEmpty ()) {
331
- final String codepoint = list .get (0 ).getText ();
332
- final PsiElement family = getNamedArgumentExpression (arguments , "fontFamily" );
333
- final String familyName = findFamilyName (family , type );
334
- info = new IconInfo (className , iconName , familyName , codepoint );
335
- }
336
- }
363
+ className = type .getText ();
364
+ arguments = newExpr .getArguments ();
365
+ }
366
+ } else if (expression instanceof DartCallExpression ) {
367
+ final DartCallExpressionImpl callExpr = (DartCallExpressionImpl )expression ;
368
+ arguments = callExpr .getArguments ();
369
+ className = callExpr .getExpression ().getText ();
370
+ }
371
+ if (KnownPaths .containsKey (className )) {
372
+ if (arguments != null ) {
373
+ final DartArgumentList argumentList = arguments .getArgumentList ();
374
+ if (argumentList != null ) {
375
+ final List <DartExpression > list = argumentList .getExpressionList ();
376
+ if (!list .isEmpty ()) {
377
+ final String codepoint = list .get (0 ).getText ();
378
+ final PsiElement family = getNamedArgumentExpression (arguments , "fontFamily" );
379
+ final String familyName = findFamilyName (family , type );
380
+ info = new IconInfo (className , iconName , familyName , codepoint );
337
381
}
338
382
}
339
383
}
0 commit comments