Skip to content

Commit

Permalink
Implement isNodeUsed() API
Browse files Browse the repository at this point in the history
  • Loading branch information
dulajdilshan committed Nov 7, 2024
1 parent 77d2d5d commit 1470a61
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 com.google.gson.Gson;
import com.google.gson.JsonElement;
import io.ballerina.flowmodelgenerator.core.model.FlowNode;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;

/**
* Generates information for a node.
*
* @since 1.4.0
*/
public class BasicNodeGenerator {

private final Gson gson;
private final FlowNode nodeToCheckUsages;

public BasicNodeGenerator(JsonElement nodeToCheckUsages) {
this.gson = new Gson();
this.nodeToCheckUsages = new Gson().fromJson(nodeToCheckUsages, FlowNode.class);
}

public LinePosition getNodeStartLocation() {
LineRange lineRange = nodeToCheckUsages.codedata().lineRange();
return lineRange.startLine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
import io.ballerina.flowmodelgenerator.core.DeleteNodeGenerator;
import io.ballerina.flowmodelgenerator.core.ErrorHandlerGenerator;
import io.ballerina.flowmodelgenerator.core.FunctionGenerator;
import io.ballerina.flowmodelgenerator.core.BasicNodeGenerator;
import io.ballerina.flowmodelgenerator.core.ModelGenerator;
import io.ballerina.flowmodelgenerator.core.NodeTemplateGenerator;
import io.ballerina.flowmodelgenerator.core.OpenApiServiceGenerator;
import io.ballerina.flowmodelgenerator.core.SourceGenerator;
import io.ballerina.flowmodelgenerator.core.SuggestedComponentService;
import io.ballerina.flowmodelgenerator.core.SuggestedModelGenerator;
import io.ballerina.flowmodelgenerator.extension.request.IsNodeUsedRequest;
import io.ballerina.flowmodelgenerator.extension.request.CopilotContextRequest;
import io.ballerina.flowmodelgenerator.extension.request.FilePathRequest;
import io.ballerina.flowmodelgenerator.extension.request.FlowModelAvailableNodesRequest;
Expand All @@ -53,11 +55,13 @@
import io.ballerina.flowmodelgenerator.extension.response.FlowModelNodeTemplateResponse;
import io.ballerina.flowmodelgenerator.extension.response.FlowModelSourceGeneratorResponse;
import io.ballerina.flowmodelgenerator.extension.response.FlowNodeDeleteResponse;
import io.ballerina.flowmodelgenerator.extension.response.IsNodeUsedResponse;
import io.ballerina.flowmodelgenerator.extension.response.OpenApiServiceGenerationResponse;
import io.ballerina.projects.Document;
import io.ballerina.projects.DocumentId;
import io.ballerina.projects.Module;
import io.ballerina.projects.Project;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;
import io.ballerina.tools.text.TextDocument;
Expand Down Expand Up @@ -435,6 +439,28 @@ public CompletableFuture<FlowModelSourceGeneratorResponse> addErrorHandler(FileP
});
}

@JsonRequest
public CompletableFuture<IsNodeUsedResponse> isNodeUsed(IsNodeUsedRequest request) {
return CompletableFuture.supplyAsync(() -> {
IsNodeUsedResponse response = new IsNodeUsedResponse();
try {
Path filePath = Path.of(request.filePath());
Optional<SemanticModel> semanticModel = this.workspaceManager.semanticModel(filePath);
Optional<Document> document = this.workspaceManager.document(filePath);
if (semanticModel.isEmpty() || document.isEmpty()) {
response.setUsed(false);
return response;
}
BasicNodeGenerator basicNodeGenerator = new BasicNodeGenerator(request.flowNode());
List<Location> references = semanticModel.get().references(document.get(), basicNodeGenerator.getNodeStartLocation());
response.setUsed(!references.isEmpty());
} catch (Throwable e) {
response.setError(e);
}
return response;
});
}

private static String getRelativePath(Path projectPath, Path filePath) {
if (projectPath == null || filePath == null) {
return "";
Expand Down
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 com.google.gson.JsonElement;

/**
* Represents the request to check whether a particular node is used.
*
* @param filePath file path of the source file
* @param flowNode flow node
* @since 1.4.0
*/
public record IsNodeUsedRequest(String filePath, JsonElement flowNode) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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;

/**
* Represents the response after checking a particular node is used.
*
* @since 1.4.0
*/
public class IsNodeUsedResponse extends AbstractFlowModelResponse {

boolean isUsed;

public boolean isUsed() {
return isUsed;
}

public void setUsed(boolean used) {
isUsed = used;
}
}

0 comments on commit 1470a61

Please sign in to comment.