Search in sources :

Example 1 with ConstantDefinitionNode

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

the class BallerinaPsiImplUtil method getAllConstantsInScope.

@NotNull
public static List<IdentifierPSINode> getAllConstantsInScope(@NotNull ScopeNode scope, int caretOffset) {
    List<IdentifierPSINode> results = new LinkedList<>();
    Collection<ConstantDefinitionNode> constantDefinitionNodes = PsiTreeUtil.findChildrenOfType(scope, ConstantDefinitionNode.class);
    for (ConstantDefinitionNode constantDefinitionNode : constantDefinitionNodes) {
        PsiElement identifier = constantDefinitionNode.getNameIdentifier();
        if (caretOffset != -1) {
            if (identifier == null || !(identifier instanceof IdentifierPSINode) || identifier.getTextOffset() > caretOffset) {
                continue;
            }
            PsiElement element = scope.findElementAt(caretOffset);
            if (element != null) {
                ConstantDefinitionNode definition = PsiTreeUtil.getParentOfType(element, ConstantDefinitionNode.class);
                if (definition != null && definition.equals(constantDefinitionNode)) {
                    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;
        }
        constantDefinitionNodes = PsiTreeUtil.findChildrenOfType(file, ConstantDefinitionNode.class);
        for (ConstantDefinitionNode variableDefinitionNode : constantDefinitionNodes) {
            PsiElement identifier = variableDefinitionNode.getNameIdentifier();
            if (identifier != null && identifier instanceof IdentifierPSINode) {
                results.add(((IdentifierPSINode) identifier));
            }
        }
    }
    return results;
}
Also used : ConstantDefinitionNode(org.ballerinalang.plugins.idea.psi.ConstantDefinitionNode) 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) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ConstantDefinitionNode

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

the class NameReference 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();
    AnnotationAttachmentNode attachmentNode = PsiTreeUtil.getParentOfType(identifier, AnnotationAttachmentNode.class);
    if (attachmentNode != null && containingFile instanceof BallerinaFile) {
        ScopeNode scope = (BallerinaFile) containingFile;
        List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
        results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
    } else if (containingPackage != null) {
        List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP, true, AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP);
        results.addAll(packages);
        PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
        ANTLRPsiNode definitionParent = PsiTreeUtil.getParentOfType(identifier, CallableUnitBodyNode.class, ServiceBodyNode.class, ResourceDefinitionNode.class, ConnectorBodyNode.class);
        TypeNameNode typeNameNode = PsiTreeUtil.getParentOfType(identifier, TypeNameNode.class);
        if ((definitionParent != null && !(definitionParent instanceof ResourceDefinitionNode)) || prevVisibleLeaf != null && (!";".equals(prevVisibleLeaf.getText()) && typeNameNode == null || prevVisibleLeaf.getText().matches("[{}]"))) {
            List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, true, true);
            results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
            // 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));
                List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
                results.addAll(BallerinaCompletionUtils.createNamespaceLookupElements(namespaces));
                List<IdentifierPSINode> endpoints = BallerinaPsiImplUtil.getAllEndpointsInResolvableScope(scope, caretOffset);
                results.addAll(BallerinaCompletionUtils.createEndpointLookupElements(endpoints));
            } else {
                ConstantDefinitionNode constantDefinitionNode = PsiTreeUtil.getParentOfType(identifier, ConstantDefinitionNode.class);
                GlobalVariableDefinitionNode globalVariableDefinitionNode = PsiTreeUtil.getParentOfType(identifier, GlobalVariableDefinitionNode.class);
                if (constantDefinitionNode != null || globalVariableDefinitionNode != null) {
                    scope = PsiTreeUtil.getParentOfType(constantDefinitionNode, BallerinaFile.class);
                }
                if (globalVariableDefinitionNode != null) {
                    scope = PsiTreeUtil.getParentOfType(globalVariableDefinitionNode, BallerinaFile.class);
                }
                if (scope != null) {
                    int caretOffset = identifier.getStartOffset();
                    List<IdentifierPSINode> globalVars = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope, caretOffset);
                    results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVars));
                    List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope, caretOffset);
                    results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
                }
            }
        }
        List<IdentifierPSINode> connectors = BallerinaPsiImplUtil.getAllConnectorsFromPackage(containingPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createConnectorLookupElements(connectors, AddSpaceInsertHandler.INSTANCE));
        List<IdentifierPSINode> structs = BallerinaPsiImplUtil.getAllStructsFromPackage(containingPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createStructLookupElements(structs));
        List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(containingPackage, true, true);
        results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
        return results;
    }
    // Try to get fields from an anonymous struct.
    PsiElement structDefinitionNode = BallerinaPsiImplUtil.resolveAnonymousStruct(identifier);
    if (structDefinitionNode == null || !(structDefinitionNode instanceof StructDefinitionNode)) {
        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);
    return results;
}
Also used : ConstantDefinitionNode(org.ballerinalang.plugins.idea.psi.ConstantDefinitionNode) BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) TopLevelDefinition(org.ballerinalang.plugins.idea.psi.scopes.TopLevelDefinition) VariableContainer(org.ballerinalang.plugins.idea.psi.scopes.VariableContainer) PsiDirectory(com.intellij.psi.PsiDirectory) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiFile(com.intellij.psi.PsiFile) LinkedList(java.util.LinkedList) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) ServiceBodyNode(org.ballerinalang.plugins.idea.psi.ServiceBodyNode) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode) ConnectorBodyNode(org.ballerinalang.plugins.idea.psi.ConnectorBodyNode) ANTLRPsiNode(org.antlr.jetbrains.adaptor.psi.ANTLRPsiNode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) LinkedList(java.util.LinkedList) ResourceDefinitionNode(org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) CallableUnitBodyNode(org.ballerinalang.plugins.idea.psi.CallableUnitBodyNode) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) LowerLevelDefinition(org.ballerinalang.plugins.idea.psi.scopes.LowerLevelDefinition) CodeBlockScope(org.ballerinalang.plugins.idea.psi.scopes.CodeBlockScope) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ConstantDefinitionNode

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

the class BallerinaDocumentationProvider method getSignature.

@NotNull
private static String getSignature(PsiElement element) {
    if (element == null) {
        return "";
    }
    StringBuilder stringBuilder = new StringBuilder();
    // element will be an identifier. So we need to get the parent of the element to identify the type to
    // generate the signature.
    PsiElement parent = element.getParent();
    // Generate the signature according to the parent type.
    if (parent instanceof FunctionDefinitionNode) {
        // Add the function signature.
        stringBuilder.append("function ");
        stringBuilder.append(element.getText());
        // Get parameters.
        String params = getParameterString(parent, false);
        // Add parameters.
        stringBuilder.append("(");
        stringBuilder.append(params);
        stringBuilder.append(")");
        // Get return types. These can be either return types or return parameters.
        List<String> returnParamsList = getReturnTypes(parent);
        String returnParams = StringUtil.join(returnParamsList, ", ");
        if (!returnParams.isEmpty()) {
            // Add return types/parameters.
            stringBuilder.append("(");
            stringBuilder.append(returnParams);
            stringBuilder.append(")");
        }
    } else if (parent instanceof ActionDefinitionNode || parent instanceof AnyIdentifierNameNode) {
        // Add the action signature.
        stringBuilder.append("action ");
        stringBuilder.append(element.getText());
        // Get parameters.
        String params = getParameterString(parent, false);
        // Add parameters.
        stringBuilder.append("(");
        stringBuilder.append(params);
        stringBuilder.append(")");
        // Get return types/parameters.
        List<String> returnParamsList = getReturnTypes(parent);
        String returnParams = StringUtil.join(returnParamsList, ", ");
        if (!returnParams.isEmpty()) {
            // Add return types/parameters.
            stringBuilder.append("(");
            stringBuilder.append(returnParams);
            stringBuilder.append(")");
        }
    } else if (parent instanceof ConnectorDefinitionNode) {
        // Add the connector signature.
        stringBuilder.append("connector ");
        stringBuilder.append(element.getText());
        // Get parameters.
        String params = getParameterString(parent, false);
        // Add parameters.
        stringBuilder.append("(");
        stringBuilder.append(params);
        stringBuilder.append(")");
    } else if (parent instanceof StructDefinitionNode) {
        // Add the function signature.
        stringBuilder.append("struct ");
        stringBuilder.append(element.getText());
        stringBuilder.append(" { }");
    } else if (parent instanceof ConstantDefinitionNode) {
        // Add the function signature.
        stringBuilder.append(parent.getText());
    }
    // If the doc is available, add package to the top.
    if (!stringBuilder.toString().isEmpty()) {
        // Add the containing package to the quick doc if there is any.
        PsiElement packagePathNode = getContainingPackage(element);
        if (packagePathNode != null) {
            stringBuilder.insert(0, packagePathNode.getText() + "<br><br>");
        }
    }
    // Return the signature.
    return stringBuilder.toString();
}
Also used : AnyIdentifierNameNode(org.ballerinalang.plugins.idea.psi.AnyIdentifierNameNode) ConstantDefinitionNode(org.ballerinalang.plugins.idea.psi.ConstantDefinitionNode) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) ActionDefinitionNode(org.ballerinalang.plugins.idea.psi.ActionDefinitionNode) LinkedList(java.util.LinkedList) ParameterTypeNameList(org.ballerinalang.plugins.idea.psi.ParameterTypeNameList) List(java.util.List) ConnectorDefinitionNode(org.ballerinalang.plugins.idea.psi.ConnectorDefinitionNode) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ConstantDefinitionNode

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

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

the class BallerinaAnnotator method annotateLeafPsiElementNodes.

private void annotateLeafPsiElementNodes(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    IElementType elementType = ((LeafPsiElement) element).getElementType();
    PsiElement parentElement = element.getParent();
    if (elementType == BallerinaTypes.AT && parentElement instanceof AnnotationAttachmentNode) {
        Annotation annotation = holder.createInfoAnnotation(element, null);
        annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.ANNOTATION);
    } else if (elementType == BallerinaTypes.QUOTED_STRING) {
        // In here, we annotate valid escape characters.
        String text = element.getText();
        Matcher matcher = VALID_ESCAPE_CHAR_PATTERN.matcher(text);
        // Get the start offset of the element.
        int startOffset = ((LeafPsiElement) element).getStartOffset();
        // Iterate through each match.
        while (matcher.find()) {
            // Get the matching group.
            String group = matcher.group(0);
            // Calculate the start and end offsets and create the range.
            TextRange range = new TextRange(startOffset + matcher.start(), startOffset + matcher.start() + group.length());
            // Create the annotation.
            Annotation annotation = holder.createInfoAnnotation(range, null);
            annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.VALID_STRING_ESCAPE);
        }
        // Annotate invalid escape characters.
        matcher = INVALID_ESCAPE_CHAR_PATTERN.matcher(text);
        // Get the start offset of the element.
        startOffset = ((LeafPsiElement) element).getStartOffset();
        // Iterate through each match.
        while (matcher.find()) {
            // Get the matching group.
            String group = matcher.group(3);
            if (group != null) {
                // Calculate the start and end offsets and create the range.
                TextRange range = new TextRange(startOffset + matcher.start(3), startOffset + matcher.start(3) + group.length());
                // Create the annotation.
                Annotation annotation = holder.createInfoAnnotation(range, "Invalid string escape");
                annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.INVALID_STRING_ESCAPE);
            }
        }
        AnnotationAttributeNode annotationAttributeNode = PsiTreeUtil.getParentOfType(element, AnnotationAttributeNode.class);
        boolean canHighlightParameters = canHighlightParameters(element);
        if (canHighlightParameters && annotationAttributeNode != null) {
            // Annotate query parameters in annotation attachments.
            matcher = PATH_PARAMETERS_PATTERN.matcher(text);
            // Get the start offset of the element.
            startOffset = ((LeafPsiElement) element).getStartOffset();
            // Iterate through each match.
            while (matcher.find()) {
                // Get the matching value without the enclosing {}.
                String value = matcher.group(2);
                if (value == null) {
                    continue;
                }
                // Calculate the start and end offsets and create the range. We need to add 2 to include the
                // {} ignored.
                TextRange range = new TextRange(startOffset + matcher.start(1), startOffset + matcher.start(1) + value.length() + 2);
                // Check whether a matching resource parameter is available.
                boolean isMatchAvailable = isMatchingParamAvailable(annotationAttributeNode, value);
                // Create the annotation.
                if (isMatchAvailable) {
                    Annotation annotation = holder.createInfoAnnotation(range, "Path parameter '" + value + "'");
                    annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.TEMPLATE_LANGUAGE_COLOR);
                } else {
                    Annotation annotation = holder.createErrorAnnotation(range, "Path parameter '" + value + "' not found in the resource signature");
                    annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.INVALID_STRING_ESCAPE);
                }
            }
        }
    } else if (elementType == BallerinaTypes.STRING_TEMPLATE_LITERAL_START || elementType == BallerinaTypes.XML_START) {
        annotateKeyword(element, holder);
    } else if (elementType == BallerinaTypes.DOCUMENTATION_TEMPLATE_START || elementType == BallerinaTypes.DEPRECATED_TEMPLATE_START) {
        // This uses an overloaded method so that the color can be easily changeable if required.
        annotateKeyword(element, holder, BallerinaSyntaxHighlightingColors.KEYWORD);
    } else if (elementType == BallerinaTypes.STRING_TEMPLATE_EXPRESSION_START || elementType == BallerinaTypes.XML_EXPRESSION_START) {
        annotateExpressionTemplateStart(element, holder);
    } else if (elementType == BallerinaTypes.STRING_TEMPLATE_TEXT || elementType == BallerinaTypes.XML_TEXT) {
        annotateText(element, holder);
    } else if (elementType == BallerinaTypes.EXPRESSION_END) {
        annotateStringLiteralTemplateEnd(element, holder);
    } else if (elementType == BallerinaTypes.DOCUMENTATION_TEMPLATE_ATTRIBUTE_START) {
        // Doc type.
        String msg = null;
        switch(element.getText().charAt(0)) {
            case 'T':
                msg = "Receiver";
                break;
            case 'P':
                msg = "Parameter";
                break;
            case 'R':
                msg = "Return Value";
                break;
            case 'F':
                msg = "Field";
                break;
            case 'V':
                msg = "Variable";
                break;
        }
        TextRange textRange = element.getTextRange();
        TextRange newTextRange = new TextRange(textRange.getStartOffset(), textRange.getEndOffset() - 2);
        Annotation annotation = holder.createInfoAnnotation(newTextRange, msg);
        annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.DOCUMENTATION_INLINE_CODE);
    } else if (element instanceof IdentifierPSINode) {
        if (parentElement.getParent() instanceof AnnotationAttachmentNode) {
            Annotation annotation = holder.createInfoAnnotation(element, null);
            annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.ANNOTATION);
            return;
        }
        if (parentElement instanceof DocumentationTemplateAttributeDescriptionNode) {
            Annotation annotation = holder.createInfoAnnotation(element, null);
            annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.DOCUMENTATION_INLINE_CODE);
        }
        PsiReference reference = element.getReference();
        if (reference == null || reference instanceof RecordKeyReference) {
            return;
        }
        PsiElement resolvedElement = reference.resolve();
        if (resolvedElement == null) {
            return;
        }
        PsiElement parent = resolvedElement.getParent();
        if (parent instanceof ConstantDefinitionNode) {
            Annotation annotation = holder.createInfoAnnotation(element, null);
            annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.CONSTANT);
        } else if (parent instanceof GlobalVariableDefinitionNode) {
            Annotation annotation = holder.createInfoAnnotation(element, null);
            annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.GLOBAL_VARIABLE);
        }
    }
}
Also used : ConstantDefinitionNode(org.ballerinalang.plugins.idea.psi.ConstantDefinitionNode) Matcher(java.util.regex.Matcher) AnnotationAttributeNode(org.ballerinalang.plugins.idea.psi.AnnotationAttributeNode) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation) IElementType(com.intellij.psi.tree.IElementType) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) DocumentationTemplateAttributeDescriptionNode(org.ballerinalang.plugins.idea.psi.DocumentationTemplateAttributeDescriptionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) RecordKeyReference(org.ballerinalang.plugins.idea.psi.references.RecordKeyReference) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode)

Aggregations

ConstantDefinitionNode (org.ballerinalang.plugins.idea.psi.ConstantDefinitionNode)9 PsiElement (com.intellij.psi.PsiElement)8 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)5 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)5 PsiReference (com.intellij.psi.PsiReference)4 LinkedList (java.util.LinkedList)4 AnnotationAttachmentNode (org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode)4 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)4 NotNull (org.jetbrains.annotations.NotNull)4 Annotation (com.intellij.lang.annotation.Annotation)3 IElementType (com.intellij.psi.tree.IElementType)3 ANTLRPsiNode (org.antlr.jetbrains.adaptor.psi.ANTLRPsiNode)3 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)3 ResourceDefinitionNode (org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode)3 ServiceBodyNode (org.ballerinalang.plugins.idea.psi.ServiceBodyNode)3 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)3 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)3 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFile (com.intellij.psi.PsiFile)2 List (java.util.List)2