Skip to content

Commit

Permalink
Merge pull request #430 from nipunayf/add-variable-no-init
Browse files Browse the repository at this point in the history
Support for variable definitions without initialization has been added
  • Loading branch information
KavinduZoysa authored Oct 10, 2024
2 parents c1b1d90 + 84ca1e1 commit dcfdd7b
Show file tree
Hide file tree
Showing 32 changed files with 1,761 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,24 @@ public void visit(ByteArrayLiteralNode byteArrayLiteralNode) {
public void visit(VariableDeclarationNode variableDeclarationNode) {
Optional<ExpressionNode> initializer = variableDeclarationNode.initializer();
if (initializer.isEmpty()) {
return;
}
ExpressionNode initializerNode = initializer.get();
this.typedBindingPatternNode = variableDeclarationNode.typedBindingPattern();

initializerNode.accept(this);

// Generate the default expression node if a node is not built
if (isNodeUnidentified()) {
startNode(NodeKind.VARIABLE)
.metadata()
.description(Assign.DESCRIPTION)
.stepOut()
.properties().expression(initializerNode);
.properties().expression(null);
} else {
ExpressionNode initializerNode = initializer.get();
this.typedBindingPatternNode = variableDeclarationNode.typedBindingPattern();
initializerNode.accept(this);

// Generate the default expression node if a node is not built
if (isNodeUnidentified()) {
startNode(NodeKind.VARIABLE)
.metadata()
.description(Assign.DESCRIPTION)
.stepOut()
.properties().expression(initializerNode);
}
}

// TODO: Find a better way on how we can achieve this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.ballerina.flowmodelgenerator.core.model.node;

import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.flowmodelgenerator.core.model.NodeBuilder;
import io.ballerina.flowmodelgenerator.core.model.NodeKind;
import io.ballerina.flowmodelgenerator.core.model.Property;
Expand Down Expand Up @@ -48,11 +49,19 @@ public void setConcreteConstData() {

@Override
public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {
sourceBuilder.newVariable();
Optional<Property> type = sourceBuilder.flowNode.getProperty(Property.DATA_TYPE_KEY);
Optional<Property> variable = sourceBuilder.flowNode.getProperty(Property.VARIABLE_KEY);
if (type.isPresent() && variable.isPresent()) {
sourceBuilder.token().expressionWithType(type.get(), variable.get());
}

Optional<Property> exprProperty = sourceBuilder.flowNode.getProperty(Property.EXPRESSION_KEY);
exprProperty.ifPresent(value -> sourceBuilder.token().expression(value).endOfStatement());

if (exprProperty.isPresent() && !exprProperty.get().toSourceCode().isEmpty()) {
sourceBuilder.token()
.keyword(SyntaxKind.EQUAL_TOKEN)
.expression(exprProperty.get());
}
sourceBuilder.token().endOfStatement();
return sourceBuilder.textEdit(false).build();
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
},
"source": "error_handler.bal",
"description": "Tests a error handler",
"forceAssign": false,
"diagram": {
"fileName": "error_handler.bal",
"nodes": [
Expand All @@ -36,6 +35,62 @@
"returning": false,
"flags": 0
},
{
"id": "37964",
"metadata": {
"label": "Variable",
"description": "Assign a value to a variable"
},
"codedata": {
"node": "VARIABLE",
"lineRange": {
"fileName": "error_handler.bal",
"startLine": {
"line": 6,
"offset": 8
},
"endLine": {
"line": 6,
"offset": 20
}
},
"sourceCode": "string? msg;"
},
"returning": false,
"properties": {
"expression": {
"metadata": {
"label": "Expression",
"description": "Expression"
},
"valueType": "EXPRESSION",
"value": "",
"optional": false,
"editable": true
},
"variable": {
"metadata": {
"label": "Variable",
"description": "Name of the variable"
},
"valueType": "IDENTIFIER",
"value": "msg",
"optional": false,
"editable": true
},
"type": {
"metadata": {
"label": "Type",
"description": "Type of the variable"
},
"valueType": "TYPE",
"value": "string|()",
"optional": false,
"editable": true
}
},
"flags": 0
},
{
"id": "39100",
"metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
},
"source": "error_handler.bal",
"description": "Tests a error handler",
"forceAssign": false,
"diagram": {
"fileName": "error_handler.bal",
"nodes": [
Expand All @@ -36,6 +35,62 @@
"returning": false,
"flags": 0
},
{
"id": "47884",
"metadata": {
"label": "Variable",
"description": "Assign a value to a variable"
},
"codedata": {
"node": "VARIABLE",
"lineRange": {
"fileName": "error_handler.bal",
"startLine": {
"line": 16,
"offset": 8
},
"endLine": {
"line": 16,
"offset": 20
}
},
"sourceCode": "string? msg;"
},
"returning": false,
"properties": {
"expression": {
"metadata": {
"label": "Expression",
"description": "Expression"
},
"valueType": "EXPRESSION",
"value": "",
"optional": false,
"editable": true
},
"variable": {
"metadata": {
"label": "Variable",
"description": "Name of the variable"
},
"valueType": "IDENTIFIER",
"value": "msg",
"optional": false,
"editable": true
},
"type": {
"metadata": {
"label": "Type",
"description": "Type of the variable"
},
"valueType": "TYPE",
"value": "string|()",
"optional": false,
"editable": true
}
},
"flags": 0
},
{
"id": "49206",
"metadata": {
Expand Down
Loading

0 comments on commit dcfdd7b

Please sign in to comment.