Search in sources :

Example 16 with ScopeNode

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

the class RecordValueReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    List<LookupElement> results = new LinkedList<>();
    IdentifierPSINode identifier = getElement();
    PsiElement prevSibling = identifier.getPrevSibling();
    if (prevSibling != null && prevSibling.getPrevSibling() != null && prevSibling.getPrevSibling() instanceof PackageNameNode) {
        PsiReference reference = prevSibling.getPrevSibling().findReferenceAt(0);
        if (reference != null) {
            PsiElement resolvedElement = reference.resolve();
            if (resolvedElement instanceof PsiDirectory) {
                PsiDirectory currentPackage = (PsiDirectory) resolvedElement;
                List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(currentPackage, true, true);
                results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
                List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(currentPackage, true, true);
                results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
                List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(currentPackage, true, true);
                results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
                List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(currentPackage, true, true);
                results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
                if (!results.isEmpty()) {
                    return results.toArray(new LookupElement[results.size()]);
                }
            }
        }
    }
    PsiFile containingFile = identifier.getContainingFile();
    PsiFile originalFile = containingFile.getOriginalFile();
    List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP, true, AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP);
    results.addAll(packages);
    // Todo - improve suggesting elements from a package in StructValueNode since it will not be identified properly
    RecordKeyValueNode recordKeyValueNode = PsiTreeUtil.getParentOfType(identifier, RecordKeyValueNode.class);
    if (recordKeyValueNode != null) {
        RecordKeyNode mapStructKeyNode = PsiTreeUtil.getChildOfType(recordKeyValueNode, RecordKeyNode.class);
        if (mapStructKeyNode != null) {
            // Todo - use util?
            List<PsiElement> importedPackages = BallerinaPsiImplUtil.getImportedPackages(containingFile);
            for (PsiElement importedPackage : importedPackages) {
                PsiReference reference = importedPackage.findReferenceAt(0);
                if (reference == null) {
                    continue;
                }
                PsiElement resolvedElement = reference.resolve();
                if (resolvedElement == null) {
                    continue;
                }
                PsiDirectory resolvedPackage = (PsiDirectory) resolvedElement;
                if (mapStructKeyNode.getText().equals(resolvedPackage.getName())) {
                    List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(resolvedPackage, false, false);
                    results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
                    Collection<EnumFieldNode> enums = PsiTreeUtil.findChildrenOfType(resolvedPackage, EnumFieldNode.class);
                    results.addAll(BallerinaCompletionUtils.createEnumFieldLookupElements(enums, (IdentifierPSINode) resolvedElement));
                    List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(resolvedPackage, false, false);
                    results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
                    List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(resolvedPackage, false, false);
                    results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
                }
            }
        }
    }
    PsiDirectory currentPackage = originalFile.getParent();
    if (currentPackage != null) {
        List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(currentPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
        List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(currentPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
        List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(currentPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
        List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(currentPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
    }
    // Todo - use a util method
    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);
        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 : RecordKeyValueNode(org.ballerinalang.plugins.idea.psi.RecordKeyValueNode) PsiReference(com.intellij.psi.PsiReference) LookupElement(com.intellij.codeInsight.lookup.LookupElement) LinkedList(java.util.LinkedList) RecordKeyNode(org.ballerinalang.plugins.idea.psi.RecordKeyNode) EnumFieldNode(org.ballerinalang.plugins.idea.psi.EnumFieldNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with ScopeNode

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

the class StatementReference method getVariantsFromCurrentPackage.

@NotNull
private List<LookupElement> getVariantsFromCurrentPackage() {
    List<LookupElement> results = new LinkedList<>();
    IdentifierPSINode identifier = getElement();
    PsiFile containingFile = identifier.getContainingFile();
    PsiFile originalFile = containingFile.getOriginalFile();
    List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP, true, AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP);
    results.addAll(packages);
    // Todo - use a util method
    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);
        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));
    }
    PsiDirectory containingPackage = originalFile.getParent();
    if (containingPackage != null) {
        List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
    }
    return results;
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with ScopeNode

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

the class FunctionReference method getVariantsFromCurrentPackage.

@NotNull
private List<LookupElement> getVariantsFromCurrentPackage() {
    List<LookupElement> results = new LinkedList<>();
    IdentifierPSINode identifier = getElement();
    PsiFile containingFile = identifier.getContainingFile();
    PsiFile originalFile = containingFile.getOriginalFile();
    PsiDirectory containingPackage = originalFile.getParent();
    if (containingPackage != null) {
        List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions, ParenthesisInsertHandler.INSTANCE));
    }
    // Todo - use a util method
    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 (BallerinaPsiImplUtil.isALambdaFunction(variable)) {
                results.add(BallerinaCompletionUtils.createVariableLookupElement(variable));
            }
        }
        List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getAllParametersInResolvableScope(scope, caretOffset);
        for (IdentifierPSINode parameter : parameters) {
            if (BallerinaPsiImplUtil.isALambdaFunction(parameter)) {
                results.add(BallerinaCompletionUtils.createParameterLookupElement(parameter));
            }
        }
        List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope);
        for (IdentifierPSINode variable : globalVariables) {
            if (BallerinaPsiImplUtil.isALambdaFunction(variable)) {
                results.add(BallerinaCompletionUtils.createGlobalVariableLookupElement(variable));
            }
        }
    }
    List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP, true, AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP);
    results.addAll(packages);
    return results;
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) LinkedList(java.util.LinkedList) 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