Skip to content

DartPsiUtil.java cleanup, nullability, etc #7684

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

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
24 changes: 16 additions & 8 deletions flutter-idea/src/io/flutter/dart/DartPsiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ public static int parseLiteralNumber(@NotNull String val) throws NumberFormatExc
}

@Nullable
public static PsiElement getNewExprFromType(PsiElement element) {
public static PsiElement getNewExprFromType(@NotNull PsiElement element) {
if (element.getNode() == null) return null;
if (element.getNode().getElementType() != DartTokenTypes.SIMPLE_TYPE) return null;
PsiElement parent = element.getParent();
if (parent == null) return null;
parent = parent.getParent();
if (parent == null) return null;
if (parent == null || parent.getNode() == null) return null;
if (parent.getNode().getElementType() != DartTokenTypes.NEW_EXPRESSION) return null;
return parent;
}

@Nullable
public static String getValueOfPositionalArgument(@NotNull DartArguments arguments, int index) {
final DartExpression expression = getPositionalArgument(arguments, index);
if (expression == null) return null;
if (expression == null || expression.getNode() == null) return null;
if (expression.getNode().getElementType() != DartTokenTypes.LITERAL_EXPRESSION) return null;
return expression.getText();
}
Expand All @@ -56,9 +57,12 @@ public static DartExpression getPositionalArgument(@NotNull DartArguments argume
public static String getValueOfNamedArgument(@NotNull DartArguments arguments, @NotNull String name) {
final PsiElement family = getNamedArgumentExpression(arguments, "fontFamily");
if (family != null) {
assert family.getNode() != null;
if (family.getNode().getElementType() == DartTokenTypes.STRING_LITERAL_EXPRESSION) {
assert family.getText() != null;
return DartPsiImplUtil.getUnquotedDartStringAndItsRange(family.getText()).first;
} else {
}
else {
return ""; // Empty string indicates arg was found but value could not be determined easily.
}
}
Expand All @@ -71,10 +75,12 @@ public static PsiElement getNamedArgumentExpression(@NotNull DartArguments argum
if (list == null) return null;
final List<DartNamedArgument> namedArgumentList = list.getNamedArgumentList();
for (DartNamedArgument namedArgument : namedArgumentList) {
assert namedArgument != null;
final DartExpression nameExpression = namedArgument.getParameterReferenceExpression();
assert nameExpression != null;
final PsiElement childId = nameExpression.getFirstChild();
final PsiElement child = nameExpression.getFirstChild();
if (name.equals(child.getText())) {
if (name.equals(child != null ? child.getText() : "")) {
return namedArgument.getExpression();
}
}
Expand All @@ -84,12 +90,14 @@ public static PsiElement getNamedArgumentExpression(@NotNull DartArguments argum
@Nullable
public static PsiElement topmostReferenceExpression(@NotNull PsiElement element) {
final PsiElement id = element.getParent();
if (id == null || id.getNode().getElementType() != DartTokenTypes.ID) return null;
if (id == null || id.getNode() == null || id.getNode().getElementType() != DartTokenTypes.ID) return null;
PsiElement refExpr = id.getParent();
if (refExpr == null || refExpr.getNode().getElementType() != DartTokenTypes.REFERENCE_EXPRESSION) return null;
if (refExpr == null || refExpr.getNode() == null || refExpr.getNode().getElementType() != DartTokenTypes.REFERENCE_EXPRESSION) {
return null;
}

PsiElement parent = refExpr.getParent();
while (parent != null && parent.getNode().getElementType() == DartTokenTypes.REFERENCE_EXPRESSION) {
while (parent != null && parent.getNode() != null && parent.getNode().getElementType() == DartTokenTypes.REFERENCE_EXPRESSION) {
refExpr = parent;
parent = parent.getParent();
}
Expand Down