Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private Path resolvePath(Path inputPath, NodeKind node, LineRange lineRange, Boo
String defaultFile = switch (node) {
case NEW_CONNECTION, MODEL_PROVIDER, EMBEDDING_PROVIDER, VECTOR_STORE, KNOWLEDGE_BASE,
DATA_LOADER, CHUNKER, CLASS_INIT -> CONNECTIONS_BAL;
case DATA_MAPPER_DEFINITION, DATA_MAPPER_CREATION -> DATA_MAPPINGS_BAL;
case FUNCTION_DEFINITION, NP_FUNCTION, NP_FUNCTION_DEFINITION, FUNCTION_CREATION -> FUNCTIONS_BAL;
case DATA_MAPPER_DEFINITION -> DATA_MAPPINGS_BAL;
case FUNCTION_DEFINITION, NP_FUNCTION, NP_FUNCTION_DEFINITION -> FUNCTIONS_BAL;
case AUTOMATION -> AUTOMATION_BAL;
case AGENT, MEMORY, MEMORY_STORE, MCP_TOOL_KIT -> AGENTS_BAL;
default -> null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@

import com.google.gson.Gson;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.flowmodelgenerator.core.model.Codedata;
import io.ballerina.flowmodelgenerator.core.model.FlowNode;
import io.ballerina.flowmodelgenerator.core.model.FormBuilder;
import io.ballerina.flowmodelgenerator.core.model.NodeBuilder;
import io.ballerina.flowmodelgenerator.core.model.NodeKind;
import io.ballerina.flowmodelgenerator.core.model.Property;
import io.ballerina.flowmodelgenerator.core.model.SourceBuilder;
import io.ballerina.flowmodelgenerator.core.utils.FileSystemUtils;
import io.ballerina.modelgenerator.commons.CommonUtils;
import io.ballerina.projects.Document;
import org.ballerinalang.model.types.TypeKind;
import org.eclipse.lsp4j.TextEdit;

Expand Down Expand Up @@ -63,6 +62,8 @@ public class DataMapperCreationBuilder extends NodeBuilder {
public static final String RETURN_TYPE = TypeKind.ANYDATA.typeName();
public static final String PARAMETER_TYPE = TypeKind.ANYDATA.typeName();

private static final String DATA_MAPPER_DEFINITION_FILE = "data_mappings.bal";

protected String getNameLabel() {
return DATA_MAPPER_NAME_LABEL;
}
Expand All @@ -79,6 +80,10 @@ protected String getParametersDoc() {
return PARAMETERS_DOC;
}

protected String getNodeDefinitionFile() {
return DATA_MAPPER_DEFINITION_FILE;
}

@Override
public void setConcreteConstData() {
metadata().label(LABEL).description(DESCRIPTION);
Expand Down Expand Up @@ -139,26 +144,30 @@ public void setOptionalProperties(NodeBuilder nodeBuilder) {

@Override
public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {
Map<Path, List<TextEdit>> definition = createDefinition(sourceBuilder);
Map<Path, List<TextEdit>> invocation = createInvocation(sourceBuilder);
Map<Path, List<TextEdit>> definition = createDefinition(sourceBuilder);

Map<Path, List<TextEdit>> combined = new HashMap<>(definition);
combined.putAll(invocation);
return combined;
}

private Map<Path, List<TextEdit>> createDefinition(SourceBuilder sourceBuilder) {
sourceBuilder.token().keyword(SyntaxKind.FUNCTION_KEYWORD);
Path rootPath = sourceBuilder.workspaceManager.projectRoot(sourceBuilder.filePath);
Path nodeDefinitionPath = rootPath.resolve(getNodeDefinitionFile());
SourceBuilder definitionBuilder =
new SourceBuilder(sourceBuilder.flowNode, sourceBuilder.workspaceManager, nodeDefinitionPath);
definitionBuilder.token().keyword(SyntaxKind.FUNCTION_KEYWORD);

Optional<Property> property = sourceBuilder.getProperty(Property.FUNCTION_NAME_KEY);
Optional<Property> property = definitionBuilder.getProperty(Property.FUNCTION_NAME_KEY);
if (property.isEmpty()) {
throw new IllegalStateException("Data mapper name is not present");
}
sourceBuilder.token()
definitionBuilder.token()
.name(property.get().value().toString())
.keyword(SyntaxKind.OPEN_PAREN_TOKEN);

Optional<Property> parameters = sourceBuilder.getProperty(Property.PARAMETERS_KEY);
Optional<Property> parameters = definitionBuilder.getProperty(Property.PARAMETERS_KEY);
if (parameters.isPresent() && parameters.get().value() instanceof Map<?, ?> paramMap) {
List<String> paramList = new ArrayList<>();
for (Map.Entry<?, ?> entry : paramMap.entrySet()) {
Expand All @@ -171,29 +180,31 @@ private Map<Path, List<TextEdit>> createDefinition(SourceBuilder sourceBuilder)
FormBuilder.NODE_PROPERTIES_TYPE);
paramList.add(paramProperties.get(Property.TYPE_KEY).value().toString() + " " + entry.getKey());
}
sourceBuilder.token().name(String.join(", ", paramList));
definitionBuilder.token().name(String.join(", ", paramList));
}
sourceBuilder.token().keyword(SyntaxKind.CLOSE_PAREN_TOKEN);
definitionBuilder.token().keyword(SyntaxKind.CLOSE_PAREN_TOKEN);

// Write the return type
Optional<Property> returnType = sourceBuilder.getProperty(Property.TYPE_KEY);
Optional<Property> returnType = definitionBuilder.getProperty(Property.TYPE_KEY);
if (returnType.isEmpty() || returnType.get().value().toString().isEmpty()) {
throw new IllegalStateException("The return type should be defined");
}
String returnTypeString = returnType.get().value().toString();
sourceBuilder.token()
definitionBuilder.token()
.keyword(SyntaxKind.RETURNS_KEYWORD)
.name(returnTypeString);

Optional<String> returnBody =
sourceBuilder.getExpressionBodyText(returnTypeString, returnType.get().imports());
definitionBuilder.getExpressionBodyText(returnTypeString, returnType.get().imports());
if (returnBody.isEmpty()) {
throw new IllegalStateException("Failed to produce the function body");
}

endSourceGeneration(sourceBuilder, returnBody.get());
return sourceBuilder
.textEdit(SourceBuilder.SourceKind.DECLARATION)
endSourceGeneration(definitionBuilder, returnBody.get());
Document document = FileSystemUtils.getDocument(definitionBuilder.workspaceManager, nodeDefinitionPath);
return definitionBuilder
.textEdit(SourceBuilder.SourceKind.DECLARATION, nodeDefinitionPath,
CommonUtils.toRange(document.syntaxTree().rootNode().lineRange().endLine()))
.build();
}

Expand All @@ -206,22 +217,16 @@ protected void endSourceGeneration(SourceBuilder sourceBuilder, String returnBod
}

private Map<Path, List<TextEdit>> createInvocation(SourceBuilder sourceBuilder) {
FlowNode flowNode = sourceBuilder.flowNode;
Codedata codedata = flowNode.codedata();
Path path = FileSystemUtils.resolveFilePathFromCodedata(codedata,
sourceBuilder.workspaceManager.projectRoot(sourceBuilder.filePath));
SourceBuilder callBuilder = new SourceBuilder(flowNode, sourceBuilder.workspaceManager, path);

callBuilder.newVariableWithInferredType();
sourceBuilder.newVariableWithInferredType();
Optional<Property> property = sourceBuilder.getProperty(Property.FUNCTION_NAME_KEY);
if (property.isEmpty()) {
throw new IllegalStateException("Name is not present");
}
callBuilder.token()
sourceBuilder.token()
.name(property.get().value().toString())
.keyword(SyntaxKind.OPEN_PAREN_TOKEN);

Optional<Property> parameters = callBuilder.getProperty(Property.PARAMETERS_KEY);
Optional<Property> parameters = sourceBuilder.getProperty(Property.PARAMETERS_KEY);
if (parameters.isPresent() && parameters.get().value() instanceof Map<?, ?> paramMap) {
List<String> argsList = new ArrayList<>();
for (Object obj : paramMap.values()) {
Expand All @@ -234,13 +239,13 @@ private Map<Path, List<TextEdit>> createInvocation(SourceBuilder sourceBuilder)
String paramName = paramProperties.get(Property.VARIABLE_KEY).value().toString();
argsList.add(paramName);
}
callBuilder.token().name(String.join(", ", argsList));
sourceBuilder.token().name(String.join(", ", argsList));
}
callBuilder.token().keyword(SyntaxKind.CLOSE_PAREN_TOKEN);
callBuilder.token().endOfStatement();
sourceBuilder.token().keyword(SyntaxKind.CLOSE_PAREN_TOKEN);
sourceBuilder.token().endOfStatement();

return callBuilder
.textEdit(SourceBuilder.SourceKind.STATEMENT, path, CommonUtils.toRange(codedata.lineRange()))
return sourceBuilder
.textEdit(SourceBuilder.SourceKind.STATEMENT)
.acceptImportWithVariableType()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class FunctionCreationBuilder extends DataMapperCreationBuilder {

public static final String OUTPUT_DOC = "Output type of the function";

private static final String FUNCTION_DEFINITION_FILE = "functions.bal";

@Override
protected String getNameLabel() {
return FUNCTION_NAME_LABEL;
Expand All @@ -64,6 +66,11 @@ public void setConcreteConstData() {
codedata().node(NodeKind.FUNCTION_CREATION);
}

@Override
protected String getNodeDefinitionFile() {
return FUNCTION_DEFINITION_FILE;
}

@Override
protected void endSourceGeneration(SourceBuilder sourceBuilder, String returnBody) {
sourceBuilder
Expand Down
Loading