Search in sources :

Example 76 with IdentifierPSINode

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

the class NameReference method resolveInPackage.

@Nullable
private PsiElement resolveInPackage(@NotNull PackageNameNode packageNameNode) {
    PsiElement resolvedElement = BallerinaPsiImplUtil.resolvePackage(packageNameNode);
    if (resolvedElement == null || !(resolvedElement instanceof PsiDirectory)) {
        return null;
    }
    PsiDirectory psiDirectory = (PsiDirectory) resolvedElement;
    IdentifierPSINode identifier = getElement();
    return BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, false, false);
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 77 with IdentifierPSINode

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

the class NameReference 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) {
        results.addAll(getVariantsFromCurrentPackage());
    } else {
        results.addAll(getVariantsFromPackage(packageNameNode));
    }
    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) LinkedList(java.util.LinkedList) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 78 with IdentifierPSINode

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

the class NameReference method resolveInCurrentPackage.

@Nullable
private PsiElement resolveInCurrentPackage() {
    IdentifierPSINode identifier = getElement();
    PsiFile containingFile = identifier.getContainingFile();
    if (containingFile == null) {
        return null;
    }
    PsiFile originalFile = containingFile.getOriginalFile();
    PsiDirectory psiDirectory = originalFile.getParent();
    if (psiDirectory == null) {
        return null;
    }
    // If the next element is '(', that means we are at a function invocation node. So match any matching
    // function first. If no matching function found, match with variables since this can be a lambda function.
    // If the next element is not '(', we match variables first.
    PsiElement nextVisibleLeaf = PsiTreeUtil.nextVisibleLeaf(identifier);
    if (nextVisibleLeaf != null && "(".equals(nextVisibleLeaf.getText())) {
        PsiElement element = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, true, true);
        if (element != null) {
            return element;
        }
        return BallerinaPsiImplUtil.resolveElementInScope(identifier, true, true, true, true, true);
    } else {
        PsiElement elementInScope = BallerinaPsiImplUtil.resolveElementInScope(identifier, true, true, true, true, true);
        if (elementInScope != null) {
            return elementInScope;
        }
        PsiElement resolvedElement;
        TypeNameNode typeNameNode = PsiTreeUtil.getParentOfType(identifier, TypeNameNode.class);
        if (typeNameNode == null) {
            resolvedElement = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, true, true, true, true, true, true, true, true);
        } else {
            resolvedElement = BallerinaPsiImplUtil.resolveElementInPackage(psiDirectory, identifier, false, true, true, true, false, false, true, true);
        }
        if (resolvedElement != null) {
            return resolvedElement;
        }
        return BallerinaPsiImplUtil.getNamespaceDefinition(identifier);
    }
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 79 with IdentifierPSINode

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

Example 80 with IdentifierPSINode

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

the class PackageNameReference method multiResolve.

@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
    IdentifierPSINode identifier = getElement();
    if (identifier == null) {
        return new ResolveResult[0];
    }
    AliasNode aliasNode = PsiTreeUtil.getParentOfType(identifier, AliasNode.class);
    if (aliasNode != null) {
        return new ResolveResult[0];
    }
    List<ResolveResult> results = new ArrayList<>();
    ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(identifier, ImportDeclarationNode.class);
    if (importDeclarationNode != null) {
        List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(identifier);
        for (PsiDirectory directory : directories) {
            results.add(new PsiElementResolveResult(directory));
        }
        // Return the results.
        return results.toArray(new ResolveResult[results.size()]);
    }
    PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.getParentOfType(identifier, PackageDeclarationNode.class);
    if (packageDeclarationNode != null) {
        // If this is a package declaration, resolve the directory.
        List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(identifier);
        for (PsiDirectory directory : directories) {
            results.add(new PsiElementResolveResult(directory));
        }
        // Return the results.
        return results.toArray(new ResolveResult[results.size()]);
    }
    PsiFile containingFile = identifier.getContainingFile();
    if (containingFile == null) {
        return new ResolveResult[0];
    }
    List<PsiElement> importedPackages = BallerinaPsiImplUtil.getImportedPackagesInCurrentFile(containingFile);
    for (PsiElement importedPackage : importedPackages) {
        String packageName = importedPackage.getText();
        if (packageName == null || packageName.isEmpty()) {
            continue;
        }
        if (packageName.equals(identifier.getText())) {
            PsiReference reference = importedPackage.findReferenceAt(0);
            if (reference != null) {
                PsiElement resolvedElement = reference.resolve();
                if (resolvedElement != null) {
                    results.add(new PsiElementResolveResult(resolvedElement));
                }
            }
        }
    }
    importedPackages = BallerinaPsiImplUtil.getPackagesImportedAsAliasInCurrentFile(containingFile);
    for (PsiElement importedPackage : importedPackages) {
        String packageName = importedPackage.getText();
        if (packageName == null || packageName.isEmpty()) {
            continue;
        }
        if (packageName.equals(identifier.getText())) {
            IdentifierPSINode nameNode = PsiTreeUtil.findChildOfType(importedPackage, IdentifierPSINode.class);
            if (nameNode != null) {
                results.add(new PsiElementResolveResult(nameNode));
            }
        }
    }
    return results.toArray(new ResolveResult[results.size()]);
}
Also used : ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) PackageDeclarationNode(org.ballerinalang.plugins.idea.psi.PackageDeclarationNode) PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) AliasNode(org.ballerinalang.plugins.idea.psi.AliasNode) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)109 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)63 LinkedList (java.util.LinkedList)58 LookupElement (com.intellij.codeInsight.lookup.LookupElement)48 Nullable (org.jetbrains.annotations.Nullable)32 PsiDirectory (com.intellij.psi.PsiDirectory)29 PsiReference (com.intellij.psi.PsiReference)25 PsiFile (com.intellij.psi.PsiFile)24 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)20 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)17 ScopeNode (org.antlr.jetbrains.adaptor.psi.ScopeNode)15 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)12 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)12 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)12 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)10 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)9 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)9 ArrayList (java.util.ArrayList)8 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)8