Search in sources :

Example 86 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class SignatureTreeVisitor method visit.

@Override
public void visit(BLangPackage pkgNode) {
    SymbolEnv pkgEnv = symTable.pkgEnvMap.get(pkgNode.symbol);
    // Then visit each top-level element sorted using the compilation unit
    String fileName = documentServiceContext.get(DocumentServiceKeys.FILE_NAME_KEY);
    BLangCompilationUnit compilationUnit = pkgNode.getCompilationUnits().stream().filter(bLangCompilationUnit -> bLangCompilationUnit.getName().equals(fileName)).findFirst().orElse(null);
    List<TopLevelNode> topLevelNodes = compilationUnit.getTopLevelNodes();
    if (!topLevelNodes.isEmpty()) {
        topLevelNodes.forEach(topLevelNode -> acceptNode((BLangNode) topLevelNode, pkgEnv));
    }
}
Also used : BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 87 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class Generator method generatePage.

/**
 * Generate the page when the bal package is passed.
 * @param balPackage The current package that is being viewed.
 * @param packages List of available packages.
 * @return A page model for the current package.
 */
public static Page generatePage(BLangPackage balPackage, List<Link> packages) {
    ArrayList<Documentable> documentables = new ArrayList<>();
    String currentPackageName = (balPackage.symbol).pkgID.name.value;
    // Check for structs in the package
    if (balPackage.getStructs().size() > 0) {
        for (BLangStruct struct : balPackage.getStructs()) {
            if (struct.getFlags().contains(Flag.PUBLIC)) {
                documentables.add(createDocForNode(struct));
            }
        }
    }
    // Check for functions in the package
    if (balPackage.getFunctions().size() > 0) {
        for (BLangFunction function : balPackage.getFunctions()) {
            if (function.getFlags().contains(Flag.PUBLIC)) {
                if (function.getReceiver() != null) {
                    if (documentables.size() > 0) {
                        for (Documentable parentDocumentable : documentables) {
                            TypeNode langType = function.getReceiver().getTypeNode();
                            String typeName = (langType instanceof BLangUserDefinedType ? ((BLangUserDefinedType) langType).typeName.value : langType.toString());
                            if (typeName.equals(parentDocumentable.name)) {
                                parentDocumentable.children.add(createDocForNode(function));
                            }
                        }
                    }
                } else {
                    // If there's no receiver type i.e. no struct binding to the function
                    documentables.add(createDocForNode(function));
                }
            }
        }
    }
    // Check for connectors in the package
    for (BLangConnector connector : balPackage.getConnectors()) {
        if (connector.getFlags().contains(Flag.PUBLIC)) {
            documentables.add(createDocForNode(connector));
        }
    }
    // Check for connectors in the package
    for (EnumNode enumNode : balPackage.getEnums()) {
        if (enumNode.getFlags().contains(Flag.PUBLIC)) {
            documentables.add(createDocForNode(enumNode));
        }
    }
    // Check for annotations
    for (BLangAnnotation annotation : balPackage.getAnnotations()) {
        if (annotation.getFlags().contains(Flag.PUBLIC)) {
            documentables.add(createDocForNode(annotation));
        }
    }
    // Check for global variables
    for (BLangVariable var : balPackage.getGlobalVariables()) {
        if (var.getFlags().contains(Flag.PUBLIC)) {
            documentables.add(createDocForNode(var));
        }
    }
    // Create the links to select which page or package is active
    List<Link> links = new ArrayList<>();
    PackageName packageNameHeading = null;
    for (Link pkgLink : packages) {
        if (pkgLink.caption.value.equals(currentPackageName)) {
            packageNameHeading = (PackageName) pkgLink.caption;
            links.add(new Link(pkgLink.caption, pkgLink.href, true));
        } else {
            links.add(new Link(pkgLink.caption, pkgLink.href, false));
        }
    }
    return new Page(packageNameHeading, documentables, links);
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) PackageName(org.ballerinalang.docgen.model.PackageName) ArrayList(java.util.ArrayList) EnumNode(org.ballerinalang.model.tree.EnumNode) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Page(org.ballerinalang.docgen.model.Page) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Documentable(org.ballerinalang.docgen.model.Documentable) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) Link(org.ballerinalang.docgen.model.Link)

Example 88 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class CompletionTestUtil method getCompletions.

/**
 * Get the completions list.
 *
 * @param documentManager Document manager instance
 * @param pos             {@link TextDocumentPositionParams} position params
 */
public static List<CompletionItem> getCompletions(WorkspaceDocumentManagerImpl documentManager, TextDocumentPositionParams pos) {
    List<CompletionItem> completions;
    TextDocumentServiceContext completionContext = new TextDocumentServiceContext();
    completionContext.put(DocumentServiceKeys.POSITION_KEY, pos);
    completionContext.put(DocumentServiceKeys.FILE_URI_KEY, pos.getTextDocument().getUri());
    BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(completionContext, documentManager, false, CompletionCustomErrorStrategy.class, false).get(0);
    completionContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
    // Visit the package to resolve the symbols
    TreeVisitor treeVisitor = new TreeVisitor(completionContext);
    bLangPackage.accept(treeVisitor);
    BLangNode symbolEnvNode = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
    if (symbolEnvNode == null) {
        completions = CompletionItemResolver.getResolverByClass(TopLevelResolver.class).resolveItems(completionContext);
    } else {
        completions = CompletionItemResolver.getResolverByClass(symbolEnvNode.getClass()).resolveItems(completionContext);
    }
    return completions;
}
Also used : TreeVisitor(org.ballerinalang.langserver.completions.TreeVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionCustomErrorStrategy(org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Example 89 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaConnectorDocGenTest method testConnectorWithSingleAction.

@Test(description = "Test single action in a connector file", enabled = false)
public void testConnectorWithSingleAction() {
    try {
        Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "helloWorldConnector.bal");
        Assert.assertNotNull(docsMap);
        Assert.assertEquals(docsMap.size(), 1);
        BallerinaDocGenTestUtils.printDocMap(docsMap);
        BLangPackage balPackage = docsMap.get(".");
        List<BLangConnector> connectors = balPackage.getConnectors();
        Assert.assertEquals(connectors.size(), 1);
        BLangConnector connectorDoc = (BLangConnector) connectors.iterator().next();
        Assert.assertEquals(connectorDoc.getParameters().size(), 4);
        List<BLangAction> actions = connectorDoc.getActions();
        BLangAction action = actions.get(0);
        Assert.assertEquals(action.getParameters().size(), 1);
        Assert.assertEquals(action.getReturnParameters().size(), 1);
    } catch (IOException e) {
        Assert.fail();
    } finally {
        BallerinaDocGenTestUtils.cleanUp();
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) IOException(java.io.IOException) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) Test(org.testng.annotations.Test)

Example 90 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class BallerinaConnectorDocGenTest method testConnectorWithMultipleAction.

@Test(description = "Test single action in a connector file", enabled = false)
public void testConnectorWithMultipleAction() {
    try {
        Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "balWith2Actions.bal");
        Assert.assertNotNull(docsMap);
        Assert.assertEquals(docsMap.size(), 1);
        BallerinaDocGenTestUtils.printDocMap(docsMap);
        BLangPackage balPackage = docsMap.get(".");
        List<BLangConnector> connectors = balPackage.getConnectors();
        Assert.assertEquals(connectors.size(), 1);
        BLangConnector connector = connectors.iterator().next();
        Assert.assertEquals(connector.getParameters().size(), 4);
        for (BLangAction action : connector.getActions()) {
            Assert.assertEquals(action.getParameters().size(), 1);
            Assert.assertEquals(action.getReturnParameters().size(), 1);
        }
    } catch (IOException e) {
        Assert.fail();
    } finally {
        BallerinaDocGenTestUtils.cleanUp();
    }
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) IOException(java.io.IOException) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) Test(org.testng.annotations.Test)

Aggregations

BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)78 Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)28 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)19 Page (org.ballerinalang.docgen.model.Page)18 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)15 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)14 IOException (java.io.IOException)13 List (java.util.List)13 Path (java.nio.file.Path)12 Compiler (org.wso2.ballerinalang.compiler.Compiler)12 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)12 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)11 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)9 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9