Search in sources :

Example 1 with InvocationNode

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

the class BallerinaPsiImplUtil method getStructDefinition.

/**
 * Finds a {@link StructDefinitionNode} which corresponds to the provided {@link VariableReferenceNode}.
 *
 * @param variableReferenceNode   an variable reference node in the assignment statement node
 * @param assignmentStatementNode an assignment statement node
 * @param structReferenceNode     an identifier which is a var variable in the assignment node
 * @return
 */
private static StructDefinitionNode getStructDefinition(@NotNull VariableReferenceNode variableReferenceNode, @NotNull AssignmentStatementNode assignmentStatementNode, @NotNull IdentifierPSINode structReferenceNode) {
    InvocationNode invocationNode = PsiTreeUtil.getChildOfType(variableReferenceNode, InvocationNode.class);
    if (invocationNode != null) {
        AnyIdentifierNameNode anyIdentifierNameNode = PsiTreeUtil.getChildOfType(invocationNode, AnyIdentifierNameNode.class);
        if (anyIdentifierNameNode != null) {
            IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(anyIdentifierNameNode, IdentifierPSINode.class);
            if (identifier != null) {
                PsiReference reference = identifier.findReferenceAt(identifier.getTextLength());
                if (reference == null) {
                    return null;
                }
                PsiElement resolvedElement = reference.resolve();
                if (resolvedElement == null) {
                    return null;
                }
                // Check whether the resolved element's parent is a connector definition.
                PsiElement definitionNode = resolvedElement.getParent();
                if (!(definitionNode instanceof ActionDefinitionNode)) {
                    return null;
                }
                return getStructDefinition(assignmentStatementNode, structReferenceNode, definitionNode);
            }
        }
    }
    // Get the first child.
    PsiElement node = variableReferenceNode.getFirstChild();
    // If te first child is a VariableReferenceNode, it can be a function invocation.
    if (node instanceof VariableReferenceNode) {
        // Check whether the node is a function invocation.
        boolean isFunctionInvocation = isFunctionInvocation((VariableReferenceNode) node);
        if (isFunctionInvocation) {
            // If it is a function invocation, the first child node will contain the function name.
            PsiElement functionName = node.getFirstChild();
            // We need to resolve the function name to the corresponding function definition.
            PsiReference reference = functionName.findReferenceAt(functionName.getTextLength());
            if (reference == null) {
                return null;
            }
            PsiElement resolvedFunctionIdentifier = reference.resolve();
            if (resolvedFunctionIdentifier == null) {
                return null;
            }
            // Check whether the resolved element's parent is a function definition.
            PsiElement definitionNode = resolvedFunctionIdentifier.getParent();
            if (!(definitionNode instanceof FunctionDefinitionNode)) {
                return null;
            }
            return getStructDefinition(assignmentStatementNode, structReferenceNode, definitionNode);
        }
    } else if (node instanceof NameReferenceNode) {
        // If the node is a NameReferenceNode, that means RHS contains a variable.
        PsiReference reference = node.findReferenceAt(node.getTextLength());
        if (reference == null) {
            return null;
        }
        // We resolve that variable to the definition.
        PsiElement resolvedDefinition = reference.resolve();
        if (resolvedDefinition == null || !(resolvedDefinition instanceof IdentifierPSINode)) {
            return null;
        }
        // Then recursively call the findStructDefinition method to get the correct definition.
        return findStructDefinition((IdentifierPSINode) resolvedDefinition);
    } else if (node instanceof FunctionInvocationNode) {
        FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getChildOfType(node, FunctionReferenceNode.class);
        if (functionReferenceNode == null) {
            return null;
        }
        PsiReference reference = functionReferenceNode.findReferenceAt(functionReferenceNode.getTextLength());
        if (reference == null) {
            return null;
        }
        PsiElement resolvedDefinition = reference.resolve();
        if (resolvedDefinition == null || !(resolvedDefinition instanceof IdentifierPSINode)) {
            return null;
        }
        PsiElement definitionNode = resolvedDefinition.getParent();
        if (!(definitionNode instanceof FunctionDefinitionNode)) {
            return null;
        }
        return getStructDefinition(assignmentStatementNode, structReferenceNode, definitionNode);
    }
    return null;
}
Also used : AnyIdentifierNameNode(org.ballerinalang.plugins.idea.psi.AnyIdentifierNameNode) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) ActionDefinitionNode(org.ballerinalang.plugins.idea.psi.ActionDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiReference(com.intellij.psi.PsiReference) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) InvocationNode(org.ballerinalang.plugins.idea.psi.InvocationNode) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode)

Example 2 with InvocationNode

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

the class RecordKeyReference method getVariantsFromCurrentPackage.

@NotNull
private List<LookupElement> getVariantsFromCurrentPackage() {
    List<LookupElement> results = new LinkedList<>();
    IdentifierPSINode identifier = getElement();
    VariableDefinitionNode variableDefinitionNode = PsiTreeUtil.getParentOfType(identifier, VariableDefinitionNode.class);
    InvocationNode invocationNode = PsiTreeUtil.getParentOfType(identifier, InvocationNode.class);
    if (variableDefinitionNode == null || invocationNode != null) {
        StructDefinitionNode structDefinitionNode = resolveStructDefinition(identifier);
        if (structDefinitionNode == null) {
            // Todo - Check for enclosing {} since the parse errors might cause issues when identifying
            // RecordLiteralNode element
            // RecordLiteralNode mapStructLiteralNode = PsiTreeUtil.getParentOfType(identifier,
            // RecordLiteralNode.class);
            // if (mapStructLiteralNode == null) {
            // return results;
            // }
            // Try to get fields from an anonymous struct.
            PsiElement definitionNode = BallerinaPsiImplUtil.resolveAnonymousStruct(identifier);
            if (definitionNode == null) {
                return results;
            }
            if (definitionNode instanceof AnonStructTypeNameNode) {
                StructBodyNode structBodyNode = PsiTreeUtil.findChildOfType(definitionNode, StructBodyNode.class);
                if (structBodyNode == null) {
                    return results;
                }
                List<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.getChildrenOfTypeAsList(structBodyNode, FieldDefinitionNode.class);
                results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
                return results;
            }
            structDefinitionNode = ((StructDefinitionNode) definitionNode);
            IdentifierPSINode structNameNode = PsiTreeUtil.getChildOfType(structDefinitionNode, IdentifierPSINode.class);
            if (structNameNode == null) {
                return results;
            }
            Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
            results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, structNameNode, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
            return results;
        }
        IdentifierPSINode structNameNode = PsiTreeUtil.getChildOfType(structDefinitionNode, IdentifierPSINode.class);
        if (structNameNode == null) {
            return results;
        }
        Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
        results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, structNameNode, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
    } else {
        TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(variableDefinitionNode, TypeNameNode.class);
        if (typeNameNode == null) {
            return results;
        } else {
            PsiReference reference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
            if (reference == null) {
                return results;
            }
            PsiElement resolvedElement = reference.resolve();
            if (resolvedElement == null) {
                return results;
            }
            PsiElement resolvedElementParent = resolvedElement.getParent();
            if (!(resolvedElementParent instanceof StructDefinitionNode)) {
                return results;
            }
            StructBodyNode structBodyNode = PsiTreeUtil.getChildOfType(resolvedElementParent, StructBodyNode.class);
            if (structBodyNode == null) {
                return results;
            }
            Collection<FieldDefinitionNode> fieldDefinitionNodes;
            if (isInSamePackage(identifier, resolvedElement)) {
                fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structBodyNode, FieldDefinitionNode.class);
            } else {
                fieldDefinitionNodes = PsiTreeUtil.getChildrenOfTypeAsList(structBodyNode, FieldDefinitionNode.class);
            }
            results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
        }
    }
    return results;
}
Also used : PsiReference(com.intellij.psi.PsiReference) LookupElement(com.intellij.codeInsight.lookup.LookupElement) InvocationNode(org.ballerinalang.plugins.idea.psi.InvocationNode) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) LinkedList(java.util.LinkedList) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) StructBodyNode(org.ballerinalang.plugins.idea.psi.StructBodyNode) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with InvocationNode

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

the class BallerinaParserDefinition method createElement.

@NotNull
public PsiElement createElement(ASTNode node) {
    IElementType elementType = node.getElementType();
    if (elementType instanceof TokenIElementType) {
        return new ANTLRPsiNode(node);
    }
    if (!(elementType instanceof RuleIElementType)) {
        return new ANTLRPsiNode(node);
    }
    RuleIElementType ruleElType = (RuleIElementType) elementType;
    switch(ruleElType.getRuleIndex()) {
        case BallerinaParser.RULE_functionDefinition:
            return new FunctionDefinitionNode(node);
        case BallerinaParser.RULE_callableUnitBody:
            return new CallableUnitBodyNode(node);
        case BallerinaParser.RULE_nameReference:
            return new NameReferenceNode(node);
        case BallerinaParser.RULE_variableReference:
            return new VariableReferenceNode(node);
        case BallerinaParser.RULE_variableDefinitionStatement:
            return new VariableDefinitionNode(node);
        case BallerinaParser.RULE_parameter:
            return new ParameterNode(node);
        case BallerinaParser.RULE_resourceDefinition:
            return new ResourceDefinitionNode(node);
        case BallerinaParser.RULE_packageName:
            return new PackageNameNode(node);
        case BallerinaParser.RULE_fullyQualifiedPackageName:
            return new FullyQualifiedPackageNameNode(node);
        case BallerinaParser.RULE_expressionList:
            return new ExpressionListNode(node);
        case BallerinaParser.RULE_expression:
            return new ExpressionNode(node);
        case BallerinaParser.RULE_functionInvocation:
            return new FunctionInvocationNode(node);
        case BallerinaParser.RULE_compilationUnit:
            return new CompilationUnitNode(node);
        case BallerinaParser.RULE_packageDeclaration:
            return new PackageDeclarationNode(node);
        case BallerinaParser.RULE_annotationAttachment:
            return new AnnotationAttachmentNode(node);
        case BallerinaParser.RULE_serviceBody:
            return new ServiceBodyNode(node);
        case BallerinaParser.RULE_importDeclaration:
            return new ImportDeclarationNode(node);
        case BallerinaParser.RULE_statement:
            return new StatementNode(node);
        case BallerinaParser.RULE_typeName:
            return new TypeNameNode(node);
        case BallerinaParser.RULE_constantDefinition:
            return new ConstantDefinitionNode(node);
        case BallerinaParser.RULE_structDefinition:
            return new StructDefinitionNode(node);
        case BallerinaParser.RULE_simpleLiteral:
            return new SimpleLiteralNode(node);
        case BallerinaParser.RULE_packageAlias:
            return new AliasNode(node);
        case BallerinaParser.RULE_parameterList:
            return new ParameterListNode(node);
        case BallerinaParser.RULE_fieldDefinition:
            return new FieldDefinitionNode(node);
        case BallerinaParser.RULE_parameterTypeNameList:
            return new ParameterTypeNameList(node);
        case BallerinaParser.RULE_parameterTypeName:
            return new ConnectorInitNode(node);
        case BallerinaParser.RULE_serviceDefinition:
            return new ServiceDefinitionNode(node);
        case BallerinaParser.RULE_valueTypeName:
            return new ValueTypeNameNode(node);
        case BallerinaParser.RULE_annotationDefinition:
            return new AnnotationAttributeValueNode(node);
        case BallerinaParser.RULE_structBody:
            return new StructBodyNode(node);
        case BallerinaParser.RULE_returnParameter:
            return new ReturnParameterNode(node);
        case BallerinaParser.RULE_attachmentPoint:
            return new AttachmentPointNode(node);
        case BallerinaParser.RULE_definition:
            return new DefinitionNode(node);
        case BallerinaParser.RULE_ifElseStatement:
            return new IfElseStatementNode(node);
        case BallerinaParser.RULE_assignmentStatement:
            return new AssignmentStatementNode(node);
        case BallerinaParser.RULE_variableReferenceList:
            return new VariableReferenceListNode(node);
        case BallerinaParser.RULE_recordLiteral:
            return new RecordLiteralNode(node);
        case BallerinaParser.RULE_globalVariableDefinition:
            return new GlobalVariableDefinitionNode(node);
        case BallerinaParser.RULE_recordKeyValue:
            return new RecordKeyValueNode(node);
        case BallerinaParser.RULE_forkJoinStatement:
            return new ForkJoinStatementNode(node);
        case BallerinaParser.RULE_returnStatement:
            return new ReturnStatementNode(node);
        case BallerinaParser.RULE_throwStatement:
            return new ThrowStatementNode(node);
        case BallerinaParser.RULE_transformerDefinition:
            return new TransformerDefinitionNode(node);
        case BallerinaParser.RULE_workerReply:
            return new WorkerReplyNode(node);
        case BallerinaParser.RULE_triggerWorker:
            return new TriggerWorkerNode(node);
        case BallerinaParser.RULE_workerDeclaration:
            return new WorkerDeclarationNode(node);
        case BallerinaParser.RULE_workerBody:
            return new WorkerBodyNode(node);
        case BallerinaParser.RULE_joinConditions:
            return new JoinConditionNode(node);
        case BallerinaParser.RULE_field:
            return new FieldNode(node);
        case BallerinaParser.RULE_recordKey:
            return new RecordKeyNode(node);
        case BallerinaParser.RULE_recordValue:
            return new RecordValueNode(node);
        case BallerinaParser.RULE_functionReference:
            return new FunctionReferenceNode(node);
        case BallerinaParser.RULE_codeBlockBody:
            return new CodeBlockBodyNode(node);
        case BallerinaParser.RULE_nonEmptyCodeBlockBody:
            return new NonEmptyCodeBlockBodyNode(node);
        case BallerinaParser.RULE_tryCatchStatement:
            return new TryCatchStatementNode(node);
        case BallerinaParser.RULE_catchClause:
            return new CatchClauseNode(node);
        case BallerinaParser.RULE_codeBlockParameter:
            return new CodeBlockParameterNode(node);
        case BallerinaParser.RULE_foreachStatement:
            return new ForEachStatementNode(node);
        case BallerinaParser.RULE_joinClause:
            return new JoinClauseNode(node);
        case BallerinaParser.RULE_timeoutClause:
            return new TimeoutClauseNode(node);
        case BallerinaParser.RULE_xmlAttrib:
            return new XmlAttribNode(node);
        case BallerinaParser.RULE_namespaceDeclaration:
            return new NamespaceDeclarationNode(node);
        case BallerinaParser.RULE_stringTemplateLiteral:
            return new StringTemplateLiteralNode(node);
        case BallerinaParser.RULE_stringTemplateContent:
            return new StringTemplateContentNode(node);
        case BallerinaParser.RULE_typeConversion:
            return new TypeConversionNode(node);
        case BallerinaParser.RULE_xmlContent:
            return new XmlContentNode(node);
        case BallerinaParser.RULE_invocation:
            return new InvocationNode(node);
        case BallerinaParser.RULE_enumDefinition:
            return new EnumDefinitionNode(node);
        case BallerinaParser.RULE_enumField:
            return new EnumFieldNode(node);
        case BallerinaParser.RULE_builtInReferenceTypeName:
            return new BuiltInReferenceTypeNameNode(node);
        case BallerinaParser.RULE_referenceTypeName:
            return new ReferenceTypeNameNode(node);
        case BallerinaParser.RULE_functionTypeName:
            return new FunctionTypeNameNode(node);
        case BallerinaParser.RULE_lambdaFunction:
            return new LambdaFunctionNode(node);
        case BallerinaParser.RULE_endpointDeclaration:
            return new EndpointDeclarationNode(node);
        case BallerinaParser.RULE_anonStructTypeName:
            return new AnonStructTypeNameNode(node);
        case BallerinaParser.RULE_userDefineTypeName:
            return new UserDefinedTypeName(node);
        case BallerinaParser.RULE_anyIdentifierName:
            return new AnyIdentifierNameNode(node);
        case BallerinaParser.RULE_documentationAttachment:
            return new DocumentationAttachmentNode(node);
        case BallerinaParser.RULE_documentationTemplateInlineCode:
            return new DocumentationTemplateInlineCodeNode(node);
        case BallerinaParser.RULE_singleBackTickDocInlineCode:
            return new SingleBackTickDocInlineCodeNode(node);
        case BallerinaParser.RULE_doubleBackTickDocInlineCode:
            return new DoubleBackTickInlineCodeNode(node);
        case BallerinaParser.RULE_tripleBackTickDocInlineCode:
            return new TripleBackTickInlineCodeNode(node);
        case BallerinaParser.RULE_deprecatedAttachment:
            return new DeprecatedAttachmentNode(node);
        case BallerinaParser.RULE_deprecatedText:
            return new DeprecatedTextNode(node);
        case BallerinaParser.RULE_singleBackTickDeprecatedInlineCode:
            return new SingleBackTickDeprecatedInlineCodeNode(node);
        case BallerinaParser.RULE_doubleBackTickDeprecatedInlineCode:
            return new DoubleBackTickDeprecatedInlineCodeNode(node);
        case BallerinaParser.RULE_tripleBackTickDeprecatedInlineCode:
            return new TripleBackTickDeprecatedInlineCodeNode(node);
        case BallerinaParser.RULE_documentationTemplateAttributeDescription:
            return new DocumentationTemplateAttributeDescriptionNode(node);
        case BallerinaParser.RULE_formalParameterList:
            return new FormalParameterListNode(node);
        case BallerinaParser.RULE_integerLiteral:
            return new IntegerLiteralNode(node);
        case BallerinaParser.RULE_matchPatternClause:
            return new MatchPatternClauseNode(node);
        default:
            return new ANTLRPsiNode(node);
    }
}
Also used : RecordKeyValueNode(org.ballerinalang.plugins.idea.psi.RecordKeyValueNode) ReturnStatementNode(org.ballerinalang.plugins.idea.psi.ReturnStatementNode) TransformerDefinitionNode(org.ballerinalang.plugins.idea.psi.TransformerDefinitionNode) TimeoutClauseNode(org.ballerinalang.plugins.idea.psi.TimeoutClauseNode) FunctionTypeNameNode(org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode) ConstantDefinitionNode(org.ballerinalang.plugins.idea.psi.ConstantDefinitionNode) BuiltInReferenceTypeNameNode(org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode) JoinConditionNode(org.ballerinalang.plugins.idea.psi.JoinConditionNode) AttachmentPointNode(org.ballerinalang.plugins.idea.psi.AttachmentPointNode) ThrowStatementNode(org.ballerinalang.plugins.idea.psi.ThrowStatementNode) ConnectorInitNode(org.ballerinalang.plugins.idea.psi.ConnectorInitNode) CatchClauseNode(org.ballerinalang.plugins.idea.psi.CatchClauseNode) SingleBackTickDeprecatedInlineCodeNode(org.ballerinalang.plugins.idea.psi.SingleBackTickDeprecatedInlineCodeNode) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) XmlContentNode(org.ballerinalang.plugins.idea.psi.XmlContentNode) MatchPatternClauseNode(org.ballerinalang.plugins.idea.psi.MatchPatternClauseNode) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) DocumentationAttachmentNode(org.ballerinalang.plugins.idea.psi.DocumentationAttachmentNode) ForkJoinStatementNode(org.ballerinalang.plugins.idea.psi.ForkJoinStatementNode) ForEachStatementNode(org.ballerinalang.plugins.idea.psi.ForEachStatementNode) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) RecordLiteralNode(org.ballerinalang.plugins.idea.psi.RecordLiteralNode) IntegerLiteralNode(org.ballerinalang.plugins.idea.psi.IntegerLiteralNode) ReferenceTypeNameNode(org.ballerinalang.plugins.idea.psi.ReferenceTypeNameNode) BuiltInReferenceTypeNameNode(org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode) TokenIElementType(org.antlr.jetbrains.adaptor.lexer.TokenIElementType) ServiceBodyNode(org.ballerinalang.plugins.idea.psi.ServiceBodyNode) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) AliasNode(org.ballerinalang.plugins.idea.psi.AliasNode) TriggerWorkerNode(org.ballerinalang.plugins.idea.psi.TriggerWorkerNode) ExpressionListNode(org.ballerinalang.plugins.idea.psi.ExpressionListNode) WorkerDeclarationNode(org.ballerinalang.plugins.idea.psi.WorkerDeclarationNode) AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) FieldNode(org.ballerinalang.plugins.idea.psi.FieldNode) EnumFieldNode(org.ballerinalang.plugins.idea.psi.EnumFieldNode) SimpleLiteralNode(org.ballerinalang.plugins.idea.psi.SimpleLiteralNode) ThrowStatementNode(org.ballerinalang.plugins.idea.psi.ThrowStatementNode) ForEachStatementNode(org.ballerinalang.plugins.idea.psi.ForEachStatementNode) StatementNode(org.ballerinalang.plugins.idea.psi.StatementNode) AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) IfElseStatementNode(org.ballerinalang.plugins.idea.psi.IfElseStatementNode) ForkJoinStatementNode(org.ballerinalang.plugins.idea.psi.ForkJoinStatementNode) ReturnStatementNode(org.ballerinalang.plugins.idea.psi.ReturnStatementNode) TryCatchStatementNode(org.ballerinalang.plugins.idea.psi.TryCatchStatementNode) WorkerBodyNode(org.ballerinalang.plugins.idea.psi.WorkerBodyNode) TryCatchStatementNode(org.ballerinalang.plugins.idea.psi.TryCatchStatementNode) TripleBackTickInlineCodeNode(org.ballerinalang.plugins.idea.psi.TripleBackTickInlineCodeNode) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) InvocationNode(org.ballerinalang.plugins.idea.psi.InvocationNode) DeprecatedAttachmentNode(org.ballerinalang.plugins.idea.psi.DeprecatedAttachmentNode) RecordKeyNode(org.ballerinalang.plugins.idea.psi.RecordKeyNode) ResourceDefinitionNode(org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode) EnumFieldNode(org.ballerinalang.plugins.idea.psi.EnumFieldNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) DocumentationTemplateAttributeDescriptionNode(org.ballerinalang.plugins.idea.psi.DocumentationTemplateAttributeDescriptionNode) ParameterTypeNameList(org.ballerinalang.plugins.idea.psi.ParameterTypeNameList) FunctionTypeNameNode(org.ballerinalang.plugins.idea.psi.FunctionTypeNameNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) ReferenceTypeNameNode(org.ballerinalang.plugins.idea.psi.ReferenceTypeNameNode) BuiltInReferenceTypeNameNode(org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode) ValueTypeNameNode(org.ballerinalang.plugins.idea.psi.ValueTypeNameNode) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) SingleBackTickDocInlineCodeNode(org.ballerinalang.plugins.idea.psi.SingleBackTickDocInlineCodeNode) CallableUnitBodyNode(org.ballerinalang.plugins.idea.psi.CallableUnitBodyNode) NonEmptyCodeBlockBodyNode(org.ballerinalang.plugins.idea.psi.NonEmptyCodeBlockBodyNode) EnumDefinitionNode(org.ballerinalang.plugins.idea.psi.EnumDefinitionNode) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) LambdaFunctionNode(org.ballerinalang.plugins.idea.psi.LambdaFunctionNode) ImportDeclarationNode(org.ballerinalang.plugins.idea.psi.ImportDeclarationNode) NonEmptyCodeBlockBodyNode(org.ballerinalang.plugins.idea.psi.NonEmptyCodeBlockBodyNode) CodeBlockBodyNode(org.ballerinalang.plugins.idea.psi.CodeBlockBodyNode) StringTemplateLiteralNode(org.ballerinalang.plugins.idea.psi.StringTemplateLiteralNode) VariableReferenceListNode(org.ballerinalang.plugins.idea.psi.VariableReferenceListNode) CompilationUnitNode(org.ballerinalang.plugins.idea.psi.CompilationUnitNode) AnonStructTypeNameNode(org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode) DocumentationTemplateInlineCodeNode(org.ballerinalang.plugins.idea.psi.DocumentationTemplateInlineCodeNode) DoubleBackTickInlineCodeNode(org.ballerinalang.plugins.idea.psi.DoubleBackTickInlineCodeNode) StringTemplateContentNode(org.ballerinalang.plugins.idea.psi.StringTemplateContentNode) UserDefinedTypeName(org.ballerinalang.plugins.idea.psi.UserDefinedTypeName) ReturnParameterNode(org.ballerinalang.plugins.idea.psi.ReturnParameterNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) XmlAttribNode(org.ballerinalang.plugins.idea.psi.XmlAttribNode) EndpointDeclarationNode(org.ballerinalang.plugins.idea.psi.EndpointDeclarationNode) WorkerReplyNode(org.ballerinalang.plugins.idea.psi.WorkerReplyNode) JoinClauseNode(org.ballerinalang.plugins.idea.psi.JoinClauseNode) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode) DeprecatedTextNode(org.ballerinalang.plugins.idea.psi.DeprecatedTextNode) AnyIdentifierNameNode(org.ballerinalang.plugins.idea.psi.AnyIdentifierNameNode) RuleIElementType(org.antlr.jetbrains.adaptor.lexer.RuleIElementType) ANTLRPsiNode(org.antlr.jetbrains.adaptor.psi.ANTLRPsiNode) RecordValueNode(org.ballerinalang.plugins.idea.psi.RecordValueNode) PackageDeclarationNode(org.ballerinalang.plugins.idea.psi.PackageDeclarationNode) IfElseStatementNode(org.ballerinalang.plugins.idea.psi.IfElseStatementNode) IElementType(com.intellij.psi.tree.IElementType) RuleIElementType(org.antlr.jetbrains.adaptor.lexer.RuleIElementType) TokenIElementType(org.antlr.jetbrains.adaptor.lexer.TokenIElementType) FormalParameterListNode(org.ballerinalang.plugins.idea.psi.FormalParameterListNode) ParameterListNode(org.ballerinalang.plugins.idea.psi.ParameterListNode) ConstantDefinitionNode(org.ballerinalang.plugins.idea.psi.ConstantDefinitionNode) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) TransformerDefinitionNode(org.ballerinalang.plugins.idea.psi.TransformerDefinitionNode) EnumDefinitionNode(org.ballerinalang.plugins.idea.psi.EnumDefinitionNode) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) ResourceDefinitionNode(org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode) ServiceDefinitionNode(org.ballerinalang.plugins.idea.psi.ServiceDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) DefinitionNode(org.ballerinalang.plugins.idea.psi.DefinitionNode) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) ServiceDefinitionNode(org.ballerinalang.plugins.idea.psi.ServiceDefinitionNode) NamespaceDeclarationNode(org.ballerinalang.plugins.idea.psi.NamespaceDeclarationNode) ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) DoubleBackTickDeprecatedInlineCodeNode(org.ballerinalang.plugins.idea.psi.DoubleBackTickDeprecatedInlineCodeNode) TripleBackTickDeprecatedInlineCodeNode(org.ballerinalang.plugins.idea.psi.TripleBackTickDeprecatedInlineCodeNode) TypeConversionNode(org.ballerinalang.plugins.idea.psi.TypeConversionNode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) AnnotationAttributeValueNode(org.ballerinalang.plugins.idea.psi.AnnotationAttributeValueNode) StructBodyNode(org.ballerinalang.plugins.idea.psi.StructBodyNode) ValueTypeNameNode(org.ballerinalang.plugins.idea.psi.ValueTypeNameNode) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) FormalParameterListNode(org.ballerinalang.plugins.idea.psi.FormalParameterListNode) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with InvocationNode

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

the class BallerinaParameterInfoHandler method showParameterInfo.

@Override
public void showParameterInfo(@NotNull Object element, @NotNull CreateParameterInfoContext context) {
    // This method will be called with the return object of the findElementForParameterInfo(). If it is null,
    // this method will not be called.
    // Since we know the type, we check and cast the object.
    PsiElement currentElement = null;
    PsiElement parentElement = null;
    List<PsiElement> list = getParameters(element);
    if (element instanceof ExpressionListNode) {
        ExpressionListNode expressionListNode = (ExpressionListNode) element;
        // We need to get the ExpressionListNode parent of current ExpressionListNode.
        // Current ExpressionListNode - "WSO2"
        // Parent ExpressionListNode - setName("WSO2")
        // By doing this, we get the function name because setName("WSO2") is also a ExpressionNode.
        PsiElement parent = PsiTreeUtil.getParentOfType(expressionListNode, FunctionInvocationNode.class);
        // node is a FunctionInvocationNode.
        if (parent == null) {
            // So if the parent is null, we consider the FunctionInvocationNode as the parent node.
            parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionNode.class);
        }
        if (parent == null) {
            parent = PsiTreeUtil.getParentOfType(expressionListNode, InvocationNode.class);
        }
        if (parent == null) {
            // So if the parent is null, we consider the ActionInvocationNode as the parent node.
            parent = PsiTreeUtil.getParentOfType(expressionListNode, ActionInvocationNode.class);
        }
        if (parent == null) {
            // So if the parent is null, we consider the ActionInvocationNode as the parent node.
            parent = PsiTreeUtil.getParentOfType(expressionListNode, ConnectorInitNode.class);
        }
        if (parent == null) {
            // So if the parent is null, we consider the ExpressionListNode as the parent node.
            parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionListNode.class);
        }
        if (parent == null) {
            parent = expressionListNode;
        }
        currentElement = expressionListNode;
        parentElement = parent;
    } else if (element instanceof FunctionInvocationNode) {
        FunctionInvocationNode functionInvocationNode = (FunctionInvocationNode) element;
        currentElement = functionInvocationNode;
        parentElement = functionInvocationNode;
    } else if (element instanceof ActionInvocationNode) {
        ActionInvocationNode actionInvocationNode = (ActionInvocationNode) element;
        currentElement = actionInvocationNode;
        parentElement = actionInvocationNode;
    } else if (element instanceof ConnectorInitNode) {
        ConnectorInitNode connectorInitNode = (ConnectorInitNode) element;
        currentElement = connectorInitNode;
        parentElement = connectorInitNode;
    } else if (element instanceof ExpressionNode) {
        ExpressionNode expressionNode = (ExpressionNode) element;
        currentElement = expressionNode;
        parentElement = expressionNode;
    } else if (element instanceof NameReferenceNode) {
        NameReferenceNode nameReferenceNode = (NameReferenceNode) element;
        currentElement = nameReferenceNode;
        parentElement = nameReferenceNode;
    } else if (element instanceof FunctionReferenceNode) {
        FunctionReferenceNode functionReferenceNode = (FunctionReferenceNode) element;
        currentElement = functionReferenceNode;
        parentElement = functionReferenceNode;
    } else if (element instanceof ConnectorReferenceNode) {
        ConnectorReferenceNode connectorReferenceNode = (ConnectorReferenceNode) element;
        currentElement = connectorReferenceNode;
        parentElement = connectorReferenceNode;
    } else if (element instanceof IdentifierPSINode) {
        IdentifierPSINode identifier = (IdentifierPSINode) element;
        currentElement = identifier;
        parentElement = identifier;
    } else if (element instanceof InvocationNode) {
        InvocationNode invocationNode = (InvocationNode) element;
        currentElement = invocationNode;
        parentElement = invocationNode;
    }
    PsiElement namedIdentifierDefNode = getNameIdentifierDefinitionNode(parentElement);
    PsiElement nameIdentifier = getNameIdentifier(currentElement, namedIdentifierDefNode);
    if (currentElement == null || nameIdentifier == null || !(nameIdentifier instanceof IdentifierPSINode)) {
        return;
    }
    if (list == null) {
        list = new LinkedList<>();
    }
    // If there are no items to show, set a custom object. Otherwise set the list as an array.
    if (list.isEmpty() && canResolve(nameIdentifier)) {
        // Todo - change how to identify no parameter situation
        context.setItemsToShow(new Object[] { "Empty" });
    } else {
        context.setItemsToShow(list.toArray(new PsiElement[list.size()]));
    }
    context.showHint(currentElement, currentElement.getTextRange().getStartOffset(), this);
}
Also used : ActionInvocationNode(org.ballerinalang.plugins.idea.psi.ActionInvocationNode) ExpressionListNode(org.ballerinalang.plugins.idea.psi.ExpressionListNode) ConnectorInitNode(org.ballerinalang.plugins.idea.psi.ConnectorInitNode) ActionInvocationNode(org.ballerinalang.plugins.idea.psi.ActionInvocationNode) InvocationNode(org.ballerinalang.plugins.idea.psi.InvocationNode) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ConnectorReferenceNode(org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode)

Example 5 with InvocationNode

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

the class BallerinaParameterInfoHandler method getParameters.

/**
 * Returns the parameter list for the given element.
 *
 * @param element
 * @return
 */
@NotNull
public static List<PsiElement> getParameters(@NotNull Object element) {
    List<PsiElement> list = new LinkedList<>();
    if (element instanceof ExpressionListNode) {
        ExpressionListNode expressionListNode = (ExpressionListNode) element;
        // We need to get the ExpressionListNode parent of current ExpressionListNode.
        // Current ExpressionListNode - "WSO2"
        // Parent ExpressionListNode - setName("WSO2")
        PsiElement parent = PsiTreeUtil.getParentOfType(expressionListNode, FunctionInvocationNode.class);
        if (parent == null) {
            // So if the parent is null, we consider the ActionInvocationNode as the parent node.
            parent = PsiTreeUtil.getParentOfType(expressionListNode, ActionInvocationNode.class);
        }
        if (parent == null) {
            parent = PsiTreeUtil.getParentOfType(expressionListNode, InvocationNode.class);
        }
        if (parent == null) {
            // So if the parent is null, we consider the ActionInvocationNode as the parent node.
            parent = PsiTreeUtil.getParentOfType(expressionListNode, ConnectorInitNode.class);
        }
        if (parent == null) {
            // So if the parent is null, we consider the FunctionInvocationNode as the parent node.
            parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionNode.class);
        }
        if (parent == null) {
            // So if the parent is null, we consider the ExpressionListNode as the parent node.
            parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionListNode.class);
        }
        if (parent == null) {
            parent = expressionListNode;
        }
        list = getItemsToShow(expressionListNode, parent);
    } else if (element instanceof FunctionInvocationNode) {
        FunctionInvocationNode functionInvocationNode = (FunctionInvocationNode) element;
        list = getItemsToShow(functionInvocationNode, functionInvocationNode);
    } else if (element instanceof ActionInvocationNode) {
        ActionInvocationNode actionInvocationNode = (ActionInvocationNode) element;
        list = getItemsToShow(actionInvocationNode, actionInvocationNode);
    } else if (element instanceof ConnectorInitNode) {
        ConnectorInitNode connectorInitNode = (ConnectorInitNode) element;
        list = getItemsToShow(connectorInitNode, connectorInitNode);
    } else if (element instanceof ExpressionNode) {
        ExpressionNode expressionNode = (ExpressionNode) element;
        list = getItemsToShow(expressionNode, expressionNode);
    } else if (element instanceof NameReferenceNode) {
        NameReferenceNode nameReferenceNode = (NameReferenceNode) element;
        list = getItemsToShow(nameReferenceNode, nameReferenceNode);
    } else if (element instanceof FunctionReferenceNode) {
        FunctionReferenceNode functionReferenceNode = (FunctionReferenceNode) element;
        list = getItemsToShow(functionReferenceNode, functionReferenceNode);
    } else if (element instanceof ConnectorReferenceNode) {
        ConnectorReferenceNode connectorReferenceNode = (ConnectorReferenceNode) element;
        list = getItemsToShow(connectorReferenceNode, connectorReferenceNode);
    } else if (element instanceof IdentifierPSINode) {
        IdentifierPSINode identifier = (IdentifierPSINode) element;
        list = getItemsToShow(identifier, identifier);
    } else if (element instanceof InvocationNode) {
        InvocationNode invocationNode = (InvocationNode) element;
        list = getItemsToShow(invocationNode, invocationNode);
    }
    return list;
}
Also used : ActionInvocationNode(org.ballerinalang.plugins.idea.psi.ActionInvocationNode) ExpressionListNode(org.ballerinalang.plugins.idea.psi.ExpressionListNode) ConnectorInitNode(org.ballerinalang.plugins.idea.psi.ConnectorInitNode) ActionInvocationNode(org.ballerinalang.plugins.idea.psi.ActionInvocationNode) InvocationNode(org.ballerinalang.plugins.idea.psi.InvocationNode) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) LinkedList(java.util.LinkedList) ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ConnectorReferenceNode(org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) FunctionInvocationNode(org.ballerinalang.plugins.idea.psi.FunctionInvocationNode) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

InvocationNode (org.ballerinalang.plugins.idea.psi.InvocationNode)5 PsiElement (com.intellij.psi.PsiElement)4 FunctionInvocationNode (org.ballerinalang.plugins.idea.psi.FunctionInvocationNode)4 FunctionReferenceNode (org.ballerinalang.plugins.idea.psi.FunctionReferenceNode)4 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)4 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 ConnectorInitNode (org.ballerinalang.plugins.idea.psi.ConnectorInitNode)3 NotNull (org.jetbrains.annotations.NotNull)3 PsiReference (com.intellij.psi.PsiReference)2 LinkedList (java.util.LinkedList)2 ActionInvocationNode (org.ballerinalang.plugins.idea.psi.ActionInvocationNode)2 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)2 AnyIdentifierNameNode (org.ballerinalang.plugins.idea.psi.AnyIdentifierNameNode)2 ConnectorReferenceNode (org.ballerinalang.plugins.idea.psi.ConnectorReferenceNode)2 ExpressionListNode (org.ballerinalang.plugins.idea.psi.ExpressionListNode)2 ExpressionNode (org.ballerinalang.plugins.idea.psi.ExpressionNode)2 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)2 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)2 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)2