Search in sources :

Example 96 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class DocumentationTest method testDocConnectorFunction.

@Test(description = "Test doc connector/function.", enabled = false)
public void testDocConnectorFunction() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/connector_function.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    BLangConnector connector = (BLangConnector) packageNode.getConnectors().get(0);
    List<BLangDocumentation> docNodes = connector.docAttachments;
    BLangDocumentation dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.getAttributes().size(), 2);
    Assert.assertEquals(dNode.documentationText, "Test Connector\n");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "url");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " url for endpoint\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "path");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " path for endpoint\n");
    dNode = connector.getActions().get(0).docAttachments.get(0);
    Assert.assertEquals(dNode.getAttributes().size(), 1);
    Assert.assertEquals(dNode.documentationText, "Test Connector action testAction ");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "s");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " which represent successful or not");
    dNode = connector.getActions().get(1).docAttachments.get(0);
    Assert.assertEquals(dNode.documentationText, "Test Connector action testSend ");
    Assert.assertEquals(dNode.getAttributes().size(), 2);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "ep");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " which represent successful or not ");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "s");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " which represent successful or not");
    docNodes = ((BLangFunction) packageNode.getFunctions().get(0)).docAttachments;
    dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "Gets a access parameter value (`true` or `false`) for a given key. " + "Please note that #foo will always be bigger than #bar.\n" + "Example:\n" + "``SymbolEnv pkgEnv = symbolEnter.packageEnvs.get(pkgNode.symbol);``\n");
    Assert.assertEquals(dNode.getAttributes().size(), 3);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "file");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " file path ``C:\\users\\OddThinking\\Documents\\My Source\\Widget\\foo.src``\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "accessMode");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " read or write mode\n");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "successful");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " boolean `true` or `false`\n");
    docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
    dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, " Documentation for File struct\n");
    Assert.assertEquals(dNode.getAttributes().size(), 1);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "path");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " struct `field path` documentation\n");
}
Also used : BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) CompileResult(org.ballerinalang.launcher.util.CompileResult) PackageNode(org.ballerinalang.model.tree.PackageNode) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) Test(org.testng.annotations.Test)

Example 97 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class DocumentationTest method testMultiple.

@Test(description = "Test doc multiple.")
public void testMultiple() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/multiple.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    List<BLangDocumentation> docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
    BLangDocumentation dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, " Documentation for Tst struct\n");
    Assert.assertEquals(dNode.getAttributes().size(), 3);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "a");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " annotation `field a` documentation\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "b");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " annotation `field b` documentation\n");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "c");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " annotation `field c` documentation");
    docNodes = ((BLangEnum) packageNode.getEnums().get(0)).docAttachments;
    dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, " Documentation for state enum\n");
    Assert.assertEquals(dNode.getAttributes().size(), 2);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "foo");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " enum `field foo` documentation\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "bar");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " enum `field bar` documentation");
    docNodes = ((BLangTransformer) packageNode.getTransformers().get(0)).docAttachments;
    dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + " Transformer Foo Person -> Employee\n" + " ");
    Assert.assertEquals(dNode.getAttributes().size(), 3);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "p");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " input struct Person source used for transformation\n ");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "e");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " output struct Employee struct which Person transformed to\n ");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "defaultAddress");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " address which serves Eg: `POSTCODE 112`\n");
    BLangService service = (BLangService) packageNode.getServices().get(0);
    docNodes = service.docAttachments;
    dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "PizzaService HTTP Service");
/*
        // Commented due to https://github.com/ballerina-lang/ballerina/issues/5586 issue
        dNode = service.getResources().get(0).docAttachments.get(0);
        Assert.assertEquals(dNode.getAttributes().size(), 2);
        Assert.assertEquals(dNode.documentationText, "Check orderPizza resource. ");
        Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "conn");
        Assert.assertEquals(dNode.getAttributes().get(0).documentationText,
                " HTTP connection. ");
        Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "req");
        Assert.assertEquals(dNode.getAttributes().get(1).documentationText,
                " In request.");

        dNode = service.getResources().get(1).docAttachments.get(0);
        Assert.assertEquals(dNode.documentationText, "Check status resource. ");
        Assert.assertEquals(dNode.getAttributes().size(), 2);
        Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "conn");
        Assert.assertEquals(dNode.getAttributes().get(0).documentationText,
                " HTTP connection. ");
        Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "req");
        Assert.assertEquals(dNode.getAttributes().get(1).documentationText,
                " In request.");*/
}
Also used : BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) CompileResult(org.ballerinalang.launcher.util.CompileResult) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Example 98 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class TextDocumentServiceUtil method prepareCompilerContext.

/**
 * Prepare the compiler context.
 *
 * @param packageRepository  Package Repository
 * @param sourceRoot         LSDocument for Source Root
 * @param preserveWhitespace Preserve Whitespace
 * @return {@link CompilerContext}     Compiler context
 */
public static CompilerContext prepareCompilerContext(PackageRepository packageRepository, LSDocument sourceRoot, boolean preserveWhitespace, WorkspaceDocumentManager documentManager, CompilerPhase compilerPhase) {
    org.wso2.ballerinalang.compiler.util.CompilerContext context = new CompilerContext();
    context.put(PackageRepository.class, packageRepository);
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(PROJECT_DIR, sourceRoot.getSourceRoot());
    if (null == compilerPhase) {
        throw new AssertionError("Compiler Phase can not be null.");
    }
    options.put(COMPILER_PHASE, compilerPhase.toString());
    options.put(PRESERVE_WHITESPACE, Boolean.valueOf(preserveWhitespace).toString());
    if (isProjectDir(sourceRoot.getSourceRoot(), sourceRoot.getURIString())) {
        context.put(SourceDirectory.class, new LangServerFSProjectDirectory(sourceRoot.getSourceRootPath(), documentManager));
    } else {
        context.put(SourceDirectory.class, new LangServerFSProgramDirectory(sourceRoot.getSourceRootPath(), documentManager));
    }
    return context;
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions) LangServerFSProgramDirectory(org.ballerinalang.langserver.workspace.repository.LangServerFSProgramDirectory) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) LangServerFSProjectDirectory(org.ballerinalang.langserver.workspace.repository.LangServerFSProjectDirectory)

Example 99 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class TextDocumentServiceUtil method getCompiler.

/**
 * Get compiler for the given context and file.
 *
 * @param context             Language server context
 * @param fileName            File name which is currently open
 * @param packageRepository   package repository
 * @param sourceRoot          LSDocument for root path of the source
 * @param preserveWhitespace  enable/disable preserve white space in compiler
 * @param customErrorStrategy custom error strategy class
 * @return {@link Compiler} ballerina compiler
 */
private static Compiler getCompiler(LanguageServerContext context, String fileName, PackageRepository packageRepository, LSDocument sourceRoot, boolean preserveWhitespace, Class customErrorStrategy, WorkspaceDocumentManager documentManager) {
    CompilerContext compilerContext = TextDocumentServiceUtil.prepareCompilerContext(packageRepository, sourceRoot, preserveWhitespace, documentManager);
    context.put(DocumentServiceKeys.FILE_NAME_KEY, fileName);
    context.put(DocumentServiceKeys.COMPILER_CONTEXT_KEY, compilerContext);
    context.put(DocumentServiceKeys.OPERATION_META_CONTEXT_KEY, new TextDocumentServiceContext());
    List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics = new ArrayList<>();
    CollectDiagnosticListener diagnosticListener = new CollectDiagnosticListener(balDiagnostics);
    compilerContext.put(DiagnosticListener.class, diagnosticListener);
    compilerContext.put(DefaultErrorStrategy.class, CustomErrorStrategyFactory.getCustomErrorStrategy(customErrorStrategy, context));
    return Compiler.getInstance(compilerContext);
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList)

Example 100 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class TextDocumentServiceUtil method getBLangPackage.

/**
 * Get the BLangPackage for a given program.
 *
 * @param context             Language Server Context
 * @param docManager          Document manager
 * @param preserveWhitespace  Enable preserve whitespace
 * @param customErrorStrategy custom error strategy class
 * @param compileFullProject  compile full project from the source root
 * @return {@link BLangPackage} BLang Package
 */
public static List<BLangPackage> getBLangPackage(LanguageServerContext context, WorkspaceDocumentManager docManager, boolean preserveWhitespace, Class customErrorStrategy, boolean compileFullProject) {
    String uri = context.get(DocumentServiceKeys.FILE_URI_KEY);
    LSDocument document = new LSDocument(uri);
    Path filePath = CommonUtil.getPath(document);
    Path fileNamePath = filePath.getFileName();
    String fileName = "";
    if (fileNamePath != null) {
        fileName = fileNamePath.toString();
    }
    String sourceRoot = TextDocumentServiceUtil.getSourceRoot(filePath);
    String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, filePath.toString());
    LSDocument sourceDocument = new LSDocument();
    sourceDocument.setUri(uri);
    sourceDocument.setSourceRoot(sourceRoot);
    PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, docManager);
    List<BLangPackage> packages = new ArrayList<>();
    if (compileFullProject) {
        if (!sourceRoot.isEmpty()) {
            File projectDir = new File(sourceRoot);
            File[] files = projectDir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if ((file.isDirectory() && !file.getName().startsWith(".")) || (!file.isDirectory() && file.getName().endsWith(".bal"))) {
                        Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
                        packages.add(compiler.compile(file.getName()));
                    }
                }
            }
        }
    } else {
        Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
        if ("".equals(pkgName)) {
            packages.add(compiler.compile(fileName));
        } else {
            packages.add(compiler.compile(pkgName));
        }
    }
    return packages;
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSDocument(org.ballerinalang.langserver.common.LSDocument) ArrayList(java.util.ArrayList) PackageRepository(org.ballerinalang.repository.PackageRepository) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) File(java.io.File) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)

Aggregations

Test (org.testng.annotations.Test)22 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 ArrayList (java.util.ArrayList)11 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)8 OMElement (org.apache.axiom.om.OMElement)8 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)8 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)8 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)8 Event (org.wso2.siddhi.core.event.Event)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Compiler (org.wso2.ballerinalang.compiler.Compiler)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)6