Skip to content

Commit

Permalink
try to reuse FieldModels instead of creating new ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Oct 30, 2023
1 parent bb53015 commit e8596a7
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -736,18 +736,21 @@ else if (resolvedExpression instanceof HaxeMethodDeclaration methodDeclaration)
return HaxeTypeResolver.getEnumReturnType(valueDeclaration, referenceExpression.resolveHaxeClass().getGenericResolver());
}
}

// if expression is enumValue we need to resolve the underlying enumType type to test assignment
if (expressionType != null && expressionType.getType() instanceof SpecificEnumValueReference type) {
SpecificHaxeClassReference enumType = type.getEnumClass();
expressionType = enumType.createHolder();
}

// anything else is resolved here (literals etc.)
if (expressionType == null) {
HaxeExpressionEvaluatorContext context = new HaxeExpressionEvaluatorContext(argument);
HaxeGenericResolver genericResolver = HaxeGenericResolverUtil.generateResolverFromScopeParents(argument);
expressionType = HaxeExpressionEvaluator.evaluate(argument, context, genericResolver.withoutUnknowns()).result;
}

// if expression is enumValue we need to resolve the underlying enumType type to test assignment
if (expressionType != null && expressionType.getType() instanceof SpecificEnumValueReference type) {
SpecificHaxeClassReference enumType = type.getEnumClass();
expressionType = enumType.createHolder();
}


return expressionType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ else if (fieldResultClassOnly.isPresent()){
.create();
}

HaxeFieldModel model = new HaxeFieldModel(fieldDeclaration);
HaxeFieldModel model = (HaxeFieldModel) fieldDeclaration.getModel();
HaxeGenericResolver classFieldResolver = HaxeGenericResolverUtil.generateResolverFromScopeParents(model.getBasePsi());
HaxeGenericResolver interfaceFieldResolver = HaxeGenericResolverUtil.generateResolverFromScopeParents(intField.getBasePsi());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
}

public static void check(final HaxeFieldDeclaration var, final AnnotationHolder holder) {
HaxeFieldModel field = new HaxeFieldModel(var);
HaxeFieldModel field = (HaxeFieldModel)var.getModel();
if (field.isProperty()) {
checkProperty(field, holder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public CreateGetterSetterFix(final HaxeClass haxeClass, Strategy strategy) {

@Override
protected String buildFunctionsText(HaxeNamedComponent namedComponent) {
if (!(namedComponent instanceof HaxeFieldDeclaration)) {
if (!(namedComponent instanceof HaxeFieldDeclaration fieldDeclaration)) {
return "";
}

HaxeFieldModel field = new HaxeFieldModel((HaxeFieldDeclaration)namedComponent);
HaxeFieldModel field = (HaxeFieldModel)fieldDeclaration.getModel();
final StringBuilder result = new StringBuilder();
if (myStratagy == Strategy.GETTER || myStratagy == Strategy.GETTERSETTER) {
HaxeNamedComponent getterMethod = myHaxeClass.findHaxeMethodByName(HaxePresentableUtil.getterName(field.getName()), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void collectFromElement(@NotNull PsiElement element, @NotNull InlayTreeSi
private static void handleFieldDeclarationHints(@NotNull PsiElement element,
@NotNull InlayTreeSink sink,
HaxeFieldDeclaration fieldDeclaration) {
HaxeFieldModel field = new HaxeFieldModel(fieldDeclaration);
HaxeFieldModel field = (HaxeFieldModel)fieldDeclaration.getModel();

if (!field.hasTypeTag()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ public static HaxeResolveResult create(@Nullable HaxeClass aClass, HaxeGenericSp
// could be due to cache for Psi resolve while it looks like resolve by QName does not and searches file tree.
HaxeClass superclass = null;
PsiElement resolve = haxeType.getReferenceExpression().resolve();
if (resolve instanceof HaxeClassDeclaration classDeclaration) {
superclass = classDeclaration.getModel().haxeClass;
if (resolve instanceof HaxeClass haxeClass) {
superclass = haxeClass.getModel().haxeClass;
}

if (superclass == null) {
// if reference resolve fails do QName resolve as fallback
// if reference resolve fails, do QName resolve as fallback
superclass = HaxeResolveUtil.tryResolveClassByQName(haxeType);
}
// hopefully it won't be necessary to traverse the entire type hierarchy and we only need to check classes and interfaces that are generic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ private List<? extends PsiElement> checkIsAccessor(HaxeReference reference) {
final HaxeFieldDeclaration varDeclaration = PsiTreeUtil.getParentOfType(reference, HaxeFieldDeclaration.class);
if (varDeclaration == null) return null;

final HaxeFieldModel fieldModel = new HaxeFieldModel(varDeclaration);
final HaxeFieldModel fieldModel = (HaxeFieldModel)varDeclaration.getModel();
final HaxeMethodModel method = accessorType == HaxeAccessorType.GET ? fieldModel.getGetterMethod() : fieldModel.getSetterMethod();

if (method != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.LogLevel;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.model.HaxeEnumValueModel;
import com.intellij.plugins.haxe.model.HaxeFieldModel;
import com.intellij.plugins.haxe.model.HaxeModel;
import com.intellij.plugins.haxe.model.*;
import com.intellij.plugins.haxe.util.HaxeAbstractEnumUtil;

import com.intellij.plugins.haxe.util.UsefulPsiTreeUtil;
Expand Down Expand Up @@ -56,15 +54,19 @@ public HaxePsiFieldImpl(ASTNode node) {
super(node);
}

private HaxeMemberModel _model = null;
@Override
public HaxeModel getModel() {
if (this instanceof HaxeEnumValueDeclaration) {
return new HaxeEnumValueModel((HaxeEnumValueDeclaration)this);
}
if (HaxeAbstractEnumUtil.isAbstractEnum(getContainingClass()) && HaxeAbstractEnumUtil.couldBeAbstractEnumField(this)) {
return new HaxeEnumValueModel((HaxeFieldDeclaration)this);
public HaxeMemberModel getModel() {
if (_model == null) {
if (this instanceof HaxeEnumValueDeclaration enumValueDeclaration) {
_model = new HaxeEnumValueModel(enumValueDeclaration);
}else if (HaxeAbstractEnumUtil.isAbstractEnum(getContainingClass()) && HaxeAbstractEnumUtil.couldBeAbstractEnumField(this)) {
_model = new HaxeEnumValueModel((HaxeFieldDeclaration)this);
}else{
_model = new HaxeFieldModel(this);
}
}
return new HaxeFieldModel(this);
return _model;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private static ConstantClass innerClassifier(HaxeExpression expr) {
}
if (resolved instanceof HaxeFieldDeclaration) {
HaxeFieldDeclaration fieldDeclaration = (HaxeFieldDeclaration)resolved;
HaxeFieldModel fieldModel = new HaxeFieldModel(fieldDeclaration);
HaxeFieldModel fieldModel = (HaxeFieldModel)fieldDeclaration.getModel();
return classifyConstantExpression(fieldModel.getInitializerExpression());
}
return resolved instanceof HaxeExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private List<HaxeFieldModel> getFieldsFromBody(HaxeAnonymousTypeBody body) {
List<HaxeFieldModel> list = new ArrayList<>();
List<HaxePsiField> children = PsiTreeUtil.getChildrenOfAnyType(body, HaxeFieldDeclaration.class, HaxeAnonymousTypeField.class, HaxeEnumValueDeclaration.class);
for (HaxePsiField field : children) {
HaxeFieldModel model = new HaxeFieldModel(field);
HaxeFieldModel model = (HaxeFieldModel)field.getModel();
list.add(model);
}
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.jetbrains.annotations.Nullable;

public abstract class HaxeBaseMemberModel implements HaxeModel {
protected static final Key<HaxeClassModel> DECLARING_CLASS_MODEL_KEY = new Key<>("HAXE_DECLARING_CLASS_MODEL");
protected PsiElement basePsi;
protected HaxeDocumentModel document = null;

Expand All @@ -43,23 +42,18 @@ public HaxeBaseMemberModel(PsiElement basePsi) {

static private HaxeNamedComponent getNamedComponentPsi(PsiElement element) {
if (element == null) return null;
if (element instanceof HaxeNamedComponent) return (HaxeNamedComponent)element;
if (element.getParent() instanceof HaxeNamedComponent) return (HaxeNamedComponent)element.getParent();
if (element instanceof HaxeNamedComponent namedComponent) return namedComponent;
if (element.getParent() instanceof HaxeNamedComponent parentNamedComponent) return parentNamedComponent;
return getNamedComponentPsi(UsefulPsiTreeUtil.getChild(element, HaxeNamedComponent.class));
}

public static HaxeBaseMemberModel fromPsi(PsiElement element) {
if (element instanceof HaxeMethod) return ((HaxeMethod)element).getModel();
if (element instanceof HaxeFieldDeclaration) {
PsiClass containingClass = ((HaxeFieldDeclaration)element).getContainingClass();
if (HaxeAbstractEnumUtil.isAbstractEnum(containingClass) && HaxeAbstractEnumUtil.couldBeAbstractEnumField(element)) {
return new HaxeEnumValueModel((HaxeFieldDeclaration)element);
}
return new HaxeFieldModel((HaxeFieldDeclaration)element);
}
if (element instanceof HaxeEnumValueDeclaration) return new HaxeEnumValueModel((HaxeEnumValueDeclaration)element);
if (element instanceof HaxeLocalVarDeclaration) return new HaxeLocalVarModel((HaxeLocalVarDeclaration)element);
if (element instanceof HaxeAnonymousTypeField) return new HaxeAnonymousTypeFieldModel((HaxeAnonymousTypeField)element);
if (element instanceof HaxeMethod method) return method.getModel();
if (element instanceof HaxeFieldDeclaration fieldDeclaration) return (HaxeBaseMemberModel)fieldDeclaration.getModel();
if (element instanceof HaxeEnumValueDeclaration enumValueDeclaration) return (HaxeBaseMemberModel) enumValueDeclaration.getModel();
if (element instanceof HaxeLocalVarDeclaration varDeclaration) return (HaxeBaseMemberModel) varDeclaration.getModel();
if (element instanceof HaxeAnonymousTypeField anonymousTypeField) return (HaxeBaseMemberModel) anonymousTypeField.getModel();

if (element instanceof HaxeParameter) return new HaxeParameterModel((HaxeParameter)element);
if (element instanceof HaxeForStatement) return null;
final PsiElement parent = element.getParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public List<HaxeMemberModel> getMembersSelf() {
public HaxeFieldModel getField(String name, @Nullable HaxeGenericResolver resolver) {
HaxePsiField field = (HaxePsiField)haxeClass.findHaxeFieldByName(name, resolver);
if (field instanceof HaxeFieldDeclaration || field instanceof HaxeAnonymousTypeField || field instanceof HaxeEnumValueDeclaration) {
return new HaxeFieldModel(field);
return (HaxeFieldModel)field.getModel();
}
return null;
}
Expand Down Expand Up @@ -625,7 +625,7 @@ public List<HaxeFieldModel> getFields() {
List<HaxeFieldModel> list = new ArrayList<>();
List<HaxePsiField> children = PsiTreeUtil.getChildrenOfAnyType(body, HaxeFieldDeclaration.class, HaxeAnonymousTypeField.class, HaxeEnumValueDeclaration.class);
for (HaxePsiField field : children) {
HaxeFieldModel model = new HaxeFieldModel(field);
HaxeFieldModel model = (HaxeFieldModel)field.getModel();
list.add(model);
}
return list;
Expand Down Expand Up @@ -835,10 +835,9 @@ public List<HaxeModel> getExposedMembers() {
if (body != null) {
for (HaxeNamedComponent declaration : PsiTreeUtil.getChildrenOfAnyType(body, HaxeFieldDeclaration.class, HaxeMethod.class)) {
if (!(declaration instanceof PsiMember)) continue;
if (declaration instanceof HaxeFieldDeclaration) {
HaxeFieldDeclaration varDeclaration = (HaxeFieldDeclaration)declaration;
if (declaration instanceof HaxeFieldDeclaration varDeclaration) {
if (varDeclaration.isPublic() && varDeclaration.isStatic()) {
out.add(new HaxeFieldModel((HaxeFieldDeclaration)declaration));
out.add(varDeclaration.getModel());
}
} else {
HaxeMethodDeclaration method = (HaxeMethodDeclaration)declaration;
Expand All @@ -853,7 +852,7 @@ public List<HaxeModel> getExposedMembers() {
if (body != null) {
List<HaxeEnumValueDeclaration> declarations = body.getEnumValueDeclarationList();
for (HaxeEnumValueDeclaration declaration : declarations) {
out.add(new HaxeFieldModel(declaration));
out.add(declaration.getModel());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import static com.intellij.plugins.haxe.util.HaxePresentableUtil.getPresentableParameterList;

public class HaxeEnumValueModel extends HaxeMemberModel {
public class HaxeEnumValueModel extends HaxeFieldModel {
private final boolean isAbstractType;
private final boolean hasConstructor;
private final boolean hasReturnType;
Expand Down
45 changes: 29 additions & 16 deletions src/main/java/com/intellij/plugins/haxe/model/HaxeFileModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.intellij.plugins.haxe.model;

import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.util.HaxeAddImportHelper;
Expand Down Expand Up @@ -95,10 +94,10 @@ public HaxeClassModel getMainClassModel() {

@Nullable
public HaxeClassModel getClassModel(String name) {
Optional<HaxeModule> module = getModuleBody();
HaxeModule module = getModuleBody();

if (module.isPresent()) {
HaxeClass haxeClass = (HaxeClass)Arrays.stream(module.get().getChildren())
if (module != null) {
HaxeClass haxeClass = (HaxeClass)Arrays.stream(module.getChildren())
.filter(element -> {
if (element instanceof HaxeClass hxClass) {
PsiIdentifier identifier = hxClass.getNameIdentifier();
Expand All @@ -115,16 +114,30 @@ public HaxeClassModel getClassModel(String name) {
return null;
}

@NotNull
public Optional<HaxeModule> getModuleBody() {
return Arrays.stream(file.getChildren())
.filter(element -> (element instanceof HaxeModule))
.map(element -> (HaxeModule)element)
.findFirst();
@Nullable
public HaxeModule getModuleBody() {
for (PsiElement element : getChildren()) {
if ((element instanceof HaxeModule module)) {
return module;
}
}
return null;
}
@Nullable
private PsiElement[] getChildren() {
return CachedValuesManager.getProjectPsiDependentCache(file, HaxeFileModel::_getChildren).getValue();
}

private static CachedValueProvider.Result<PsiElement[]> _getChildren(HaxeFile file) {
PsiElement[] children = file.getChildren();
return new CachedValueProvider.Result<>(children, children);
}

@NotNull
public PsiElement[] getModuleBodyChildren() {
return getModuleBody().map(PsiElement::getChildren).orElseGet(() ->PsiElement.EMPTY_ARRAY);
HaxeModule body = getModuleBody();
if (body == null) return PsiElement.EMPTY_ARRAY;
return body.getChildren();
}

@NotNull
Expand Down Expand Up @@ -185,35 +198,35 @@ public Stream<HaxeClassModel> getClassModelsStream() {
}

public List<HaxeImportStatement> getImportStatements() {
return Arrays.stream(file.getChildren())
return Arrays.stream(getChildren())
.filter(element -> element instanceof HaxeImportStatement)
.map(element -> ((HaxeImportStatement)element))
.collect(Collectors.toList());
}

public List<HaxeImportModel> getImportModels() {
return Arrays.stream(file.getChildren())
return Arrays.stream(getChildren())
.filter(element -> element instanceof HaxeImportStatement)
.map(element -> ((HaxeImportStatement)element).getModel())
.collect(Collectors.toList());
}

public List<HaxeUsingStatement> getUsingStatements() {
return Arrays.stream(file.getChildren())
return Arrays.stream(getChildren())
.filter(element -> element instanceof HaxeUsingStatement)
.map(element -> (HaxeUsingStatement)element)
.collect(Collectors.toList());
}

public List<HaxeUsingModel> getUsingModels() {
return Arrays.stream(file.getChildren())
return Arrays.stream(getChildren())
.filter(element -> element instanceof HaxeUsingStatement)
.map(element -> ((HaxeUsingStatement)element).getModel())
.collect(Collectors.toList());
}
@NotNull
public List<HaxeImportableModel> getOrderedImportAndUsingModels() {
PsiElement[] children = file.getChildren();
PsiElement[] children = getChildren();
List<HaxeImportableModel> result = new ArrayList<>();
for(PsiElement child : children) {

Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/intellij/plugins/haxe/model/HaxeMemberModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
*/
package com.intellij.plugins.haxe.model;

import com.intellij.plugins.haxe.lang.psi.HaxeBinaryExpression;
import com.intellij.plugins.haxe.lang.psi.HaxeClass;
import com.intellij.plugins.haxe.lang.psi.HaxePsiModifier;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.metadata.HaxeMetadataList;
import com.intellij.plugins.haxe.metadata.psi.HaxeMeta;
import com.intellij.plugins.haxe.metadata.psi.HaxeMetadataCompileTimeMeta;
import com.intellij.plugins.haxe.metadata.psi.HaxeMetadataContent;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMember;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -98,17 +98,18 @@ private boolean isOverriddenPublicMethod() {

@Override
public HaxeClassModel getDeclaringClass() {
HaxeClassModel model = getBasePsi().getUserData(DECLARING_CLASS_MODEL_KEY);
if (model == null) {
PsiClass containingClass = getMemberPsi().getContainingClass();
if (containingClass instanceof HaxeClass) {
model = ((HaxeClass)containingClass).getModel();
getBasePsi().putUserData(DECLARING_CLASS_MODEL_KEY, model);
}
}
return CachedValuesManager.getProjectPsiDependentCache(getMemberPsi(), HaxeMemberModel::_getDeclaringClass).getValue();
}

return model;
private static CachedValueProvider.Result<HaxeClassModel> _getDeclaringClass(PsiMember member) {
PsiClass containingClass = member.getContainingClass();
if (containingClass instanceof HaxeClass haxeClass) {
return new CachedValueProvider.Result<>(haxeClass.getModel(), member);
}else {
return new CachedValueProvider.Result<>(null, member);
}
}

public boolean isInInterface() {
return getDeclaringClass().isInterface();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public HaxeMemberModel getMemberModel() {
final PsiMember parentPsi = PsiTreeUtil.getParentOfType(getBasePsi(), HaxeEnumValueDeclaration.class, HaxeMethod.class);
if (parentPsi instanceof HaxeMethod) {
model = ((HaxeMethod)parentPsi).getModel();
} else if (parentPsi instanceof HaxeEnumValueDeclaration) {
model = new HaxeFieldModel((HaxePsiField)parentPsi);
} else if (parentPsi instanceof HaxeEnumValueDeclaration haxeEnumValueDeclaration) {
model = (HaxeFieldModel) haxeEnumValueDeclaration.getModel();
}
if (model != null) {
getBasePsi().putUserData(PARAMETER_MEMBER_MODEL_KEY, model);
Expand Down
Loading

0 comments on commit e8596a7

Please sign in to comment.