Search in sources :

Example 1 with ExpressionVariableDefinitionStatementNode

use of org.ballerinalang.plugins.idea.psi.ExpressionVariableDefinitionStatementNode 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)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 LinkedList (java.util.LinkedList)1 ScopeNode (org.antlr.jetbrains.adaptor.psi.ScopeNode)1 AssignmentStatementNode (org.ballerinalang.plugins.idea.psi.AssignmentStatementNode)1 ExpressionVariableDefinitionStatementNode (org.ballerinalang.plugins.idea.psi.ExpressionVariableDefinitionStatementNode)1 ForEachStatementNode (org.ballerinalang.plugins.idea.psi.ForEachStatementNode)1 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)1 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)1 TransformerDefinitionNode (org.ballerinalang.plugins.idea.psi.TransformerDefinitionNode)1 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)1 VariableReferenceListNode (org.ballerinalang.plugins.idea.psi.VariableReferenceListNode)1 VariableReferenceNode (org.ballerinalang.plugins.idea.psi.VariableReferenceNode)1 NotNull (org.jetbrains.annotations.NotNull)1