Search in sources :

Example 61 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getAllActionsFromAConnector.

/**
 * Returns all actions/native actions defined in the given ConnectorDefinitionNode.
 *
 * @param connectorDefinitionNode PsiElement which represent a Connector Definition
 * @return List of all actions/native actions defined in the given ConnectorDefinitionNode.
 */
@NotNull
public static List<IdentifierPSINode> getAllActionsFromAConnector(@NotNull PsiElement connectorDefinitionNode) {
    List<IdentifierPSINode> results = new ArrayList<>();
    Collection<ActionDefinitionNode> actionDefinitionNodes = PsiTreeUtil.findChildrenOfType(connectorDefinitionNode, ActionDefinitionNode.class);
    for (ActionDefinitionNode definitionNode : actionDefinitionNodes) {
        IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(definitionNode, IdentifierPSINode.class);
        results.add(identifier);
    }
    return results;
}
Also used : ActionDefinitionNode(org.ballerinalang.plugins.idea.psi.ActionDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getErrorStruct.

/**
 * Returns corresponding error struct for type casts and type conversions.
 *
 * @param assignmentStatementNode {@link AssignmentStatementNode} which we are currently checking
 * @param referenceNode           resolved variable reference node
 * @param isTypeCast              indicates whether the {@link AssignmentStatementNode} contains a type cast
 * @param isTypeConversion        indicates whether the {@link AssignmentStatementNode} contains a type conversion
 * @return {@code TypeCastError} if the provided assignment statement is a type cast operation.
 * {@code TypeConversionError} if the provided assignment statement is a type conversion operation.
 */
@Nullable
public static StructDefinitionNode getErrorStruct(@NotNull AssignmentStatementNode assignmentStatementNode, @NotNull IdentifierPSINode referenceNode, boolean isTypeCast, boolean isTypeConversion) {
    List<IdentifierPSINode> variablesFromVarAssignment = BallerinaPsiImplUtil.getVariablesFromVarAssignment(assignmentStatementNode);
    // might be unnecessary and identified at the semantic analyzer.
    if (variablesFromVarAssignment.size() < 2) {
        return null;
    }
    // Get the second value from the assignment statement.
    IdentifierPSINode errorVariable = variablesFromVarAssignment.get(1);
    // This should be the provided reference node. Since the error is returned as the second variable.
    if (!errorVariable.equals(referenceNode)) {
        return null;
    }
    // Now we need to find the error.bal file which contains the definition of these error structs.
    Project project = referenceNode.getProject();
    VirtualFile errorFile = BallerinaPsiImplUtil.findFileInSDK(project, referenceNode, "/ballerina/builtin/core/source.bal");
    if (errorFile == null) {
        return null;
    }
    // Find the file.
    PsiFile psiFile = PsiManager.getInstance(project).findFile(errorFile);
    if (psiFile == null) {
        return null;
    }
    // Get all struct definitions in the file.
    Collection<StructDefinitionNode> structDefinitionNodes = PsiTreeUtil.findChildrenOfType(psiFile, StructDefinitionNode.class);
    // Iterate through all struct definitions.
    for (StructDefinitionNode definitionNode : structDefinitionNodes) {
        IdentifierPSINode nameNode = PsiTreeUtil.getChildOfType(definitionNode, IdentifierPSINode.class);
        if (nameNode != null) {
            // Check and return the matching definition.
            if (isTypeCast && "TypeCastError".equals(nameNode.getText())) {
                return definitionNode;
            }
            if (isTypeConversion && "TypeConversionError".equals(nameNode.getText())) {
                return definitionNode;
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 63 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getAllEndpointsInScope.

@NotNull
private static List<IdentifierPSINode> getAllEndpointsInScope(@NotNull ScopeNode scope, int caretOffset) {
    List<IdentifierPSINode> results = new LinkedList<>();
    Collection<EndpointDeclarationNode> endpointDeclarationNodes = PsiTreeUtil.getChildrenOfTypeAsList(scope, EndpointDeclarationNode.class);
    for (EndpointDeclarationNode endpointDeclarationNode : endpointDeclarationNodes) {
        IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(endpointDeclarationNode, IdentifierPSINode.class);
        if (identifier != null) {
            results.add((identifier));
        }
    }
    return results;
}
Also used : EndpointDeclarationNode(org.ballerinalang.plugins.idea.psi.EndpointDeclarationNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 64 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method getAllLocalVariablesInResolvableScope.

/**
 * Returns all local variables in provided scope and all parent contexts.
 *
 * @param scope
 * @param caretOffset
 * @return
 */
@NotNull
public static List<IdentifierPSINode> getAllLocalVariablesInResolvableScope(@NotNull ScopeNode scope, int caretOffset) {
    List<IdentifierPSINode> results = new LinkedList<>();
    if (scope instanceof VariableContainer || scope instanceof CodeBlockScope) {
        results.addAll(getAllLocalVariablesInScope(scope, caretOffset));
        ScopeNode context = scope.getContext();
        if (context != null && !(scope instanceof RestrictedScope)) {
            results.addAll(getAllLocalVariablesInResolvableScope(context, caretOffset));
        }
    } else if (scope instanceof ParameterContainer || scope instanceof LowerLevelDefinition) {
        ScopeNode context = scope.getContext();
        if (context != null) {
            results.addAll(getAllLocalVariablesInResolvableScope(context, caretOffset));
        }
    }
    return results;
}
Also used : VariableContainer(org.ballerinalang.plugins.idea.psi.scopes.VariableContainer) ParameterContainer(org.ballerinalang.plugins.idea.psi.scopes.ParameterContainer) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) RestrictedScope(org.ballerinalang.plugins.idea.psi.scopes.RestrictedScope) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) LowerLevelDefinition(org.ballerinalang.plugins.idea.psi.scopes.LowerLevelDefinition) LinkedList(java.util.LinkedList) CodeBlockScope(org.ballerinalang.plugins.idea.psi.scopes.CodeBlockScope) NotNull(org.jetbrains.annotations.NotNull)

Example 65 with IdentifierPSINode

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

the class BallerinaPsiImplUtil method resolveAliasNode.

/**
 * Resolves the given alias node to the corresponding directory.
 *
 * @param aliasNode an alias node
 * @return {@link PsiDirectory} which is the definition of the alias node.
 */
@Nullable
public static PsiDirectory resolveAliasNode(@NotNull AliasNode aliasNode) {
    ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(aliasNode, ImportDeclarationNode.class);
    FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.getChildOfType(importDeclarationNode, FullyQualifiedPackageNameNode.class);
    if (fullyQualifiedPackageNameNode == null) {
        return null;
    }
    PackageNameNode[] packageNameNodes = PsiTreeUtil.getChildrenOfType(fullyQualifiedPackageNameNode, PackageNameNode.class);
    if (packageNameNodes == null) {
        return null;
    }
    PackageNameNode lastElement = ArrayUtil.getLastElement(packageNameNodes);
    if (lastElement == null) {
        return null;
    }
    PsiElement packageName = lastElement.getNameIdentifier();
    if (!(packageName instanceof IdentifierPSINode)) {
        return null;
    }
    List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(((IdentifierPSINode) packageName));
    if (directories.isEmpty()) {
        return null;
    }
    return directories.get(0);
}
Also used : FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) 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