Search in sources :

Example 1 with TopLevelNode

use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.

the class CommandUtil method getStructDocumentationByPosition.

/**
 * Get the Documentation attachment for the struct definition.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the struct in the source
 * @return {@link String}   Documentation attachment for the struct
 */
static DocAttachmentInfo getStructDocumentationByPosition(BLangPackage bLangPackage, int line) {
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangStruct) {
            BLangStruct structNode = (BLangStruct) topLevelNode;
            DiagnosticPos structPos = CommonUtil.toZeroBasedPosition(structNode.getPosition());
            int structStart = structPos.getStartLine();
            if (structStart == line) {
                return getStructNodeDocumentation(structNode, line);
            }
        }
    }
    return null;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 2 with TopLevelNode

use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.

the class CommandUtil method getServiceDocumentationByPosition.

/**
 * Get the Documentation attachment for the service.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the service in the source
 * @return {@link String}   Documentation attachment for the service
 */
static DocAttachmentInfo getServiceDocumentationByPosition(BLangPackage bLangPackage, int line) {
    // TODO: Currently resource position is invalid and we use the annotation attachment positions.
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangService) {
            BLangService serviceNode = (BLangService) topLevelNode;
            DiagnosticPos servicePos = CommonUtil.toZeroBasedPosition(serviceNode.getPosition());
            List<BLangAnnotationAttachment> annotationAttachments = serviceNode.getAnnotationAttachments();
            if (!annotationAttachments.isEmpty()) {
                DiagnosticPos lastAttachmentPos = CommonUtil.toZeroBasedPosition(annotationAttachments.get(annotationAttachments.size() - 1).getPosition());
                if (lastAttachmentPos.getEndLine() < line && line < servicePos.getEndLine()) {
                    return getServiceNodeDocumentation(serviceNode, lastAttachmentPos.getEndLine() + 1);
                }
            } else if (servicePos.getStartLine() == line) {
                return getServiceNodeDocumentation(serviceNode, line);
            }
        }
    }
    return null;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 3 with TopLevelNode

use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.

the class SwaggerConverterUtils method generateSwaggerDefinitions.

/**
 * This method will generate ballerina string from swagger definition. Since ballerina service definition is super
 * set of swagger definition we will take both swagger and ballerina definition and merge swagger changes to
 * ballerina definition selectively to prevent data loss
 *
 * @param ballerinaSource ballerina definition to be process as ballerina definition
 * @param serviceName service name
 * @return String representation of converted ballerina source
 * @throws IOException when error occur while processing input swagger and ballerina definitions.
 */
public static String generateSwaggerDefinitions(String ballerinaSource, String serviceName) throws IOException {
    // Get the ballerina model using the ballerina source code.
    BFile balFile = new BFile();
    balFile.setContent(ballerinaSource);
    BLangCompilationUnit topCompilationUnit = SwaggerConverterUtils.getTopLevelNodeFromBallerinaFile(balFile);
    String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME);
    String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME);
    SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias);
    String swaggerSource = StringUtils.EMPTY;
    for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
        if (topLevelNode instanceof BLangService) {
            ServiceNode serviceDefinition = (ServiceNode) topLevelNode;
            // Generate swagger string for the mentioned service name.
            if (StringUtils.isNotBlank(serviceName)) {
                if (serviceDefinition.getName().getValue().equals(serviceName)) {
                    Swagger swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                    swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
                    break;
                }
            } else {
                // If no service name mentioned, then generate swagger definition for the first service.
                Swagger swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
                break;
            }
        }
    }
    return swaggerSource;
}
Also used : ServiceNode(org.ballerinalang.model.tree.ServiceNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) Swagger(io.swagger.models.Swagger) BFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BFile) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 4 with TopLevelNode

use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.

the class SwaggerConverterUtils method generateOAS3Definitions.

/**
 * This method will generate open API 3.X specification for given ballerina service. Since we will need to
 * support both swagger 2.0 and OAS 3.0 it was implemented to convert to swagger by default and convert it
 * to OAS on demand.
 *
 * @param ballerinaSource ballerina source to be converted to swagger/OAS definition
 * @param serviceName specific service name within ballerina source that need to map OAS
 * @return Generated OAS3 string output.
 * @throws IOException When error occurs while converting, parsing input source.
 */
public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException {
    // Get the ballerina model using the ballerina source code.
    BFile balFile = new BFile();
    balFile.setContent(ballerinaSource);
    // Create empty swagger object.
    Swagger swaggerDefinition = new Swagger();
    BLangCompilationUnit topCompilationUnit = SwaggerConverterUtils.getTopLevelNodeFromBallerinaFile(balFile);
    String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME);
    String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME);
    SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias);
    String swaggerSource = StringUtils.EMPTY;
    for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
        if (topLevelNode instanceof BLangService) {
            ServiceNode serviceDefinition = (ServiceNode) topLevelNode;
            // Generate swagger string for the mentioned service name.
            if (StringUtils.isNotBlank(serviceName)) {
                if (serviceDefinition.getName().getValue().equals(serviceName)) {
                    swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                    break;
                }
            } else {
                // If no service name mentioned, then generate swagger definition for the first service.
                swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                break;
            }
        }
    }
    swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
    SwaggerConverter converter = new SwaggerConverter();
    return Yaml.pretty(converter.readContents(swaggerSource, null, null).getOpenAPI());
}
Also used : ServiceNode(org.ballerinalang.model.tree.ServiceNode) Swagger(io.swagger.models.Swagger) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) SwaggerConverter(io.swagger.v3.parser.converter.SwaggerConverter) BFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BFile) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 5 with TopLevelNode

use of org.ballerinalang.model.tree.TopLevelNode in project ballerina by ballerina-lang.

the class TreeVisitor method visit.

// Visitor methods
public void visit(BLangPackage pkgNode) {
    SymbolEnv pkgEnv = this.symTable.pkgEnvMap.get(pkgNode.symbol);
    // Then visit each top-level element sorted using the compilation unit
    String fileName = documentServiceContext.get(DocumentServiceKeys.FILE_NAME_KEY);
    List<TopLevelNode> topLevelNodes = pkgNode.topLevelNodes.stream().filter(node -> node.getPosition().getSource().getCompilationUnitName().equals(fileName)).collect(Collectors.toList());
    if (topLevelNodes.isEmpty()) {
        this.setTerminateVisitor(true);
        acceptNode(null, null);
    } else {
        cursorPositionResolver = PackageNodeScopeResolver.class;
        topLevelNodes.forEach(topLevelNode -> {
            cursorPositionResolver = TopLevelNodeScopeResolver.class;
            this.blockOwnerStack.push(pkgNode);
            acceptNode((BLangNode) topLevelNode, pkgEnv);
        });
    }
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token) BLangWorkerReceive(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerReceive) BLangBreak(org.wso2.ballerinalang.compiler.tree.statements.BLangBreak) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) LSNodeVisitor(org.ballerinalang.langserver.common.LSNodeVisitor) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally) Map(java.util.Map) ServiceScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ServiceScopeResolver) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) SymbolResolver(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolResolver) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) Names(org.wso2.ballerinalang.compiler.util.Names) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) ConnectorScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ConnectorScopeResolver) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock) ScopeResolverConstants(org.ballerinalang.langserver.completions.util.ScopeResolverConstants) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ObjectTypeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ObjectTypeScopeResolver) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) Collectors(java.util.stream.Collectors) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) List(java.util.List) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) ResourceParamScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ResourceParamScopeResolver) BlockStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.BlockStatementScopeResolver) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangBind(org.wso2.ballerinalang.compiler.tree.statements.BLangBind) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) SymbolEnter(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolEnter) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) PackageNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.PackageNodeScopeResolver) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangAbort(org.wso2.ballerinalang.compiler.tree.statements.BLangAbort) HashMap(java.util.HashMap) BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction) Stack(java.util.Stack) ArrayList(java.util.ArrayList) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) Position(org.eclipse.lsp4j.Position) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangForkJoin(org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin) BLangEndpointTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangEndpointTypeNode) MatchStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.MatchStatementScopeResolver) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangNext(org.wso2.ballerinalang.compiler.tree.statements.BLangNext) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangThrow(org.wso2.ballerinalang.compiler.tree.statements.BLangThrow) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Name(org.wso2.ballerinalang.compiler.util.Name) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) TopLevelNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.TopLevelNodeScopeResolver) BLangWorkerSend(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerSend) Node(org.ballerinalang.model.tree.Node) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Aggregations

TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)11 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)7 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)5 Swagger (io.swagger.models.Swagger)2 ArrayList (java.util.ArrayList)2 BFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BFile)2 ServiceNode (org.ballerinalang.model.tree.ServiceNode)2 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)2 BLangCompilationUnit (org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)2 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)2 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)2 BLangImportPackage (org.wso2.ballerinalang.compiler.tree.BLangImportPackage)2 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)2 BLangObject (org.wso2.ballerinalang.compiler.tree.BLangObject)2 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)2 BLangTransformer (org.wso2.ballerinalang.compiler.tree.BLangTransformer)2 SwaggerConverter (io.swagger.v3.parser.converter.SwaggerConverter)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1