use of org.wso2.ballerinalang.compiler.tree.BLangService in project ballerina by ballerina-lang.
the class ServiceScopeResolver 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) {
BLangService bLangService = (BLangService) treeVisitor.getBlockOwnerStack().peek();
List<BLangResource> resources = bLangService.resources;
List<BLangVariableDef> variableDefs = bLangService.vars;
int serviceEndLine = bLangService.pos.getEndLine();
int serviceEndCol = bLangService.pos.getEndColumn();
int nodeEndLine = node.getPosition().getEndLine();
int nodeEndCol = node.getPosition().getEndColumn();
boolean isLastChildNode;
if (resources.isEmpty()) {
isLastChildNode = variableDefs.indexOf(node) == (variableDefs.size() - 1);
} else {
isLastChildNode = resources.indexOf(node) == (resources.size() - 1);
}
boolean isWithinScope = (isLastChildNode && (curLine < serviceEndLine || (curLine == serviceEndLine && curCol < serviceEndCol)) && (nodeEndLine < curLine || (nodeEndLine == curLine && nodeEndCol < curCol)));
if (isWithinScope) {
treeVisitor.setPreviousNode((BLangNode) node);
}
return isWithinScope;
}
use of org.wso2.ballerinalang.compiler.tree.BLangService in project ballerina by ballerina-lang.
the class DocumentationTest method testDeprecated.
@Test(description = "Test doc deprecated.", enabled = false)
public void testDeprecated() {
CompileResult compileResult = BCompileUtil.compile("test-src/documentation/deprecated.bal");
Assert.assertEquals(compileResult.getWarnCount(), 0);
PackageNode packageNode = compileResult.getAST();
List<BLangDeprecatedNode> dNodes = ((BLangFunction) packageNode.getFunctions().get(0)).deprecatedAttachments;
BLangDeprecatedNode dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This function is deprecated use `openFile(string accessMode){}` instead.\n");
dNodes = ((BLangStruct) packageNode.getStructs().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Struct is deprecated use `File2` instead.\n");
dNodes = ((BLangEnum) packageNode.getEnums().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Enum is deprecated use `Enum2` instead.\n");
dNodes = ((BLangEnum) packageNode.getEnums().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Enum is deprecated use `Enum2` instead.\n");
dNodes = ((BLangVariable) packageNode.getGlobalVariables().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "use ```const string testConst = " + "\"TestConstantDocumentation\";``` instead");
dNodes = ((BLangConnector) packageNode.getConnectors().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Connector is deprecated use `Connector(string url2){}` instead.\n");
dNodes = ((BLangConnector) packageNode.getConnectors().get(0)).getActions().get(0).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This action is deprecated use `Connector.test(string url2){}` instead.\n" + " ");
dNodes = ((BLangService) packageNode.getServices().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Service is deprecated use `PizzaHutService{}` instead.\n");
dNodes = ((BLangService) packageNode.getServices().get(0)).getResources().get(0).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "This Resource is deprecated use `PizzaHutService.orderFromPizza()` instead.");
}
use of org.wso2.ballerinalang.compiler.tree.BLangService in project ballerina by ballerina-lang.
the class DocumentationTest method testMultiple.
@Test(description = "Test doc multiple.")
public void testMultiple() {
CompileResult compileResult = BCompileUtil.compile("test-src/documentation/multiple.bal");
Assert.assertEquals(0, compileResult.getWarnCount());
PackageNode packageNode = compileResult.getAST();
List<BLangDocumentation> docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
BLangDocumentation dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, " Documentation for Tst struct\n");
Assert.assertEquals(dNode.getAttributes().size(), 3);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "a");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " annotation `field a` documentation\n");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "b");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " annotation `field b` documentation\n");
Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "c");
Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " annotation `field c` documentation");
docNodes = ((BLangEnum) packageNode.getEnums().get(0)).docAttachments;
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");
docNodes = ((BLangTransformer) packageNode.getTransformers().get(0)).docAttachments;
dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " Transformer Foo Person -> Employee\n" + " ");
Assert.assertEquals(dNode.getAttributes().size(), 3);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "p");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " input struct Person source used for transformation\n ");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "e");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " output struct Employee struct which Person transformed to\n ");
Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "defaultAddress");
Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " address which serves Eg: `POSTCODE 112`\n");
BLangService service = (BLangService) packageNode.getServices().get(0);
docNodes = service.docAttachments;
dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "PizzaService HTTP Service");
/*
// Commented due to https://github.com/ballerina-lang/ballerina/issues/5586 issue
dNode = service.getResources().get(0).docAttachments.get(0);
Assert.assertEquals(dNode.getAttributes().size(), 2);
Assert.assertEquals(dNode.documentationText, "Check orderPizza resource. ");
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "conn");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText,
" HTTP connection. ");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "req");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText,
" In request.");
dNode = service.getResources().get(1).docAttachments.get(0);
Assert.assertEquals(dNode.documentationText, "Check status resource. ");
Assert.assertEquals(dNode.getAttributes().size(), 2);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "conn");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText,
" HTTP connection. ");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "req");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText,
" In request.");*/
}
use of org.wso2.ballerinalang.compiler.tree.BLangService 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;
}
Aggregations