diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index 5f5f5dd885..dc9ed9b3ec 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -1,13 +1,13 @@ [package] org = "ballerina" name = "http" -version = "2.9.1" +version = "2.10.0" authors = ["Ballerina"] keywords = ["http", "network", "service", "listener", "client"] repository = "https://github.com/ballerina-platform/module-ballerina-http" icon = "icon.png" license = ["Apache-2.0"] -distribution = "2201.7.0" +distribution = "2201.8.0" export = ["http", "http.httpscerr"] [platform.java11] @@ -16,8 +16,8 @@ graalvmCompatible = true [[platform.java11.dependency]] groupId = "io.ballerina.stdlib" artifactId = "http-native" -version = "2.9.1" -path = "../native/build/libs/http-native-2.9.1-SNAPSHOT.jar" +version = "2.10.0" +path = "../native/build/libs/http-native-2.10.0-SNAPSHOT.jar" [[platform.java11.dependency]] groupId = "io.ballerina.stdlib" diff --git a/ballerina/CompilerPlugin.toml b/ballerina/CompilerPlugin.toml index 87ccfe2ae8..dfc1773f50 100644 --- a/ballerina/CompilerPlugin.toml +++ b/ballerina/CompilerPlugin.toml @@ -3,4 +3,4 @@ id = "http-compiler-plugin" class = "io.ballerina.stdlib.http.compiler.HttpCompilerPlugin" [[dependency]] -path = "../compiler-plugin/build/libs/http-compiler-plugin-2.9.1-SNAPSHOT.jar" +path = "../compiler-plugin/build/libs/http-compiler-plugin-2.10.0-SNAPSHOT.jar" diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index edbb437d15..bc7dea0394 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.7.0" +distribution-version = "2201.8.0-20230716-001200-4e224cd5" [[package]] org = "ballerina" @@ -76,7 +76,7 @@ modules = [ [[package]] org = "ballerina" name = "http" -version = "2.9.1" +version = "2.10.0" dependencies = [ {org = "ballerina", name = "auth"}, {org = "ballerina", name = "cache"}, diff --git a/build-config/resources/Ballerina.toml b/build-config/resources/Ballerina.toml index 97f72e376b..17519a5a86 100644 --- a/build-config/resources/Ballerina.toml +++ b/build-config/resources/Ballerina.toml @@ -7,7 +7,7 @@ keywords = ["http", "network", "service", "listener", "client"] repository = "https://github.com/ballerina-platform/module-ballerina-http" icon = "icon.png" license = ["Apache-2.0"] -distribution = "2201.7.0" +distribution = "2201.8.0" export = ["http", "http.httpscerr"] [platform.java11] diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/AbstractLSCompilerPluginTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/AbstractLSCompilerPluginTest.java new file mode 100644 index 0000000000..ddadb2d19b --- /dev/null +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/AbstractLSCompilerPluginTest.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 io.ballerina.stdlib.http.compiler; + +import io.ballerina.projects.ProjectEnvironmentBuilder; +import io.ballerina.projects.environment.Environment; +import io.ballerina.projects.environment.EnvironmentBuilder; + +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Abstract compiler plugin test for LS. + */ +public class AbstractLSCompilerPluginTest { + protected static final Path RESOURCE_PATH = Paths.get("src", "test", "resources"); + protected static final Path DISTRIBUTION_PATH = Paths.get("../", "target", "ballerina-runtime"); + + protected static ProjectEnvironmentBuilder getEnvironmentBuilder() { + Environment environment = EnvironmentBuilder.getBuilder().setBallerinaHome(DISTRIBUTION_PATH).build(); + return ProjectEnvironmentBuilder.getBuilder(environment); + } +} diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/codeaction/AbstractCodeActionTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/codeaction/AbstractCodeActionTest.java index 442ab25513..d04c0a32a2 100644 --- a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/codeaction/AbstractCodeActionTest.java +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/codeaction/AbstractCodeActionTest.java @@ -26,23 +26,20 @@ import io.ballerina.projects.Package; import io.ballerina.projects.PackageCompilation; import io.ballerina.projects.Project; -import io.ballerina.projects.ProjectEnvironmentBuilder; import io.ballerina.projects.directory.ProjectLoader; -import io.ballerina.projects.environment.Environment; -import io.ballerina.projects.environment.EnvironmentBuilder; import io.ballerina.projects.plugins.codeaction.CodeActionArgument; import io.ballerina.projects.plugins.codeaction.CodeActionContextImpl; import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext; import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContextImpl; import io.ballerina.projects.plugins.codeaction.CodeActionInfo; import io.ballerina.projects.plugins.codeaction.DocumentEdit; +import io.ballerina.stdlib.http.compiler.AbstractLSCompilerPluginTest; import io.ballerina.tools.text.LinePosition; import org.testng.Assert; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -50,18 +47,10 @@ /** * Abstract implementation of codeaction tests. */ -public abstract class AbstractCodeActionTest { - - protected static final Path RESOURCE_PATH = Paths.get("src", "test", "resources"); - protected static final Path DISTRIBUTION_PATH = Paths.get("../", "target", "ballerina-runtime"); +public abstract class AbstractCodeActionTest extends AbstractLSCompilerPluginTest { private static final Gson GSON = new Gson(); - - private static ProjectEnvironmentBuilder getEnvironmentBuilder() { - Environment environment = EnvironmentBuilder.getBuilder().setBallerinaHome(DISTRIBUTION_PATH).build(); - return ProjectEnvironmentBuilder.getBuilder(environment); - } - + /** * Performs the actual test. This will verify both the code action title/args and the edits made once executed. * diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/completion/AbstractCompletionTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/completion/AbstractCompletionTest.java new file mode 100644 index 0000000000..e76296e07f --- /dev/null +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/completion/AbstractCompletionTest.java @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 io.ballerina.stdlib.http.compiler.completion; + +import com.google.gson.Gson; +import io.ballerina.compiler.syntax.tree.ModulePartNode; +import io.ballerina.compiler.syntax.tree.NonTerminalNode; +import io.ballerina.projects.CompletionManager; +import io.ballerina.projects.CompletionResult; +import io.ballerina.projects.Document; +import io.ballerina.projects.DocumentId; +import io.ballerina.projects.Module; +import io.ballerina.projects.Package; +import io.ballerina.projects.PackageCompilation; +import io.ballerina.projects.Project; +import io.ballerina.projects.directory.ProjectLoader; +import io.ballerina.projects.plugins.completion.CompletionContext; +import io.ballerina.projects.plugins.completion.CompletionContextImpl; +import io.ballerina.projects.plugins.completion.CompletionException; +import io.ballerina.projects.plugins.completion.CompletionItem; +import io.ballerina.stdlib.http.compiler.AbstractLSCompilerPluginTest; +import io.ballerina.tools.text.LinePosition; +import io.ballerina.tools.text.TextRange; +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Abstract implementation of completion tests. + */ +public abstract class AbstractCompletionTest extends AbstractLSCompilerPluginTest { + + private static final Gson GSON = new Gson(); + + + @Test(dataProvider = "completion-data-provider") + protected void test(String filePath, int line, int offset, String configFile) + throws IOException { + + Path sourceFilePath = Path.of(filePath); + Path configfilePath = Path.of(configFile); + TestConfig expectedList = + GSON.fromJson(Files.newBufferedReader(configfilePath), TestConfig.class); + List expectedItems = expectedList.getItems(); + LinePosition cursorPos = LinePosition.from(line, offset); + + Project project = ProjectLoader.loadProject(sourceFilePath, getEnvironmentBuilder()); + CompletionResult completionResult = getCompletions(sourceFilePath, cursorPos, project); + List actualItems = completionResult.getCompletionItems(); + List errors = completionResult.getErrors(); + Assert.assertTrue(errors.isEmpty()); + Assert.assertTrue(compareCompletionItems(actualItems, expectedItems)); + } + + private static boolean compareCompletionItems(List actualItems, + List expectedItems) { + List actualList = actualItems.stream() + .map(AbstractCompletionTest::getCompletionItemPropertyString) + .collect(Collectors.toList()); + List expectedList = expectedItems.stream() + .map(AbstractCompletionTest::getCompletionItemPropertyString) + .collect(Collectors.toList()); + return actualList.containsAll(expectedList) && actualItems.size() == expectedItems.size(); + } + + private static String getCompletionItemPropertyString(CompletionItem completionItem) { + // Here we replace the Windows specific \r\n to \n for evaluation only + String additionalTextEdits = ""; + if (completionItem.getAdditionalTextEdits() != null && !completionItem.getAdditionalTextEdits().isEmpty()) { + additionalTextEdits = "," + GSON.toJson(completionItem.getAdditionalTextEdits()); + } + return ("{" + + completionItem.getInsertText() + "," + + completionItem.getLabel() + "," + + completionItem.getPriority() + + additionalTextEdits + + "}").replace("\r\n", "\n").replace("\\r\\n", "\\n"); + } + + private CompletionResult getCompletions(Path filePath, LinePosition cursorPos, Project project) { + + Package currentPackage = project.currentPackage(); + PackageCompilation compilation = currentPackage.getCompilation(); + CompletionManager completionManager = compilation.getCompletionManager(); + + DocumentId documentId = project.documentId(filePath); + Document document = currentPackage.getDefaultModule().document(documentId); + Module module = project.currentPackage().module(documentId.moduleId()); + + int cursorPositionInTree = document.textDocument().textPositionFrom(cursorPos); + TextRange range = TextRange.from(cursorPositionInTree, 0); + NonTerminalNode nodeAtCursor = ((ModulePartNode) document.syntaxTree().rootNode()).findNode(range); + + CompletionContext completionContext = CompletionContextImpl.from(filePath.toUri().toString(), + filePath, cursorPos, cursorPositionInTree, nodeAtCursor, document, + module.getCompilation().getSemanticModel()); + + return completionManager.completions(completionContext); + } + + @DataProvider(name = "completion-data-provider") + public abstract Object[][] dataProvider(); + + /** + * Represents the completion test config. + */ + public static class TestConfig { + private List items; + + public void setItems(List items) { + this.items = items; + } + + public List getItems() { + return items; + } + } + +} diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/completion/ServiceDeclarationNodeContextTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/completion/ServiceDeclarationNodeContextTest.java new file mode 100644 index 0000000000..807180cc01 --- /dev/null +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/completion/ServiceDeclarationNodeContextTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 io.ballerina.stdlib.http.compiler.completion; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.nio.file.Path; + +/** + * Tests the completions in ServiceDeclarationNode context. + */ +public class ServiceDeclarationNodeContextTest extends AbstractCompletionTest { + + + @Override + @Test(dataProvider = "completion-data-provider") + protected void test(String filePath, int line, int offset, String configFile) + throws IOException { + Path filePathFromSourceRoot = RESOURCE_PATH.resolve("ballerina_sources") + .resolve(filePath); + Path configPath = RESOURCE_PATH.resolve("completion") + .resolve(getConfigDir()) + .resolve(configFile); + super.test(filePathFromSourceRoot.toString(), line, offset, configPath.toString()); + } + + @DataProvider(name = "completion-data-provider") + @Override + public Object[][] dataProvider() { + return new Object[][]{ + {"sample_completion_package_1/main.bal", 22, 5, "expected_completions1.json"}, + {"sample_completion_package_2/main.bal", 22, 5, "expected_completions1.json"}, + {"sample_completion_package_2/main.bal", 35, 5, "expected_completions1.json"} + }; + } + + protected String getConfigDir() { + return "service_declaration"; + } +} diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_1/Ballerina.toml b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_1/Ballerina.toml new file mode 100644 index 0000000000..343cbe9aec --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_1/Ballerina.toml @@ -0,0 +1,5 @@ +[package] +org = "http_test" +name = "sample_completion_package_1" +version = "0.1.0" + diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_1/main.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_1/main.bal new file mode 100644 index 0000000000..efcc19b9f9 --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_1/main.bal @@ -0,0 +1,24 @@ +// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. 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. + +// This is added to test some auto generated code segments. +// Please ignore the indentation. + +import ballerina/http; + +service /greeting on new http:Listener(9090) { + r +} diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_2/Ballerina.toml b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_2/Ballerina.toml new file mode 100644 index 0000000000..0c2c5d78cc --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_2/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "http_test" +name = "sample_completion_package_2" +version = "0.1.0" diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_2/main.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_2/main.bal new file mode 100644 index 0000000000..fde46f74fd --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_completion_package_2/main.bal @@ -0,0 +1,37 @@ +// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. 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. + +// This is added to test some auto generated code segments. +// Please ignore the indentation. + +import ballerina/http; + +service /greeting on new http:Listener(9090) { + r + + resource function post() returns string { + return "Hello Ballerina"; + } +} + +service /greeting on new http:Listener(8080) { + + resource function post() returns string { + return "Hello Ballerina"; + } + + r +} diff --git a/compiler-plugin-tests/src/test/resources/completion/service_declaration/expected_completions1.json b/compiler-plugin-tests/src/test/resources/completion/service_declaration/expected_completions1.json new file mode 100644 index 0000000000..0242b10c11 --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/completion/service_declaration/expected_completions1.json @@ -0,0 +1,34 @@ +{ + "items": [ + { + "label": "resource function get path()", + "insertText": "resource function get ${1:path}(${2}) ${3}{\n\t${4}\n}", + "priority": "HIGH" + }, + { + "label": "resource function post path()", + "insertText": "resource function post ${1:path}(${2}) ${3}{\n\t${4}\n}", + "priority": "HIGH" + }, + { + "label": "resource function put path()", + "insertText": "resource function put ${1:path}(${2}) ${3}{\n\t${4}\n}", + "priority": "HIGH" + }, + { + "label": "resource function delete path()", + "insertText": "resource function delete ${1:path}(${2}) ${3}{\n\t${4}\n}", + "priority": "HIGH" + }, + { + "label": "resource function head path()", + "insertText": "resource function head ${1:path}(${2}) ${3}{\n\t${4}\n}", + "priority": "HIGH" + }, + { + "label": "resource function options path()", + "insertText": "resource function options ${1:path}(${2}) ${3}{\n\t${4}\n}", + "priority": "HIGH" + } + ] +} diff --git a/compiler-plugin-tests/src/test/resources/testng.xml b/compiler-plugin-tests/src/test/resources/testng.xml index 98a56e6894..7d43c52ab2 100644 --- a/compiler-plugin-tests/src/test/resources/testng.xml +++ b/compiler-plugin-tests/src/test/resources/testng.xml @@ -22,6 +22,7 @@ + diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpCompilerPlugin.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpCompilerPlugin.java index f65864f807..38e59beadb 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpCompilerPlugin.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpCompilerPlugin.java @@ -18,9 +18,11 @@ package io.ballerina.stdlib.http.compiler; +import io.ballerina.compiler.syntax.tree.Node; import io.ballerina.projects.plugins.CompilerPlugin; import io.ballerina.projects.plugins.CompilerPluginContext; import io.ballerina.projects.plugins.codeaction.CodeAction; +import io.ballerina.projects.plugins.completion.CompletionProvider; import io.ballerina.stdlib.http.compiler.codeaction.AddHeaderParameterCodeAction; import io.ballerina.stdlib.http.compiler.codeaction.AddInterceptorRemoteMethodCodeAction; import io.ballerina.stdlib.http.compiler.codeaction.AddInterceptorResourceMethodCodeAction; @@ -31,6 +33,7 @@ import io.ballerina.stdlib.http.compiler.codeaction.ChangeHeaderParamTypeToStringCodeAction; import io.ballerina.stdlib.http.compiler.codeaction.ChangeReturnTypeWithCallerCodeAction; import io.ballerina.stdlib.http.compiler.codemodifier.HttpServiceModifier; +import io.ballerina.stdlib.http.compiler.completion.HttpServiceBodyContextProvider; import java.util.List; @@ -44,6 +47,7 @@ public void init(CompilerPluginContext context) { context.addCodeModifier(new HttpServiceModifier()); context.addCodeAnalyzer(new HttpServiceAnalyzer()); getCodeActions().forEach(context::addCodeAction); + getCompletionProviders().forEach(context::addCompletionProvider); } private List getCodeActions() { @@ -59,4 +63,8 @@ private List getCodeActions() { new AddInterceptorRemoteMethodCodeAction() ); } + + private List> getCompletionProviders() { + return List.of(new HttpServiceBodyContextProvider()); + } } diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/completion/Constants.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/completion/Constants.java new file mode 100644 index 0000000000..4f3263c0a5 --- /dev/null +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/completion/Constants.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 io.ballerina.stdlib.http.compiler.completion; + + +import java.util.List; + +/** + * Constants related to compiler plugin completion implementation. + */ +public class Constants { + public static final String GET = "get"; + public static final String POST = "post"; + + public static final String PUT = "put"; + public static final String DELETE = "delete"; + + public static final String HEAD = "head"; + + public static final String OPTIONS = "options"; + + public static final List METHODS = List.of(GET, POST, PUT, DELETE, HEAD, OPTIONS); + +} diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/completion/HttpServiceBodyContextProvider.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/completion/HttpServiceBodyContextProvider.java new file mode 100644 index 0000000000..e072698243 --- /dev/null +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/completion/HttpServiceBodyContextProvider.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 io.ballerina.stdlib.http.compiler.completion; + +import io.ballerina.compiler.syntax.tree.ServiceDeclarationNode; +import io.ballerina.projects.plugins.completion.CompletionContext; +import io.ballerina.projects.plugins.completion.CompletionException; +import io.ballerina.projects.plugins.completion.CompletionItem; +import io.ballerina.projects.plugins.completion.CompletionProvider; +import io.ballerina.projects.plugins.completion.CompletionUtil; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * Completion provider for Http services. + */ +public class HttpServiceBodyContextProvider implements CompletionProvider { + + private static final String NAME = "HttpServiceBodyProvider"; + + + @Override + public String name() { + return NAME; + } + + @Override + public List getCompletions(CompletionContext completionContext, + ServiceDeclarationNode serviceDeclarationNode) + throws CompletionException { + + //Add resource function snippets for get,post,delete,put,head,and options http methods. + return Constants.METHODS.stream().map(method -> { + String insertText = String.format("resource function %s %s(%s) %s{%s}", method + , CompletionUtil.getPlaceHolderText(1, "path") + , CompletionUtil.getPlaceHolderText(2), CompletionUtil.getPlaceHolderText(3) + , CompletionUtil.LINE_BREAK + CompletionUtil.PADDING + CompletionUtil.getPlaceHolderText(4) + + CompletionUtil.LINE_BREAK); + String label = "resource function " + method + " path()"; + return new CompletionItem(label, insertText, CompletionItem.Priority.HIGH); + }).collect(Collectors.toList()); + } + + @Override + public List> getSupportedNodes() { + return List.of(ServiceDeclarationNode.class); + } +} diff --git a/gradle.properties b/gradle.properties index c335e9ea47..4d6dd9930e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ org.gradle.caching=true group=io.ballerina.stdlib -version=2.9.1-SNAPSHOT -ballerinaLangVersion=2201.7.0 +version=2.10.0-SNAPSHOT +ballerinaLangVersion=2201.8.0-20230716-001200-4e224cd5 ballerinaTomlParserVersion=1.2.2 commonsLang3Version=3.8.1 nettyVersion=4.1.94.Final