Search in sources :

Example 16 with BallerinaFile

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

the class BallerinaPsiImplUtil method getAllGlobalVariablesInScope.

@NotNull
public static List<IdentifierPSINode> getAllGlobalVariablesInScope(@NotNull ScopeNode scope, int caretOffset) {
    List<IdentifierPSINode> results = new LinkedList<>();
    Collection<GlobalVariableDefinitionNode> variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(scope, GlobalVariableDefinitionNode.class);
    for (GlobalVariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
        PsiElement identifier = variableDefinitionNode.getNameIdentifier();
        if (caretOffset != -1) {
            if (identifier == null || !(identifier instanceof IdentifierPSINode) || identifier.getTextOffset() > caretOffset) {
                continue;
            }
            PsiElement element = scope.findElementAt(caretOffset);
            if (element != null) {
                GlobalVariableDefinitionNode definition = PsiTreeUtil.getParentOfType(element, GlobalVariableDefinitionNode.class);
                if (definition != null && definition.equals(variableDefinitionNode)) {
                    continue;
                }
            }
        }
        results.add(((IdentifierPSINode) identifier));
    }
    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;
        }
        variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(file, GlobalVariableDefinitionNode.class);
        for (GlobalVariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
            PsiElement identifier = variableDefinitionNode.getNameIdentifier();
            if (identifier != null && identifier instanceof IdentifierPSINode) {
                results.add(((IdentifierPSINode) identifier));
            }
        }
    }
    return results;
}
Also used : BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) LinkedList(java.util.LinkedList) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

BallerinaFile (org.ballerinalang.plugins.idea.psi.BallerinaFile)16 PsiElement (com.intellij.psi.PsiElement)12 PsiFile (com.intellij.psi.PsiFile)9 LinkedList (java.util.LinkedList)7 NotNull (org.jetbrains.annotations.NotNull)7 PsiDirectory (com.intellij.psi.PsiDirectory)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ArrayList (java.util.ArrayList)4 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)4 PackageDeclarationNode (org.ballerinalang.plugins.idea.psi.PackageDeclarationNode)4 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)4 Nullable (org.jetbrains.annotations.Nullable)4 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)3 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)3 PsiReference (com.intellij.psi.PsiReference)3 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 FullyQualifiedPackageNameNode (org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode)3 ImportDeclarationNode (org.ballerinalang.plugins.idea.psi.ImportDeclarationNode)3 RuntimeConfigurationError (com.intellij.execution.configurations.RuntimeConfigurationError)2 Module (com.intellij.openapi.module.Module)2