-
Notifications
You must be signed in to change notification settings - Fork 42
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 #403 from nipunayf/completions-api
Provide completions for expressions in the expression editor
- Loading branch information
Showing
10 changed files
with
1,027 additions
and
55 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
...sion/src/main/java/io/ballerina/flowmodelgenerator/extension/ExpressionEditorService.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,115 @@ | ||
/* | ||
* 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 io.ballerina.flowmodelgenerator.extension.request.ExpressionEditorCompletionRequest; | ||
import io.ballerina.projects.Document; | ||
import io.ballerina.tools.text.TextDocument; | ||
import io.ballerina.tools.text.TextDocumentChange; | ||
import io.ballerina.tools.text.TextEdit; | ||
import io.ballerina.tools.text.TextRange; | ||
import org.ballerinalang.annotation.JavaSPIService; | ||
import org.ballerinalang.langserver.commons.service.spi.ExtendedLanguageServerService; | ||
import org.ballerinalang.langserver.commons.workspace.WorkspaceManager; | ||
import org.eclipse.lsp4j.CompletionItem; | ||
import org.eclipse.lsp4j.CompletionList; | ||
import org.eclipse.lsp4j.CompletionParams; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.TextDocumentIdentifier; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest; | ||
import org.eclipse.lsp4j.jsonrpc.services.JsonSegment; | ||
import org.eclipse.lsp4j.services.LanguageServer; | ||
|
||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@JavaSPIService("org.ballerinalang.langserver.commons.service.spi.ExtendedLanguageServerService") | ||
@JsonSegment("expressionEditor") | ||
public class ExpressionEditorService implements ExtendedLanguageServerService { | ||
|
||
private WorkspaceManager workspaceManager; | ||
private LanguageServer langServer; | ||
|
||
@Override | ||
public void init(LanguageServer langServer, WorkspaceManager workspaceManager) { | ||
this.workspaceManager = workspaceManager; | ||
this.langServer = langServer; | ||
} | ||
|
||
@Override | ||
public Class<?> getRemoteInterface() { | ||
return null; | ||
} | ||
|
||
@JsonRequest | ||
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion( | ||
ExpressionEditorCompletionRequest request) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
// Load the project | ||
Path filePath = Path.of(request.filePath()); | ||
this.workspaceManager.loadProject(filePath); | ||
Path projectPath = this.workspaceManager.projectRoot(filePath); | ||
|
||
// Create a temporary directory and load the project | ||
ProjectCacheManager projectCacheManager = new ProjectCacheManager(projectPath, filePath); | ||
projectCacheManager.createTempDirectory(); | ||
Path destination = projectCacheManager.getDestination(); | ||
this.workspaceManager.loadProject(destination); | ||
|
||
// Get the document | ||
Optional<Document> document = this.workspaceManager.document(destination); | ||
if (document.isEmpty()) { | ||
return Either.forLeft(List.of()); | ||
} | ||
TextDocument textDocument = document.get().textDocument(); | ||
|
||
// Determine the cursor position | ||
int textPosition = textDocument.textPositionFrom(request.startLine()); | ||
String statement = String.format("_ = %s;%n", request.expression()); | ||
TextEdit textEdit = TextEdit.from(TextRange.from(textPosition, 0), statement); | ||
TextDocument newTextDocument = | ||
textDocument.apply(TextDocumentChange.from(List.of(textEdit).toArray(new TextEdit[0]))); | ||
projectCacheManager.writeContent(newTextDocument); | ||
document.get().modify() | ||
.withContent(String.join(System.lineSeparator(), newTextDocument.textLines())) | ||
.apply(); | ||
|
||
// Generate the completion params | ||
Position position = | ||
new Position(request.startLine().line(), request.startLine().offset() + 4 + request.offset()); | ||
TextDocumentIdentifier identifier = new TextDocumentIdentifier(destination.toUri().toString()); | ||
CompletionParams params = new CompletionParams(identifier, position, request.context()); | ||
|
||
// Get the completions | ||
CompletableFuture<Either<List<CompletionItem>, CompletionList>> completableFuture = | ||
langServer.getTextDocumentService().completion(params); | ||
Either<List<CompletionItem>, CompletionList> completions = completableFuture.join(); | ||
projectCacheManager.deleteCache(); | ||
return completions; | ||
} catch (Throwable e) { | ||
return Either.forLeft(List.of()); | ||
} | ||
}); | ||
} | ||
} |
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
100 changes: 100 additions & 0 deletions
100
...xtension/src/main/java/io/ballerina/flowmodelgenerator/extension/ProjectCacheManager.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,100 @@ | ||
/* | ||
* 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 io.ballerina.tools.text.TextDocument; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.Comparator; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Manages the cache of the temporarily copied project directory. | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
public class ProjectCacheManager { | ||
|
||
private final Path sourceDir; | ||
private final Path filePath; | ||
private Path destinationPath; | ||
|
||
public ProjectCacheManager(Path sourceDir, Path filePath) { | ||
this.sourceDir = sourceDir; | ||
this.filePath = filePath; | ||
} | ||
|
||
public void createTempDirectory() throws IOException { | ||
// Create a temporary directory | ||
Path tempDir = Files.createTempDirectory("project-cache"); | ||
Path tempDesintaitonPath = tempDir.resolve(sourceDir.getFileName()); | ||
destinationPath = tempDesintaitonPath; | ||
|
||
// Copy contents from sourceDir to destinationDir | ||
if (Files.isDirectory(sourceDir)) { | ||
try (Stream<Path> paths = Files.walk(sourceDir)) { | ||
paths.forEach(source -> { | ||
try { | ||
Files.copy(source, tempDesintaitonPath.resolve(sourceDir.relativize(source)), | ||
StandardCopyOption.REPLACE_EXISTING); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to copy project directory to cache", e); | ||
} | ||
}); | ||
} | ||
return; | ||
} | ||
Files.copy(sourceDir, tempDesintaitonPath, StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
|
||
public void deleteCache() throws IOException { | ||
if (Files.isDirectory(destinationPath)) { | ||
try (Stream<Path> paths = Files.walk(destinationPath)) { | ||
paths.sorted(Comparator.reverseOrder()).forEach(source -> { | ||
try { | ||
Files.delete(source); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to delete destination directory", e); | ||
} | ||
}); | ||
} | ||
return; | ||
} | ||
Files.delete(destinationPath); | ||
} | ||
|
||
public void writeContent(TextDocument textDocument) throws IOException { | ||
if (destinationPath == null) { | ||
throw new RuntimeException("Destination directory is not created"); | ||
} | ||
Files.writeString(destinationPath, new String(textDocument.toCharArray()), StandardOpenOption.CREATE, | ||
StandardOpenOption.TRUNCATE_EXISTING); | ||
} | ||
|
||
public Path getDestination() { | ||
if (destinationPath == null) { | ||
throw new RuntimeException("Destination directory is not created"); | ||
} | ||
return destinationPath.resolve(sourceDir.relativize(sourceDir.resolve(filePath))); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
.../io/ballerina/flowmodelgenerator/extension/request/ExpressionEditorCompletionRequest.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,41 @@ | ||
/* | ||
* 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.JsonObject; | ||
import io.ballerina.tools.text.LinePosition; | ||
import org.eclipse.lsp4j.CompletionContext; | ||
|
||
/** | ||
* Represents a request for expression editor completion. | ||
* | ||
* @param filePath The file path which contains the expression | ||
* @param expression The modified expression | ||
* @param branch The branch of the expression if exists | ||
* @param property The property of the expression | ||
* @param startLine The start line of the node | ||
* @param offset The offset of cursor compared to the start of the expression | ||
* @param context The completion context | ||
* @param node The node which contains the expression | ||
*/ | ||
public record ExpressionEditorCompletionRequest(String filePath, String expression, String branch, String property, | ||
LinePosition startLine, int offset, CompletionContext context, | ||
JsonObject node) { | ||
} |
1 change: 1 addition & 0 deletions
1
...F/services/org.ballerinalang.langserver.commons.service.spi.ExtendedLanguageServerService
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
io.ballerina.flowmodelgenerator.extension.FlowModelGeneratorService | ||
io.ballerina.flowmodelgenerator.extension.ExpressionEditorService |
Oops, something went wrong.