-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #514 from LakshanWeerasinghe/fix-#456
Add api to get enclosed function def range
- Loading branch information
Showing
12 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/EnclosedNodeFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2024, 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.core; | ||
|
||
import io.ballerina.compiler.syntax.tree.ModulePartNode; | ||
import io.ballerina.compiler.syntax.tree.NonTerminalNode; | ||
import io.ballerina.compiler.syntax.tree.SyntaxKind; | ||
import io.ballerina.projects.Document; | ||
import io.ballerina.tools.text.LinePosition; | ||
import io.ballerina.tools.text.LineRange; | ||
import io.ballerina.tools.text.TextRange; | ||
import org.ballerinalang.langserver.common.utils.PositionUtil; | ||
|
||
/** | ||
* Enclosed node finder. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public class EnclosedNodeFinder { | ||
|
||
private final Document document; | ||
private final LinePosition position; | ||
|
||
public EnclosedNodeFinder(Document document, LinePosition position) { | ||
this.document = document; | ||
this.position = position; | ||
} | ||
|
||
public LineRange findEnclosedNode() { | ||
ModulePartNode modulePartNode = document.syntaxTree().rootNode(); | ||
int positionOffset = PositionUtil.getPositionOffset(PositionUtil.toPosition(position), document.syntaxTree()); | ||
TextRange textRange = TextRange.from(positionOffset, 1); | ||
NonTerminalNode nonTerminalNode = modulePartNode.findNode(textRange); | ||
while (nonTerminalNode != null && nonTerminalNode.kind() != SyntaxKind.FUNCTION_DEFINITION) { | ||
nonTerminalNode = nonTerminalNode.parent(); | ||
} | ||
if (nonTerminalNode == null) { | ||
throw new RuntimeException("No enclosed node found"); | ||
} | ||
return nonTerminalNode.lineRange(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...c/main/java/io/ballerina/flowmodelgenerator/extension/request/EnclosedFuncDefRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2024, 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; | ||
|
||
import io.ballerina.tools.text.LinePosition; | ||
|
||
/** | ||
* Represents the request for the flow model getEnclosedFlowDesignModel API. | ||
* | ||
* @param filePath The file path of the source file. | ||
* @param position The position of the source. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public record EnclosedFuncDefRequest(String filePath, LinePosition position) { | ||
} |
56 changes: 56 additions & 0 deletions
56
...main/java/io/ballerina/flowmodelgenerator/extension/response/EnclosedFuncDefResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2024, 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; | ||
|
||
import io.ballerina.tools.text.LinePosition; | ||
|
||
/** | ||
* Represents the response for the flow model getEnclosedFlowDesignModel API. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public class EnclosedFuncDefResponse extends AbstractFlowModelResponse { | ||
private String filePath; | ||
private LinePosition startLine; | ||
private LinePosition endLine; | ||
|
||
public void setFilePath(String filePath) { | ||
this.filePath = filePath; | ||
} | ||
|
||
public void setStartLine(LinePosition startLine) { | ||
this.startLine = startLine; | ||
} | ||
|
||
public void setEndLine(LinePosition endLine) { | ||
this.endLine = endLine; | ||
} | ||
|
||
public String getFilePath() { | ||
return filePath; | ||
} | ||
|
||
public LinePosition getStartLine() { | ||
return startLine; | ||
} | ||
|
||
public LinePosition getEndLine() { | ||
return endLine; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...n/src/test/java/io/ballerina/flowmodelgenerator/extension/GetEnclosedFunctionDefTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (c) 2024, 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.JsonPrimitive; | ||
import io.ballerina.flowmodelgenerator.extension.request.EnclosedFuncDefRequest; | ||
import io.ballerina.tools.text.LinePosition; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Tests for the flow model source generator service. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public class GetEnclosedFunctionDefTest extends AbstractLSTest { | ||
|
||
@Override | ||
@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 filePath = getSourcePath(testConfig.filePath()); | ||
EnclosedFuncDefRequest request = new EnclosedFuncDefRequest(filePath, testConfig.position()); | ||
JsonObject response = getResponse(request); | ||
JsonObject acutalJsonObj = testConfig.response(); | ||
acutalJsonObj.add("filePath", new JsonPrimitive( | ||
getSourcePath(testConfig.response().get("filePath").getAsString()))); | ||
if (!testConfig.response().equals(response)) { | ||
String pathToReplace = response.get("filePath").getAsString() | ||
.replace(sourceDir.toString() + "/", ""); | ||
response.addProperty("filePath", pathToReplace); | ||
TestConfig updatedConfig = new TestConfig(testConfig.description(), testConfig.filePath(), | ||
testConfig.position(), response); | ||
// updateConfig(configJsonPath, updatedConfig); | ||
Assert.fail(String.format("Failed test: '%s' (%s)", testConfig.description(), configJsonPath)); | ||
} | ||
} | ||
|
||
@Override | ||
protected String getResourceDir() { | ||
return "get_enclosed_func_def"; | ||
} | ||
|
||
@Override | ||
protected Class<? extends AbstractLSTest> clazz() { | ||
return GetEnclosedFunctionDefTest.class; | ||
} | ||
|
||
@Override | ||
protected String getApiName() { | ||
return "getEnclosedFunctionDef"; | ||
} | ||
|
||
/** | ||
* Represents the test configuration for the delete node test. | ||
* | ||
* @param description The description of the test. | ||
* @param filePath The path to the source file that contains the nodes to be deleted. | ||
* @param position The position of the node to be deleted. | ||
* @param response The expected response. | ||
*/ | ||
private record TestConfig(String description, String filePath, LinePosition position, JsonObject response) { | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...r-ls-extension/src/test/resources/get_enclosed_func_def/config/current_package_ref_1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"description": "current package reference", | ||
"filePath": "proj_1/functions.bal", | ||
"position": { | ||
"line": 3, | ||
"offset": 8 | ||
}, | ||
"response": { | ||
"filePath" : "proj_1/functions.bal", | ||
"startLine" : { | ||
"line" : 0, | ||
"offset" : 0 | ||
}, | ||
"endLine" : { | ||
"line" : 6, | ||
"offset" : 1 | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...r-ls-extension/src/test/resources/get_enclosed_func_def/config/current_package_ref_2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"description": "current package reference", | ||
"filePath": "proj_1/functions.bal", | ||
"position": { | ||
"line": 9, | ||
"offset": 0 | ||
}, | ||
"response": { | ||
"filePath": "proj_1/functions.bal", | ||
"startLine": { | ||
"line": 8, | ||
"offset": 0 | ||
}, | ||
"endLine": { | ||
"line": 10, | ||
"offset": 1 | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...r-ls-extension/src/test/resources/get_enclosed_func_def/config/current_package_ref_3.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"description": "current package reference", | ||
"filePath": "proj_1/functions.bal", | ||
"position": { | ||
"line": 15, | ||
"offset": 4 | ||
}, | ||
"response": { | ||
"filePath": "proj_1/functions.bal", | ||
"startLine": { | ||
"line": 12, | ||
"offset": 0 | ||
}, | ||
"endLine": { | ||
"line": 16, | ||
"offset": 1 | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...erator-ls-extension/src/test/resources/get_enclosed_func_def/source/proj_1/Ballerina.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
org = "wso2" | ||
name = "proj_1" | ||
version = "0.1.0" | ||
|
||
bi = true |
17 changes: 17 additions & 0 deletions
17
...nerator-ls-extension/src/test/resources/get_enclosed_func_def/source/proj_1/functions.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function foo() returns string { | ||
int i; | ||
for (int i = 0; i < 10; i++) { | ||
i = i + 1; | ||
} | ||
return "Hello " + i.toString(); | ||
} | ||
|
||
function bar() returns string { | ||
return foo(); | ||
} | ||
|
||
function baz() returns string { | ||
int a = 1; | ||
int b = 2; | ||
return a + b; | ||
} |
3 changes: 3 additions & 0 deletions
3
...-generator-ls-extension/src/test/resources/get_enclosed_func_def/source/proj_1/sample.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public function main() returns error? { | ||
foo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters