Search in sources :

Example 1 with ScopeNode

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

the class BallerinaPsiImplUtil method getAllEndpointsInResolvableScope.

@NotNull
public static List<IdentifierPSINode> getAllEndpointsInResolvableScope(@NotNull ScopeNode scope, int caretOffset) {
    List<IdentifierPSINode> results = new LinkedList<>();
    if (scope instanceof VariableContainer || scope instanceof CodeBlockScope || scope instanceof TopLevelDefinition || scope instanceof LowerLevelDefinition) {
        results.addAll(getAllEndpointsInScope(scope, caretOffset));
        ScopeNode context = scope.getContext();
        if (context != null) {
            results.addAll(getAllEndpointsInResolvableScope(context, caretOffset));
        }
    }
    return results;
}
Also used : VariableContainer(org.ballerinalang.plugins.idea.psi.scopes.VariableContainer) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) LowerLevelDefinition(org.ballerinalang.plugins.idea.psi.scopes.LowerLevelDefinition) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) LinkedList(java.util.LinkedList) CodeBlockScope(org.ballerinalang.plugins.idea.psi.scopes.CodeBlockScope) TopLevelDefinition(org.ballerinalang.plugins.idea.psi.scopes.TopLevelDefinition) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ScopeNode

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

the class BallerinaPsiImplUtil method resolveElementInScope.

@Nullable
public static PsiElement resolveElementInScope(@NotNull IdentifierPSINode identifier, boolean matchLocalVariables, boolean matchParameters, boolean matchGlobalVariables, boolean matchConstants, boolean matchEndpoint) {
    ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
    if (scope == null) {
        return null;
    }
    int caretOffset = identifier.getStartOffset();
    if (matchLocalVariables) {
        List<IdentifierPSINode> variables = BallerinaPsiImplUtil.getAllLocalVariablesInResolvableScope(scope, caretOffset);
        PsiElement element = getMatchingElement(identifier, variables);
        if (element != null) {
            return element;
        }
    }
    if (matchEndpoint) {
        List<IdentifierPSINode> endpoints = BallerinaPsiImplUtil.getAllEndpointsInResolvableScope(scope, caretOffset);
        PsiElement element = getMatchingElement(identifier, endpoints);
        if (element != null) {
            return element;
        }
    }
    if (matchParameters) {
        List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getAllParametersInResolvableScope(scope, caretOffset);
        PsiElement element = getMatchingElement(identifier, parameters);
        if (element != null) {
            return element;
        }
    }
    if (matchGlobalVariables) {
        List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope);
        PsiElement element = getMatchingElement(identifier, globalVariables);
        if (element != null) {
            return element;
        }
    }
    if (matchConstants) {
        List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
        PsiElement element = getMatchingElement(identifier, constants);
        if (element != null) {
            return element;
        }
    }
    return null;
}
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) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ScopeNode

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

the class BallerinaPsiImplUtil method getAllXmlNamespacesInResolvableScope.

@NotNull
public static List<PsiElement> getAllXmlNamespacesInResolvableScope(@NotNull ScopeNode scope, int caretOffset) {
    List<PsiElement> results = new LinkedList<>();
    if (scope instanceof VariableContainer || scope instanceof CodeBlockScope || scope instanceof BallerinaFile) {
        results.addAll(getAllXmlNamespacesInScope(scope, caretOffset));
        ScopeNode context = scope.getContext();
        if (context != null) {
            results.addAll(getAllXmlNamespacesInResolvableScope(context, caretOffset));
        }
    } else if (scope instanceof ParameterContainer || scope instanceof TopLevelDefinition || scope instanceof LowerLevelDefinition) {
        ScopeNode context = scope.getContext();
        if (context != null) {
            results.addAll(getAllXmlNamespacesInResolvableScope(context, caretOffset));
        }
    }
    if (scope instanceof BallerinaFile) {
        PsiFile originalFile = ((BallerinaFile) scope).getOriginalFile();
        PsiDirectory containingPackage = originalFile.getParent();
        if (containingPackage == null) {
            return results;
        }
        PsiFile[] files = containingPackage.getFiles();
        for (PsiFile file : files) {
            // Do't check the current file again.
            if (file.equals(originalFile)) {
                continue;
            }
            Collection<NamespaceDeclarationNode> namespaceDeclarationNodes = PsiTreeUtil.findChildrenOfType(file, NamespaceDeclarationNode.class);
            for (NamespaceDeclarationNode namespaceDeclarationNode : namespaceDeclarationNodes) {
                PsiElement identifier = namespaceDeclarationNode.getNameIdentifier();
                if (identifier != null) {
                    results.add(identifier);
                }
            }
        }
    }
    return results;
}
Also used : BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) LinkedList(java.util.LinkedList) TopLevelDefinition(org.ballerinalang.plugins.idea.psi.scopes.TopLevelDefinition) VariableContainer(org.ballerinalang.plugins.idea.psi.scopes.VariableContainer) ParameterContainer(org.ballerinalang.plugins.idea.psi.scopes.ParameterContainer) NamespaceDeclarationNode(org.ballerinalang.plugins.idea.psi.NamespaceDeclarationNode) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) LowerLevelDefinition(org.ballerinalang.plugins.idea.psi.scopes.LowerLevelDefinition) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) CodeBlockScope(org.ballerinalang.plugins.idea.psi.scopes.CodeBlockScope) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ScopeNode

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

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

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