Skip to content

Commit

Permalink
adding logic for objectLiterals in type resolve from usage
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Aug 11, 2024
1 parent 6924f66 commit 86ba9de
Showing 1 changed file with 83 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@


import static com.intellij.plugins.haxe.model.type.HaxeExpressionEvaluatorHandlers.*;
import static com.intellij.plugins.haxe.model.type.HaxeExpressionEvaluatorHandlers.handleWithRecursionGuard;

@CustomLog
public class HaxeExpressionEvaluator {
Expand Down Expand Up @@ -607,6 +608,15 @@ private static ResultHolder checkSearchResult(HaxeExpressionEvaluatorContext con
}
}
}
if (expression.getParent() instanceof HaxeObjectLiteralElement literalElement) {
HaxeObjectLiteral objectLiteral = PsiTreeUtil.getParentOfType(literalElement, HaxeObjectLiteral.class);
if(objectLiteral != null) {
ResultHolder objectLiteralType = findObjectLiteralType(context, resolver, objectLiteral);
if (objectLiteralType != null && !objectLiteralType.isUnknown()) {
return objectLiteralType;
}
}
}
}
if (reference instanceof HaxeReferenceExpression referenceExpression) {
// reference is callExpression
Expand Down Expand Up @@ -663,17 +673,31 @@ private static ResultHolder checkSearchResult(HaxeExpressionEvaluatorContext con
return null;
}

private static @Nullable ResultHolder findUsageAsParameterInFunctionCall(HaxeReferenceExpression referenceExpression,
private static @Nullable ResultHolder findUsageAsParameterInFunctionCall(HaxeExpression referenceExpression,
HaxeCallExpression callExpression,
HaxeCallExpressionList list,
SpecificFunctionReference functionReference) {
int index = -1;
if (list != null) index = list.getExpressionList().indexOf(referenceExpression);
if (index == -1) return null;
HaxeCallExpressionUtil.CallExpressionValidation validation =
HaxeCallExpressionUtil.checkFunctionCall(callExpression, functionReference);

if (validation.isStaticExtension()) index++;
return validation.getParameterIndexToType().getOrDefault(index, null);
}
private static @Nullable ResultHolder findUsageAsParameterInFunctionCall(HaxeExpression referenceExpression,
HaxeCallExpression callExpression,
HaxeCallExpressionList list,
PsiElement resolved) {
int index = -1;
if (list != null) index = list.getExpressionList().indexOf(referenceExpression);
if (index > -1 && resolved instanceof HaxeMethod method) {
if (index == -1) return null;

if (resolved instanceof HaxeMethod method) {
HaxeCallExpressionUtil.CallExpressionValidation validation = HaxeCallExpressionUtil.checkMethodCall(callExpression, method);
if (validation.isStaticExtension()) index++;
ResultHolder paramType = validation.getParameterIndexToType().getOrDefault(index, null);
if (paramType != null) return paramType;
return validation.getParameterIndexToType().getOrDefault(index, null);
}
return null;
}
Expand Down Expand Up @@ -809,8 +833,63 @@ public static ResultHolder searchReferencesForTypeParameters(final HaxePsiField
}
}
}
if(expression.getParent() instanceof HaxeObjectLiteralElement literalElement) {
HaxeObjectLiteral objectLiteral = PsiTreeUtil.getParentOfType(literalElement, HaxeObjectLiteral.class);
if(objectLiteral == null) continue;

ResultHolder objectLiteralType = findObjectLiteralType(context, resolver, objectLiteral);

if (objectLiteralType != null && !objectLiteralType.isUnknown()) {
SpecificHaxeClassReference typeFromUsage = objectLiteralType.getClassType();
if (typeFromUsage != null && typeFromUsage.getHaxeClassModel() != null) {
HaxeBaseMemberModel objectLiteralElementAsMember = typeFromUsage.getHaxeClassModel()
.getMember(literalElement.getName(), typeFromUsage.getGenericResolver());

if (objectLiteralElementAsMember != null) {
ResultHolder objectLiteralElementType = objectLiteralElementAsMember.getResultType(resolver);
if (objectLiteralElementType.getClassType() != null) {
@NotNull ResultHolder[] specifics = objectLiteralElementType.getClassType().getSpecifics();

for (int i = 0; i < type.getSpecifics().length; i++) {
if (type.getSpecifics()[i].isUnknown()) {
type.getSpecifics()[i] = specifics[i];
}
else {
ResultHolder unified = HaxeTypeUnifier.unify(specifics[i], type.getSpecifics()[i]);
type.getSpecifics()[i] = unified;
}
}
}
}
}
}
}
}
}
return resultHolder;
}

private static @Nullable ResultHolder findObjectLiteralType(HaxeExpressionEvaluatorContext context,
HaxeGenericResolver resolver,
HaxeObjectLiteral objectLiteral) {
ResultHolder objectLiteralType = null;

// we need to find where the literal is used to find correct type
if (objectLiteral.getParent() instanceof HaxeAssignExpression assignExpression) {
objectLiteralType = handleWithRecursionGuard(assignExpression.getLeftExpression(), context, resolver);

}
if (objectLiteral.getParent().getParent() instanceof HaxeCallExpression callExpression) {
if (callExpression.getExpression() instanceof HaxeReference callExpressionReference) {
PsiElement resolve = callExpressionReference.resolve();
ResultHolder holder = handleWithRecursionGuard(resolve, context, resolver);
if (holder != null && holder.getFunctionType() != null) {
SpecificFunctionReference functionCall = holder.getFunctionType();
HaxeCallExpressionList list = callExpression.getExpressionList();
objectLiteralType = findUsageAsParameterInFunctionCall(objectLiteral, callExpression, list, functionCall);
}
}
}
return objectLiteralType;
}
}

0 comments on commit 86ba9de

Please sign in to comment.