Search in sources :

Example 66 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getAllMatchingElementsFromPackage.

/**
 * Returns all the elements in the given directory(package) which matches the given xpath.
 *
 * @param directory which is used to get the functions
 * @return all functions in the given directory(package)
 */
@NotNull
private static <T extends PsiElement> List<IdentifierPSINode> getAllMatchingElementsFromPackage(@NotNull PsiDirectory directory, @NotNull Class<T> clazz, boolean includePrivate, boolean includeBuiltIns) {
    Project project = directory.getProject();
    List<IdentifierPSINode> results = new ArrayList<>();
    VirtualFile virtualFile = directory.getVirtualFile();
    VirtualFile[] children = virtualFile.getChildren();
    for (VirtualFile child : children) {
        if (child.isDirectory()) {
            continue;
        }
        PsiFile psiFile = PsiManager.getInstance(project).findFile(child);
        if (!(psiFile instanceof BallerinaFile)) {
            continue;
        }
        results.addAll(getMatchingElementsFromAFile(psiFile, clazz, includePrivate));
    }
    if (includeBuiltIns) {
        // Add elements from built-in packages
        for (String builtInDirectory : builtInDirectories) {
            VirtualFile file = BallerinaPsiImplUtil.findFileInSDK(project, directory, builtInDirectory);
            if (file == null) {
                return results;
            }
            VirtualFile[] builtInFiles = file.getChildren();
            for (VirtualFile builtInFile : builtInFiles) {
                if (builtInFile.isDirectory() || !"bal".equals(builtInFile.getExtension())) {
                    continue;
                }
                // Find the file.
                PsiFile psiFile = PsiManager.getInstance(project).findFile(builtInFile);
                if (psiFile == null) {
                    return results;
                }
                results.addAll(getMatchingElementsFromAFile(psiFile, clazz, includePrivate));
            }
        }
    }
    return results;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) NotNull(org.jetbrains.annotations.NotNull)

Example 67 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getAttachedFunctions.

@NotNull
public static List<IdentifierPSINode> getAttachedFunctions(@NotNull StructDefinitionNode structDefinitionNode) {
    List<IdentifierPSINode> attachedFunctions = new LinkedList<>();
    PsiFile containingFile = structDefinitionNode.getContainingFile();
    PsiDirectory containingPackage = containingFile.getParent();
    if (containingPackage != null) {
        ApplicationManager.getApplication().runReadAction(() -> {
            List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, false, false);
            for (IdentifierPSINode function : functions) {
                ProgressManager.checkCanceled();
                if (!isAttachedFunction(function)) {
                    continue;
                }
                PsiElement attachedStruct = getAttachedStruct(function);
                if (attachedStruct != null && !attachedStruct.equals(structDefinitionNode)) {
                    continue;
                }
                attachedFunctions.add(function);
            }
        });
    }
    return attachedFunctions;
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) LinkedList(java.util.LinkedList) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 68 with IdentifierPSINode

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

Example 69 with IdentifierPSINode

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

the class LanguageInjectorUtils method isValid.

/**
 * Checks whether the provided host is suitable for language injecting.
 *
 * @param host          host string to be checked
 * @param packageNames  valid package names
 * @param functionNames valid function names
 * @return {@code true} if suitable for injecting language, {@code false} otherwise.
 */
public static boolean isValid(@NotNull PsiLanguageInjectionHost host, @NotNull Set<String> packageNames, @NotNull Set<String> functionNames) {
    if (!(host instanceof QuotedLiteralString)) {
        return false;
    }
    ExpressionListNode expressionListNode = PsiTreeUtil.getTopmostParentOfType(host, ExpressionListNode.class);
    if (expressionListNode == null) {
        return false;
    }
    PsiElement firstChild = expressionListNode.getFirstChild();
    if (firstChild.getChildren().length > 1) {
        return false;
    }
    FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getPrevSiblingOfType(expressionListNode, FunctionReferenceNode.class);
    if (functionReferenceNode == null) {
        return false;
    }
    PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(functionReferenceNode, PackageNameNode.class);
    if (packageNameNode == null) {
        return false;
    }
    IdentifierPSINode functionName = PsiTreeUtil.getChildOfType(functionReferenceNode, IdentifierPSINode.class);
    if (functionName == null) {
        return false;
    }
    ExpressionNode expressionNode = PsiTreeUtil.getParentOfType(host, ExpressionNode.class);
    if (expressionNode == null) {
        return false;
    }
    Collection<QuotedLiteralString> quotedLiteralStrings = PsiTreeUtil.findChildrenOfType(expressionNode, QuotedLiteralString.class);
    // We ignore this case since the string might not be identified correctly and will cause issues.
    if (quotedLiteralStrings.size() > 1) {
        return false;
    }
    return packageNames.contains(packageNameNode.getText()) && functionNames.contains(functionName.getText());
}
Also used : ExpressionListNode(org.ballerinalang.plugins.idea.psi.ExpressionListNode) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) PsiElement(com.intellij.psi.PsiElement)

Example 70 with IdentifierPSINode

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

the class ActionInvocationReference method resolve.

@Nullable
@Override
public PsiElement resolve() {
    IdentifierPSINode identifier = getElement();
    PsiElement parent = identifier.getParent();
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(parent);
    PsiReference variableReference = null;
    if (prevVisibleLeaf != null && ".".equals(prevVisibleLeaf.getText())) {
        PsiElement connectorVariable = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
        if (connectorVariable != null) {
            variableReference = connectorVariable.findReferenceAt(connectorVariable.getTextLength());
        }
    } else {
        PsiElement prevSibling = parent.getPrevSibling();
        variableReference = prevSibling.findReferenceAt(prevSibling.getTextLength());
    }
    if (variableReference == null) {
        return null;
    }
    PsiElement variableDefinition = variableReference.resolve();
    if (variableDefinition == null) {
        return null;
    }
    PsiElement variableDefinitionParent = variableDefinition.getParent();
    ConnectorDefinitionNode connectorDefinitionNode;
    if (variableDefinitionParent instanceof EndpointDeclarationNode) {
        connectorDefinitionNode = BallerinaPsiImplUtil.getConnectorDefinition(((EndpointDeclarationNode) variableDefinitionParent));
    } else {
        connectorDefinitionNode = BallerinaPsiImplUtil.resolveConnectorFromVariableDefinitionNode(variableDefinitionParent);
    }
    if (connectorDefinitionNode == null) {
        return null;
    }
    Collection<ActionDefinitionNode> actionDefinitionNodes = PsiTreeUtil.findChildrenOfType(connectorDefinitionNode, ActionDefinitionNode.class);
    for (ActionDefinitionNode actionDefinitionNode : actionDefinitionNodes) {
        IdentifierPSINode actionIdentifier = PsiTreeUtil.getChildOfType(actionDefinitionNode, IdentifierPSINode.class);
        if (actionIdentifier == null) {
            continue;
        }
        if (actionIdentifier.getText().equals(identifier.getText())) {
            return actionIdentifier;
        }
    }
    return null;
}
Also used : EndpointDeclarationNode(org.ballerinalang.plugins.idea.psi.EndpointDeclarationNode) ActionDefinitionNode(org.ballerinalang.plugins.idea.psi.ActionDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) ConnectorDefinitionNode(org.ballerinalang.plugins.idea.psi.ConnectorDefinitionNode) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

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