use of com.goide.psi.impl.GoCType in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoDocumentationProvider method getTypePresentation.
@NotNull
public static String getTypePresentation(@Nullable PsiElement element, @NotNull Function<PsiElement, String> presentationFunction) {
if (element instanceof GoType) {
GoType type = (GoType) element;
if (type instanceof GoMapType) {
GoType keyType = ((GoMapType) type).getKeyType();
GoType valueType = ((GoMapType) type).getValueType();
return "map[" + getTypePresentation(keyType, presentationFunction) + "]" + getTypePresentation(valueType, presentationFunction);
}
if (type instanceof GoChannelType) {
ASTNode typeNode = type.getNode();
GoType innerType = ((GoChannelType) type).getType();
ASTNode innerTypeNode = innerType != null ? innerType.getNode() : null;
if (typeNode != null && innerTypeNode != null) {
StringBuilder result = new StringBuilder();
for (ASTNode node : typeNode.getChildren(null)) {
if (node.equals(innerTypeNode)) {
break;
}
if (node.getElementType() != TokenType.WHITE_SPACE) {
result.append(XmlStringUtil.escapeString(node.getText()));
}
}
result.append(" ").append(getTypePresentation(innerType, presentationFunction));
return result.toString();
}
}
if (type instanceof GoParType) {
return "(" + getTypePresentation(((GoParType) type).getActualType(), presentationFunction) + ")";
}
if (type instanceof GoArrayOrSliceType) {
return "[]" + getTypePresentation(((GoArrayOrSliceType) type).getType(), presentationFunction);
}
if (type instanceof GoPointerType) {
GoType inner = ((GoPointerType) type).getType();
return inner instanceof GoSpecType ? getTypePresentation(inner, presentationFunction) : "*" + getTypePresentation(inner, presentationFunction);
}
if (type instanceof GoTypeList) {
return "(" + StringUtil.join(((GoTypeList) type).getTypeList(), element1 -> getTypePresentation(element1, presentationFunction), ", ") + ")";
}
if (type instanceof GoFunctionType) {
return getSignatureOwnerTypePresentation((GoFunctionType) type, presentationFunction);
}
if (type instanceof GoSpecType) {
return getTypePresentation(GoPsiImplUtil.getTypeSpecSafe(type), presentationFunction);
}
if (type instanceof GoCType) {
return "C";
}
if (type instanceof GoLightType) {
LOG.error("Cannot build presentable text for type: " + type.getClass().getSimpleName() + " - " + type.getText());
return "";
}
if (type instanceof GoStructType) {
StringBuilder result = new StringBuilder("struct {");
result.append(StringUtil.join(((GoStructType) type).getFieldDeclarationList(), declaration -> {
GoAnonymousFieldDefinition anon = declaration.getAnonymousFieldDefinition();
String fieldString = anon != null ? getTypePresentation(anon.getGoTypeInner(null), presentationFunction) : StringUtil.join(declaration.getFieldDefinitionList(), PsiElement::getText, ", ") + " " + getTypePresentation(declaration.getType(), presentationFunction);
GoTag tag = declaration.getTag();
return fieldString + (tag != null ? tag.getText() : "");
}, "; "));
return result.append("}").toString();
}
GoTypeReferenceExpression typeRef = GoPsiImplUtil.getTypeReference(type);
if (typeRef != null) {
PsiElement resolve = typeRef.resolve();
if (resolve != null) {
return getTypePresentation(resolve, presentationFunction);
}
}
}
return presentationFunction.fun(element);
}
Aggregations