use of org.wso2.ballerinalang.compiler.tree.BLangConnector in project ballerina by ballerina-lang.
the class AnnotationDesugar method rewritePackageAnnotations.
protected void rewritePackageAnnotations(BLangPackage pkgNode) {
BLangFunction initFunction = pkgNode.initFunction;
// Remove last return statement. we will add it later. TODO : Fix this properly.
initFunction.body.stmts.remove(initFunction.body.stmts.size() - 1);
// This is the variable which store all package level annotations.
BLangVariable annotationMap = createGlobalAnnotationMapVar(pkgNode);
// handle Service Annotations.
for (BLangService service : pkgNode.services) {
generateAnnotations(service, service.name.value, initFunction, annotationMap);
for (BLangResource resource : service.resources) {
String key = service.name.value + DOT + resource.name.value;
generateAnnotations(resource, key, initFunction, annotationMap);
}
}
// Handle Function Annotations.
for (BLangFunction function : pkgNode.functions) {
generateAnnotations(function, function.symbol.name.value, initFunction, annotationMap);
}
// Handle Connector Annotations.
for (BLangConnector connector : pkgNode.connectors) {
generateAnnotations(connector, connector.name.value, initFunction, annotationMap);
for (BLangAction action : connector.actions) {
String key = connector.name.value + DOT + action.name.value;
generateAnnotations(connector, key, initFunction, annotationMap);
}
}
// Handle Struct Annotations.
for (BLangStruct struct : pkgNode.structs) {
generateAnnotations(struct, struct.name.value, initFunction, annotationMap);
for (BLangVariable field : struct.fields) {
String key = struct.name.value + DOT + field.name.value;
generateAnnotations(field, key, initFunction, annotationMap);
}
}
for (BLangEndpoint globalEndpoint : pkgNode.globalEndpoints) {
generateAnnotations(globalEndpoint, globalEndpoint.name.value, initFunction, annotationMap);
}
ASTBuilderUtil.createReturnStmt(pkgNode.pos, initFunction.body);
}
use of org.wso2.ballerinalang.compiler.tree.BLangConnector in project ballerina by ballerina-lang.
the class HoverUtil method getHoverInformation.
/**
* Get the hover information for the given hover context.
*
* @param bLangPackage resolved bLangPackage for the hover context.
* @param hoverContext context of the hover.
* @return hover content.
*/
public static Hover getHoverInformation(BLangPackage bLangPackage, TextDocumentServiceContext hoverContext) {
Hover hover;
switch(hoverContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_PARENT_KEY)) {
case ContextConstants.FUNCTION:
BLangFunction bLangFunction = bLangPackage.functions.stream().filter(function -> function.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangFunction != null) {
if (bLangFunction.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangFunction.docAttachments);
} else {
hover = getAnnotationContent(bLangFunction.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.STRUCT:
BLangStruct bLangStruct = bLangPackage.structs.stream().filter(struct -> struct.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangStruct != null) {
if (bLangStruct.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangStruct.docAttachments);
} else {
hover = getAnnotationContent(bLangStruct.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.OBJECT:
BLangObject bLangObject = bLangPackage.objects.stream().filter(object -> object.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangObject != null) {
if (bLangObject.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangObject.docAttachments);
} else {
hover = getAnnotationContent(bLangObject.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.ENUM:
BLangEnum bLangEnum = bLangPackage.enums.stream().filter(bEnum -> bEnum.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangEnum != null) {
if (bLangEnum.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangEnum.docAttachments);
} else {
hover = getAnnotationContent(bLangEnum.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.TRANSFORMER:
BLangTransformer bLangTransformer = bLangPackage.transformers.stream().filter(bTransformer -> bTransformer.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangTransformer != null) {
if (bLangTransformer.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangTransformer.docAttachments);
} else {
hover = getAnnotationContent(bLangTransformer.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.CONNECTOR:
BLangConnector bLangConnector = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangConnector != null) {
if (bLangConnector.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangConnector.docAttachments);
} else {
hover = getAnnotationContent(bLangConnector.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.ACTION:
BLangAction bLangAction = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(((BLangInvocation) hoverContext.get(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY)).symbol.owner.name.getValue())).flatMap(connector -> connector.actions.stream()).filter(bAction -> bAction.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangAction != null) {
if (bLangAction.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangAction.docAttachments);
} else {
hover = getAnnotationContent(bLangAction.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.ENDPOINT:
BLangEndpoint bLangEndpoint = bLangPackage.globalEndpoints.stream().filter(globalEndpoint -> globalEndpoint.name.value.equals(hoverContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangEndpoint != null) {
hover = getAnnotationContent(bLangEndpoint.annAttachments);
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.VARIABLE:
BLangVariable bLangVariable = bLangPackage.globalVars.stream().filter(globalVar -> globalVar.name.getValue().equals(hoverContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangVariable != null) {
if (bLangVariable.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangVariable.docAttachments);
} else {
hover = getAnnotationContent(bLangVariable.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
default:
hover = new Hover();
List<Either<String, MarkedString>> contents = new ArrayList<>();
contents.add(Either.forLeft(""));
hover.setContents(contents);
break;
}
return hover;
}
use of org.wso2.ballerinalang.compiler.tree.BLangConnector in project ballerina by ballerina-lang.
the class ConnectorScopeResolver method isWithinScopeAfterLastChildNode.
/**
* Check whether the given node is within the scope and located after the last child node.
* @param node Current Node to evaluate
* @param treeVisitor Operation Tree Visitor
* @param curLine line of the cursor
* @param curCol column of the cursor
* @return {@link Boolean} whether the last child node or not
*/
protected boolean isWithinScopeAfterLastChildNode(Node node, TreeVisitor treeVisitor, int curLine, int curCol) {
BLangConnector bLangConnector = (BLangConnector) treeVisitor.getBlockOwnerStack().peek();
List<BLangAction> actions = bLangConnector.actions;
List<BLangVariableDef> variableDefs = bLangConnector.varDefs;
int connectorEndLine = bLangConnector.pos.getEndLine();
int connectorEndCol = bLangConnector.pos.getEndColumn();
int nodeEndLine = node.getPosition().getEndLine();
int nodeEndCol = node.getPosition().getEndColumn();
boolean isLastChildNode;
if (actions.isEmpty()) {
isLastChildNode = variableDefs.indexOf(node) == (variableDefs.size() - 1);
} else {
isLastChildNode = actions.indexOf(node) == (actions.size() - 1);
}
boolean isWithinScope = (isLastChildNode && (curLine < connectorEndLine || (curLine == connectorEndLine && curCol < connectorEndCol)) && (nodeEndLine < curLine || (nodeEndLine == curLine && nodeEndCol < curCol)));
if (isWithinScope) {
treeVisitor.setPreviousNode((BLangNode) node);
}
return isWithinScope;
}
use of org.wso2.ballerinalang.compiler.tree.BLangConnector 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);
}
use of org.wso2.ballerinalang.compiler.tree.BLangConnector 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();
}
}
Aggregations