diff --git a/flow-model-generator/modules/flow-model-generator-ls-extension/src/main/java/io/ballerina/flowmodelgenerator/extension/ExpressionEditorService.java b/flow-model-generator/modules/flow-model-generator-ls-extension/src/main/java/io/ballerina/flowmodelgenerator/extension/ExpressionEditorService.java index 5668177c..af85b55e 100644 --- a/flow-model-generator/modules/flow-model-generator-ls-extension/src/main/java/io/ballerina/flowmodelgenerator/extension/ExpressionEditorService.java +++ b/flow-model-generator/modules/flow-model-generator-ls-extension/src/main/java/io/ballerina/flowmodelgenerator/extension/ExpressionEditorService.java @@ -38,9 +38,7 @@ import org.eclipse.lsp4j.jsonrpc.services.JsonSegment; import org.eclipse.lsp4j.services.LanguageServer; -import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.StandardOpenOption; import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; @@ -92,8 +90,7 @@ public CompletableFuture, CompletionList>> completio TextEdit textEdit = TextEdit.from(TextRange.from(textPosition, 0), statement); TextDocument newTextDocument = textDocument.apply(TextDocumentChange.from(List.of(textEdit).toArray(new TextEdit[0]))); - Files.write(destination, new String(newTextDocument.toCharArray()).getBytes(), - StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + projectCacheManager.writeContent(newTextDocument); document.get().modify() .withContent(String.join(System.lineSeparator(), newTextDocument.textLines())) .apply(); @@ -110,7 +107,7 @@ public CompletableFuture, CompletionList>> completio Either, CompletionList> completions = completableFuture.join(); projectCacheManager.deleteCache(); return completions; - } catch (Exception ignored) { + } catch (Throwable e) { return Either.forLeft(List.of()); } }); diff --git a/flow-model-generator/modules/flow-model-generator-ls-extension/src/main/java/io/ballerina/flowmodelgenerator/extension/ProjectCacheManager.java b/flow-model-generator/modules/flow-model-generator-ls-extension/src/main/java/io/ballerina/flowmodelgenerator/extension/ProjectCacheManager.java index ff94fa15..469e101e 100644 --- a/flow-model-generator/modules/flow-model-generator-ls-extension/src/main/java/io/ballerina/flowmodelgenerator/extension/ProjectCacheManager.java +++ b/flow-model-generator/modules/flow-model-generator-ls-extension/src/main/java/io/ballerina/flowmodelgenerator/extension/ProjectCacheManager.java @@ -18,10 +18,13 @@ 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; @@ -34,7 +37,7 @@ public class ProjectCacheManager { private final Path sourceDir; private final Path filePath; - private Path destinationDir; + private Path destinationPath; public ProjectCacheManager(Path sourceDir, Path filePath) { this.sourceDir = sourceDir; @@ -44,28 +47,29 @@ public ProjectCacheManager(Path sourceDir, Path filePath) { public void createTempDirectory() throws IOException { // Create a temporary directory Path tempDir = Files.createTempDirectory("project-cache"); - destinationDir = tempDir.resolve(sourceDir.getFileName()); + Path tempDesintaitonPath = tempDir.resolve(sourceDir.getFileName()); + destinationPath = tempDesintaitonPath; // Copy contents from sourceDir to destinationDir if (Files.isDirectory(sourceDir)) { try (Stream paths = Files.walk(sourceDir)) { paths.forEach(source -> { try { - Files.copy(source, destinationDir.resolve(sourceDir.relativize(source)), + 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); } }); } - } else { - Files.copy(sourceDir, destinationDir, StandardCopyOption.REPLACE_EXISTING); + return; } + Files.copy(sourceDir, tempDesintaitonPath, StandardCopyOption.REPLACE_EXISTING); } public void deleteCache() throws IOException { - if (Files.isDirectory(destinationDir)) { - try (Stream paths = Files.walk(destinationDir)) { + if (Files.isDirectory(destinationPath)) { + try (Stream paths = Files.walk(destinationPath)) { paths.sorted(Comparator.reverseOrder()).forEach(source -> { try { Files.delete(source); @@ -74,12 +78,23 @@ public void deleteCache() throws IOException { } }); } - } else { - Files.delete(destinationDir); + 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() { - return destinationDir.resolve(sourceDir.relativize(sourceDir.resolve(filePath))); + if (destinationPath == null) { + throw new RuntimeException("Destination directory is not created"); + } + return destinationPath.resolve(sourceDir.relativize(sourceDir.resolve(filePath))); } -} \ No newline at end of file +} diff --git a/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/java/io/ballerina/flowmodelgenerator/extension/ExpressionEditorCompletionTest.java b/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/java/io/ballerina/flowmodelgenerator/extension/ExpressionEditorCompletionTest.java index ef8f3aa8..6289655a 100644 --- a/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/java/io/ballerina/flowmodelgenerator/extension/ExpressionEditorCompletionTest.java +++ b/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/java/io/ballerina/flowmodelgenerator/extension/ExpressionEditorCompletionTest.java @@ -58,7 +58,7 @@ public void test(Path config) throws IOException { TestConfig updatedConfig = new TestConfig(testConfig.description(), testConfig.filePath(), testConfig.expression(), testConfig.branch(), testConfig.property(), testConfig.startLine(), testConfig.offset(), testConfig.context(), testConfig.node(), actualCompletions); - updateConfig(configJsonPath, updatedConfig); +// updateConfig(configJsonPath, updatedConfig); Assert.fail(String.format("Failed test: '%s' (%s)", testConfig.description(), configJsonPath)); } }