Search in sources :

Example 6 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getAllAnnotationAttachmentsForType.

@NotNull
public static List<IdentifierPSINode> getAllAnnotationAttachmentsForType(@NotNull PsiDirectory packageElement, @NotNull String type, boolean includePrivate, boolean includeBuiltIns) {
    List<IdentifierPSINode> results = new ArrayList<>();
    // Todo - Do we need to add public to Annotations as well?
    List<IdentifierPSINode> annotationDefinitions = getAllMatchingElementsFromPackage(packageElement, AnnotationDefinitionNode.class, includePrivate, includeBuiltIns);
    if (annotationDefinitions.isEmpty()) {
        return results;
    }
    for (IdentifierPSINode annotationDefinition : annotationDefinitions) {
        if (annotationDefinition.getParent() instanceof AnnotationDefinitionNode) {
            AnnotationDefinitionNode annotationDefinitionNode = (AnnotationDefinitionNode) annotationDefinition.getParent();
            Collection<AttachmentPointNode> attachmentPointNodes = PsiTreeUtil.findChildrenOfType(annotationDefinitionNode, AttachmentPointNode.class);
            if (attachmentPointNodes.isEmpty()) {
                results.add(annotationDefinition);
                continue;
            }
            for (AttachmentPointNode attachmentPointNode : attachmentPointNodes) {
                // will not be available.
                if (type.equals(attachmentPointNode.getText().replaceAll("<\\w*>", ""))) {
                    results.add(annotationDefinition);
                    break;
                }
            }
        }
    }
    return results;
}
Also used : AttachmentPointNode(org.ballerinalang.plugins.idea.psi.AttachmentPointNode) AnnotationDefinitionNode(org.ballerinalang.plugins.idea.psi.AnnotationDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getAllParametersInScope.

@NotNull
private static List<IdentifierPSINode> getAllParametersInScope(@NotNull ScopeNode scope, int caretOffset) {
    List<IdentifierPSINode> results = new LinkedList<>();
    Collection<ParameterNode> parameterNodes = PsiTreeUtil.findChildrenOfType(scope, ParameterNode.class);
    for (ParameterNode parameter : parameterNodes) {
        ScopeNode parentScope = PsiTreeUtil.getParentOfType(parameter, ScopeNode.class);
        if (!scope.equals(parentScope)) {
            continue;
        }
        PsiElement identifier = parameter.getNameIdentifier();
        if (identifier != null && identifier instanceof IdentifierPSINode) {
            results.add(((IdentifierPSINode) identifier));
        }
    }
    Collection<CodeBlockParameterNode> codeBlockParameterNodes = PsiTreeUtil.findChildrenOfType(scope, CodeBlockParameterNode.class);
    for (CodeBlockParameterNode parameter : codeBlockParameterNodes) {
        PsiElement elementAtCaret = scope.getContainingFile().findElementAt(caretOffset);
        if (elementAtCaret == null) {
            return results;
        }
        if (parameter.getTextRange().getEndOffset() >= caretOffset) {
            return results;
        }
        ScopeNode closestScope = PsiTreeUtil.getParentOfType(parameter, ScopeNode.class);
        if (closestScope == null || !closestScope.equals(scope)) {
            continue;
        }
        PsiElement identifier = parameter.getNameIdentifier();
        if (identifier != null && identifier instanceof IdentifierPSINode) {
            results.add(((IdentifierPSINode) identifier));
        }
    }
    return results;
}
Also used : ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) LinkedList(java.util.LinkedList) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method isRedeclaredVar.

/**
 * Used to identify variables used in var assignment statements are redeclared variables or not.
 *
 * @param identifier an identifier
 * @return {@code true} if the variable is declared before in a resolvable scope, {@code false} otherwise.
 */
public static boolean isRedeclaredVar(@NotNull IdentifierPSINode identifier) {
    ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
    if (scope != null) {
        int caretOffset = identifier.getStartOffset();
        List<IdentifierPSINode> variables = BallerinaPsiImplUtil.getAllLocalVariablesInResolvableScope(scope, caretOffset);
        for (IdentifierPSINode variable : variables) {
            if (variable != null && variable.getText().equals(identifier.getText())) {
                return true;
            }
        }
        List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getAllParametersInResolvableScope(scope, caretOffset);
        for (IdentifierPSINode parameter : parameters) {
            if (parameter != null && parameter.getText().equals(identifier.getText())) {
                return true;
            }
        }
        List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope);
        for (IdentifierPSINode variable : globalVariables) {
            if (variable != null && variable.getText().equals(identifier.getText())) {
                return true;
            }
        }
        List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
        for (IdentifierPSINode constant : constants) {
            if (constant != null && constant.getText().equals(identifier.getText())) {
                return true;
            }
        }
        List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
        for (PsiElement namespace : namespaces) {
            if (namespace != null && namespace.getText().equals(identifier.getText())) {
                return true;
            }
        }
        List<IdentifierPSINode> endpoints = BallerinaPsiImplUtil.getAllEndpointsInResolvableScope(scope, caretOffset);
        for (IdentifierPSINode endpoint : endpoints) {
            if (endpoint != null && endpoint.getText().equals(identifier.getText())) {
                return true;
            }
        }
    }
    return false;
}
Also used : IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 9 with IdentifierPSINode

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

the class AnnotationAttributeReference method resolve.

@Nullable
@Override
public PsiElement resolve() {
    IdentifierPSINode identifier = getElement();
    AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getParentOfType(identifier, AnnotationAttachmentNode.class);
    if (annotationAttachmentNode == null) {
        return null;
    }
    AnnotationReferenceNode annotationReferenceNode = PsiTreeUtil.getChildOfType(annotationAttachmentNode, AnnotationReferenceNode.class);
    if (annotationReferenceNode == null) {
        return null;
    }
    int textLength = annotationReferenceNode.getTextLength();
    PsiReference reference = annotationReferenceNode.findReferenceAt(textLength);
    if (reference == null) {
        return null;
    }
    PsiElement annotationName = reference.resolve();
    if (annotationName == null || !(annotationName instanceof IdentifierPSINode)) {
        return null;
    }
    PsiElement annotationDefinition = annotationName.getParent();
    Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(annotationDefinition, FieldDefinitionNode.class);
    return BallerinaPsiImplUtil.resolveReference(fieldDefinitionNodes, identifier);
}
Also used : IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) AnnotationReferenceNode(org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode) PsiElement(com.intellij.psi.PsiElement) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with IdentifierPSINode

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

the class InvocationReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    StructDefinitionNode structDefinition = getStructDefinitionNode();
    if (structDefinition == null) {
        return new LookupElement[0];
    }
    List<IdentifierPSINode> attachedFunctions = BallerinaPsiImplUtil.getAttachedFunctions(structDefinition);
    List<LookupElement> results = BallerinaCompletionUtils.createAttachedFunctionsLookupElements(attachedFunctions);
    return results.toArray(new LookupElement[results.size()]);
}
Also used : StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)109 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)63 LinkedList (java.util.LinkedList)58 LookupElement (com.intellij.codeInsight.lookup.LookupElement)48 Nullable (org.jetbrains.annotations.Nullable)32 PsiDirectory (com.intellij.psi.PsiDirectory)29 PsiReference (com.intellij.psi.PsiReference)25 PsiFile (com.intellij.psi.PsiFile)24 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)20 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)17 ScopeNode (org.antlr.jetbrains.adaptor.psi.ScopeNode)15 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)12 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)12 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)12 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)10 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)9 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)9 ArrayList (java.util.ArrayList)8 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)8