use of org.wso2.ballerinalang.compiler.tree.BLangFunction 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.BLangFunction in project ballerina by ballerina-lang.
the class SignatureTreeVisitor method visit.
@Override
public void visit(BLangFunction funcNode) {
BSymbol funcSymbol = funcNode.symbol;
if (Symbols.isNative(funcSymbol)) {
return;
}
SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcSymbol.scope, symbolEnv);
blockOwnerStack.push(funcNode);
this.acceptNode(funcNode.body, funcEnv);
blockOwnerStack.pop();
// Process workers
if (terminateVisitor && !funcNode.workers.isEmpty()) {
terminateVisitor = false;
}
funcNode.workers.forEach(e -> this.symbolEnter.defineNode(e, funcEnv));
funcNode.workers.forEach(e -> this.acceptNode(e, funcEnv));
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class CommandExecutor method getDocumentEditForNode.
/**
* Get the document edit attachment info for a given particular node.
* @param node Node given
* @return Doc Attachment Info
*/
private static CommandUtil.DocAttachmentInfo getDocumentEditForNode(Node node) {
CommandUtil.DocAttachmentInfo docAttachmentInfo = null;
int replaceFrom;
switch(node.getKind()) {
case FUNCTION:
if (((BLangFunction) node).docAttachments.isEmpty()) {
replaceFrom = CommonUtil.toZeroBasedPosition(((BLangFunction) node).getPosition()).getStartLine();
docAttachmentInfo = CommandUtil.getFunctionNodeDocumentation((BLangFunction) node, replaceFrom);
}
break;
case STRUCT:
if (((BLangStruct) node).docAttachments.isEmpty()) {
replaceFrom = CommonUtil.toZeroBasedPosition(((BLangStruct) node).getPosition()).getStartLine();
docAttachmentInfo = CommandUtil.getStructNodeDocumentation((BLangStruct) node, replaceFrom);
}
break;
case ENUM:
if (((BLangEnum) node).docAttachments.isEmpty()) {
replaceFrom = CommonUtil.toZeroBasedPosition(((BLangEnum) node).getPosition()).getStartLine();
docAttachmentInfo = CommandUtil.getEnumNodeDocumentation((BLangEnum) node, replaceFrom);
}
break;
case TRANSFORMER:
if (((BLangTransformer) node).docAttachments.isEmpty()) {
replaceFrom = CommonUtil.toZeroBasedPosition(((BLangTransformer) node).getPosition()).getStartLine();
docAttachmentInfo = CommandUtil.getTransformerNodeDocumentation((BLangTransformer) node, replaceFrom);
}
break;
case RESOURCE:
if (((BLangResource) node).docAttachments.isEmpty()) {
BLangResource bLangResource = (BLangResource) node;
replaceFrom = getReplaceFromForServiceOrResource(bLangResource, bLangResource.getAnnotationAttachments());
docAttachmentInfo = CommandUtil.getResourceNodeDocumentation(bLangResource, replaceFrom);
}
break;
case SERVICE:
if (((BLangService) node).docAttachments.isEmpty()) {
BLangService bLangService = (BLangService) node;
replaceFrom = getReplaceFromForServiceOrResource(bLangService, bLangService.getAnnotationAttachments());
docAttachmentInfo = CommandUtil.getServiceNodeDocumentation(bLangService, replaceFrom);
}
break;
default:
break;
}
return docAttachmentInfo;
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class SymbolEnter method validateFunctionsAttachedToStructs.
private void validateFunctionsAttachedToStructs(BLangFunction funcNode, BInvokableSymbol funcSymbol, SymbolEnv invokableEnv) {
BInvokableType funcType = (BInvokableType) funcSymbol.type;
BStructSymbol structSymbol = (BStructSymbol) funcNode.receiver.type.tsymbol;
BSymbol symbol = symResolver.lookupMemberSymbol(funcNode.receiver.pos, structSymbol.scope, invokableEnv, names.fromIdNode(funcNode.name), SymTag.VARIABLE);
if (symbol != symTable.notFoundSymbol) {
dlog.error(funcNode.pos, DiagnosticCode.STRUCT_FIELD_AND_FUNC_WITH_SAME_NAME, funcNode.name.value, funcNode.receiver.type.toString());
return;
}
BStructType structType = (BStructType) funcNode.receiver.type;
BAttachedFunction attachedFunc = new BAttachedFunction(names.fromIdNode(funcNode.name), funcSymbol, funcType);
structSymbol.attachedFuncs.add(attachedFunc);
if (funcNode.name.value.equals(structType.tsymbol.name.value + Names.INIT_FUNCTION_SUFFIX.value)) {
structSymbol.defaultsValuesInitFunc = attachedFunc;
return;
}
// Check whether this attached function is a struct initializer.
if (!structType.tsymbol.name.value.equals(funcNode.name.value)) {
// Not a struct initializer.
return;
}
if (!funcNode.requiredParams.isEmpty() || !funcNode.retParams.isEmpty()) {
dlog.error(funcNode.pos, DiagnosticCode.INVALID_STRUCT_INITIALIZER_FUNCTION, funcNode.name.value, funcNode.receiver.type.toString());
}
structSymbol.initializerFunc = attachedFunc;
}
use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.
the class SymbolEnter method defineObjectInitFunction.
private void defineObjectInitFunction(BLangObject object, SymbolEnv conEnv) {
BLangFunction initFunction = object.initFunction;
if (initFunction == null) {
initFunction = createInitFunction(object.pos, "", Names.OBJECT_INIT_SUFFIX);
}
initFunction.attachedFunction = true;
// Set cached receiver to the init function
initFunction.receiver = object.receiver;
initFunction.flagSet.add(Flag.ATTACHED);
// Add object level variables to the init function
BLangFunction finalInitFunction = initFunction;
object.fields.stream().filter(f -> f.expr != null).forEachOrdered(v -> finalInitFunction.initFunctionStmts.put(v.symbol, (BLangStatement) createAssignmentStmt(v)));
object.initFunction = initFunction;
defineNode(object.initFunction, conEnv);
}
Aggregations