Search in sources :

Example 11 with ScopeNode

use of org.antlr.jetbrains.adaptor.psi.ScopeNode 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 12 with ScopeNode

use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method getAllLocalVariablesInResolvableScope.

/**
 * Returns all local variables in provided scope and all parent contexts.
 *
 * @param scope
 * @param caretOffset
 * @return
 */
@NotNull
public static List<IdentifierPSINode> getAllLocalVariablesInResolvableScope(@NotNull ScopeNode scope, int caretOffset) {
    List<IdentifierPSINode> results = new LinkedList<>();
    if (scope instanceof VariableContainer || scope instanceof CodeBlockScope) {
        results.addAll(getAllLocalVariablesInScope(scope, caretOffset));
        ScopeNode context = scope.getContext();
        if (context != null && !(scope instanceof RestrictedScope)) {
            results.addAll(getAllLocalVariablesInResolvableScope(context, caretOffset));
        }
    } else if (scope instanceof ParameterContainer || scope instanceof LowerLevelDefinition) {
        ScopeNode context = scope.getContext();
        if (context != null) {
            results.addAll(getAllLocalVariablesInResolvableScope(context, caretOffset));
        }
    }
    return results;
}
Also used : VariableContainer(org.ballerinalang.plugins.idea.psi.scopes.VariableContainer) ParameterContainer(org.ballerinalang.plugins.idea.psi.scopes.ParameterContainer) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) RestrictedScope(org.ballerinalang.plugins.idea.psi.scopes.RestrictedScope) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) LowerLevelDefinition(org.ballerinalang.plugins.idea.psi.scopes.LowerLevelDefinition) LinkedList(java.util.LinkedList) CodeBlockScope(org.ballerinalang.plugins.idea.psi.scopes.CodeBlockScope) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with ScopeNode

use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method isValidVariable.

private static boolean isValidVariable(@NotNull VariableDefinitionNode variableDefinitionNode, @NotNull ScopeNode scope, int caretOffset) {
    PsiElement elementAtCaret = scope.getContainingFile().findElementAt(caretOffset);
    if (elementAtCaret == null) {
        return false;
    }
    ScopeNode closestScope = PsiTreeUtil.getParentOfType(variableDefinitionNode, ScopeNode.class);
    if (closestScope == null) {
        return false;
    }
    if (!closestScope.equals(scope)) {
        return false;
    }
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(elementAtCaret);
    // statement, so there might be other elements before as well.
    if (prevVisibleLeaf == null || ";".equals(prevVisibleLeaf.getText())) {
        // If we are in a new statement,
        PsiElement identifier = variableDefinitionNode.getNameIdentifier();
        if (identifier != null && identifier.getTextOffset() < caretOffset) {
            return true;
        }
        return false;
    }
    // Check previous elements.
    PsiElement prevVisibleLeafParent = PsiTreeUtil.getParentOfType(prevVisibleLeaf, VariableDefinitionNode.class);
    if (prevVisibleLeafParent != null) {
        PsiElement identifier = variableDefinitionNode.getNameIdentifier();
        if (identifier != null && identifier.getTextOffset() < caretOffset && !variableDefinitionNode.equals(prevVisibleLeafParent)) {
            return true;
        }
    } else {
        PsiElement identifier = variableDefinitionNode.getNameIdentifier();
        if (identifier != null && identifier.getTextOffset() < caretOffset) {
            return true;
        }
    }
    return false;
}
Also used : ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 14 with ScopeNode

use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method getNamespaceDefinition.

@Nullable
public static PsiElement getNamespaceDefinition(@NotNull IdentifierPSINode identifier) {
    ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
    if (scope != null) {
        int caretOffset = identifier.getStartOffset();
        List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
        for (PsiElement namespace : namespaces) {
            if (namespace == null || namespace.getText().isEmpty()) {
                continue;
            }
            if (namespace.getText().equals(identifier.getText())) {
                return namespace;
            }
        }
    }
    return null;
}
Also used : ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with ScopeNode

use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.

the class NameSpaceReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    List<LookupElement> results = new LinkedList<>();
    IdentifierPSINode identifier = getElement();
    PsiElement parent = identifier.getParent();
    PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(parent, PackageNameNode.class);
    if (packageNameNode == null) {
        ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
        if (scope != null) {
            int caretOffset = identifier.getStartOffset();
            List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
            results.addAll(BallerinaCompletionUtils.createNamespaceLookupElements(namespaces));
            List<IdentifierPSINode> variables = BallerinaPsiImplUtil.getAllLocalVariablesInResolvableScope(scope, caretOffset);
            results.addAll(BallerinaCompletionUtils.createVariableLookupElements(variables));
            List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getAllParametersInResolvableScope(scope, caretOffset);
            results.addAll(BallerinaCompletionUtils.createParameterLookupElements(parameters));
            List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope);
            results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
            List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
            results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
        }
    }
    return results.toArray(new LookupElement[results.size()]);
}
Also used : PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) LinkedList(java.util.LinkedList) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ScopeNode (org.antlr.jetbrains.adaptor.psi.ScopeNode)18 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)15 PsiElement (com.intellij.psi.PsiElement)12 LinkedList (java.util.LinkedList)11 NotNull (org.jetbrains.annotations.NotNull)11 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)7 LookupElement (com.intellij.codeInsight.lookup.LookupElement)6 PsiDirectory (com.intellij.psi.PsiDirectory)5 PsiFile (com.intellij.psi.PsiFile)5 Nullable (org.jetbrains.annotations.Nullable)5 CodeBlockScope (org.ballerinalang.plugins.idea.psi.scopes.CodeBlockScope)4 LowerLevelDefinition (org.ballerinalang.plugins.idea.psi.scopes.LowerLevelDefinition)4 VariableContainer (org.ballerinalang.plugins.idea.psi.scopes.VariableContainer)4 TopLevelDefinition (org.ballerinalang.plugins.idea.psi.scopes.TopLevelDefinition)3 BallerinaFile (org.ballerinalang.plugins.idea.psi.BallerinaFile)2 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)2 JoinClauseNode (org.ballerinalang.plugins.idea.psi.JoinClauseNode)2 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)2 WorkerDeclarationNode (org.ballerinalang.plugins.idea.psi.WorkerDeclarationNode)2 ParameterContainer (org.ballerinalang.plugins.idea.psi.scopes.ParameterContainer)2