Search in sources :

Example 6 with TopLevelNode

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

the class SwaggerConverterUtils method getAlias.

/**
 * Gets the alias for a given package from a bLang file root node.
 * @param topCompilationUnit The root node.
 * @param packageName The package name.
 * @return The alias.
 */
private static String getAlias(BLangCompilationUnit topCompilationUnit, String packageName) {
    for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
        if (topLevelNode instanceof BLangImportPackage) {
            BLangImportPackage importPackage = (BLangImportPackage) topLevelNode;
            String packagePath = importPackage.getPackageName().stream().map(BLangIdentifier::getValue).collect(Collectors.joining("."));
            if (packageName.equals(packagePath)) {
                return importPackage.getAlias().getValue();
            }
        }
    }
    return null;
}
Also used : BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 7 with TopLevelNode

use of org.ballerinalang.model.tree.TopLevelNode 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 8 with TopLevelNode

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

the class CommandUtil method getTransformerDocumentationByPosition.

/**
 * Get the Documentation attachment for the transformer.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the transformer in the source
 * @return {@link String}   Documentation attachment for the transformer
 */
static DocAttachmentInfo getTransformerDocumentationByPosition(BLangPackage bLangPackage, int line) {
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangTransformer) {
            BLangTransformer transformerNode = (BLangTransformer) topLevelNode;
            DiagnosticPos transformerPos = CommonUtil.toZeroBasedPosition(transformerNode.getPosition());
            int transformerStart = transformerPos.getStartLine();
            if (transformerStart == line) {
                return getTransformerNodeDocumentation(transformerNode, line);
            }
        }
    }
    return null;
}
Also used : BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 9 with TopLevelNode

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

the class CommandUtil method getResourceDocumentationByPosition.

/**
 * Get the Documentation attachment for the resource.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the resource in the source
 * @return {@link String}   Documentation attachment for the resource
 */
static DocAttachmentInfo getResourceDocumentationByPosition(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;
            for (BLangResource bLangResource : serviceNode.getResources()) {
                List<BLangAnnotationAttachment> annotationAttachments = bLangResource.getAnnotationAttachments();
                DiagnosticPos resourcePos = CommonUtil.toZeroBasedPosition(bLangResource.getPosition());
                if (!annotationAttachments.isEmpty()) {
                    DiagnosticPos lastAttachmentPos = CommonUtil.toZeroBasedPosition(annotationAttachments.get(annotationAttachments.size() - 1).getPosition());
                    if (lastAttachmentPos.getEndLine() < line && line < resourcePos.getEndLine()) {
                        return getResourceNodeDocumentation(bLangResource, lastAttachmentPos.getEndLine() + 1);
                    }
                } else if (resourcePos.getStartLine() == line) {
                    return getResourceNodeDocumentation(bLangResource, 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) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 10 with TopLevelNode

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

the class CommandUtil method getFunctionDocumentationByPosition.

/**
 * Get the Documentation attachment for the function.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the function in the source
 * @return {@link String}   Documentation attachment for the function
 */
static DocAttachmentInfo getFunctionDocumentationByPosition(BLangPackage bLangPackage, int line) {
    List<FunctionNode> filteredFunctions = new ArrayList<>();
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangFunction) {
            filteredFunctions.add((BLangFunction) topLevelNode);
        } else if (topLevelNode instanceof BLangObject) {
            filteredFunctions.addAll(((BLangObject) topLevelNode).getFunctions());
        }
        for (FunctionNode filteredFunction : filteredFunctions) {
            DiagnosticPos functionPos = CommonUtil.toZeroBasedPosition((DiagnosticPos) filteredFunction.getPosition());
            int functionStart = functionPos.getStartLine();
            if (functionStart == line) {
                return getFunctionNodeDocumentation(filteredFunction, line);
            }
        }
    }
    return null;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) FunctionNode(org.ballerinalang.model.tree.FunctionNode) ArrayList(java.util.ArrayList) 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