Search in sources :

Example 21 with TypeNameNode

use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method getType.

// Todo - merge to a single method
@Nullable
private static PsiElement getType(@NotNull FieldDefinitionNode fieldDefinitionNode) {
    TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(fieldDefinitionNode, TypeNameNode.class);
    if (typeNameNode == null) {
        return null;
    }
    PsiElement typeNode = getType(typeNameNode, true);
    if (typeNode == null) {
        return null;
    }
    return typeNode;
}
Also used : BuiltInReferenceTypeNameNode(org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode) FunctionTypeNameNode(org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) ValueTypeNameNode(org.ballerinalang.plugins.idea.psi.ValueTypeNameNode) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with TypeNameNode

use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.

the class NameReference method resolveInCurrentPackage.

@Nullable
private PsiElement resolveInCurrentPackage() {
    IdentifierPSINode identifier = getElement();
    PsiFile containingFile = identifier.getContainingFile();
    if (containingFile == null) {
        return null;
    }
    PsiFile originalFile = containingFile.getOriginalFile();
    PsiDirectory psiDirectory = originalFile.getParent();
    if (psiDirectory == null) {
        return null;
    }
    // If the next element is '(', that means we are at a function invocation node. So match any matching
    // function first. If no matching function found, match with variables since this can be a lambda function.
    // If the next element is not '(', we match variables first.
    PsiElement nextVisibleLeaf = PsiTreeUtil.nextVisibleLeaf(identifier);
    if (nextVisibleLeaf != null && "(".equals(nextVisibleLeaf.getText())) {
        PsiElement element = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, true, true);
        if (element != null) {
            return element;
        }
        return BallerinaPsiImplUtil.resolveElementInScope(identifier, true, true, true, true, true);
    } else {
        PsiElement elementInScope = BallerinaPsiImplUtil.resolveElementInScope(identifier, true, true, true, true, true);
        if (elementInScope != null) {
            return elementInScope;
        }
        PsiElement resolvedElement;
        TypeNameNode typeNameNode = PsiTreeUtil.getParentOfType(identifier, TypeNameNode.class);
        if (typeNameNode == null) {
            resolvedElement = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, true, true);
        } else {
            resolvedElement = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, false, true, true, true, false, false, true, true);
        }
        if (resolvedElement != null) {
            return resolvedElement;
        }
        return BallerinaPsiImplUtil.getNamespaceDefinition(identifier);
    }
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with TypeNameNode

use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.

the class RecordKeyReference method resolve.

@Nullable
private PsiElement resolve(@NotNull PsiElement definitionNode) {
    IdentifierPSINode identifier = getElement();
    TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(definitionNode, TypeNameNode.class);
    if (typeNameNode == null) {
        return null;
    } else {
        // Todo - Add getType util method
        PsiReference reference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
        if (reference == null) {
            TypeNameNode actualType = PsiTreeUtil.getChildOfType(typeNameNode, TypeNameNode.class);
            if (actualType == null) {
                return null;
            }
            reference = actualType.findReferenceAt(actualType.getTextLength());
            if (reference == null) {
                return null;
            }
        }
        PsiElement resolvedElement = reference.resolve();
        if (resolvedElement == null) {
            return null;
        }
        PsiElement resolvedElementParent = resolvedElement.getParent();
        if (resolvedElementParent instanceof StructDefinitionNode) {
            // Todo - use an util method
            Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(resolvedElementParent, FieldDefinitionNode.class);
            for (FieldDefinitionNode fieldDefinitionNode : fieldDefinitionNodes) {
                IdentifierPSINode fieldName = PsiTreeUtil.getChildOfType(fieldDefinitionNode, IdentifierPSINode.class);
                if (fieldName != null && identifier.getText().equals(fieldName.getText())) {
                    return fieldName;
                }
            }
        }
    }
    return null;
}
Also used : StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with TypeNameNode

use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.

the class FieldReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    IdentifierPSINode identifier = getElement();
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
    // Todo - remove hard coded "."
    if (prevVisibleLeaf == null || !".".equals(prevVisibleLeaf.getText())) {
        return new LookupElement[0];
    }
    PsiElement previousField = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
    if (previousField == null) {
        return new LookupElement[0];
    }
    PsiReference reference = previousField.findReferenceAt(0);
    if (reference == null) {
        PsiElement prevSibling = identifier.getParent().getPrevSibling();
        if (prevSibling == null) {
            return new LookupElement[0];
        }
        if (prevSibling instanceof VariableReferenceNode) {
            PsiElement[] children = prevSibling.getChildren();
            if (children.length <= 0) {
                return new LookupElement[0];
            }
            PsiElement firstChild = children[0].getFirstChild();
            if (firstChild == null) {
                return new LookupElement[0];
            }
            PsiReference functionReference = firstChild.findReferenceAt(firstChild.getTextLength());
            if (functionReference == null) {
                return new LookupElement[0];
            }
            PsiElement resolvedElement = functionReference.resolve();
            if (resolvedElement == null) {
                return new LookupElement[0];
            }
            PsiElement parent = resolvedElement.getParent();
            if (parent instanceof FunctionDefinitionNode) {
                List<TypeNameNode> returnTypes = BallerinaPsiImplUtil.getReturnTypes(((FunctionDefinitionNode) parent));
                if (returnTypes.size() == 1) {
                    TypeNameNode typeNameNode = returnTypes.get(0);
                    List<LookupElement> functions = Arrays.asList((LookupElement[]) TypeReference.getVariants(typeNameNode));
                    return functions.toArray(new LookupElement[functions.size()]);
                }
            }
        }
    }
    if (reference == null) {
        return new LookupElement[0];
    }
    // Todo - use util method
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null || !(resolvedElement instanceof IdentifierPSINode)) {
        return new LookupElement[0];
    }
    PsiElement resolvedElementParent = resolvedElement.getParent();
    StructDefinitionNode structDefinitionNode = null;
    List<LookupElement> results = new LinkedList<>();
    // Resolve the corresponding resolvedElementParent to get the struct definition.
    if (resolvedElementParent instanceof VariableDefinitionNode || resolvedElementParent instanceof CodeBlockParameterNode || resolvedElementParent instanceof ParameterNode) {
        structDefinitionNode = BallerinaPsiImplUtil.resolveStructFromDefinitionNode(resolvedElementParent);
    } else if (resolvedElementParent instanceof FieldDefinitionNode) {
        structDefinitionNode = BallerinaPsiImplUtil.resolveTypeNodeStruct((resolvedElementParent));
    } else if (resolvedElementParent instanceof NameReferenceNode) {
        AssignmentStatementNode assignmentStatementNode = PsiTreeUtil.getParentOfType(resolvedElement, AssignmentStatementNode.class);
        if (assignmentStatementNode != null) {
            structDefinitionNode = BallerinaPsiImplUtil.getStructDefinition(assignmentStatementNode, ((IdentifierPSINode) resolvedElement));
        } else {
            structDefinitionNode = BallerinaPsiImplUtil.findStructDefinition((IdentifierPSINode) resolvedElement);
        }
        if (structDefinitionNode != null) {
            IdentifierPSINode structName = PsiTreeUtil.findChildOfType(structDefinitionNode, IdentifierPSINode.class);
            if (structName != null) {
                resolvedElement = structName;
            }
        }
    } else if (resolvedElementParent instanceof EnumDefinitionNode) {
        Collection<EnumFieldNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(resolvedElementParent, EnumFieldNode.class);
        results.addAll(BallerinaCompletionUtils.createEnumFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement));
        return results.toArray(new LookupElement[results.size()]);
    } else if (resolvedElementParent instanceof StructDefinitionNode) {
        structDefinitionNode = ((StructDefinitionNode) resolvedElementParent);
    }
    if (structDefinitionNode == null) {
        return new LookupElement[0];
    }
    Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
    results.addAll(BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement, null));
    List<IdentifierPSINode> attachedFunctions = BallerinaPsiImplUtil.getAttachedFunctions(structDefinitionNode);
    results.addAll(BallerinaCompletionUtils.createAttachedFunctionsLookupElements(attachedFunctions));
    return results.toArray(new LookupElement[results.size()]);
}
Also used : AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) PsiReference(com.intellij.psi.PsiReference) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) LinkedList(java.util.LinkedList) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) EnumFieldNode(org.ballerinalang.plugins.idea.psi.EnumFieldNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) Collection(java.util.Collection) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) EnumDefinitionNode(org.ballerinalang.plugins.idea.psi.EnumDefinitionNode) PsiElement(com.intellij.psi.PsiElement) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with TypeNameNode

use of org.ballerinalang.plugins.idea.psi.TypeNameNode in project ballerina by ballerina-lang.

the class BallerinaParameterInfoHandler method getParameterPresentations.

public static List<String> getParameterPresentations(ParameterTypeNameList node) {
    List<String> params = new LinkedList<>();
    if (node == null) {
        return params;
    }
    // Get type name nodes.
    Collection<ParameterTypeName> parameterTypeNames = PsiTreeUtil.findChildrenOfType(node, ParameterTypeName.class);
    for (ParameterTypeName parameterTypeName : parameterTypeNames) {
        TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(parameterTypeName, TypeNameNode.class);
        if (typeNameNode != null) {
            params.add(parameterTypeName.getText());
        }
    }
    return params;
}
Also used : TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) ParameterTypeName(org.ballerinalang.plugins.idea.psi.ParameterTypeName) LinkedList(java.util.LinkedList)

Aggregations

TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)25 PsiElement (com.intellij.psi.PsiElement)18 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)16 ValueTypeNameNode (org.ballerinalang.plugins.idea.psi.ValueTypeNameNode)15 BuiltInReferenceTypeNameNode (org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode)14 FunctionTypeNameNode (org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode)14 PsiReference (com.intellij.psi.PsiReference)13 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)12 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)11 Nullable (org.jetbrains.annotations.Nullable)11 LinkedList (java.util.LinkedList)9 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)9 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)8 NotNull (org.jetbrains.annotations.NotNull)7 CodeBlockParameterNode (org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode)6 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)6 LookupElement (com.intellij.codeInsight.lookup.LookupElement)5 ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)5 ReturnParameterNode (org.ballerinalang.plugins.idea.psi.ReturnParameterNode)5 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)4