Skip to content

Commit db773ae

Browse files
committed
Usable but not stable
1 parent a1037d1 commit db773ae

File tree

2 files changed

+92
-48
lines changed

2 files changed

+92
-48
lines changed

src/io/flutter/editor/FlutterIconLineMarkerProvider.java

+82-38
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.jetbrains.lang.dart.psi.DartType;
4242
import com.jetbrains.lang.dart.psi.DartVarAccessDeclaration;
4343
import com.jetbrains.lang.dart.psi.DartVarInit;
44+
import com.jetbrains.lang.dart.psi.impl.DartCallExpressionImpl;
4445
import com.jetbrains.lang.dart.util.DartPsiImplUtil;
4546
import com.jetbrains.lang.dart.util.DartResolveUtil;
4647
import gnu.trove.THashSet;
@@ -97,14 +98,21 @@ public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
9798
final VirtualFile dir = file.getParent();
9899
if (dir.isInLocalFileSystem()) {
99100
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+
}
100108
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();
103111
break;
104112
}
105113
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();
108116
break;
109117
}
110118
}
@@ -118,17 +126,24 @@ public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
118126
final DartArguments arguments = DartPsiImplUtil.getArguments((DartCallExpression)parent);
119127
if (arguments == null) return null;
120128
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+
//}
125133
final PsiElement fontPackage = getNamedArgumentExpression(arguments, "fontPackage");
126-
if (fontPackage != null) return null; // See previous TODO
134+
//if (fontPackage != null) return null; // See previous TODO
127135
final String argument = getValueOfPositionalArgument(arguments, 0);
128136
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+
}
132147
}
133148
}
134149
else if (parent.getNode().getElementType() == DartTokenTypes.SIMPLE_TYPE) {
@@ -188,6 +203,20 @@ else if (name.equals("CupertinoIcons")) {
188203
return null;
189204
}
190205

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+
191220
private Icon getIconFromCode(@NotNull String value) {
192221
final int code;
193222
try {
@@ -227,7 +256,14 @@ private IconInfo findDefinition(@NotNull String className, @NotNull String iconN
227256
private Icon findIconFromDef(@NotNull String iconClassName, @NotNull IconInfo iconDef, @NotNull String path) {
228257
final VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path);
229258
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+
}
231267
final List<VirtualFile> ttfFiles = new ArrayList<>();
232268
VfsUtilCore.visitChildrenRecursively(parent, new VirtualFileVisitor<Object>() {
233269
@Override
@@ -242,14 +278,15 @@ public boolean visitFile(@NotNull VirtualFile file) {
242278
}
243279
});
244280
double match = -1;
245-
String family = iconDef.familyName;
281+
final String family = iconDef.familyName;
246282
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+
}
253290
}
254291
}
255292
if (bestFileMatch != null) {
@@ -294,16 +331,16 @@ static class IconInfoVisitor extends DartRecursiveVisitor {
294331
this.iconName = iconName;
295332
}
296333

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) {
299336
LOG.info("Check superclass constructor for font family: " + type.getName());
300337
return null; // TODO Check superclass of <type> for a constructor that includes the family.
301338
}
302339
else if (expression instanceof DartStringLiteralExpression) {
303340
final Pair<String, TextRange> pair = DartPsiImplUtil.getUnquotedDartStringAndItsRange(expression.getText().trim());
304341
return pair.first;
305342
}
306-
else if (expression.getNode().getElementType() == DartTokenTypes.REFERENCE_EXPRESSION) {
343+
else if (expression != null && expression.getNode().getElementType() == DartTokenTypes.REFERENCE_EXPRESSION) {
307344
final Pair<String, TextRange> pair = DartPsiImplUtil.getUnquotedDartStringAndItsRange(expression.getText().trim());
308345
final String varName = pair.first;
309346
return staticVars.get(varName);
@@ -316,24 +353,31 @@ public void visitVarAccessDeclaration(@NotNull DartVarAccessDeclaration o) {
316353
if (o.getComponentName().getText().trim().equals(iconName)) {
317354
final DartVarInit init = (DartVarInit)o.getParent().getLastChild();
318355
final DartExpression expression = init.getExpression();
356+
String className = null;
357+
DartArguments arguments = null;
358+
DartType type = null;
319359
if (expression instanceof DartNewExpression) {
320360
final DartNewExpression newExpr = (DartNewExpression)expression;
321-
final DartType type = newExpr.getType();
361+
type = newExpr.getType();
322362
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);
337381
}
338382
}
339383
}

src/io/flutter/font/FontPreviewProcessor.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import com.intellij.openapi.util.io.FileUtil;
1010
import com.intellij.openapi.vfs.LocalFileSystem;
1111
import com.intellij.openapi.vfs.VirtualFile;
12-
import com.intellij.openapi.vfs.ex.temp.TempFileSystem;
13-
import com.intellij.psi.PsiElement;
1412
import com.intellij.psi.PsiFile;
1513
import com.intellij.psi.PsiManager;
1614
import com.intellij.psi.search.FileTypeIndex;
@@ -65,14 +63,18 @@ private boolean wasRunRecently() {
6563
if (delta.toEpochMilli() < 60) {
6664
return true;
6765
}
68-
} catch (DateTimeException ex) {
66+
}
67+
catch (DateTimeException ex) {
6968
// ignored
7069
}
7170
LAST_RUN = current;
7271
return false;
7372
}
7473

7574
private void findFontClasses(@NotNull Project project, @NotNull String packageName) {
75+
if (packageName.isEmpty()) {
76+
return;
77+
}
7678
GlobalSearchScope scope = new ProjectAndLibrariesScope(project);
7779
Collection<VirtualFile> files = DartLibraryIndex.getFilesByLibName(scope, packageName);
7880
if (files.isEmpty()) {
@@ -86,15 +88,15 @@ private void findFontClasses(@NotNull Project project, @NotNull String packageNa
8688
while (index < fileList.size()) {
8789
final VirtualFile file = fileList.get(index++);
8890
final String path = file.getPath();
91+
final int packageIndex = path.indexOf(packageName);
92+
if (packageIndex < 0) continue;
8993
if (ANALYZED_FILES.contains(path)) {
9094
continue;
9195
}
9296
ANALYZED_FILES.add(path);
9397
if (isInSdk(path)) {
9498
continue;
9599
}
96-
final int packageIndex = path.indexOf(packageName);
97-
if (packageIndex < 0) continue;
98100
final VirtualFile filteredFile = filterImports(file);
99101
if (filteredFile == null) {
100102
continue;
@@ -117,8 +119,9 @@ private void findFontClasses(@NotNull Project project, @NotNull String packageNa
117119
final Set<String> knownPaths = FlutterIconLineMarkerProvider.KnownPaths.get(name.getName());
118120
if (knownPaths == null) {
119121
FlutterIconLineMarkerProvider.KnownPaths.put(name.getName(), new THashSet<String>(Collections.singleton(path)));
120-
} else {
121-
knownPaths.add(name.getName());
122+
}
123+
else {
124+
knownPaths.add(path);
122125
}
123126
}
124127
}
@@ -129,9 +132,6 @@ private void findFontClasses(@NotNull Project project, @NotNull String packageNa
129132
// ignored
130133
}
131134
if (size == FlutterIconLineMarkerProvider.KnownPaths.size()) {
132-
if (!file.getPath().equals("/Users/messick/.pub-cache/hosted/pub.dartlang.org/flutter_icons-1.1.0/lib/flutter_icons.dart")) {
133-
continue;
134-
}
135135
try {
136136
final String source = new String(file.contentsToByteArray());
137137
final BufferedReader reader = new BufferedReader(new StringReader(source));

0 commit comments

Comments
 (0)