diff --git a/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/codeaction/providers/AddLockCodeAction.java b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/codeaction/providers/AddLockCodeAction.java new file mode 100644 index 000000000000..7f547a44b277 --- /dev/null +++ b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/codeaction/providers/AddLockCodeAction.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.ballerinalang.langserver.codeaction.providers; + +import io.ballerina.compiler.syntax.tree.AssignmentStatementNode; +import io.ballerina.compiler.syntax.tree.Node; +import io.ballerina.compiler.syntax.tree.StatementNode; +import io.ballerina.compiler.syntax.tree.SyntaxKind; +import io.ballerina.tools.diagnostics.Diagnostic; +import io.ballerina.tools.text.LinePosition; +import io.ballerina.tools.text.TextDocument; +import io.ballerina.tools.text.TextRange; +import org.ballerinalang.annotation.JavaSPIService; +import org.ballerinalang.langserver.codeaction.CodeActionNodeValidator; +import org.ballerinalang.langserver.codeaction.CodeActionUtil; +import org.ballerinalang.langserver.common.constants.CommandConstants; +import org.ballerinalang.langserver.common.utils.CommonUtil; +import org.ballerinalang.langserver.common.utils.PositionUtil; +import org.ballerinalang.langserver.commons.CodeActionContext; +import org.ballerinalang.langserver.commons.codeaction.spi.DiagBasedPositionDetails; +import org.ballerinalang.langserver.commons.codeaction.spi.DiagnosticBasedCodeActionProvider; +import org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.CodeActionKind; +import org.eclipse.lsp4j.Position; +import org.eclipse.lsp4j.Range; +import org.eclipse.lsp4j.TextEdit; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +/** + * A code action to wrap an isolated variable with a lock. + * + * @since 2201.9.0 + */ +@JavaSPIService("org.ballerinalang.langserver.commons.codeaction.spi.LSCodeActionProvider") +public class AddLockCodeAction implements DiagnosticBasedCodeActionProvider { + + private static final String NAME = "Add lock"; + private static final Set DIAGNOSTIC_CODES = Set.of("BCE3957", "BCE3962"); + + @Override + public boolean validate(Diagnostic diagnostic, DiagBasedPositionDetails positionDetails, + CodeActionContext context) { + return DIAGNOSTIC_CODES.contains(diagnostic.diagnosticInfo().code()) + && CodeActionNodeValidator.validate(context.nodeAtRange()); + } + + @Override + public List getCodeActions(Diagnostic diagnostic, DiagBasedPositionDetails positionDetails, + CodeActionContext context) { + // Determine the enclosing statement node of the isolated variable + Optional statementNode = getMatchingStatementNode(positionDetails.matchedNode()); + if (statementNode.isEmpty()) { + return Collections.emptyList(); + } + + // Check if there are multiple isolated variables within a single statement + List diagnostics = context.diagnostics(context.filePath()); + if (diagnostics.size() > 1 && hasMultipleIsolationVars(statementNode.get(), diagnostic, diagnostics)) { + return Collections.emptyList(); + } + + // Generate and return the text edit for the lock statement + TextEdit surroundWithLockEditText = getTextEdit(statementNode.get()); + return Collections.singletonList(CodeActionUtil.createCodeAction( + CommandConstants.SURROUND_WITH_LOCK, + List.of(surroundWithLockEditText), + context.fileUri(), + CodeActionKind.QuickFix) + ); + } + + private static Optional getMatchingStatementNode(Node matchedNode) { + Node parentNode = matchedNode.parent(); + while (parentNode != null && !(parentNode instanceof StatementNode)) { + // Lock statement does not support async calls + if (parentNode.kind() == SyntaxKind.START_ACTION) { + return Optional.empty(); + } + parentNode = parentNode.parent(); + } + + // Check if the lock statement contains any async calls + if (parentNode != null && parentNode.kind() == SyntaxKind.ASSIGNMENT_STATEMENT) { + SyntaxKind kind = ((AssignmentStatementNode) parentNode).expression().kind(); + if (kind == SyntaxKind.RECEIVE_ACTION || kind == SyntaxKind.START_ACTION) { + return Optional.empty(); + } + } + + return Optional.ofNullable((StatementNode) parentNode); + } + + private static TextEdit getTextEdit(Node node) { + TextDocument textDocument = node.syntaxTree().textDocument(); + TextRange textRange = node.textRangeWithMinutiae(); + LinePosition startLinePosition = textDocument.linePositionFrom(textRange.startOffset()); + LinePosition endLinePosition = textDocument.linePositionFrom(textRange.endOffset()); + Position startPosition = PositionUtil.toPosition(startLinePosition); + Position endPosition = PositionUtil.toPosition(endLinePosition); + + String spaces = " ".repeat(node.lineRange().startLine().offset()); + String statement = node.toSourceCode(); + String indentedStatement = statement.substring(0, statement.length() - 1).replace("\n", "\n\t") + "\n"; + + String editText = + spaces + "lock {" + CommonUtil.LINE_SEPARATOR + "\t" + indentedStatement + spaces + "}" + "\n"; + return new TextEdit(new Range(startPosition, endPosition), editText); + } + + private static boolean hasMultipleIsolationVars(StatementNode statementNode, Diagnostic currentDiagnostic, + List diagnostics) { + return diagnostics.stream().anyMatch(diagnostic -> !currentDiagnostic.equals(diagnostic) && + DIAGNOSTIC_CODES.contains(diagnostic.diagnosticInfo().code()) && + PositionUtil.isWithinLineRange(diagnostic.location().lineRange(), statementNode.lineRange())); + } + + @Override + public String getName() { + return NAME; + } +} diff --git a/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/common/constants/CommandConstants.java b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/common/constants/CommandConstants.java index 72216df953e8..fcef1e6fba22 100644 --- a/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/common/constants/CommandConstants.java +++ b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/common/constants/CommandConstants.java @@ -158,6 +158,8 @@ public class CommandConstants { public static final String SURROUND_WITH_DO_ON_FAIL = "Surround with do/on-fail"; + public static final String SURROUND_WITH_LOCK = "Surround with lock"; + public static final String CONVERT_MODULE_VAR_TO_LISTENER_DECLARATION = "Convert module variable '%s' to listener declaration"; diff --git a/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/codeaction/AddLockCodeActionTest.java b/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/codeaction/AddLockCodeActionTest.java new file mode 100644 index 000000000000..8f9919efa329 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/codeaction/AddLockCodeActionTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://wso2.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.ballerinalang.langserver.codeaction; + +import org.ballerinalang.langserver.commons.workspace.WorkspaceDocumentException; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.io.IOException; + +/** + * Test class to test the add lock code action. + * + * @since 2201.9.0 + */ +public class AddLockCodeActionTest extends AbstractCodeActionTest { + + @Test(dataProvider = "codeaction-data-provider") + @Override + public void test(String config) throws IOException, WorkspaceDocumentException { + super.test(config); + } + + @DataProvider(name = "codeaction-data-provider") + @Override + public Object[][] dataProvider() { + return new Object[][]{ + {"add_lock1.json"}, + {"add_lock2.json"}, + {"add_lock3.json"}, + {"add_lock4.json"}, + {"add_lock5.json"}, + {"add_lock6.json"}, + {"add_lock7.json"}, + {"add_lock8.json"}, + {"add_lock9.json"}, + {"add_lock10.json"}, + {"add_lock11.json"}, + {"add_lock12.json"}, + {"add_lock13.json"}, + {"add_lock14.json"}, + {"add_lock15.json"}, + {"add_lock16.json"}, + {"add_lock17.json"}, + {"add_lock18.json"}, + {"add_lock19.json"}, + {"add_lock20.json"}, + {"add_lock21.json"}, + {"add_lock22.json"}, + {"add_lock23.json"}, + {"add_lock24.json"}, + {"add_lock25.json"}, + {"add_lock26.json"} + }; + } + + @Test(dataProvider = "negative-data-provider") + @Override + public void negativeTest(String config) throws IOException, WorkspaceDocumentException { + super.negativeTest(config); + } + + @DataProvider(name = "negative-data-provider") + public Object[][] negativeDataProvider() { + return new Object[][]{ + {"add_lock_negative1.json"}, + {"add_lock_negative2.json"}, + {"add_lock_negative3.json"}, + {"add_lock_negative4.json"}, + {"add_lock_negative5.json"}, + {"add_lock_negative6.json"}, + {"add_lock_negative7.json"} + }; + } + + @Override + public String getResourceDir() { + return "add-lock"; + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock1.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock1.json new file mode 100644 index 000000000000..25db25c9e761 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock1.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 3, + "character": 9 + }, + "source": "add_lock1.bal", + "description": "Wrap in a lock statement for a simple isolated variable", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 4, + "character": 0 + } + }, + "newText": " lock {\n\t _ = i + 2;\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock10.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock10.json new file mode 100644 index 000000000000..332c7ffad4fe --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock10.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 20 + }, + "source": "add_lock10.bal", + "description": "Wrap in a lock statement for returning a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t return self.mp.get(\"a\");\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock11.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock11.json new file mode 100644 index 000000000000..0f1c44d99e04 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock11.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 20 + }, + "source": "add_lock11.bal", + "description": "Wrap in a lock statement for returning a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t return self.mp.get(\"a\");\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock12.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock12.json new file mode 100644 index 000000000000..b5d352627a62 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock12.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 20 + }, + "source": "add_lock12.bal", + "description": "Wrap in a lock statement for returning a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t return self.arr[0];\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock13.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock13.json new file mode 100644 index 000000000000..13bc674f94ed --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock13.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 10 + }, + "source": "add_lock13.bal", + "description": "Wrap in a lock statement for updating a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t self.arr.push(inputArr);\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock14.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock14.json new file mode 100644 index 000000000000..d81a9acede79 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock14.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 12 + }, + "source": "add_lock14.bal", + "description": "Wrap in a lock statement for updating a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t self.mp[\"a\"] = inputArr;\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock15.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock15.json new file mode 100644 index 000000000000..3704c8de304a --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock15.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 3, + "character": 13 + }, + "source": "add_lock15.bal", + "description": "Wrap in a lock statement for returning a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 4, + "character": 0 + } + }, + "newText": " lock {\n\t return mp[\"a\"];\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock16.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock16.json new file mode 100644 index 000000000000..c909f5e196c9 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock16.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 3, + "character": 19 + }, + "source": "add_lock16.bal", + "description": "Wrap in a lock statement within an anonymous function", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 4, + "character": 0 + } + }, + "newText": " lock {\n\t int[][] arr2 = b;\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock17.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock17.json new file mode 100644 index 000000000000..042f827cb7dd --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock17.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 19, + "character": 9 + }, + "source": "add_lock17.bal", + "description": "Wrap in a lock statement for a isolated object", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 19, + "character": 0 + }, + "end": { + "line": 20, + "character": 0 + } + }, + "newText": " lock {\n\t _ = s2.get(0);\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock18.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock18.json new file mode 100644 index 000000000000..51fdd839d33f --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock18.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 3, + "character": 39 + }, + "source": "add_lock18.bal", + "description": "Wrap in a lock statement for a isolated variable within a query", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 6, + "character": 0 + } + }, + "newText": " lock {\n\t int[] listResult = from var el in arr\n\t where el > 10\n\t select el;\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock19.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock19.json new file mode 100644 index 000000000000..860cda898f6c --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock19.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 14, + "character": 24 + }, + "source": "add_lock19.bal", + "description": "Wrap in a lock statement for a isolated variable within a resource access", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 14, + "character": 0 + }, + "end": { + "line": 15, + "character": 0 + } + }, + "newText": " lock {\n\t cl->/path.accessor(arr[0]);\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock2.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock2.json new file mode 100644 index 000000000000..eda8f5c4e633 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock2.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 12 + }, + "source": "add_lock2.bal", + "description": "Wrap in a lock statement for a simple class variable", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t _ = self.i + 1;\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock20.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock20.json new file mode 100644 index 000000000000..c35e097fdeb1 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock20.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 14, + "character": 14 + }, + "source": "add_lock20.bal", + "description": "Wrap in a lock statement for a isolated variable within a remote method", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 14, + "character": 0 + }, + "end": { + "line": 15, + "character": 0 + } + }, + "newText": " lock {\n\t cl->fn(arr[0]);\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock21.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock21.json new file mode 100644 index 000000000000..31c41a7e8a18 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock21.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 6, + "character": 14 + }, + "source": "add_lock21.bal", + "description": "Wrap in a lock statement for a isolated variable within a worker", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 6, + "character": 0 + }, + "end": { + "line": 7, + "character": 0 + } + }, + "newText": " lock {\n\t arr[2] = val;\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock22.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock22.json new file mode 100644 index 000000000000..d87187e93e20 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock22.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 3, + "character": 9 + }, + "source": "add_lock22.bal", + "description": "Wrap in a lock statement for a isolated variable within an if body", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 8, + "character": 0 + } + }, + "newText": " lock {\n\t if arr[1] > 0 {\n\t return \"positive\";\n\t } else {\n\t return \"negative\";\n\t }\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock23.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock23.json new file mode 100644 index 000000000000..248b2edbf21a --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock23.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 7, + "character": 15 + }, + "source": "add_lock23.bal", + "description": "Wrap in a lock statement for a isolated variable within a match", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 14, + "character": 0 + } + }, + "newText": " lock {\n\t match val {\n\t 1 => {\n\t return \"one\";\n\t }\n\t 2 if arr[1] == 2 => {\n\t return \"two\";\n\t }\n\t _ => {\n\t return \"other\";\n\t }\n\t }\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock24.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock24.json new file mode 100644 index 000000000000..46e8325fdb7a --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock24.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 8, + "character": 20 + }, + "source": "add_lock24.bal", + "description": "Wrap in a lock statement for a isolated variable within a match", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 8, + "character": 0 + }, + "end": { + "line": 9, + "character": 0 + } + }, + "newText": " lock {\n\t return arr[2].toString();\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock25.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock25.json new file mode 100644 index 000000000000..05e9429c4b16 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock25.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 5, + "character": 9 + }, + "source": "add_lock25.bal", + "description": "Wrap in a lock statement for a statement with a comment", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 6, + "character": 0 + } + }, + "newText": " lock {\n\t // Comment1\n\t // Comment2\n\t _ = i + 2;\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock26.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock26.json new file mode 100644 index 000000000000..595d498b8416 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock26.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 3, + "character": 22 + }, + "source": "add_lock26.bal", + "description": "Wrap in a lock statement for a isolated variable in a if condition", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 6, + "character": 0 + } + }, + "newText": " lock {\n\t if count >= moduleCount / 2 {\n\t return true;\n\t }\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock3.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock3.json new file mode 100644 index 000000000000..25c38c9de8f8 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock3.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 15 + }, + "source": "add_lock3.bal", + "description": "Wrap in a lock statement for accessing a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t self.mp[\"a\"] = \"2\";\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock4.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock4.json new file mode 100644 index 000000000000..03360b3b46c5 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock4.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 20 + }, + "source": "add_lock4.bal", + "description": "Wrap in a lock statement for accessing a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t return self.mp.get(\"a\");\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock5.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock5.json new file mode 100644 index 000000000000..20d159d7b2a4 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock5.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 20 + }, + "source": "add_lock5.bal", + "description": "Wrap in a lock statement for returning a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t return self.arr[0];\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock6.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock6.json new file mode 100644 index 000000000000..911b54ca41dc --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock6.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 11 + }, + "source": "add_lock6.bal", + "description": "Wrap in a lock statement for updating a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t self.arr.push(inputArr);\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock7.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock7.json new file mode 100644 index 000000000000..7324bc5ba245 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock7.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 4, + "character": 10 + }, + "source": "add_lock7.bal", + "description": "Wrap in a lock statement for updating a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 4, + "character": 0 + }, + "end": { + "line": 5, + "character": 0 + } + }, + "newText": " lock {\n\t self.mp[\"a\"] = inputArr;\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock8.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock8.json new file mode 100644 index 000000000000..8235f99ec413 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock8.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 3, + "character": 5 + }, + "source": "add_lock8.bal", + "description": "Wrap in a lock statement for updating a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 4, + "character": 0 + } + }, + "newText": " lock {\n\t mp[\"a\"] = inputArr;\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock9.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock9.json new file mode 100644 index 000000000000..fbeb25f0c78d --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock9.json @@ -0,0 +1,30 @@ +{ + "position": { + "line": 3, + "character": 11 + }, + "source": "add_lock9.bal", + "description": "Wrap in a lock statement for returning a mutable storage", + "expected": [ + { + "title": "Surround with lock", + "kind": "quickfix", + "edits": [ + { + "range": { + "start": { + "line": 3, + "character": 0 + }, + "end": { + "line": 4, + "character": 0 + } + }, + "newText": " lock {\n\t return mp[\"a\"];\n }\n" + } + ], + "resolvable": false + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative1.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative1.json new file mode 100644 index 000000000000..81134a8f4e2e --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative1.json @@ -0,0 +1,12 @@ +{ + "position": { + "line": 4, + "character": 8 + }, + "source": "add_lock_negative7.bal", + "expected": [ + { + "title": "Surround with lock" + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative2.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative2.json new file mode 100644 index 000000000000..d61694aff04f --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative2.json @@ -0,0 +1,12 @@ +{ + "position": { + "line": 4, + "character": 5 + }, + "source": "add_lock_negative2.bal", + "expected": [ + { + "title": "Surround with lock" + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative3.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative3.json new file mode 100644 index 000000000000..df8b677ef2a6 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative3.json @@ -0,0 +1,12 @@ +{ + "position": { + "line": 2, + "character": 9 + }, + "source": "add_lock_negative3.bal", + "expected": [ + { + "title": "Surround with lock" + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative4.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative4.json new file mode 100644 index 000000000000..9416d685f991 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative4.json @@ -0,0 +1,12 @@ +{ + "position": { + "line": 5, + "character": 28 + }, + "source": "add_lock_negative4.bal", + "expected": [ + { + "title": "Surround with lock" + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative5.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative5.json new file mode 100644 index 000000000000..afd942aada92 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative5.json @@ -0,0 +1,12 @@ +{ + "position": { + "line": 4, + "character": 30 + }, + "source": "add_lock_negative5.bal", + "expected": [ + { + "title": "Surround with lock" + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative6.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative6.json new file mode 100644 index 000000000000..e7003352eb6b --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative6.json @@ -0,0 +1,12 @@ +{ + "position": { + "line": 5, + "character": 14 + }, + "source": "add_lock_negative6.bal", + "expected": [ + { + "title": "Surround with lock" + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative7.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative7.json new file mode 100644 index 000000000000..2f690c6d0863 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative7.json @@ -0,0 +1,12 @@ +{ + "position": { + "line": 1, + "character": 1 + }, + "source": "add_lock_negative7.bal", + "expected": [ + { + "title": "Surround with lock" + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative8.json b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative8.json new file mode 100644 index 000000000000..046bee13c353 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/config/add_lock_negative8.json @@ -0,0 +1,12 @@ +{ + "position": { + "line": 2, + "character": 42 + }, + "source": "add_lock_negative8.bal", + "expected": [ + { + "title": "Surround with lock" + } + ] +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock1.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock1.bal new file mode 100644 index 000000000000..6826e4306398 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock1.bal @@ -0,0 +1,5 @@ +isolated int i = 1; + +function fn() { + _ = i + 2; +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock10.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock10.bal new file mode 100644 index 000000000000..44f714e83ed2 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock10.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private map mp = {"a": [1, 2]}; + + function fn() returns int[] { + return self.mp.get("a"); + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock11.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock11.bal new file mode 100644 index 000000000000..ac5c09f1774a --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock11.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private final map mp = {"a": [1, 2]}; + + function fn() returns int[] { + return self.mp.get("a"); + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock12.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock12.bal new file mode 100644 index 000000000000..5b73fc57ea79 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock12.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private int[][] arr = [[1, 2]]; + + function fn() returns int[] { + return self.arr[0]; + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock13.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock13.bal new file mode 100644 index 000000000000..341081dcf2b0 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock13.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private final int[][] arr = [[1, 2]]; + + function fn(int[] inputArr) { + self.arr.push(inputArr); + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock14.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock14.bal new file mode 100644 index 000000000000..443570ecbb21 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock14.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private map mp = {"a": [1, 2]}; + + function fn(int[] inputArr) { + self.mp["a"] = inputArr; + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock15.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock15.bal new file mode 100644 index 000000000000..ae0f4564e746 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock15.bal @@ -0,0 +1,5 @@ +isolated map mp = {"a": [1, 2]}; + +isolated function fn() returns int[]? { + return mp["a"]; +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock16.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock16.bal new file mode 100644 index 000000000000..359476961667 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock16.bal @@ -0,0 +1,5 @@ +isolated int[][] b = [[1, 2]]; + +function name() returns function => isolated function() { + int[][] arr2 = b; +}; diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock17.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock17.bal new file mode 100644 index 000000000000..ed9653b24b52 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock17.bal @@ -0,0 +1,21 @@ +isolated class Stacks { + private int[][] list = []; + + isolated function get(int i) returns int[] { + lock { + return self.list[i].clone(); + } + } + + isolated function put(int[] & readonly arr) { + lock { + self.list.push(arr); + } + } +} + +isolated Stacks s2 = new; + +isolated function fn() { + _ = s2.get(0); +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock18.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock18.bal new file mode 100644 index 000000000000..f7d7e30249d9 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock18.bal @@ -0,0 +1,7 @@ +isolated int[] arr = [1, 2, 3]; + +isolated function name() { + int[] listResult = from var el in arr + where el > 10 + select el; +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock19.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock19.bal new file mode 100644 index 000000000000..f2e6b2cc7536 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock19.bal @@ -0,0 +1,16 @@ +isolated int[] arr = [1, 2, 3, 4, 5]; + +isolated client class MyClient { + private int[] arr = [1, 2]; + + isolated resource function accessor path(int i) { + lock { + self.arr.push(i); + } + } +} + +isolated function fn() { + MyClient cl = new; + cl->/path.accessor(arr[0]); +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock2.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock2.bal new file mode 100644 index 000000000000..38aa688e5fa7 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock2.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private int i = 1; + + function fn() { + _ = self.i + 1; + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock20.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock20.bal new file mode 100644 index 000000000000..bafb6c0bdebb --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock20.bal @@ -0,0 +1,16 @@ +isolated int[] arr = [1, 2, 3, 4, 5]; + +isolated client class MyClient { + private int[] arr = [1, 2]; + + isolated remote function fn(int i) { + lock { + self.arr.push(i); + } + } +} + +isolated function fn() { + MyClient cl = new; + cl->fn(arr[0]); +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock21.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock21.bal new file mode 100644 index 000000000000..199a2d553ce2 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock21.bal @@ -0,0 +1,14 @@ +isolated int[] arr = [1, 2, 3]; + +isolated function name() { + fork { + worker A { + int val = <- B; + arr[2] = val; + } + + worker B { + arr[0] ->> A; + } + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock22.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock22.bal new file mode 100644 index 000000000000..004f753d27fc --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock22.bal @@ -0,0 +1,9 @@ +isolated int[] arr = [1, 2, 3]; + +function fn() returns string { + if arr[1] > 0 { + return "positive"; + } else { + return "negative"; + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock23.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock23.bal new file mode 100644 index 000000000000..9bc65aa5b326 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock23.bal @@ -0,0 +1,15 @@ +isolated int[] arr = [1, 2, 3]; + +function fn(int val) returns string { + match val { + 1 => { + return "one"; + } + 2 if arr[1] == 2 => { + return "two"; + } + _ => { + return "other"; + } + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock24.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock24.bal new file mode 100644 index 000000000000..31e96d9b6fc1 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock24.bal @@ -0,0 +1,15 @@ +isolated int[] arr = [1, 2, 3]; + +function fn(int val) returns string { + match val { + 1 => { + return "one"; + } + 2 => { + return arr[2].toString(); + } + _ => { + return "other"; + } + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock25.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock25.bal new file mode 100644 index 000000000000..e591b8c387f3 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock25.bal @@ -0,0 +1,8 @@ +isolated int i = 1; + +function fn() { + // Comment1 + // Comment2 + _ = i + 2; + // Comment +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock26.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock26.bal new file mode 100644 index 000000000000..da263515914a --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock26.bal @@ -0,0 +1,8 @@ +isolated int moduleCount = 0; + +function name(int count) returns boolean { + if count >= moduleCount / 2 { + return true; + } + return false; +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock3.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock3.bal new file mode 100644 index 000000000000..520a32d53f9a --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock3.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private map mp = {"a": "1"}; + + function fn() { + self.mp["a"] = "2"; + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock4.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock4.bal new file mode 100644 index 000000000000..a6488f8af694 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock4.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private map & readonly mp = {"a": [1, 2]}; + + function fn() returns int[] { + return self.mp.get("a"); + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock5.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock5.bal new file mode 100644 index 000000000000..153993af96d3 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock5.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private int[][] & readonly arr = [[1, 2]]; + + function fn() returns int[] { + return self.arr[0]; + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock6.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock6.bal new file mode 100644 index 000000000000..345748525e78 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock6.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private final int[][] arr = [[1, 2]]; + + function fn(int[] & readonly inputArr) { + self.arr.push(inputArr); + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock7.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock7.bal new file mode 100644 index 000000000000..66c3613675b2 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock7.bal @@ -0,0 +1,7 @@ +isolated class IsolatedClass { + private map mp = {"a": [1, 2]}; + + function fn(int[] & readonly inputArr) { + self.mp["a"] = inputArr; + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock8.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock8.bal new file mode 100644 index 000000000000..50315c4d0d11 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock8.bal @@ -0,0 +1,5 @@ +isolated map mp = {"a": [1, 2]}; + +isolated function fn(int[] & readonly inputArr) { + mp["a"] = inputArr; +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock9.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock9.bal new file mode 100644 index 000000000000..56e850ff97fa --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock9.bal @@ -0,0 +1,5 @@ +isolated map & readonly mp = {"a": [1, 2]}; + +isolated function fn() returns int[]? { + return mp["a"]; +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative1.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative1.bal new file mode 100644 index 000000000000..c7122e21fbc9 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative1.bal @@ -0,0 +1,6 @@ +isolated int i = 1; +isolated int j = 2; + +isolated function fn() { + _ = i + j; +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative2.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative2.bal new file mode 100644 index 000000000000..b467a2e1e9f1 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative2.bal @@ -0,0 +1,6 @@ +isolated map mp = {"a": [1, 2, 3]}; +isolated int[] arr = [12, 2]; + +isolated function fn() { + mp["a"] = arr; +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative3.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative3.bal new file mode 100644 index 000000000000..f8994b142cb7 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative3.bal @@ -0,0 +1,3 @@ +isolated int[] arr = [1, 2, 3]; + +int i = arr[0]; diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative4.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative4.bal new file mode 100644 index 000000000000..38d7aa851686 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative4.bal @@ -0,0 +1,8 @@ +isolated int[] arr = [1, 2, 3]; +isolated boolean condition = false; + +isolated function name() { + int[] listResult = from var el in arr + where el > 10 && condition + select el; +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative5.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative5.bal new file mode 100644 index 000000000000..edf4c15bde6f --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative5.bal @@ -0,0 +1,11 @@ +isolated class IsolatedClass { + private final int[][] arr = [[1, 2]]; + + function fn(int[] & readonly inputArr) { + _ = start asyncFn(self.arr[0]); + } +} + +isolated function asyncFn(int[] arr) { + +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative6.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative6.bal new file mode 100644 index 000000000000..47b18a33eac5 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative6.bal @@ -0,0 +1,13 @@ +isolated int[] arr = [1, 2, 3]; + +isolated function name() { + fork { + worker A { + arr[2] = <- B; + } + + worker B { + arr[0] -> A; + } + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative7.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative7.bal new file mode 100644 index 000000000000..1a8d81beefc9 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative7.bal @@ -0,0 +1,7 @@ +isolated future[] asyncArr = []; + +isolated function name() { + asyncArr[0] = start asyncFn(); +} + +isolated function asyncFn() returns int => 0; diff --git a/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative8.bal b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative8.bal new file mode 100644 index 000000000000..ae387cc17ccb --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/codeaction/add-lock/source/add_lock_negative8.bal @@ -0,0 +1,3 @@ +isolated int[] arr = [12, 2]; + +isolated function name() returns int => arr[0];