Search in sources :

Example 86 with IdentifierPSINode

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

the class StatementReference method resolve.

@Nullable
@Override
public PsiElement resolve() {
    IdentifierPSINode identifier = getElement();
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
    if (prevVisibleLeaf != null && ":".equals(prevVisibleLeaf.getText())) {
        PsiElement packageNameNode = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
        if (packageNameNode == null) {
            return null;
        }
        PsiElement resolvedElement = BallerinaPsiImplUtil.resolvePackage(packageNameNode);
        if (resolvedElement == null || !(resolvedElement instanceof PsiDirectory)) {
            return null;
        }
        PsiDirectory containingPackage = (PsiDirectory) resolvedElement;
        return BallerinaPsiImplUtil.resolveElementInPackage(containingPackage, identifier, true, true, true, true, true, true, false, false);
    } else {
        return BallerinaPsiImplUtil.resolveElementInScope(identifier, true, true, true, true, true);
    }
}
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 87 with IdentifierPSINode

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

the class StatementReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    List<LookupElement> results = new LinkedList<>();
    IdentifierPSINode identifier = getElement();
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
    if (prevVisibleLeaf == null) {
        return new LookupElement[0];
    }
    if (":".equals(prevVisibleLeaf.getText())) {
        PsiElement packageNameNode = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
        if (packageNameNode == null || !(packageNameNode instanceof IdentifierPSINode)) {
            return new LookupElement[0];
        }
        results.addAll(getVariantsFromPackage(packageNameNode));
    } else if (".".equals(prevVisibleLeaf.getText())) {
        // Todo - suggest length field
        PsiElement previousField = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
        if (previousField == null) {
            return new LookupElement[0];
        }
        PsiReference reference = previousField.findReferenceAt(0);
        if (reference == null) {
            return new Object[0];
        }
        PsiElement resolvedElement = reference.resolve();
        if (resolvedElement == null) {
            return new LookupElement[0];
        }
        // Todo - use util method
        PsiElement resolvedElementParent = resolvedElement.getParent();
        StructDefinitionNode structDefinitionNode = null;
        // Resolve the corresponding resolvedElementParent to get the struct definition.
        if (resolvedElementParent instanceof VariableDefinitionNode) {
            structDefinitionNode = BallerinaPsiImplUtil.resolveStructFromDefinitionNode(((VariableDefinitionNode) resolvedElementParent));
        } else if (resolvedElementParent instanceof FieldDefinitionNode) {
            structDefinitionNode = BallerinaPsiImplUtil.resolveTypeNodeStruct((resolvedElementParent));
        }
        if (structDefinitionNode == null) {
            return results.toArray(new LookupElement[results.size()]);
        }
        Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
        List<LookupElement> fields = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
        results.addAll(fields);
    } else {
        results.addAll(getVariantsFromCurrentPackage());
    }
    return results.toArray(new LookupElement[results.size()]);
}
Also used : VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) Collection(java.util.Collection) LinkedList(java.util.LinkedList) List(java.util.List) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) LinkedList(java.util.LinkedList) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 88 with IdentifierPSINode

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

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

the class StructReference 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();
    List<IdentifierPSINode> structs = BallerinaPsiImplUtil.getAllStructsFromPackage(containingPackage, true, true);
    results.addAll(BallerinaCompletionUtils.createStructLookupElements(structs));
    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) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 90 with IdentifierPSINode

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

the class StructReference method getVariantsFromPackage.

@NotNull
private Collection<? extends LookupElement> getVariantsFromPackage(PackageNameNode packageNameNode) {
    List<LookupElement> results = new LinkedList<>();
    PsiElement resolvedElement = BallerinaPsiImplUtil.resolvePackage(packageNameNode);
    if (resolvedElement == null || !(resolvedElement instanceof PsiDirectory)) {
        return results;
    }
    PsiDirectory containingPackage = (PsiDirectory) resolvedElement;
    List<IdentifierPSINode> structs = BallerinaPsiImplUtil.getAllStructsFromPackage(containingPackage, false, false);
    results.addAll(BallerinaCompletionUtils.createStructLookupElements(structs));
    return results;
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) 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)

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