Search in sources :

Example 1 with BLangPackage

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

the class SignatureHelpUtil method getSignatureInfoModel.

/**
 * Get the required signature information filled model.
 *
 * @param bInvokableSymbol                  Invokable symbol
 * @param signatureContext                  Signature operation context
 * @return {@link SignatureInfoModel}       SignatureInfoModel containing signature information
 */
private static SignatureInfoModel getSignatureInfoModel(BInvokableSymbol bInvokableSymbol, TextDocumentServiceContext signatureContext) {
    Map<String, String> paramDescMap = new HashMap<>();
    SignatureInfoModel signatureInfoModel = new SignatureInfoModel();
    List<ParameterInfoModel> paramModels = new ArrayList<>();
    String functionName = signatureContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
    CompilerContext compilerContext = signatureContext.get(DocumentServiceKeys.COMPILER_CONTEXT_KEY);
    BLangPackage bLangPackage = signatureContext.get(DocumentServiceKeys.LS_PACKAGE_CACHE_KEY).findPackage(compilerContext, bInvokableSymbol.pkgID);
    BLangFunction blangFunction = bLangPackage.getFunctions().stream().filter(bLangFunction -> bLangFunction.getName().getValue().equals(functionName)).findFirst().orElse(null);
    if (!blangFunction.getDocumentationAttachments().isEmpty()) {
        // Get the first documentation attachment
        BLangDocumentation bLangDocumentation = blangFunction.getDocumentationAttachments().get(0);
        signatureInfoModel.setSignatureDescription(bLangDocumentation.documentationText.trim());
        bLangDocumentation.attributes.forEach(attribute -> {
            if (attribute.docTag.equals(DocTag.PARAM)) {
                paramDescMap.put(attribute.documentationField.getValue(), attribute.documentationText.trim());
            }
        });
    } else {
        // TODO: Should be deprecated in due course
        // Iterate over the attachments list and extract the attachment Description Map
        blangFunction.getAnnotationAttachments().forEach(annotationAttachment -> {
            BLangExpression expr = annotationAttachment.expr;
            if (expr instanceof BLangRecordLiteral) {
                List<BLangRecordLiteral.BLangRecordKeyValue> recordKeyValues = ((BLangRecordLiteral) expr).keyValuePairs;
                for (BLangRecordLiteral.BLangRecordKeyValue recordKeyValue : recordKeyValues) {
                    BLangExpression key = recordKeyValue.key.expr;
                    BLangExpression value = recordKeyValue.getValue();
                    if (key instanceof BLangSimpleVarRef && ((BLangSimpleVarRef) key).getVariableName().getValue().equals("value") && value instanceof BLangLiteral) {
                        String annotationValue = ((BLangLiteral) value).getValue().toString();
                        if (annotationAttachment.getAnnotationName().getValue().equals("Param")) {
                            String paramName = annotationValue.substring(0, annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD));
                            String annotationDesc = annotationValue.substring(annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD) + 1).trim();
                            paramDescMap.put(paramName, annotationDesc);
                        } else if (annotationAttachment.getAnnotationName().getValue().equals("Description")) {
                            signatureInfoModel.setSignatureDescription(annotationValue);
                        }
                    }
                }
            }
        });
    }
    bInvokableSymbol.getParameters().forEach(bVarSymbol -> {
        ParameterInfoModel parameterInfoModel = new ParameterInfoModel();
        parameterInfoModel.setParamType(bVarSymbol.getType().toString());
        parameterInfoModel.setParamValue(bVarSymbol.getName().getValue());
        parameterInfoModel.setDescription(paramDescMap.get(bVarSymbol.getName().getValue()));
        paramModels.add(parameterInfoModel);
    });
    signatureInfoModel.setParameterInfoModels(paramModels);
    return signatureInfoModel;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) HashMap(java.util.HashMap) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 2 with BLangPackage

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

the class HtmlDocTest method testAnonymousStructs.

@Test(description = "Tests whether anonymous structs are documented.")
public void testAnonymousStructs() {
    BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"Represents a person\"}" + "@Field {value:\"id: The identification number\"}\n" + "@Field {value:\"address: The address of the person.\"}" + "public struct Person {" + "  string id;" + "  struct {" + "     string address1;" + "     string address2;" + "     string state = \"MN\";" + "  } address;" + "}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 1);
    Assert.assertTrue(page.constructs.get(0) instanceof StructDoc, "Documentable of type StructDoc expected.");
    StructDoc personStructDoc = (StructDoc) page.constructs.get(0);
    Assert.assertEquals(personStructDoc.fields.size(), 2, "2 fields are expected.");
    Assert.assertEquals(personStructDoc.fields.get(0).name, "id", "Field \"id\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).name, "address", "Field \"address\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).description, "The address of the person.");
    Assert.assertEquals(personStructDoc.fields.get(1).dataType, "struct {string address1, string address2, string state}");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) StructDoc(org.ballerinalang.docgen.model.StructDoc) Test(org.testng.annotations.Test)

Example 3 with BLangPackage

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

the class HtmlDocTest method testGlobalVariables.

@Test(description = "Annotation in a package should be shown in the constructs")
public void testGlobalVariables() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "public int total = 98;" + "public string content = \"Name\";");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 2);
    Assert.assertEquals(page.constructs.get(0).name, "total");
    Assert.assertEquals(page.constructs.get(1).name, "content");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) Test(org.testng.annotations.Test)

Example 4 with BLangPackage

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

the class HtmlDocTest method testMultiPackage.

@Test(description = "Multiple packages should be shown even when one page is generated")
public void testMultiPackage() throws Exception {
    List<Link> packages = new ArrayList<>();
    packages.add(new Link(new PackageName("a.b.c", ""), "", false));
    packages.add(new Link(new PackageName("x.y", ""), "", false));
    packages.add(new Link(new PackageName("x.y.z", ""), "", false));
    BLangPackage bLangPackage = createPackage("package x.y;");
    Page page = Generator.generatePage(bLangPackage, packages);
    Assert.assertEquals(page.links.size(), 3);
    Assert.assertFalse(page.links.get(0).active);
    Assert.assertTrue(page.links.get(1).active);
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) PackageName(org.ballerinalang.docgen.model.PackageName) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) Link(org.ballerinalang.docgen.model.Link) Test(org.testng.annotations.Test)

Example 5 with BLangPackage

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

the class HtmlDocTest method testFunctionsWithoutStructBindings.

@Test(description = "One function without a struct bindings in a package should not be grouped together with the" + "structs shown in the constructs")
public void testFunctionsWithoutStructBindings() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "public function hello(){} " + "public struct Message { string message; int id;}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 2);
    Assert.assertEquals(page.constructs.get(0).name, "Message");
    Assert.assertEquals(page.constructs.get(1).name, "hello");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) 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