Skip to content

Commit

Permalink
cache GenericParams for HaxeClassModel
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Jan 7, 2024
1 parent 808e60f commit 820805d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
25 changes: 18 additions & 7 deletions src/main/java/com/intellij/plugins/haxe/model/HaxeClassModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.lang.psi.impl.AbstractHaxePsiClass;
import com.intellij.plugins.haxe.metadata.HaxeMetadataList;
import com.intellij.plugins.haxe.metadata.psi.HaxeMeta;
import com.intellij.plugins.haxe.metadata.psi.impl.HaxeMetadataTypeName;
import com.intellij.plugins.haxe.model.type.*;
Expand Down Expand Up @@ -704,14 +703,14 @@ public void writeCompatibleTypes(Set<HaxeClassModel> output) {
}

public boolean hasGenericParams() {
return getPsi().getGenericParam() != null;
return getGenericParamPsi() != null;
}

@NotNull
public List<HaxeGenericParamModel> getGenericParams() {
final List<HaxeGenericParamModel> out = new ArrayList<>();
// anonymous structures does not have TypeParameters on their own, but their parent may declar them.
HaxeGenericParam genericParam = isAnonymous() ? getGenericParamFromParent() : getPsi().getGenericParam();
HaxeGenericParam genericParam = getGenericParamPsi();
if (genericParam != null) {
int index = 0;
for (HaxeGenericListPart part : genericParam.getGenericListPartList()) {
Expand All @@ -722,11 +721,22 @@ public List<HaxeGenericParamModel> getGenericParams() {
return out;
}

@Nullable
private HaxeGenericParam getGenericParamPsi() {
return CachedValuesManager.getProjectPsiDependentCache(haxeClass, HaxeClassModel::getGenericParamPsiCached).getValue();
}

private static CachedValueProvider.Result<HaxeGenericParam> getGenericParamPsiCached(@NotNull HaxeClass haxeClass) {
boolean isAnonymous = haxeClass instanceof HaxeAnonymousType;
HaxeGenericParam param = isAnonymous ? getGenericParamFromParent(haxeClass) : haxeClass.getGenericParam();
return CachedValueProvider.Result.create(param, haxeClass);
}

/**
* only intended for typedefs with anonymous structures
*/
private HaxeGenericParam getGenericParamFromParent() {
HaxeTypedefDeclaration type = PsiTreeUtil.getParentOfType(getPsi(), HaxeTypedefDeclaration.class);
private static HaxeGenericParam getGenericParamFromParent(HaxeClass haxeClass) {
HaxeTypedefDeclaration type = PsiTreeUtil.getParentOfType(haxeClass, HaxeTypedefDeclaration.class);
if (type == null) return null;
return type.getGenericParam();
}
Expand All @@ -736,10 +746,11 @@ private HaxeGenericParam getGenericParamFromParent() {
*/
@NotNull
public HaxeGenericResolver getGenericResolver(@Nullable HaxeGenericResolver parentResolver) {
if (getPsi().getGenericParam() != null) {
HaxeGenericParam param = getGenericParamPsi();
if (param != null) {

HaxeGenericResolver resolver = new HaxeGenericResolver();
for (HaxeGenericListPart part : getPsi().getGenericParam().getGenericListPartList()) {
for (HaxeGenericListPart part : param.getGenericListPartList()) {
HaxeGenericParamModel model = new HaxeGenericParamModel(part, 0);
ResultHolder constraint = model.getConstraint(parentResolver);
if (null == constraint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,20 +690,6 @@ else if (from.getSpecifics().length == 0) {

//used to help prevent stack overflows in canAssignToFromSpecificType
private static boolean referencesAreDifferent(@NotNull SpecificHaxeClassReference first, @NotNull SpecificHaxeClassReference second) {
//if(Objects.equals(first.getHaxeClassModel(),second.getHaxeClassModel())) {
// @NotNull ResultHolder[] firstSpecifics = first.getSpecifics();
// @NotNull ResultHolder[] secondSpecifics = second.getSpecifics();
// if (firstSpecifics.length != secondSpecifics.length) return true;
// for (int i = 0; i < firstSpecifics.length; i++) {
// SpecificTypeReference firstGeneric = firstSpecifics[i].getType();
// SpecificTypeReference secondGeneric = secondSpecifics[i].getType();
// if(!firstGeneric.toPresentationString().equals(secondGeneric.toPresentationString())) {
// return true;
// }
// }
// return false;
//}
//return true;
return !first.toPresentationString().equalsIgnoreCase(second.toPresentationString());
}

Expand Down

0 comments on commit 820805d

Please sign in to comment.