Search in sources :

Example 11 with BLangDocumentation

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

the class HoverUtil method getDocumentationContent.

/**
 * Get documentation content.
 *
 * @param docAnnotation list of doc annotation
 * @return {@link Hover} hover object.
 */
private static Hover getDocumentationContent(List<BLangDocumentation> docAnnotation) {
    Hover hover = new Hover();
    StringBuilder content = new StringBuilder();
    BLangDocumentation bLangDocumentation = docAnnotation.get(0);
    Map<String, List<BLangDocumentationAttribute>> filterAttributes = filterDocumentationAttributes(docAnnotation.get(0));
    if (!bLangDocumentation.documentationText.isEmpty()) {
        content.append(getFormattedHoverDocContent(ContextConstants.DESCRIPTION, bLangDocumentation.documentationText));
    }
    if (filterAttributes.get(ContextConstants.DOC_RECEIVER) != null) {
        content.append(getFormattedHoverDocContent(ContextConstants.DOC_RECEIVER, getDocAttributes(filterAttributes.get(ContextConstants.DOC_RECEIVER))));
    }
    if (filterAttributes.get(ContextConstants.DOC_PARAM) != null) {
        content.append(getFormattedHoverDocContent(ContextConstants.DOC_PARAM, getDocAttributes(filterAttributes.get(ContextConstants.DOC_PARAM))));
    }
    if (filterAttributes.get(ContextConstants.DOC_FIELD) != null) {
        content.append(getFormattedHoverDocContent(ContextConstants.DOC_FIELD, getDocAttributes(filterAttributes.get(ContextConstants.DOC_FIELD))));
    }
    if (filterAttributes.get(ContextConstants.DOC_RETURN) != null) {
        content.append(getFormattedHoverDocContent(ContextConstants.DOC_RETURN, getDocAttributes(filterAttributes.get(ContextConstants.DOC_RETURN))));
    }
    if (filterAttributes.get(ContextConstants.DOC_VARIABLE) != null) {
        content.append(getFormattedHoverDocContent(ContextConstants.DOC_VARIABLE, getDocAttributes(filterAttributes.get(ContextConstants.DOC_VARIABLE))));
    }
    List<Either<String, MarkedString>> contents = new ArrayList<>();
    contents.add(Either.forLeft(content.toString()));
    hover.setContents(contents);
    return hover;
}
Also used : Hover(org.eclipse.lsp4j.Hover) ArrayList(java.util.ArrayList) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) ArrayList(java.util.ArrayList) List(java.util.List) MarkedString(org.eclipse.lsp4j.MarkedString)

Example 12 with BLangDocumentation

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

the class SemanticAnalyzer method visit.

@Override
public void visit(BLangDocumentation docNode) {
    Set<BLangIdentifier> visitedAttributes = new HashSet<>();
    for (BLangDocumentationAttribute attribute : docNode.attributes) {
        if (!visitedAttributes.add(attribute.documentationField)) {
            this.dlog.warning(attribute.pos, DiagnosticCode.DUPLICATE_DOCUMENTED_ATTRIBUTE, attribute.documentationField);
            continue;
        }
        Name attributeName = names.fromIdNode(attribute.documentationField);
        BSymbol attributeSymbol = this.env.scope.lookup(attributeName).symbol;
        if (attributeSymbol == null) {
            this.dlog.warning(attribute.pos, DiagnosticCode.NO_SUCH_DOCUMENTABLE_ATTRIBUTE, attribute.documentationField, attribute.docTag.getValue());
            continue;
        }
        int ownerSymTag = env.scope.owner.tag;
        if ((ownerSymTag & SymTag.ANNOTATION) == SymTag.ANNOTATION) {
            if (attributeSymbol.tag != SymTag.ANNOTATION_ATTRIBUTE || ((BAnnotationAttributeSymbol) attributeSymbol).docTag != attribute.docTag) {
                this.dlog.warning(attribute.pos, DiagnosticCode.NO_SUCH_DOCUMENTABLE_ATTRIBUTE, attribute.documentationField, attribute.docTag.getValue());
                continue;
            }
        } else {
            if (attributeSymbol.tag != SymTag.VARIABLE || ((BVarSymbol) attributeSymbol).docTag != attribute.docTag) {
                this.dlog.warning(attribute.pos, DiagnosticCode.NO_SUCH_DOCUMENTABLE_ATTRIBUTE, attribute.documentationField, attribute.docTag.getValue());
                continue;
            }
        }
        attribute.type = attributeSymbol.type;
    }
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangDocumentationAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangDocumentationAttribute) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BAnnotationAttributeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationAttributeSymbol) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) HashSet(java.util.HashSet) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 13 with BLangDocumentation

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

the class HoverUtil method filterDocumentationAttributes.

/**
 * Filter documentation attributes to each tags.
 *
 * @param bLangDocumentation documentation node
 * @return {@link Map} filtered content map
 */
private static Map<String, List<BLangDocumentationAttribute>> filterDocumentationAttributes(BLangDocumentation bLangDocumentation) {
    Map<String, List<BLangDocumentationAttribute>> filteredAttributes = new HashMap<>();
    for (BLangDocumentationAttribute bLangDocumentationAttribute : bLangDocumentation.attributes) {
        if (filteredAttributes.get(bLangDocumentationAttribute.docTag.name()) == null) {
            filteredAttributes.put(bLangDocumentationAttribute.docTag.name(), new ArrayList<>());
            filteredAttributes.get(bLangDocumentationAttribute.docTag.name()).add(bLangDocumentationAttribute);
        } else {
            filteredAttributes.get(bLangDocumentationAttribute.docTag.name()).add(bLangDocumentationAttribute);
        }
    }
    return filteredAttributes;
}
Also used : HashMap(java.util.HashMap) BLangDocumentationAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangDocumentationAttribute) ArrayList(java.util.ArrayList) List(java.util.List) MarkedString(org.eclipse.lsp4j.MarkedString)

Example 14 with BLangDocumentation

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

the class DocumentationTest method testDeprecatedTransformer.

@Test(description = "Test doc deprecated Transformer.")
public void testDeprecatedTransformer() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/deprecated_transformer.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    List<BLangDeprecatedNode> dNodes = ((BLangTransformer) packageNode.getTransformers().get(0)).deprecatedAttachments;
    BLangDeprecatedNode dNode = dNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, "\n" + "  This Transformer is deprecated use\n" + "  `transformer <Person p, Employee e> Bar(any defaultAddress) { e.name = p.firstName; }\n" + "  ` instead.\n");
    List<BLangDocumentation> docNodes = ((BLangTransformer) packageNode.getTransformers().get(0)).docAttachments;
    BLangDocumentation docNode = docNodes.get(0);
    Assert.assertNotNull(docNode);
    Assert.assertEquals(docNode.documentationText, "\n Transformer Foo Person -> Employee\n ");
    Assert.assertEquals(docNode.getAttributes().size(), 3);
    Assert.assertEquals(docNode.getAttributes().get(0).documentationField.getValue(), "p");
    Assert.assertEquals(docNode.getAttributes().get(0).documentationText, " input struct Person source used for transformation\n ");
    Assert.assertEquals(docNode.getAttributes().get(1).documentationField.getValue(), "e");
    Assert.assertEquals(docNode.getAttributes().get(1).documentationText, " output struct Employee struct which Person transformed to\n ");
    Assert.assertEquals(docNode.getAttributes().get(2).documentationField.getValue(), "defaultAddress");
    Assert.assertEquals(docNode.getAttributes().get(2).documentationText, " address which serves Eg: `POSTCODE 112`\n");
}
Also used : BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) CompileResult(org.ballerinalang.launcher.util.CompileResult) BLangDeprecatedNode(org.wso2.ballerinalang.compiler.tree.BLangDeprecatedNode) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Example 15 with BLangDocumentation

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

the class DocumentationTest method testDocEnum.

@Test(description = "Test doc annotation enum.")
public void testDocEnum() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/enum.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    List<BLangDocumentation> docNodes = ((BLangEnum) packageNode.getEnums().get(0)).docAttachments;
    BLangDocumentation 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");
}
Also used : BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) CompileResult(org.ballerinalang.launcher.util.CompileResult) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Aggregations

BLangDocumentation (org.wso2.ballerinalang.compiler.tree.BLangDocumentation)17 CompileResult (org.ballerinalang.launcher.util.CompileResult)14 PackageNode (org.ballerinalang.model.tree.PackageNode)14 Test (org.testng.annotations.Test)14 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 ArrayList (java.util.ArrayList)3 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)3 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)3 HashMap (java.util.HashMap)2 List (java.util.List)2 MarkedString (org.eclipse.lsp4j.MarkedString)2 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)2 BLangTransformer (org.wso2.ballerinalang.compiler.tree.BLangTransformer)2 BLangDocumentationAttribute (org.wso2.ballerinalang.compiler.tree.expressions.BLangDocumentationAttribute)2 HashSet (java.util.HashSet)1 Hover (org.eclipse.lsp4j.Hover)1 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)1 BAnnotationAttributeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationAttributeSymbol)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)1