Search in sources :

Example 1 with GoLightType

use of com.goide.psi.impl.GoLightType 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);
}
Also used : GoCType(com.goide.psi.impl.GoCType) GoVendoringUtil(com.goide.project.GoVendoringUtil) com.goide.psi(com.goide.psi) VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdFilter(com.intellij.util.indexing.IdFilter) StubIndex(com.intellij.psi.stubs.StubIndex) ContainerUtil(com.intellij.util.containers.ContainerUtil) AbstractDocumentationProvider(com.intellij.lang.documentation.AbstractDocumentationProvider) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Comparing(com.intellij.openapi.util.Comparing) GoAllPublicNamesIndex(com.goide.stubs.index.GoAllPublicNamesIndex) Project(com.intellij.openapi.project.Project) GoSdkUtil(com.goide.sdk.GoSdkUtil) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) DumbService(com.intellij.openapi.project.DumbService) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) GoPsiImplUtil(com.goide.psi.impl.GoPsiImplUtil) StringUtil(com.intellij.openapi.util.text.StringUtil) GoParameterInfoHandler(com.goide.editor.GoParameterInfoHandler) GoUtil(com.goide.util.GoUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) DocumentationManagerProtocol(com.intellij.codeInsight.documentation.DocumentationManagerProtocol) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) GoIdFilter(com.goide.stubs.index.GoIdFilter) List(java.util.List) GoAllPrivateNamesIndex(com.goide.stubs.index.GoAllPrivateNamesIndex) Function(com.intellij.util.Function) XmlStringUtil(com.intellij.xml.util.XmlStringUtil) com.intellij.psi(com.intellij.psi) GoPackageUtil(com.goide.sdk.GoPackageUtil) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) GoLightType(com.goide.psi.impl.GoLightType) GoCType(com.goide.psi.impl.GoCType) ASTNode(com.intellij.lang.ASTNode) GoLightType(com.goide.psi.impl.GoLightType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GoParameterInfoHandler (com.goide.editor.GoParameterInfoHandler)1 GoVendoringUtil (com.goide.project.GoVendoringUtil)1 com.goide.psi (com.goide.psi)1 GoCType (com.goide.psi.impl.GoCType)1 GoLightType (com.goide.psi.impl.GoLightType)1 GoPsiImplUtil (com.goide.psi.impl.GoPsiImplUtil)1 GoPackageUtil (com.goide.sdk.GoPackageUtil)1 GoSdkUtil (com.goide.sdk.GoSdkUtil)1 GoAllPrivateNamesIndex (com.goide.stubs.index.GoAllPrivateNamesIndex)1 GoAllPublicNamesIndex (com.goide.stubs.index.GoAllPublicNamesIndex)1 GoIdFilter (com.goide.stubs.index.GoIdFilter)1 GoUtil (com.goide.util.GoUtil)1 DocumentationManagerProtocol (com.intellij.codeInsight.documentation.DocumentationManagerProtocol)1 ASTNode (com.intellij.lang.ASTNode)1 AbstractDocumentationProvider (com.intellij.lang.documentation.AbstractDocumentationProvider)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Module (com.intellij.openapi.module.Module)1 ModuleUtilCore (com.intellij.openapi.module.ModuleUtilCore)1 DumbService (com.intellij.openapi.project.DumbService)1 Project (com.intellij.openapi.project.Project)1