use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class ObjectTypeScopeResolver method isCursorBeforeNode.
/**
* Check whether the cursor is positioned before the given node start.
*
* @param nodePosition Position of the node
* @param node Node
* @param treeVisitor {@link TreeVisitor} current tree visitor instance
* @param completionContext Completion operation context
* @return {@link Boolean} Whether the cursor is before the node start or not
*/
@Override
public boolean isCursorBeforeNode(DiagnosticPos nodePosition, Node node, TreeVisitor treeVisitor, TextDocumentServiceContext completionContext) {
if (!(treeVisitor.getBlockOwnerStack().peek() instanceof BLangObject)) {
return false;
}
BLangObject ownerObject = (BLangObject) treeVisitor.getBlockOwnerStack().peek();
DiagnosticPos zeroBasedPos = CommonUtil.toZeroBasedPosition(nodePosition);
DiagnosticPos blockOwnerPos = CommonUtil.toZeroBasedPosition((DiagnosticPos) treeVisitor.getBlockOwnerStack().peek().getPosition());
int line = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
int col = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();
boolean isLastField = false;
if ((!ownerObject.fields.isEmpty() && node instanceof BLangVariable && ownerObject.fields.indexOf(node) == ownerObject.fields.size() - 1 && ownerObject.functions.isEmpty()) || (!ownerObject.functions.isEmpty() && node instanceof BLangFunction && ownerObject.functions.indexOf(node) == ownerObject.functions.size() - 1)) {
isLastField = true;
}
if ((line < zeroBasedPos.getStartLine() || (line == zeroBasedPos.getStartLine() && col < zeroBasedPos.getStartColumn())) || (isLastField && ((blockOwnerPos.getEndLine() > line && zeroBasedPos.getEndLine() < line) || (blockOwnerPos.getEndLine() == line && blockOwnerPos.getEndColumn() > col)))) {
Map<Name, Scope.ScopeEntry> visibleSymbolEntries = treeVisitor.resolveAllVisibleSymbols(treeVisitor.getSymbolEnv());
treeVisitor.populateSymbols(visibleSymbolEntries, null);
treeVisitor.setTerminateVisitor(true);
return true;
}
return false;
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class Generator method createDocForNode.
/**
* Create documentation for functions.
* @param functionNode ballerina function node.
* @return documentation for functions.
*/
public static FunctionDoc createDocForNode(BLangFunction functionNode) {
String functionName = functionNode.getName().value;
List<Variable> parameters = new ArrayList<>();
List<Variable> returnParams = new ArrayList<>();
// Iterate through the parameters
if (functionNode.getParameters().size() > 0) {
for (BLangVariable param : functionNode.getParameters()) {
String dataType = type(param);
String desc = paramAnnotation(functionNode, param);
Variable variable = new Variable(param.getName().value, dataType, desc);
parameters.add(variable);
}
}
// Iterate through the return types
if (functionNode.getReturnParameters().size() > 0) {
for (int i = 0; i < functionNode.getReturnParameters().size(); i++) {
BLangVariable returnParam = functionNode.getReturnParameters().get(i);
String dataType = type(returnParam);
String desc = returnParamAnnotation(functionNode, i);
Variable variable = new Variable(returnParam.getName().value, dataType, desc);
returnParams.add(variable);
}
}
return new FunctionDoc(functionName, description(functionNode), new ArrayList<>(), parameters, returnParams);
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class BallerinaDocGenForCurrentPackageTest method testStruct.
@Test(description = "Test a bal file with a function in the current package")
public void testStruct() {
try {
Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "balFileInCurrentPackage.bal");
Assert.assertNotNull(docsMap);
Assert.assertEquals(docsMap.size(), 1);
BLangPackage balPackage = docsMap.get(".");
List<BLangFunction> functions = balPackage.getFunctions();
Assert.assertEquals(functions.size(), 1);
BLangFunction function = functions.iterator().next();
Assert.assertEquals(function.getParameters().size(), 1);
Assert.assertEquals(function.getAnnotationAttachments().size(), 2);
} catch (IOException e) {
Assert.fail();
} finally {
BallerinaDocGenTestUtils.cleanUp();
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class BallerinaFunctionDocGenTest method testABalWithMultipleFunctions.
@Test(description = "Test a Bal file with multiple Functions")
public void testABalWithMultipleFunctions() {
try {
Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "balWith2Functions.bal");
Assert.assertNotNull(docsMap);
Assert.assertEquals(docsMap.size(), 1);
BallerinaDocGenTestUtils.printDocMap(docsMap);
BLangPackage doc = docsMap.get(".");
Collection<BLangFunction> functions = doc.getFunctions();
Assert.assertEquals(functions.size(), 2);
Iterator<BLangFunction> iterator = functions.iterator();
BLangFunction function = iterator.next();
Assert.assertEquals(function.getParameters().size(), 1);
Assert.assertEquals(function.getReturnParameters().size(), 1);
BLangFunction function1 = iterator.next();
Assert.assertEquals(function1.getParameters().size(), 2);
Assert.assertEquals(function1.getReturnParameters().size(), 0);
} catch (IOException e) {
Assert.fail();
} finally {
BallerinaDocGenTestUtils.cleanUp();
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class SymbolEnter method validateFuncReceiver.
private boolean validateFuncReceiver(BLangFunction funcNode) {
if (funcNode.receiver == null) {
return true;
}
BType varType = symResolver.resolveTypeNode(funcNode.receiver.typeNode, env);
funcNode.receiver.type = varType;
if (varType.tag == TypeTags.ERROR) {
return true;
}
if (varType.tag != TypeTags.BOOLEAN && varType.tag != TypeTags.STRING && varType.tag != TypeTags.INT && varType.tag != TypeTags.FLOAT && varType.tag != TypeTags.BLOB && varType.tag != TypeTags.JSON && varType.tag != TypeTags.XML && varType.tag != TypeTags.MAP && varType.tag != TypeTags.TABLE && varType.tag != TypeTags.STREAM && varType.tag != TypeTags.FUTURE && varType.tag != TypeTags.STRUCT) {
dlog.error(funcNode.receiver.pos, DiagnosticCode.FUNC_DEFINED_ON_NOT_SUPPORTED_TYPE, funcNode.name.value, varType.toString());
return false;
}
if (!this.env.enclPkg.symbol.pkgID.equals(varType.tsymbol.pkgID)) {
dlog.error(funcNode.receiver.pos, DiagnosticCode.FUNC_DEFINED_ON_NON_LOCAL_TYPE, funcNode.name.value, varType.toString());
return false;
}
return true;
}
Aggregations