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;
}
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));
}
}
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;
}
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;
}
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;
}
Aggregations