PsiElement firstChildExp = argument.getFirstChild();
GoType[] goTypes = argument.getType();
if (goTypes.length > 0 && goTypes[0] != null) {
GoType goType = goTypes[0];
stringBuilder.append(GoTypes.getRepresentation(goType, currentFile));
} else if (firstChildExp instanceof GoLiteral) {
/*
* Resolves the type of a literal
*/
stringBuilder.append(((GoLiteral) firstChildExp).getType().name().toLowerCase());
} else {
/*
* This block try to resolve the return type of a closure being called on the paramenter list
* ex: unresolvedFn(func()int{return 25*4}())
* will generate:
* func unresolvedFn(arg0 int){}
* @note i think any one will do that, but we never know :D
*/
PsiElement firstChild = firstChildExp.getFirstChild();
if (firstChild instanceof GoLiteralFunction) {
GoType[] returnType = ((GoLiteralFunction) firstChild).getReturnTypes();
if (returnType.length > 0) {
stringBuilder.append("<berila>");
} else {
stringBuilder.append("interface{}");
}
} else if (firstChild instanceof GoLiteral) {
GoLiteral.Type type = ((GoLiteral) firstChild).getType();
//Fix TEST PR ##321 this only happens on test. i don't know why
if (type == GoLiteral.Type.Float || type == GoLiteral.Type.ImaginaryFloat) {
stringBuilder.append("float32");
} else {
stringBuilder.append(type.name().toLowerCase());
}
} else {
stringBuilder.append("interface{}");
}
}
arg++;
}
stringBuilder.append(")");
} else {
/*
* Try to resolve the type declaration for the generated function based on called function
* ex: when http.HandleFunc("/",myIndexHandle)
* will generate:
* func myIndexHandle(arg0 http.ResponseWriter, arg1 *http.Request){}
*/
GoCallOrConvExpression callExpression = findParentOfType(e, GoCallOrConvExpression.class);
GoType[] type = callExpression.getBaseExpression().getType();
if ( type.length != 1 || ! (type[0] instanceof GoTypeFunction) )
return "";
GoTypeFunction function = (GoTypeFunction) type[0];
int pos = -1;
for (GoExpr expr : callExpression.getArguments()) {
pos++;
if ( expr.getTextOffset() == e.getTextOffset() && expr.getTextLength() == e.getTextLength() )
break;
}
GoType desiredType = function.getParameterType(pos);
if ( desiredType instanceof GoTypeFunction ) {
GoTypeFunction typeFunction = (GoTypeFunction) desiredType;
int i = 0;
stringBuilder.append("(");
GoType parameterType = null;
while ( (parameterType = typeFunction.getParameterType(i)) != null) {
stringList.add(String.format("arg%d", i));
if ( i > 0 )
stringBuilder.append(",");
stringBuilder.append(String.format("$v%d$ ", i));
stringBuilder.append(GoTypes.getRepresentation(parameterType, currentFile));
i++;
}
stringBuilder.append(")");
GoType[] results = typeFunction.getResultTypes();
if ( results.length > 1 ) stringBuilder.append("(");
for (int i1 = 0; i1 < results.length; i1++) {
GoType goType = results[i1];
if( i1 > 0)
stringBuilder.append(",");
stringBuilder.append(GoTypes.getRepresentation(goType, currentFile));
}
if ( results.length > 1 ) stringBuilder.append(")");