Skip to content

Commit

Permalink
Fix spotbug failure
Browse files Browse the repository at this point in the history
  • Loading branch information
nipunayf committed Sep 19, 2024
1 parent d465aa4 commit fc7db66
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,8 +90,7 @@ public CompletableFuture<Either<List<CompletionItem>, 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();
Expand All @@ -110,7 +107,7 @@ public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completio
Either<List<CompletionItem>, CompletionList> completions = completableFuture.join();
projectCacheManager.deleteCache();
return completions;
} catch (Exception ignored) {
} catch (Throwable e) {
return Either.forLeft(List.of());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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<Path> 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<Path> paths = Files.walk(destinationDir)) {
if (Files.isDirectory(destinationPath)) {
try (Stream<Path> paths = Files.walk(destinationPath)) {
paths.sorted(Comparator.reverseOrder()).forEach(source -> {
try {
Files.delete(source);
Expand All @@ -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)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down

0 comments on commit fc7db66

Please sign in to comment.