use of org.wso2.ballerinalang.compiler.tree.BLangTransformer in project ballerina by ballerina-lang.
the class Desugar method addTransformerReturn.
private void addTransformerReturn(BLangTransformer transformerNode) {
// This will only check whether last statement is a return and just add a return statement.
// This won't analyze if else blocks etc to see whether return statements are present
BLangBlockStmt blockStmt = transformerNode.body;
if (transformerNode.workers.size() == 0 && (blockStmt.stmts.size() < 1 || blockStmt.stmts.get(blockStmt.stmts.size() - 1).getKind() != NodeKind.RETURN)) {
BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
DiagnosticPos invPos = transformerNode.pos;
DiagnosticPos pos = new DiagnosticPos(invPos.src, invPos.eLine, invPos.eLine, invPos.sCol, invPos.sCol);
returnStmt.pos = pos;
if (!transformerNode.retParams.isEmpty()) {
returnStmt.namedReturnVariables = transformerNode.retParams;
}
blockStmt.addStatement(returnStmt);
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangTransformer in project ballerina by ballerina-lang.
the class Desugar method visit.
public void visit(BLangTransformer transformerNode) {
addTransformerReturn(transformerNode);
SymbolEnv tranEnv = SymbolEnv.createTransformerEnv(transformerNode, transformerNode.symbol.scope, env);
transformerNode.body = rewrite(transformerNode.body, tranEnv);
addArgInitExpr(transformerNode, transformerNode.retParams.get(0));
BInvokableSymbol transformerSymbol = transformerNode.symbol;
List<BVarSymbol> params = transformerSymbol.params;
params.add(0, transformerNode.source.symbol);
BInvokableType transformerType = (BInvokableType) transformerSymbol.type;
transformerType.paramTypes.add(0, transformerNode.source.type);
result = transformerNode;
}
use of org.wso2.ballerinalang.compiler.tree.BLangTransformer in project ballerina by ballerina-lang.
the class BLangPackageBuilder method endTransformerDef.
public void endTransformerDef(DiagnosticPos pos, Set<Whitespace> ws, boolean publicFunc, String name, boolean paramsAvailable) {
BLangTransformer transformer = (BLangTransformer) this.invokableNodeStack.pop();
transformer.pos = pos;
transformer.addWS(ws);
transformer.setName(this.createIdentifier(name));
if (paramsAvailable) {
this.varListStack.pop().forEach(variableNode -> {
((BLangVariable) variableNode).docTag = DocTag.PARAM;
transformer.addParameter(variableNode);
});
}
// get the source and the target params
List<VariableNode> mappingParams = this.varListStack.pop();
// set the first mapping-param as the source for transformer
VariableNode source = mappingParams.remove(0);
((BLangVariable) source).docTag = DocTag.RECEIVER;
transformer.setSource(source);
mappingParams.forEach(variableNode -> {
((BLangVariable) variableNode).docTag = DocTag.RECEIVER;
transformer.addReturnParameter(variableNode);
});
if (publicFunc) {
transformer.flagSet.add(Flag.PUBLIC);
}
this.compUnit.addTopLevelNode(transformer);
}
use of org.wso2.ballerinalang.compiler.tree.BLangTransformer in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
public void visit(BLangTransformer transformerNode) {
if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(transformerNode.getPosition(), transformerNode, this, this.documentServiceContext)) {
SymbolEnv transformerEnv = SymbolEnv.createTransformerEnv(transformerNode, transformerNode.symbol.scope, symbolEnv);
this.blockOwnerStack.push(transformerNode);
// Cursor position is calculated against the Block statement scope resolver
cursorPositionResolver = BlockStatementScopeResolver.class;
this.acceptNode(transformerNode.body, transformerEnv);
this.blockOwnerStack.pop();
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangTransformer 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;
}
Aggregations