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 @@ -207,6 +207,45 @@ public LineRange generateStatement() {
return statementLineRange;
}

/**
* Generates a Ballerina function definition with the provided signature, containing the statement
* generated by {@link #generateStatement()} as the function body.
*
* <p>
* TODO: This is a temporary workaround to provide completions for the data mapper, and should be removed once
* the API extension is complete.
*
* @param functionSignature the function signature (e.g., "(Person person, Admission admission) returns Employee")
* @return the line range of the generated function definition
*/
public LineRange generateFunctionDefinition(String functionSignature) {
// Get the text position of the start line
TextDocument textDocument = documentContext.document().textDocument();
LinePosition cursorStartLine = startLine();
int textPosition = textDocument.textPositionFrom(cursorStartLine);

// Build function header with opening brace and indentation
String functionHeader = String.format("function %s {%n ", functionSignature);

// Apply the function header
List<TextEdit> textEdits = new ArrayList<>();
textEdits.add(TextEdit.from(TextRange.from(textPosition, 0), functionHeader));
applyTextEdits(textEdits);

// Calculate the number of lines added by the function header
int functionHeaderLines = functionHeader.split(System.lineSeparator()).length - 1;

// Update the start line to account for the function header
// Store original start line for later calculation
this.startLine = LinePosition.from(
cursorStartLine.line() + functionHeaderLines,
4 // Indentation of 4 spaces
);

// Generate the statement inside the function body
return generateStatement();
}

public LineRange getExpressionLineRange() {
LinePosition startLine = info().startLine();
LinePosition endLine =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@

package io.ballerina.flowmodelgenerator.core.expressioneditor.services;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode;
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
import io.ballerina.flowmodelgenerator.core.expressioneditor.ExpressionEditorContext;
import io.ballerina.flowmodelgenerator.core.model.Property;
import io.ballerina.tools.text.LineRange;
import org.ballerinalang.langserver.common.utils.CommonUtil;
import org.ballerinalang.langserver.common.utils.PositionUtil;
import org.eclipse.lsp4j.CompletionContext;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
Expand All @@ -45,6 +52,7 @@ public class CompletionRequest extends DebouncedExpressionEditorRequest<Either<L
private final CompletionContext completionContext;
private final TextDocumentService textDocumentService;
private static final String RESERVED_VARIABLE_NAME = "__reserved__";
private static final Gson GSON = new Gson();

public CompletionRequest(ExpressionEditorContext context, CompletionContext completionContext,
TextDocumentService textDocumentService) {
Expand All @@ -55,7 +63,28 @@ public CompletionRequest(ExpressionEditorContext context, CompletionContext comp

@Override
public Either<List<CompletionItem>, CompletionList> getResponse(ExpressionEditorContext context) {
context.generateStatement();
String valueType = context.getProperty().valueType();

// TODO: Added a special case for expressions used within the data mapper, as inputs are not provided through
// the traditional method.
boolean generatedFunctionDefinition = false;
if (Property.ValueType.DATA_MAPPING_EXPRESSION.name().equals(valueType)) {
JsonElement lineRangeJson = context.info().codedata().get("lineRange");
LineRange lineRange = GSON.fromJson(lineRangeJson, LineRange.class);

if (lineRange != null) {
NonTerminalNode node = CommonUtil.findNode(PositionUtil.toRange(lineRange),
context.documentContext().document().syntaxTree());
if (node instanceof FunctionDefinitionNode functionDefinitionNode) {
context.generateFunctionDefinition(functionDefinitionNode.functionSignature().toString());
generatedFunctionDefinition = true;
}
}
}
if (!generatedFunctionDefinition) {
context.generateStatement();
}

Position position = context.getCursorPosition();
TextDocumentIdentifier identifier = new TextDocumentIdentifier(context.fileUri());
CompletionParams params = new CompletionParams(identifier, position, completionContext);
Expand All @@ -67,7 +96,6 @@ public Either<List<CompletionItem>, CompletionList> getResponse(ExpressionEditor

// Filter the completions if it is a lvexpr
// TODO: Extend the implementation to a different class
String valueType = context.getProperty().valueType();
if (Property.ValueType.LV_EXPRESSION.name().equals(valueType)) {
List<CompletionItem> completionsList;
if (completions.getLeft() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ public enum ValueType {
FIXED_PROPERTY,
RAW_TEMPLATE,
REPEATABLE_PROPERTY,
ACTION_PATH
ACTION_PATH,
DATA_MAPPING_EXPRESSION
}

public static ValueType valueTypeFrom(String s) {
Expand Down
Loading
Loading