Skip to content

Commit

Permalink
Merge pull request #42574 from nipunayf/fix-42482
Browse files Browse the repository at this point in the history
[Master] Add the relevant new import text edits via the `Create variable with type` code action
  • Loading branch information
nipunayf committed Apr 26, 2024
2 parents 95baf7c + 94b9268 commit 8b1ac03
Show file tree
Hide file tree
Showing 21 changed files with 788 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected CreateVariableCodeAction.CreateVariableOut getCreateVariableTextEdits(

List<TextEdit> edits = new ArrayList<>();
List<Integer> renamePositions = new ArrayList<>();
List<String> types = getPossibleTypes(typeDescriptor, context);
List<String> types = getPossibleTypes(typeDescriptor, context, importsAcceptor);
Position insertPos = range.getStart();
List<Position> varRenamePositions = new ArrayList<>();
for (String type : types) {
Expand All @@ -174,7 +174,8 @@ protected CreateVariableCodeAction.CreateVariableOut getCreateVariableTextEdits(
* @param context CodeActionContext
* @return {@link List<String>}
*/
private List<String> getPossibleTypes(TypeSymbol typeSymbol, CodeActionContext context) {
private List<String> getPossibleTypes(TypeSymbol typeSymbol, CodeActionContext context,
ImportsAcceptor importsAcceptor) {
typeSymbol = getRawType(typeSymbol, context);
Set<String> possibleTypes = new HashSet<>();
List<TypeSymbol> errorTypes = new ArrayList<>();
Expand All @@ -190,25 +191,25 @@ private List<String> getPossibleTypes(TypeSymbol typeSymbol, CodeActionContext c
.memberTypeDescriptors()
.stream()
.map(memberTSymbol -> getRawType(memberTSymbol, context))
.map(symbol -> getTypeName(symbol, context))
.map(symbol -> getTypeName(symbol, context, importsAcceptor))
.collect(Collectors.toList()));
} else if (memberTypeSymbol.typeKind() == TypeDescKind.ERROR ||
CommonUtil.getRawType(memberTypeSymbol).typeKind() == TypeDescKind.ERROR) {
errorTypes.add(memberTypeSymbol);
} else {
possibleTypes.add(getTypeName(memberTypeSymbol, context));
possibleTypes.add(getTypeName(memberTypeSymbol, context, importsAcceptor));
}
});
} else {
String type = getTypeName(typeSymbol, context);
String type = getTypeName(typeSymbol, context, importsAcceptor);
if (!"any".equals(type)) {
return Collections.singletonList(type);
}
}

if (!errorTypes.isEmpty()) {
String errorTypeStr = errorTypes.stream()
.map(type -> getTypeName(type, context))
.map(type -> getTypeName(type, context, importsAcceptor))
.collect(Collectors.joining("|"));
return possibleTypes.stream()
.filter(type -> !"any".equals(type))
Expand Down Expand Up @@ -249,7 +250,7 @@ private boolean isInActionStatement(NonTerminalNode actionNode) {
return actionNode.parent().kind() == SyntaxKind.ACTION_STATEMENT;
}

private String getTypeName(TypeSymbol symbol, CodeActionContext context) {
private String getTypeName(TypeSymbol symbol, CodeActionContext context, ImportsAcceptor importsAcceptor) {
Optional<ModuleSymbol> module = symbol.getModule();
if (module.isPresent()) {
String fqPrefix = "";
Expand All @@ -259,9 +260,9 @@ private String getTypeName(TypeSymbol symbol, CodeActionContext context) {
}
String moduleQualifiedName = fqPrefix + (symbol.getName().isPresent() ? symbol.getName().get()
: getRawType(symbol, context).signature());
return FunctionGenerator.processModuleIDsInText(new ImportsAcceptor(context), moduleQualifiedName, context);
return FunctionGenerator.processModuleIDsInText(importsAcceptor, moduleQualifiedName, context);
}
return FunctionGenerator.processModuleIDsInText(new ImportsAcceptor(context), symbol.signature(), context);
return FunctionGenerator.processModuleIDsInText(importsAcceptor, symbol.signature(), context);
}

private boolean isLangAnnotationModule(ModuleID moduleID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ public Object[][] dataProvider() {
{"createVariableWithCheck18.json"},
{"createVariableWithCheck19.json"},
{"createVariableWithCheck20.json"},

// Related to imported types
{"createVariableWithInferredType1.json"},
{"createVariableWithInferredType2.json"},
{"createVariableWithInferredType3.json"},
{"createVariableWithInferredType4.json"}
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"position": {
"line": 27,
"character": 23
"character": 24
},
"source": "source1.bal",
"expected": [
{
"title": "Create variable",
"title": "Create variable with 'module1:Response|mod2:ClientError2'",
"kind": "quickfix",
"edits": [
{
Expand All @@ -20,7 +20,53 @@
"character": 4
}
},
"newText": "stream<targetType, module1:ClientError?> streamResult = "
"newText": "module1:Response|mod2:ClientError2 push = "
},
{
"range": {
"start": {
"line": 1,
"character": 0
},
"end": {
"line": 1,
"character": 0
}
},
"newText": "import ballerina/module1.mod2;\n"
}
],
"resolvable": false
},
{
"title": "Create variable with 'anydata|mod2:ClientError2'",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 27,
"character": 4
},
"end": {
"line": 27,
"character": 4
}
},
"newText": "anydata|mod2:ClientError2 push = "
},
{
"range": {
"start": {
"line": 1,
"character": 0
},
"end": {
"line": 1,
"character": 0
}
},
"newText": "import ballerina/module1.mod2;\n"
}
],
"resolvable": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ function testRemoteMethodCallWithCheck2() returns error? {
check 'client->/path4/["pathParam"];
}

function testResourceAccessOfStreamReturnType() returns error? {
'client->/responses
function testImportFromModule() returns error? {
'client->push(path = "/path");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"position": {
"line": 5,
"character": 16
},
"source": "createVariableWithInferredType.bal",
"expected": [
{
"title": "Create variable",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 5,
"character": 4
},
"end": {
"line": 5,
"character": 4
}
},
"newText": "anydata|mod2:ClientError2 pull = "
},
{
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 0
}
},
"newText": "import ballerina/module1.mod2;\n"
}
],
"resolvable": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"position": {
"line": 9,
"character": 18
},
"source": "createVariableWithInferredType.bal",
"expected": [
{
"title": "Create variable",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 9,
"character": 4
},
"end": {
"line": 9,
"character": 4
}
},
"newText": "stream<targetType, mod2:ClientError2?> streamResult = "
},
{
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 0
}
},
"newText": "import ballerina/module1.mod2;\n"
}
],
"resolvable": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"position": {
"line": 13,
"character": 18
},
"source": "createVariableWithInferredType.bal",
"expected": [
{
"title": "Create variable",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 13,
"character": 4
},
"end": {
"line": 13,
"character": 4
}
},
"newText": "anydata|module1:ClientError clear = "
}
],
"resolvable": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"position": {
"line": 17,
"character": 18
},
"source": "createVariableWithInferredType.bal",
"expected": [
{
"title": "Create variable",
"kind": "quickfix",
"edits": [
{
"range": {
"start": {
"line": 17,
"character": 4
},
"end": {
"line": 17,
"character": 4
}
},
"newText": "stream<targetType, module1:ClientError?> streamResult = "
}
],
"resolvable": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ballerina/module1;

module1:Client 'client = new (url="http://ballerina.io");

function testImportedSimpleType() returns error? {
'client->pull(path = "/path");
}

function testImportedStreamType() returns error? {
'client->/responses.delete()
}

function testSimpleType() returns error? {
'client->clear(path = "/path");
}

function testStreamType() returns error? {
'client->/responses
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,40 @@
"title": "editor.action.triggerParameterHints",
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "/responses.delete(module1:TargetType2 targetType)",
"kind": "Function",
"detail": "stream<targetType, mod2:ClientError2?>",
"documentation": {
"right": {
"kind": "markdown",
"value": "**Package:** _ballerina/module1:0.1.0_ \n \nSample resource function to return a stream of objects.\n \n**Params** \n- `module1:TargetType2` targetType: Response or `anydata`, which is expected to be returned after data binding(Defaultable) \n \n**Return** `stream<targetType, mod2:ClientError2?>` \n- A stream of targetType and/or ClientError \n \n"
}
},
"sortText": "C",
"filterText": "responses|delete",
"insertText": "/responses.delete(${1});",
"insertTextFormat": "Snippet",
"additionalTextEdits": [
{
"range": {
"start": {
"line": 5,
"character": 22
},
"end": {
"line": 5,
"character": 23
}
},
"newText": ""
}
],
"command": {
"title": "editor.action.triggerParameterHints",
"command": "editor.action.triggerParameterHints"
}
}
]
}
Loading

0 comments on commit 8b1ac03

Please sign in to comment.