use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral in project ballerina by ballerina-lang.
the class Generator method fieldAnnotation.
/**
* Get description annotation of the field.
* @param node parent node.
* @param param field.
* @return description of the field.
*/
private static String fieldAnnotation(BLangNode node, BLangNode param) {
String subName = "";
if (param instanceof BLangVariable) {
BLangVariable paramVariable = (BLangVariable) param;
subName = (paramVariable.getName() == null) ? paramVariable.type.tsymbol.name.value : paramVariable.getName().getValue();
} else if (param instanceof BLangEnum.Enumerator) {
BLangEnum.Enumerator paramEnumVal = (BLangEnum.Enumerator) param;
subName = paramEnumVal.getName().getValue();
}
for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
continue;
}
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
String attribVal = bLangLiteral.toString();
if (annotation.getAnnotationName().getValue().equals("Field") && attribVal.startsWith(subName + ":")) {
return attribVal.split(subName + ":")[1].trim();
}
}
// annotation's value
for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
continue;
}
if (annotation.getAnnotationName().getValue().equals("Field")) {
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
return bLangLiteral.toString();
}
}
return "";
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral in project ballerina by ballerina-lang.
the class Generator method paramAnnotation.
/**
* Get description annotation of the parameter.
* @param node parent node.
* @param param parameter.
* @return description of the parameter.
*/
private static String paramAnnotation(BLangNode node, BLangVariable param) {
String subName = param.getName() == null ? param.type.tsymbol.name.value : param.getName().getValue();
for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
continue;
}
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
String attribVal = bLangLiteral.toString();
if ((annotation.getAnnotationName().getValue().equals("Param")) && attribVal.startsWith(subName + ":")) {
return attribVal.split(subName + ":")[1].trim();
}
}
return "";
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral in project ballerina by ballerina-lang.
the class Generator method annotFieldAnnotation.
/**
* Get description annotation of the annotation attribute.
* @param annotationNode parent node.
* @param annotAttribute annotation attribute.
* @return description of the annotation attribute.
*/
private static String annotFieldAnnotation(BLangAnnotation annotationNode, BLangAnnotAttribute annotAttribute) {
List<? extends AnnotationAttachmentNode> annotationAttachments = getAnnotationAttachments(annotationNode);
for (AnnotationAttachmentNode annotation : annotationAttachments) {
if ("Field".equals(annotation.getAnnotationName().getValue())) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
String value = bLangLiteral.toString();
if (value.startsWith(annotAttribute.getName().getValue())) {
String[] valueParts = value.split(":");
return valueParts.length == 2 ? valueParts[1] : valueParts[0];
}
}
}
return "";
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral in project ballerina by ballerina-lang.
the class SignatureHelpUtil method getSignatureInfoModel.
/**
* Get the required signature information filled model.
*
* @param bInvokableSymbol Invokable symbol
* @param signatureContext Signature operation context
* @return {@link SignatureInfoModel} SignatureInfoModel containing signature information
*/
private static SignatureInfoModel getSignatureInfoModel(BInvokableSymbol bInvokableSymbol, TextDocumentServiceContext signatureContext) {
Map<String, String> paramDescMap = new HashMap<>();
SignatureInfoModel signatureInfoModel = new SignatureInfoModel();
List<ParameterInfoModel> paramModels = new ArrayList<>();
String functionName = signatureContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
CompilerContext compilerContext = signatureContext.get(DocumentServiceKeys.COMPILER_CONTEXT_KEY);
BLangPackage bLangPackage = signatureContext.get(DocumentServiceKeys.LS_PACKAGE_CACHE_KEY).findPackage(compilerContext, bInvokableSymbol.pkgID);
BLangFunction blangFunction = bLangPackage.getFunctions().stream().filter(bLangFunction -> bLangFunction.getName().getValue().equals(functionName)).findFirst().orElse(null);
if (!blangFunction.getDocumentationAttachments().isEmpty()) {
// Get the first documentation attachment
BLangDocumentation bLangDocumentation = blangFunction.getDocumentationAttachments().get(0);
signatureInfoModel.setSignatureDescription(bLangDocumentation.documentationText.trim());
bLangDocumentation.attributes.forEach(attribute -> {
if (attribute.docTag.equals(DocTag.PARAM)) {
paramDescMap.put(attribute.documentationField.getValue(), attribute.documentationText.trim());
}
});
} else {
// TODO: Should be deprecated in due course
// Iterate over the attachments list and extract the attachment Description Map
blangFunction.getAnnotationAttachments().forEach(annotationAttachment -> {
BLangExpression expr = annotationAttachment.expr;
if (expr instanceof BLangRecordLiteral) {
List<BLangRecordLiteral.BLangRecordKeyValue> recordKeyValues = ((BLangRecordLiteral) expr).keyValuePairs;
for (BLangRecordLiteral.BLangRecordKeyValue recordKeyValue : recordKeyValues) {
BLangExpression key = recordKeyValue.key.expr;
BLangExpression value = recordKeyValue.getValue();
if (key instanceof BLangSimpleVarRef && ((BLangSimpleVarRef) key).getVariableName().getValue().equals("value") && value instanceof BLangLiteral) {
String annotationValue = ((BLangLiteral) value).getValue().toString();
if (annotationAttachment.getAnnotationName().getValue().equals("Param")) {
String paramName = annotationValue.substring(0, annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD));
String annotationDesc = annotationValue.substring(annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD) + 1).trim();
paramDescMap.put(paramName, annotationDesc);
} else if (annotationAttachment.getAnnotationName().getValue().equals("Description")) {
signatureInfoModel.setSignatureDescription(annotationValue);
}
}
}
}
});
}
bInvokableSymbol.getParameters().forEach(bVarSymbol -> {
ParameterInfoModel parameterInfoModel = new ParameterInfoModel();
parameterInfoModel.setParamType(bVarSymbol.getType().toString());
parameterInfoModel.setParamValue(bVarSymbol.getName().getValue());
parameterInfoModel.setDescription(paramDescMap.get(bVarSymbol.getName().getValue()));
paramModels.add(parameterInfoModel);
});
signatureInfoModel.setParameterInfoModels(paramModels);
return signatureInfoModel;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral in project ballerina by ballerina-lang.
the class ParserUtils method createNewStruct.
/**
* Create new struct.
*
* @param name name of the struct
* @param fields field definiton statements
* @return {Function} function
*/
private static Struct createNewStruct(String name, List<BLangVariable> fields, String fileName) {
Struct struct = new Struct(name);
fields.forEach((field) -> {
String defaultValue = null;
if (field.getInitialExpression() != null) {
defaultValue = ((BLangLiteral) field.getInitialExpression()).getValue().toString();
}
StructField structField = createNewStructField(field.getName().getValue(), field.getTypeNode().type.toString(), defaultValue);
struct.addStructField(structField);
});
struct.setFileName(fileName);
return struct;
}
Aggregations