Skip to content

Revert "🐛 Resolves the referenced element before parsing colors" #6779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 5 additions & 53 deletions flutter-idea/src/io/flutter/editor/FlutterColorProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import com.intellij.psi.impl.source.tree.AstBufferUtil;
import com.jetbrains.lang.dart.DartLanguage;
import com.jetbrains.lang.dart.DartTokenTypes;
import com.jetbrains.lang.dart.psi.*;
import com.jetbrains.lang.dart.psi.DartArgumentList;
import com.jetbrains.lang.dart.psi.DartArguments;
import com.jetbrains.lang.dart.psi.DartExpression;
import com.jetbrains.lang.dart.psi.DartLiteralExpression;
import io.flutter.FlutterBundle;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -35,6 +38,7 @@ public Color getColorFrom(@NotNull PsiElement element) {
if (element.getNode().getElementType() != DartTokenTypes.IDENTIFIER) return null;

final String name = element.getText();
if (!(name.equals("Colors") || name.equals("CupertinoColors") || name.equals("Color"))) return null;

final PsiElement refExpr = topmostReferenceExpression(element);
if (refExpr == null) return null;
Expand All @@ -58,28 +62,6 @@ else if (parent.getNode().getElementType() == DartTokenTypes.SIMPLE_TYPE) {
return parseColorElements(parent, refExpr);
}
else {
if (parent.getNode().getElementType() == DartTokenTypes.VAR_INIT ||
parent.getNode().getElementType() == DartTokenTypes.FUNCTION_BODY) {
final PsiElement reference = resolveReferencedElement(refExpr);
if (reference != null && reference.getLastChild() != null) {
Color tryParseColor = null;
if (reference instanceof DartCallExpression) {
final DartExpression expression = ((DartCallExpression)reference).getExpression();
if (expression != null && expression.getLastChild() instanceof DartReferenceExpression) {
tryParseColor = parseColorElements(reference, expression);
if (tryParseColor != null) return tryParseColor;
}
}
final PsiElement lastChild = reference.getLastChild();
if (lastChild instanceof DartArguments && reference.getParent() != null) {
tryParseColor = parseColorElements(reference, reference.getParent());
}
else {
tryParseColor = parseColorElements(reference, reference.getLastChild());
}
if (tryParseColor != null) return tryParseColor;
}
}
// name.equals(refExpr.getFirstChild().getText()) -> Colors.blue
final PsiElement idNode = refExpr.getFirstChild();
if (idNode == null) return null;
Expand All @@ -100,36 +82,6 @@ else if (parent.getNode().getElementType() == DartTokenTypes.SIMPLE_TYPE) {
return null;
}

@Nullable
private PsiElement resolveReferencedElement(@NotNull PsiElement element) {
if (element instanceof DartCallExpression && element.getFirstChild().getText().equals("Color")) {
return element;
}
final PsiElement symbol = element.getLastChild();
final PsiElement result;
if (symbol instanceof DartReference) {
result = ((DartReference)symbol).resolve();
}
else if (element instanceof DartReference) {
result = ((DartReference)element).resolve();
}
else {
return null;
}
if (!(result instanceof DartComponentName) || result.getParent() == null) return null;
final PsiElement declaration = result.getParent().getParent();
if (!(declaration instanceof DartVarDeclarationList)) return null;
final PsiElement lastChild = declaration.getLastChild();
if (!(lastChild instanceof DartVarInit)) return null;

final PsiElement effectiveElement = lastChild.getLastChild();
// Recursively determine reference if the initialization is still a `DartReference`.
if (effectiveElement instanceof DartReference && !(effectiveElement instanceof DartCallExpression)) {
return resolveReferencedElement(effectiveElement);
}
return effectiveElement;
}

@Nullable
private Color parseColorElements(@NotNull PsiElement parent, @NotNull PsiElement refExpr) {
final PsiElement selectorNode = refExpr.getLastChild();
Expand Down