Skip to content

Commit

Permalink
Fix error when trying to insert typeTag for types that are objectLite…
Browse files Browse the repository at this point in the history
…rals
  • Loading branch information
m0rkeulv committed Mar 9, 2025
1 parent 0d54292 commit 2e5ffa5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## 1.6.3
* Added: Support for code folding for switch expressions, arrays, maps and object literals.
* Changed: formatter now removes spaces between expressions and semicolons
* bugfix: Fixed error when trying to insert typeTag for objectLiterals
* bugfix: Fixed incorrect priority in parser (ternaryExpressions must be parsed before coalescingExpressions)

## 1.6.2
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pluginName = Haxe Toolkit Support
pluginRepositoryUrl = https://github.com/HaxeFoundation/intellij-haxe

# SemVer format -> https://semver.org
pluginVersion = 1.6.2
pluginVersion = 1.6.3

# IntelliJ Platform Properties -> https://github.com/JetBrains/gradle-intellij-plugin#intellij-platform-properties
platformType = IU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
import com.intellij.plugins.haxe.lang.psi.HaxeLocalVarDeclarationList;
import com.intellij.plugins.haxe.lang.psi.HaxePsiField;
import com.intellij.plugins.haxe.lang.psi.HaxeTypeTag;
import com.intellij.plugins.haxe.model.HaxeClassModel;
import com.intellij.plugins.haxe.model.HaxeObjectLiteralClassModel;
import com.intellij.plugins.haxe.model.evaluator.HaxeExpressionEvaluator;
import com.intellij.plugins.haxe.model.evaluator.HaxeExpressionEvaluatorContext;
import com.intellij.plugins.haxe.model.type.ResultHolder;
import com.intellij.plugins.haxe.model.type.SpecificHaxeClassReference;
import com.intellij.plugins.haxe.model.type.SpecificTypeReference;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
Expand Down Expand Up @@ -65,7 +69,19 @@ public void invoke(@NotNull final Project project, Editor editor, PsiFile file)
if (isMissingTypeTag) {
ResultHolder type = HaxeExpressionEvaluator.evaluate(field, new HaxeExpressionEvaluatorContext(field), null).result;
if (!(type == null || type.isUnknown())) {
HaxeTypeTag tag = createTypeTag(project, type.getType().toPresentationString());

SpecificTypeReference typeReference = type.getType();
String presentationString = typeReference.toPresentationString();

// object literals needs to be handled in a different way as the Presentation string is usually shortened
if (typeReference instanceof SpecificHaxeClassReference classReference) {
HaxeClassModel haxeClassModel = classReference.getHaxeClassModel();
if(haxeClassModel instanceof HaxeObjectLiteralClassModel objectLiteralModel) {
presentationString = objectLiteralModel.buildTypeString();
}
}

HaxeTypeTag tag = createTypeTag(project, presentationString);
field.addAfter(tag, field.getComponentName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.lang.psi.impl.HaxeObjectLiteralImpl;
import com.intellij.plugins.haxe.model.type.HaxeGenericResolver;
import com.intellij.plugins.haxe.model.type.ResultHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -73,4 +74,25 @@ public List<HaxeModel> getExposedMembers() {
public HaxeFieldModel getField(String name, @Nullable HaxeGenericResolver resolver) {
return super.getField(name, resolver);
}

public String buildTypeString() {
StringBuilder builder = new StringBuilder();
builder.append("{");
List<HaxeBaseMemberModel> members = getMembers(null);
int memberCount = members.size();
for (int i = 0; i < memberCount; i++) {
HaxeBaseMemberModel member = members.get(i);
String name = member.getName();
ResultHolder resultType = member.getResultType();
builder.append(name);
builder.append(":");
builder.append(resultType.toTypeString());
if(i+1 <memberCount ) {
builder.append(",");
}

}
builder.append("}");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,15 @@ static ResultHolder handleReferenceExpression( HaxeExpressionEvaluatorContext co
else {
typeHolder = SpecificHaxeClassReference.withoutGenerics(classReference).createHolder();
}

// check if pure Class Reference
if (reference instanceof HaxeReferenceExpressionImpl expression) {
if (expression.isPureClassReferenceOf(haxeClass)) {
// make sure its not an import statement
if (PsiTreeUtil.getParentOfType(expression, HaxeImportStatement.class) == null) {
typeHolder = wrapTypeInClassOrEnum(element, haxeClass, model);
// make sure we do not wrap type in class if reference is type in ObjectLiteral
if (!(element.getParent() instanceof HaxeObjectLiteralElement)) {
// check if pure Class Reference
if (reference instanceof HaxeReferenceExpressionImpl expression) {
if (expression.isPureClassReferenceOf(haxeClass)) {
// make sure its not an import statement
if (PsiTreeUtil.getParentOfType(expression, HaxeImportStatement.class) == null) {
typeHolder = wrapTypeInClassOrEnum(element, haxeClass, model);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ public String toStringWithoutConstant() {
public String toPresentationString() {
return this.getType().toPresentationString(false);
}
public String toTypeString() {
return this.getType().toTypeString();
}
public String toPresentationString(boolean showOnlyConstraintForTypeParam) {
return this.getType().toPresentationString(showOnlyConstraintForTypeParam);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,4 +654,12 @@ public SpecificHaxeClassReference wrapInNullType(@NotNull PsiElement context) {

public abstract PsiElement getTypePsi();

public String toTypeString() {
if(this instanceof SpecificHaxeClassReference classReference) {
if(classReference.getHaxeClassModel() instanceof HaxeObjectLiteralClassModel objectLiteralModel) {
return objectLiteralModel.buildTypeString();
}
}
return toPresentationString();
}
}

0 comments on commit 2e5ffa5

Please sign in to comment.