Skip to content

Commit

Permalink
better handling of typeParameters with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Feb 15, 2024
1 parent d3e50c6 commit cb7a683
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.model.HaxeClassModel;
import com.intellij.plugins.haxe.model.HaxeDocumentModel;
import com.intellij.plugins.haxe.model.HaxeGenericParamModel;
import com.intellij.plugins.haxe.model.fixer.HaxeFixer;
import com.intellij.plugins.haxe.model.type.HaxeTypeResolver;
import com.intellij.plugins.haxe.model.type.SpecificHaxeClassReference;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIdentifier;
import org.jetbrains.annotations.NotNull;

import java.util.List;

import static com.intellij.plugins.haxe.ide.annotator.HaxeSemanticAnnotatorInspections.INVALID_TYPE_NAME;
import static com.intellij.plugins.haxe.lang.psi.HaxePsiModifier.DYNAMIC;

Expand Down Expand Up @@ -81,15 +84,25 @@ private static void checkTypeParametersForType(HaxeType type, AnnotationHolder h
if (haxeClass != null) {
// Dynamic is special and does not require Type parameter to de specified
if (DYNAMIC.equalsIgnoreCase(haxeClass.getName())) return;
String typeName = getTypeName(type.getReferenceExpression().getIdentifier());
if (typeName.startsWith("$"))return; // ignore when type is from macro variable

int typeParameterCount = type.getTypeParam() == null ? 0 : type.getTypeParam().getTypeList().getTypeListPartList().size();
int classParameterCount = countTypeParameters(haxeClass);
int classParameterCountMin = minTypeParameters(haxeClass);
int classParameterCountMax = maxTypeParameters(haxeClass);

if (typeParameterCount < classParameterCountMin) {

if (typeParameterCount != classParameterCount) {
String typeName = getTypeName(type.getReferenceExpression().getIdentifier());

holder.newAnnotation(HighlightSeverity.ERROR,
HaxeBundle.message("haxe.inspections.parameter.count.mismatch.description", typeName, classParameterCountMin, typeParameterCount))
.range(type)
.create();
}
if (typeParameterCount > classParameterCountMax) {
if (typeName.startsWith("$"))return; // ignore when type is from macro variable
holder.newAnnotation(HighlightSeverity.ERROR,
HaxeBundle.message("haxe.inspections.parameter.count.mismatch.description", typeName, classParameterCount,
typeParameterCount))
HaxeBundle.message("haxe.inspections.parameter.count.mismatch.description", typeName, classParameterCountMax, typeParameterCount))
.range(type)
.create();
}
Expand All @@ -98,10 +111,14 @@ private static void checkTypeParametersForType(HaxeType type, AnnotationHolder h
}


static private int countTypeParameters(HaxeClass haxeClass) {
HaxeGenericParam param = haxeClass.getGenericParam();
if (param == null) return 0;
return param.getGenericListPartList().size();
static private int minTypeParameters(HaxeClass haxeClass) {
List<HaxeGenericParamModel> params = haxeClass.getModel().getGenericParams();
boolean allHasDefaults = params.stream().allMatch(HaxeGenericParamModel::hasDefault);
if (allHasDefaults) return 0;
return params.size();
}
static private int maxTypeParameters(HaxeClass haxeClass) {
return haxeClass.getModel().getGenericParams().size();
}

private static String getTypeName(PsiIdentifier identifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ public class HaxeGenericParamModel {
final private HaxeGenericListPart part;
final private String name;
final private int index;
final private HaxeTypeOrAnonymous defaultType;
final private HaxeFunctionType defaultFunction;

public HaxeGenericParamModel(@NotNull HaxeGenericListPart part, int index) {
this.index = index;
this.part = part;
this.defaultType = part.getTypeOrAnonymous();
this.defaultFunction = part.getFunctionType();
this.name = part.getComponentName().getText();
}

Expand All @@ -49,6 +53,11 @@ public String getName() {
return this.name;
}

public boolean hasDefault() {
return defaultType != null || defaultFunction != null;
}


public HaxeGenericListPart getPsi() { return part; }

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,24 @@ class Test
//Wrong
var myMap5:<error descr="Invalid number of type parameters for Map (expected: 2 got: 1)">Map<String></error> = new Map();
var myMap6:<error descr="Invalid number of type parameters for Map (expected: 2 got: 0)">Map</error> = new Map<String, Int>();

//OK (default type parameters on all)
var defaultTypeParam1:Foo;
var defaultTypeParam2:Foo<String>;
var defaultTypeParam3:Foo<Int>;
var defaultTypeParam4:Foo<String, String>;

// OK
var defaultTypeParam4:Bar<String, String>;
// Wrong (all requred when only some have default value)
var defaultTypeParam1:<error descr="Invalid number of type parameters for Bar (expected: 2 got: 0)">Bar</error>;
var defaultTypeParam2:<error descr="Invalid number of type parameters for Bar (expected: 2 got: 1)">Bar<String></error>;
var defaultTypeParam3:<error descr="Invalid number of type parameters for Bar (expected: 2 got: 1)">Bar<Int></error>;


}
}
}
class Foo<T = String, P= Int> {}

class Bar<T = String, P> {}

0 comments on commit cb7a683

Please sign in to comment.