Skip to content

Nested function types support #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gen/com/intellij/plugins/haxe/lang/parser/HaxeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2620,15 +2620,15 @@ private static boolean functionPrototypeDeclarationWithAttributes_8(PsiBuilder b
}

/* ********************************************************** */
// '->' '?'? typeOrAnonymous
// '->' '?'? functionTypeOrWrapper
public static boolean functionType(PsiBuilder b, int l) {
if (!recursion_guard_(b, l, "functionType")) return false;
if (!nextTokenIs(b, OARROW)) return false;
boolean r;
Marker m = enter_section_(b, l, _LEFT_, null);
r = consumeToken(b, OARROW);
r = r && functionType_1(b, l + 1);
r = r && typeOrAnonymous(b, l + 1);
r = r && functionTypeOrWrapper(b, l + 1);
exit_section_(b, l, m, FUNCTION_TYPE, r, false, null);
return r;
}
Expand Down
4 changes: 2 additions & 2 deletions gen/com/intellij/plugins/haxe/lang/psi/HaxeFunctionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

public interface HaxeFunctionType extends HaxePsiCompositeElement {

@Nullable
HaxeFunctionType getFunctionType();
@NotNull
List<HaxeFunctionType> getFunctionTypeList();

@NotNull
List<HaxeTypeOrAnonymous> getTypeOrAnonymousList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public void accept(@NotNull PsiElementVisitor visitor) {
}

@Override
@Nullable
public HaxeFunctionType getFunctionType() {
return findChildByClass(HaxeFunctionType.class);
@NotNull
public List<HaxeFunctionType> getFunctionTypeList() {
return PsiTreeUtil.getChildrenOfTypeAsList(this, HaxeFunctionType.class);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion grammar/haxe.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ arrayLiteral ::= '[' (expressionList ','?)? ']'

private functionTypeWrapper ::= functionTypeOrWrapper functionType*
private functionTypeOrWrapper ::= typeOrAnonymous | '(' functionTypeWrapper ')'
left functionType ::= '->' '?'? typeOrAnonymous
left functionType ::= '->' '?'? functionTypeOrWrapper

/*
* Haxe and Java have this backward from each other.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ public boolean isVisible(TreeElement treeNode) {
if (!(treeNode instanceof HaxeStructureViewElement)) return true;
final PsiElement element = ((HaxeStructureViewElement)treeNode).getRealElement();

if (HaxeComponentType.typeOf(element) == HaxeComponentType.FIELD) {
return false;
}

return true;
return HaxeComponentType.typeOf(element) != HaxeComponentType.FIELD;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to leave the old code in place for these types of changes. I write them as separate returns when I need to break in the debugger on a certain return value, because you can't set a breakpoint on the return after the expression has been evaluated. The other pattern that I use is

ret = expr; 
return ret;

My thinking for leaving them is that if I needed the returns separate for debugging in the past, then I'll probably need it again, and it doesn't make the code any harder to understand. It's just a style thing, though.

}

public boolean isReverted() {
Expand Down
16 changes: 13 additions & 3 deletions src/common/com/intellij/plugins/haxe/util/HaxePresentableUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,16 @@ public static String buildTypeText(HaxeNamedComponent element, HaxeTypeTag typeT
public static String buildTypeText(HaxeNamedComponent element, HaxeTypeTag typeTag, HaxeGenericSpecialization specialization) {
if (typeTag != null)
{
final HaxeFunctionType haxeFunctionType = (typeTag.getFunctionType() == null) ? null :
typeTag.getFunctionType().getFunctionType();
List<HaxeFunctionType> list;
HaxeFunctionType functionType = typeTag.getFunctionType();
list = functionType != null ? functionType.getFunctionTypeList() : null;
HaxeFunctionType type = null;
if (list != null) {
type = list.size() > 0 ? list.get(0) : null;
}

final HaxeFunctionType haxeFunctionType = (functionType == null) ? null :
type;
if (haxeFunctionType != null) {
return buildTypeText(element, haxeFunctionType, specialization);
}
Expand Down Expand Up @@ -142,7 +150,9 @@ private static String buildTypeText(HaxeNamedComponent element,
"->" +
buildTypeText(element, typeOrAnonymousList.get(1).getType(), specialization);
}
return buildTypeText(element, functionType.getFunctionType(), specialization) +
List<HaxeFunctionType> list = functionType.getFunctionTypeList();
HaxeFunctionType type = list.size() > 0 ? list.get(0) : null;
return buildTypeText(element, type, specialization) +
"->" +
buildTypeText(element, typeOrAnonymousList.get(0).getType(), specialization);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,17 @@ private static HaxeClassResolveResult tryResolveFunctionType(@Nullable HaxeFunct
final HaxeTypeOrAnonymous returnTypeOrAnonymous =
functionType.getTypeOrAnonymousList().get(functionType.getTypeOrAnonymousList().size() - 1);
final HaxeClassResolveResult result = tryResolveClassByTypeTag(returnTypeOrAnonymous.getType(), specialization);
functionType = functionType.getFunctionType();
List<HaxeFunctionType> list = functionType.getFunctionTypeList();
functionType = list.size() > 0 ? list.get(0) : null;
while (functionType != null) {
// todo: anonymous types :(
final List<HaxeTypeOrAnonymous> typeList = functionType.getTypeOrAnonymousList();
Collections.reverse(typeList);
for (HaxeTypeOrAnonymous typeOrAnonymous : typeList) {
result.getFunctionTypes().add(tryResolveClassByTypeTag(typeOrAnonymous.getType(), specialization));
}
functionType = functionType.getFunctionType();
List<HaxeFunctionType> list2 = functionType.getFunctionTypeList();
functionType = list2.size() > 0 ? list2.get(0) : null;
}
Collections.reverse(result.getFunctionTypes());
return result;
Expand Down
3 changes: 3 additions & 0 deletions testData/parsing/haxe/declarations/typedef/FunctionTypedef.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package test;

typedef MyFnPtr = String->Int->Void;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// No satisfactory golden file has been generated yet.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package test;

typedef MyFnPtr = String->(Int->Int->Void)->Void;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// No satisfactory golden master has been generated yet.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public void testSimple() throws Throwable {
public void testPoints() throws Throwable {
doTest(true);
}

public void testFunctionTypedef() throws Throwable {
doTest(true);
}

public void testFunctionTypedefWithFunctionParameter() throws Throwable {
doTest(true);
}
}