Search in sources :

Example 56 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getAllLocalVariablesInScope.

/**
 * Returns all local variables in the provided scope.
 *
 * @param scope
 * @param caretOffset
 * @return
 */
@NotNull
public static List<IdentifierPSINode> getAllLocalVariablesInScope(@NotNull ScopeNode scope, int caretOffset) {
    List<IdentifierPSINode> results = new LinkedList<>();
    Collection<VariableDefinitionNode> variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(scope, VariableDefinitionNode.class);
    for (VariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
        if (isValidVariable(variableDefinitionNode, scope, caretOffset)) {
            PsiElement identifier = variableDefinitionNode.getNameIdentifier();
            if (identifier != null && identifier instanceof IdentifierPSINode) {
                results.add(((IdentifierPSINode) identifier));
            }
        }
    }
    if (scope instanceof TransformerDefinitionNode) {
        Collection<ExpressionVariableDefinitionStatementNode> nodes = PsiTreeUtil.findChildrenOfType(scope, ExpressionVariableDefinitionStatementNode.class);
        for (ExpressionVariableDefinitionStatementNode node : nodes) {
            ScopeNode closestScope = PsiTreeUtil.getParentOfType(node, TransformerDefinitionNode.class);
            if (closestScope == null || !closestScope.equals(scope)) {
                continue;
            }
            PsiElement identifier = node.getNameIdentifier();
            if (identifier != null && identifier instanceof IdentifierPSINode) {
                results.add(((IdentifierPSINode) identifier));
            }
        }
    }
    if (scope instanceof ForEachStatementNode) {
        VariableReferenceListNode variableReferenceListNode = PsiTreeUtil.getChildOfType(scope, VariableReferenceListNode.class);
        if (variableReferenceListNode != null) {
            List<VariableReferenceNode> variableReferenceNodes = PsiTreeUtil.getChildrenOfTypeAsList(variableReferenceListNode, VariableReferenceNode.class);
            for (VariableReferenceNode variableReferenceNode : variableReferenceNodes) {
                if (variableReferenceNode != null) {
                    IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(variableReferenceNode, IdentifierPSINode.class);
                    if (identifier != null) {
                        results.add(identifier);
                    }
                }
            }
        }
    }
    Collection<AssignmentStatementNode> assignmentStatementNodes = PsiTreeUtil.findChildrenOfType(scope, AssignmentStatementNode.class);
    for (AssignmentStatementNode assignmentStatementNode : assignmentStatementNodes) {
        if (isValidVarAssignment(assignmentStatementNode, scope, caretOffset)) {
            results.addAll(getVariablesFromVarAssignment(assignmentStatementNode));
        }
    }
    return results;
}
Also used : TransformerDefinitionNode(org.ballerinalang.plugins.idea.psi.TransformerDefinitionNode) AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) ExpressionVariableDefinitionStatementNode(org.ballerinalang.plugins.idea.psi.ExpressionVariableDefinitionStatementNode) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) VariableReferenceListNode(org.ballerinalang.plugins.idea.psi.VariableReferenceListNode) LinkedList(java.util.LinkedList) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) ForEachStatementNode(org.ballerinalang.plugins.idea.psi.ForEachStatementNode) 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) NotNull(org.jetbrains.annotations.NotNull)

Example 57 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method resolveReference.

@Nullable
public static <T extends PsiElement> PsiElement resolveReference(@NotNull Collection<T> definitionNodes, @NotNull IdentifierPSINode referenceIdentifier) {
    for (T definitionNode : definitionNodes) {
        IdentifierPSINode fieldName = PsiTreeUtil.getChildOfType(definitionNode, IdentifierPSINode.class);
        if (fieldName == null) {
            continue;
        }
        String text = fieldName.getText();
        if (text.equals(referenceIdentifier.getText())) {
            return fieldName;
        }
    }
    return null;
}
Also used : IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) Nullable(org.jetbrains.annotations.Nullable)

Example 58 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getVariablesFromVarAssignment.

@NotNull
public static List<IdentifierPSINode> getVariablesFromVarAssignment(@NotNull AssignmentStatementNode assignmentStatementNode) {
    List<IdentifierPSINode> results = new LinkedList<>();
    VariableReferenceListNode variableReferenceListNode = PsiTreeUtil.getChildOfType(assignmentStatementNode, VariableReferenceListNode.class);
    if (variableReferenceListNode == null) {
        return results;
    }
    VariableReferenceNode[] variableReferenceNodes = PsiTreeUtil.getChildrenOfType(variableReferenceListNode, VariableReferenceNode.class);
    if (variableReferenceNodes == null) {
        return results;
    }
    for (VariableReferenceNode variableReferenceNode : variableReferenceNodes) {
        IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(variableReferenceNode, IdentifierPSINode.class);
        if (identifier != null && !"_".equals(identifier.getText())) {
            results.add(identifier);
        }
    }
    return results;
}
Also used : IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) VariableReferenceListNode(org.ballerinalang.plugins.idea.psi.VariableReferenceListNode) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 59 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getParameters.

/**
 * Returns parameter for provided definition node.
 *
 * @param definitionNode
 * @return
 */
@NotNull
public static List<IdentifierPSINode> getParameters(@NotNull PsiElement definitionNode) {
    List<IdentifierPSINode> parameters = new LinkedList<>();
    ParameterListNode parameterListNode = PsiTreeUtil.getChildOfType(definitionNode, ParameterListNode.class);
    if (parameterListNode == null) {
        return parameters;
    }
    // Get parameter nodes.
    Collection<ParameterNode> parameterNodes = PsiTreeUtil.getChildrenOfTypeAsList(parameterListNode, ParameterNode.class);
    for (ParameterNode parameterNode : parameterNodes) {
        IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(parameterNode, IdentifierPSINode.class);
        if (identifier == null) {
            continue;
        }
        parameters.add(identifier);
    }
    return parameters;
}
Also used : ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) 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) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 60 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getMatchingElementsFromAFile.

public static <T extends PsiElement> List<IdentifierPSINode> getMatchingElementsFromAFile(@NotNull PsiFile psiFile, @NotNull Class<T> clazz, boolean includePrivate) {
    List<IdentifierPSINode> results = new ArrayList<>();
    Collection<T> definitions = PsiTreeUtil.findChildrenOfType(psiFile, clazz);
    for (T definition : definitions) {
        PsiElement firstChild = definition.getFirstChild();
        if (!includePrivate) {
            if (firstChild instanceof LeafPsiElement) {
                IElementType elementType = ((LeafPsiElement) firstChild).getElementType();
                if (elementType != BallerinaTypes.PUBLIC) {
                    continue;
                }
            } else {
                // is not present.
                continue;
            }
        }
        IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(definition, IdentifierPSINode.class);
        results.add(identifier);
    }
    return results;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

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