Skip to content

Commit

Permalink
Merge pull request #41170 from KavinduZoysa/fix-issue-40614
Browse files Browse the repository at this point in the history
Add create function CA for var type decleration and select-expr
  • Loading branch information
KavinduZoysa authored Aug 11, 2023
2 parents e6f4594 + 494ae5c commit f9405bc
Show file tree
Hide file tree
Showing 16 changed files with 461 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.TypeBuilder;
import io.ballerina.compiler.api.Types;
import io.ballerina.compiler.api.symbols.ArrayTypeSymbol;
import io.ballerina.compiler.api.symbols.ClassSymbol;
import io.ballerina.compiler.api.symbols.ErrorTypeSymbol;
import io.ballerina.compiler.api.symbols.FunctionTypeSymbol;
Expand All @@ -28,6 +29,7 @@
import io.ballerina.compiler.api.symbols.ParameterSymbol;
import io.ballerina.compiler.api.symbols.RecordFieldSymbol;
import io.ballerina.compiler.api.symbols.RecordTypeSymbol;
import io.ballerina.compiler.api.symbols.StreamTypeSymbol;
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.api.symbols.SymbolKind;
import io.ballerina.compiler.api.symbols.TypeDescKind;
Expand Down Expand Up @@ -63,11 +65,13 @@
import io.ballerina.compiler.syntax.tree.RecordFieldWithDefaultValueNode;
import io.ballerina.compiler.syntax.tree.RemoteMethodCallActionNode;
import io.ballerina.compiler.syntax.tree.ReturnStatementNode;
import io.ballerina.compiler.syntax.tree.SelectClauseNode;
import io.ballerina.compiler.syntax.tree.SeparatedNodeList;
import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode;
import io.ballerina.compiler.syntax.tree.SpecificFieldNode;
import io.ballerina.compiler.syntax.tree.StartActionNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.TypedBindingPatternNode;
import io.ballerina.compiler.syntax.tree.UnaryExpressionNode;
import io.ballerina.compiler.syntax.tree.VariableDeclarationNode;
import io.ballerina.compiler.syntax.tree.WhileStatementNode;
Expand Down Expand Up @@ -114,9 +118,7 @@ public void visit(RecordFieldWithDefaultValueNode recordFieldWithDefaultValueNod

@Override
public void visit(ModuleVariableDeclarationNode moduleVariableDeclarationNode) {
Symbol symbol = semanticModel.symbol(moduleVariableDeclarationNode).orElse(null);
TypeSymbol typeDescriptor = SymbolUtil.getTypeDescriptor(symbol).orElse(null);
checkAndSetTypeResult(typeDescriptor);
visitVariableDeclaration(moduleVariableDeclarationNode, moduleVariableDeclarationNode.typedBindingPattern());
}

@Override
Expand All @@ -134,9 +136,7 @@ public void visit(AssignmentStatementNode assignmentStatementNode) {

@Override
public void visit(VariableDeclarationNode variableDeclarationNode) {
Symbol symbol = semanticModel.symbol(variableDeclarationNode).orElse(null);
TypeSymbol typeDescriptor = SymbolUtil.getTypeDescriptor(symbol).orElse(null);
checkAndSetTypeResult(typeDescriptor);
visitVariableDeclaration(variableDeclarationNode, variableDeclarationNode.typedBindingPattern());
}

@Override
Expand Down Expand Up @@ -486,6 +486,19 @@ public void visit(GroupingKeyVarDeclarationNode groupingKeyVarDeclarationNode) {
TypeSymbol typeDescriptor = SymbolUtil.getTypeDescriptor(symbol).orElse(null);
checkAndSetTypeResult(typeDescriptor);
}

@Override
public void visit(SelectClauseNode selectClauseNode) {
selectClauseNode.parent().parent().accept(this);
if (resultFound) {
TypeDescKind kind = this.returnTypeSymbol.typeKind();
if (kind == TypeDescKind.ARRAY) {
checkAndSetTypeResult(((ArrayTypeSymbol) returnTypeSymbol).memberTypeDescriptor());
} else if (kind == TypeDescKind.STREAM) {
checkAndSetTypeResult(((StreamTypeSymbol) returnTypeSymbol).typeParameter());
}
}
}

@Override
public void visit(PanicStatementNode panicStatementNode) {
Expand Down Expand Up @@ -522,4 +535,19 @@ private void resetResult() {
public Optional<TypeSymbol> getReturnTypeSymbol() {
return Optional.ofNullable(returnTypeSymbol);
}

private void visitVariableDeclaration(Node variableDeclarationNode, TypedBindingPatternNode typedBindingNode) {
Symbol symbol = semanticModel.symbol(variableDeclarationNode).orElse(null);
Optional<TypeSymbol> typeDescriptor = SymbolUtil.getTypeDescriptor(symbol);
TypeSymbol ts = null;
if (typeDescriptor.isPresent()) {
ts = typeDescriptor.get();
if (ts.typeKind() == TypeDescKind.COMPILATION_ERROR &&
typedBindingNode.typeDescriptor().kind() == SyntaxKind.VAR_TYPE_DESC) {
Types types = semanticModel.types();
ts = types.builder().UNION_TYPE.withMemberTypes(types.ANY, types.ERROR).build();
}
}
checkAndSetTypeResult(ts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ public Object[][] dataProvider() {
{"create_function_in_local_var1.json"},
{"create_function_in_local_var2.json"},
{"create_function_in_start_action1.json"},

{"undefinedFunctionCodeAction25.json"},
{"undefinedFunctionCodeAction26.json"},
{"undefinedFunctionCodeAction27.json"},
{"undefinedFunctionCodeAction28.json"},
{"undefinedFunctionCodeAction29.json"},
{"undefinedFunctionCodeAction30.json"},
{"undefinedFunctionCodeAction31.json"},
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"position": {
"line": 1,
"character": 52
},
"source": "createUndefinedFunction14.bal",
"expected": [
{
"title": "Create function 'toUpperCase(...)'",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 2,
"character": 1
},
"end": {
"line": 2,
"character": 1
}
},
"newText": "\n\nfunction toUpperCase(string letter) returns any|error {\n return ;\n}"
}
],
"resolvable": true,
"data": {
"extName": "org.ballerinalang.langserver.codeaction.BallerinaCodeActionExtension",
"codeActionName": "Create Function",
"fileUri": "createUndefinedFunction14.bal",
"range": {
"start": {
"line": 1,
"character": 46
},
"end": {
"line": 1,
"character": 65
}
},
"actionData": {
"key": "node.range",
"value": {
"start": {
"line": 1.0,
"character": 46.0
},
"end": {
"line": 1.0,
"character": 65.0
}
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"position": {
"line": 1,
"character": 54
},
"source": "createUndefinedFunction15.bal",
"expected": [
{
"title": "Create function 'toUpperCase(...)'",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 2,
"character": 1
},
"end": {
"line": 2,
"character": 1
}
},
"newText": "\n\nfunction toUpperCase(string letter) returns int {\n return 0;\n}"
}
],
"resolvable": true,
"data": {
"extName": "org.ballerinalang.langserver.codeaction.BallerinaCodeActionExtension",
"codeActionName": "Create Function",
"fileUri": "createUndefinedFunction15.bal",
"range": {
"start": {
"line": 1,
"character": 48
},
"end": {
"line": 1,
"character": 67
}
},
"actionData": {
"key": "node.range",
"value": {
"start": {
"line": 1.0,
"character": 48.0
},
"end": {
"line": 1.0,
"character": 67.0
}
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"position": {
"line": 1,
"character": 18
},
"source": "createUndefinedFunction16.bal",
"expected": [
{
"title": "Create function 'toUpperCase(...)'",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 2,
"character": 1
},
"end": {
"line": 2,
"character": 1
}
},
"newText": "\n\nfunction toUpperCase(any a) returns any|error {\n return ;\n}"
}
],
"resolvable": true,
"data": {
"extName": "org.ballerinalang.langserver.codeaction.BallerinaCodeActionExtension",
"codeActionName": "Create Function",
"fileUri": "createUndefinedFunction16.bal",
"range": {
"start": {
"line": 1,
"character": 11
},
"end": {
"line": 1,
"character": 30
}
},
"actionData": {
"key": "node.range",
"value": {
"start": {
"line": 1.0,
"character": 11.0
},
"end": {
"line": 1.0,
"character": 30.0
}
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"position": {
"line": 0,
"character": 16
},
"source": "createUndefinedFunction17.bal",
"expected": [
{
"title": "Create function 'toUpperCase(...)'",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 0,
"character": 28
},
"end": {
"line": 0,
"character": 28
}
},
"newText": "\n\nfunction toUpperCase(any a) returns any|error {\n return ;\n}"
}
],
"resolvable": true,
"data": {
"extName": "org.ballerinalang.langserver.codeaction.BallerinaCodeActionExtension",
"codeActionName": "Create Function",
"fileUri": "createUndefinedFunction17.bal",
"range": {
"start": {
"line": 0,
"character": 8
},
"end": {
"line": 0,
"character": 27
}
},
"actionData": {
"key": "node.range",
"value": {
"start": {
"line": 0.0,
"character": 8.0
},
"end": {
"line": 0.0,
"character": 27.0
}
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"position": {
"line": 0,
"character": 50
},
"source": "createUndefinedFunction18.bal",
"expected": [
{
"title": "Create function 'toUpperCase(...)'",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 0,
"character": 64
},
"end": {
"line": 0,
"character": 64
}
},
"newText": "\n\nfunction toUpperCase(string letter) returns int {\n return 0;\n}"
}
],
"resolvable": true,
"data": {
"extName": "org.ballerinalang.langserver.codeaction.BallerinaCodeActionExtension",
"codeActionName": "Create Function",
"fileUri": "createUndefinedFunction18.bal",
"range": {
"start": {
"line": 0,
"character": 44
},
"end": {
"line": 0,
"character": 63
}
},
"actionData": {
"key": "node.range",
"value": {
"start": {
"line": 0.0,
"character": 44.0
},
"end": {
"line": 0.0,
"character": 63.0
}
}
}
}
}
]
}
Loading

0 comments on commit f9405bc

Please sign in to comment.