Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…a-dev-tools into oas-client
  • Loading branch information
KavinduZoysa committed Jan 6, 2025
2 parents 2e2b6ff + a1e2fe2 commit d03bae3
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class NewConnectionBuilder extends NodeBuilder {
public static final String CHECK_ERROR_DOC = "Terminate on error";
public static final String CONNECTION_NAME_LABEL = "Connection Name";
public static final String CONNECTION_TYPE_LABEL = "Connection Type";
protected static Logger log = LoggerFactory.getLogger(NewConnectionBuilder.class);
protected static final Logger log = LoggerFactory.getLogger(NewConnectionBuilder.class);

@Override
public void setConcreteConstData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
import io.ballerina.flowmodelgenerator.extension.request.ExpressionEditorDiagnosticsRequest;
import io.ballerina.flowmodelgenerator.extension.request.ExpressionEditorSignatureRequest;
import io.ballerina.flowmodelgenerator.extension.request.FunctionCallTemplateRequest;
import io.ballerina.flowmodelgenerator.extension.request.ImportModuleRequest;
import io.ballerina.flowmodelgenerator.extension.request.VisibleVariableTypeRequest;
import io.ballerina.flowmodelgenerator.extension.response.ExpressionEditorDiagnosticsResponse;
import io.ballerina.flowmodelgenerator.extension.response.ExpressionEditorTypeResponse;
import io.ballerina.flowmodelgenerator.extension.response.FunctionCallTemplateResponse;
import io.ballerina.flowmodelgenerator.extension.response.SuccessResponse;
import io.ballerina.flowmodelgenerator.extension.response.VisibleVariableTypesResponse;
import io.ballerina.projects.Document;
import io.ballerina.projects.Project;
Expand Down Expand Up @@ -251,6 +253,33 @@ public CompletableFuture<FunctionCallTemplateResponse> functionCallTemplate(Func
});
}

@JsonRequest
public CompletableFuture<SuccessResponse> importModule(ImportModuleRequest request) {
return CompletableFuture.supplyAsync(() -> {
SuccessResponse response = new SuccessResponse();
try {
String fileUri = CommonUtils.getExprUri(request.filePath());
Optional<Document> document = workspaceManagerProxy.get(fileUri).document(Path.of(request.filePath()));
if (document.isPresent()) {
ExpressionEditorContext expressionEditorContext = new ExpressionEditorContext(
workspaceManagerProxy.get(fileUri),
Path.of(request.filePath()), document.get());
String importStatement = request.importStatement()
.replaceFirst("^import\\s+", "")
.replaceAll(";\\n$", "");
Optional<TextEdit> importTextEdit = expressionEditorContext
.getImport(importStatement);
importTextEdit.ifPresent(textEdit -> expressionEditorContext.applyTextEdits(List.of(textEdit)));
response.setSuccess(true);
}
} catch (Exception e) {
response.setError(e);
response.setSuccess(false);
}
return response;
});
}

@JsonRequest
public CompletableFuture<ExpressionEditorDiagnosticsResponse> diagnostics(
ExpressionEditorDiagnosticsRequest request) {
Expand Down Expand Up @@ -293,5 +322,4 @@ public CompletableFuture<ExpressionEditorDiagnosticsResponse> diagnostics(
return response;
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://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 io.ballerina.flowmodelgenerator.extension.request;

/**
* Represents a request for module snippet.
*
* @param filePath The file path which contains the expression
* @param importStatement The import statement to be applied
*/
public record ImportModuleRequest(String filePath, String importStatement) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://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 io.ballerina.flowmodelgenerator.extension.response;

/**
* This is a class to indicate if the API success or not.
*
* @since 2.0.0
*/
public class SuccessResponse extends AbstractFlowModelResponse {

private boolean success;

public boolean success() {
return this.success;
}

public void setSuccess(boolean success) {
this.success = success;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public abstract class AbstractLSTest {

protected Endpoint serviceEndpoint;
private BallerinaLanguageServer languageServer;
protected static final List<String> UNDEFINED_DIAGNOSTICS_CODES = List.of("BCE2000", "BCE2011");

@BeforeClass
public final void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*/
public class FunctionCallTemplateTest extends AbstractLSTest {

private static final List<String> UNDEFINED_DIAGNOSTICS_CODES = List.of("BCE2000", "BCE2011");


@Override
@Test(dataProvider = "data-provider")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://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 io.ballerina.flowmodelgenerator.extension;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import io.ballerina.flowmodelgenerator.core.ExpressionEditorContext;
import io.ballerina.flowmodelgenerator.extension.request.ExpressionEditorDiagnosticsRequest;
import io.ballerina.flowmodelgenerator.extension.request.ImportModuleRequest;
import io.ballerina.tools.text.LinePosition;
import org.eclipse.lsp4j.Diagnostic;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
* This is a test class that tests the module snippet API.
*
* @since 2.0.0
*/
public class ImportModuleTest extends AbstractLSTest {

@Test(dataProvider = "data-provider")
public void test(Path config) throws IOException {
Path configJsonPath = configDir.resolve(config);
TestConfig testConfig = gson.fromJson(Files.newBufferedReader(configJsonPath), TestConfig.class);
String sourcePath = getSourcePath(testConfig.filePath());
notifyDidOpen(sourcePath);

// Send module snippet request
ImportModuleRequest moduleSnippetRequest = new ImportModuleRequest(sourcePath, testConfig.importStatement());
JsonObject successResponse = getResponse(moduleSnippetRequest);
successResponse.get("success").getAsBoolean();
Assert.assertTrue(successResponse.get("success").getAsBoolean(), "Module snippet request failed");

// Send diagnostics request
ExpressionEditorContext.Info info =
new ExpressionEditorContext.Info(testConfig.expression(), LinePosition.from(1, 0),
0, new JsonObject(), null, null);
ExpressionEditorDiagnosticsRequest diagnosticsRequest =
new ExpressionEditorDiagnosticsRequest(sourcePath, info);
JsonObject response = getResponse(diagnosticsRequest, "expressionEditor/diagnostics");
List<Diagnostic> diagnostics = gson.fromJson(response.get("diagnostics").getAsJsonArray(),
new TypeToken<List<Diagnostic>>() { }.getType());
notifyDidClose(sourcePath);

// Check for undefined import/function diagnostic codes
for (Diagnostic diagnostic : diagnostics) {
String code = diagnostic.getCode().getLeft();
Assert.assertFalse(UNDEFINED_DIAGNOSTICS_CODES.contains(code),
"Undefined diagnostic code found: " + code);
}
}

@Override
protected String getResourceDir() {
return "import_module";
}

@Override
protected String getApiName() {
return "importModule";
}

@Override
protected String getServiceName() {
return "expressionEditor";
}

@Override
protected Class<? extends AbstractLSTest> clazz() {
return ImportModuleTest.class;
}

private record TestConfig(String description, String filePath, String importStatement, String expression) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"description": "",
"filePath": "source.bal",
"importStatement": "import ballerina/log;\n",
"expression": "log:p"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"description": "",
"filePath": "source.bal",
"importStatement": "import ballerina/io;\n",
"expression": "io:print"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ballerina/io;

public function testFunctionCall() {
int result = 10;
io:println("Result: ", result);
}

function fn() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ under the License.
<class name="io.ballerina.flowmodelgenerator.extension.ErrorHandlerGeneratorTest"/>
<class name="io.ballerina.flowmodelgenerator.extension.GetEnclosedFunctionDefTest"/>
<class name="io.ballerina.flowmodelgenerator.extension.FunctionCallTemplateTest"/>
<class name="io.ballerina.flowmodelgenerator.extension.ImportModuleTest"/>
<class name="io.ballerina.flowmodelgenerator.extension.ClientGeneratorTest"/>

<!--types manager-->
Expand Down

0 comments on commit d03bae3

Please sign in to comment.