refactor, rename and reducing complexity
This commit is contained in:
+3
-1
@@ -1,5 +1,7 @@
|
||||
package p.studio.vfs;
|
||||
|
||||
import p.studio.vfs.messages.VfsProjectContext;
|
||||
|
||||
public interface ProjectDocumentVfsFactory {
|
||||
ProjectDocumentVfs open(VfsProjectContext projectContext);
|
||||
VfsProjectDocument open(VfsProjectContext projectContext);
|
||||
}
|
||||
|
||||
+35
-2
@@ -1,7 +1,40 @@
|
||||
package p.studio.vfs;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import p.studio.vfs.messages.VfsDocumentAccessContext;
|
||||
import p.studio.vfs.messages.VfsUnsupportedReason;
|
||||
|
||||
public sealed interface VfsDocumentOpenResult permits VfsTextDocument, VfsUnsupportedDocument {
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public sealed interface VfsDocumentOpenResult permits VfsDocumentOpenResult.VfsUnsupportedDocument, VfsDocumentOpenResult.VfsTextDocument {
|
||||
Path path();
|
||||
|
||||
record VfsUnsupportedDocument(
|
||||
Path path,
|
||||
VfsUnsupportedReason reason) implements VfsDocumentOpenResult {
|
||||
|
||||
public VfsUnsupportedDocument {
|
||||
path = Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
|
||||
Objects.requireNonNull(reason, "reason");
|
||||
}
|
||||
}
|
||||
|
||||
record VfsTextDocument(
|
||||
Path path,
|
||||
String documentName,
|
||||
String typeId,
|
||||
String content,
|
||||
String lineSeparator,
|
||||
boolean dirty,
|
||||
VfsDocumentAccessContext accessContext) implements VfsDocumentOpenResult {
|
||||
|
||||
public VfsTextDocument {
|
||||
path = Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
|
||||
Objects.requireNonNull(documentName, "documentName");
|
||||
Objects.requireNonNull(typeId, "typeId");
|
||||
Objects.requireNonNull(content, "content");
|
||||
Objects.requireNonNull(lineSeparator, "lineSeparator");
|
||||
Objects.requireNonNull(accessContext, "accessContext");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -1,10 +1,12 @@
|
||||
package p.studio.vfs;
|
||||
|
||||
import p.studio.vfs.messages.*;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface ProjectDocumentVfs extends AutoCloseable {
|
||||
public interface VfsProjectDocument extends AutoCloseable {
|
||||
VfsProjectContext projectContext();
|
||||
|
||||
VfsProjectSnapshot snapshot();
|
||||
@@ -23,7 +25,7 @@ public interface ProjectDocumentVfs extends AutoCloseable {
|
||||
throw new UnsupportedOperationException("Document access context updates are not supported by this VFS implementation.");
|
||||
}
|
||||
|
||||
default VfsTextDocument updateDocument(final Path path, final String content) {
|
||||
default VfsDocumentOpenResult.VfsTextDocument updateDocument(final Path path, final String content) {
|
||||
throw new UnsupportedOperationException("Document mutation is not supported by this VFS implementation.");
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package p.studio.vfs;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public record VfsTextDocument(
|
||||
Path path,
|
||||
String documentName,
|
||||
String typeId,
|
||||
String content,
|
||||
String lineSeparator,
|
||||
boolean dirty,
|
||||
VfsDocumentAccessContext accessContext) implements VfsDocumentOpenResult {
|
||||
|
||||
public VfsTextDocument {
|
||||
path = Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
|
||||
Objects.requireNonNull(documentName, "documentName");
|
||||
Objects.requireNonNull(typeId, "typeId");
|
||||
Objects.requireNonNull(content, "content");
|
||||
Objects.requireNonNull(lineSeparator, "lineSeparator");
|
||||
Objects.requireNonNull(accessContext, "accessContext");
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package p.studio.vfs;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public record VfsUnsupportedDocument(
|
||||
Path path,
|
||||
VfsUnsupportedReason reason) implements VfsDocumentOpenResult {
|
||||
|
||||
public VfsUnsupportedDocument {
|
||||
path = Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
|
||||
Objects.requireNonNull(reason, "reason");
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
public enum VfsDocumentAccessMode {
|
||||
READ_ONLY,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
public enum VfsDocumentSaveStatus {
|
||||
SAVED,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
public final class VfsDocumentTypeIds {
|
||||
public static final String TEXT = "text";
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package p.studio.vfs;
|
||||
package p.studio.vfs.messages;
|
||||
|
||||
public enum VfsUnsupportedReason {
|
||||
OUTSIDE_PROJECT,
|
||||
+4
-2
@@ -1,10 +1,12 @@
|
||||
package p.studio.vfs;
|
||||
|
||||
import p.studio.vfs.messages.VfsProjectContext;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class FilesystemProjectDocumentVfsFactory implements ProjectDocumentVfsFactory {
|
||||
@Override
|
||||
public ProjectDocumentVfs open(final VfsProjectContext projectContext) {
|
||||
return new FilesystemProjectDocumentVfs(Objects.requireNonNull(projectContext, "projectContext"));
|
||||
public VfsProjectDocument open(final VfsProjectContext projectContext) {
|
||||
return new FilesystemVfsProjectDocument(Objects.requireNonNull(projectContext, "projectContext"));
|
||||
}
|
||||
}
|
||||
|
||||
+8
-14
@@ -2,6 +2,7 @@ package p.studio.vfs;
|
||||
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.compiler.models.FrontendSpec;
|
||||
import p.studio.vfs.messages.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
@@ -11,24 +12,17 @@ import java.nio.charset.CodingErrorAction;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
|
||||
final class FilesystemVfsProjectDocument implements VfsProjectDocument {
|
||||
private final VfsProjectContext projectContext;
|
||||
private final Map<Path, EditableDocumentSnapshot> editableSnapshots = new LinkedHashMap<>();
|
||||
private final Map<Path, Map<String, String>> accessContextAttributes = new HashMap<>();
|
||||
private VfsProjectSnapshot snapshot;
|
||||
|
||||
FilesystemProjectDocumentVfs(final VfsProjectContext projectContext) {
|
||||
FilesystemVfsProjectDocument(final VfsProjectContext projectContext) {
|
||||
this.projectContext = Objects.requireNonNull(projectContext, "projectContext");
|
||||
this.snapshot = buildSnapshot();
|
||||
}
|
||||
@@ -73,7 +67,7 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
|
||||
final var supportedDocument = requireSupportedDocument(path);
|
||||
return openSupportedDocument(supportedDocument);
|
||||
} catch (UnsupportedDocumentException unsupportedDocumentException) {
|
||||
return new VfsUnsupportedDocument(
|
||||
return new VfsDocumentOpenResult.VfsUnsupportedDocument(
|
||||
unsupportedDocumentException.path(),
|
||||
unsupportedDocumentException.reason());
|
||||
}
|
||||
@@ -92,7 +86,7 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
|
||||
}
|
||||
|
||||
@Override
|
||||
public VfsTextDocument updateDocument(final Path path, final String content) {
|
||||
public VfsDocumentOpenResult.VfsTextDocument updateDocument(final Path path, final String content) {
|
||||
final var supportedDocument = requireSupportedDocument(path);
|
||||
if (supportedDocument.accessMode() != VfsDocumentAccessMode.EDITABLE) {
|
||||
throw new IllegalStateException("Document is hard read-only: " + supportedDocument.path());
|
||||
@@ -194,11 +188,11 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
|
||||
return toVfsTextDocument(supportedDocument, false, accessContext);
|
||||
}
|
||||
|
||||
private VfsTextDocument toVfsTextDocument(
|
||||
private VfsDocumentOpenResult.VfsTextDocument toVfsTextDocument(
|
||||
final SupportedDocument supportedDocument,
|
||||
final boolean dirty,
|
||||
final VfsDocumentAccessContext accessContext) {
|
||||
return new VfsTextDocument(
|
||||
return new VfsDocumentOpenResult.VfsTextDocument(
|
||||
supportedDocument.path(),
|
||||
supportedDocument.documentName(),
|
||||
supportedDocument.typeId(),
|
||||
+26
-29
@@ -2,18 +2,15 @@ package p.studio.vfs;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.vfs.messages.*;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class FilesystemProjectDocumentVfsTest {
|
||||
final class FilesystemVfsProjectDocumentTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@@ -24,7 +21,7 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
Files.writeString(tempDir.resolve(".env"), "TOKEN=1\n");
|
||||
Files.writeString(tempDir.resolve("README.md"), "# project\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsProjectSnapshot snapshot = vfs.snapshot();
|
||||
|
||||
@@ -40,10 +37,10 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final Path file = tempDir.resolve("main.pbs");
|
||||
Files.writeString(file, "fn main(): void\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsDocumentOpenResult result = vfs.openDocument(file);
|
||||
final VfsTextDocument document = assertInstanceOf(VfsTextDocument.class, result);
|
||||
final var document = assertInstanceOf(VfsDocumentOpenResult.VfsTextDocument.class, result);
|
||||
|
||||
assertEquals("main.pbs", document.documentName());
|
||||
assertEquals("pbs", document.typeId());
|
||||
@@ -58,10 +55,10 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final Path file = tempDir.resolve("prometeu.json");
|
||||
Files.writeString(file, "{\n \"name\": \"Example\"\n}\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsDocumentOpenResult result = vfs.openDocument(file);
|
||||
final VfsTextDocument document = assertInstanceOf(VfsTextDocument.class, result);
|
||||
final var document = assertInstanceOf(VfsDocumentOpenResult.VfsTextDocument.class, result);
|
||||
|
||||
assertEquals(VfsDocumentTypeIds.JSON, document.typeId());
|
||||
assertEquals(VfsDocumentAccessMode.EDITABLE, document.accessContext().accessMode());
|
||||
@@ -72,10 +69,10 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final Path file = tempDir.resolve("events.ndjson");
|
||||
Files.writeString(file, "{\"event\":\"start\"}\n{\"event\":\"done\"}\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsDocumentOpenResult result = vfs.openDocument(file);
|
||||
final VfsTextDocument document = assertInstanceOf(VfsTextDocument.class, result);
|
||||
final var document = assertInstanceOf(VfsDocumentOpenResult.VfsTextDocument.class, result);
|
||||
|
||||
assertEquals(VfsDocumentTypeIds.NDJSON, document.typeId());
|
||||
}
|
||||
@@ -85,10 +82,10 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final Path file = tempDir.resolve("script.sh");
|
||||
Files.writeString(file, "#!/usr/bin/env bash\nprintf \"hi\"\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsDocumentOpenResult result = vfs.openDocument(file);
|
||||
final VfsTextDocument document = assertInstanceOf(VfsTextDocument.class, result);
|
||||
final var document = assertInstanceOf(VfsDocumentOpenResult.VfsTextDocument.class, result);
|
||||
|
||||
assertEquals(VfsDocumentTypeIds.BASH, document.typeId());
|
||||
assertEquals(VfsDocumentAccessMode.EDITABLE, document.accessContext().accessMode());
|
||||
@@ -99,7 +96,7 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final Path file = tempDir.resolve("main.pbs");
|
||||
Files.writeString(file, "fn main(): void\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> vfs.updateDocument(file, "fn main(): int\n"));
|
||||
}
|
||||
@@ -109,20 +106,20 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final Path file = tempDir.resolve("notes.txt");
|
||||
Files.writeString(file, "alpha\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsTextDocument updated = vfs.updateDocument(file, "beta\n");
|
||||
final var updated = vfs.updateDocument(file, "beta\n");
|
||||
|
||||
assertTrue(updated.dirty());
|
||||
assertEquals("beta\n", updated.content());
|
||||
assertEquals("alpha\n", Files.readString(file));
|
||||
assertEquals("beta\n", assertInstanceOf(VfsTextDocument.class, vfs.openDocument(file)).content());
|
||||
assertEquals("beta\n", assertInstanceOf(VfsDocumentOpenResult.VfsTextDocument.class, vfs.openDocument(file)).content());
|
||||
|
||||
final VfsDocumentSaveResult saveResult = vfs.saveDocument(file);
|
||||
|
||||
assertEquals(VfsDocumentSaveStatus.SAVED, saveResult.status());
|
||||
assertEquals("beta\n", Files.readString(file));
|
||||
assertFalse(assertInstanceOf(VfsTextDocument.class, vfs.openDocument(file)).dirty());
|
||||
assertFalse(assertInstanceOf(VfsDocumentOpenResult.VfsTextDocument.class, vfs.openDocument(file)).dirty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -132,7 +129,7 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
Files.writeString(editable, "alpha\n");
|
||||
Files.writeString(frontend, "fn main(): void\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
vfs.updateDocument(editable, "beta\n");
|
||||
|
||||
final var results = vfs.saveAllDocuments();
|
||||
@@ -149,7 +146,7 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final Path file = tempDir.resolve("prometeu.json");
|
||||
Files.writeString(file, "{\n \"name\": \"Example\"\n}\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsDocumentAccessContext initialContext = vfs.accessContext(file);
|
||||
final VfsDocumentAccessContext updatedContext = vfs.updateAccessContext(file, Map.of("statusBar", "future-toggle"));
|
||||
@@ -166,10 +163,10 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final Path file = tempDir.resolve("sprite.bin");
|
||||
Files.write(file, new byte[]{0x01, 0x00, 0x02});
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsDocumentOpenResult result = vfs.openDocument(file);
|
||||
final VfsUnsupportedDocument unsupported = assertInstanceOf(VfsUnsupportedDocument.class, result);
|
||||
final var unsupported = assertInstanceOf(VfsDocumentOpenResult.VfsUnsupportedDocument.class, result);
|
||||
|
||||
assertEquals(VfsUnsupportedReason.BINARY_CONTENT, unsupported.reason());
|
||||
}
|
||||
@@ -179,10 +176,10 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final Path outsideFile = tempDir.getParent().resolve("outside.pbs");
|
||||
Files.writeString(outsideFile, "fn stray(): void\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsDocumentOpenResult result = vfs.openDocument(outsideFile);
|
||||
final VfsUnsupportedDocument unsupported = assertInstanceOf(VfsUnsupportedDocument.class, result);
|
||||
final var unsupported = assertInstanceOf(VfsDocumentOpenResult.VfsUnsupportedDocument.class, result);
|
||||
|
||||
assertEquals(VfsUnsupportedReason.OUTSIDE_PROJECT, unsupported.reason());
|
||||
}
|
||||
@@ -193,7 +190,7 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
Files.createDirectories(tempDir.resolve("assets"));
|
||||
Files.writeString(src.resolve("main.pbs"), "fn main(): void\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
Files.writeString(src.resolve("later.pbs"), "fn later(): void\n");
|
||||
|
||||
final VfsProjectSnapshot refreshedSnapshot = vfs.refresh(new VfsRefreshRequest(src));
|
||||
@@ -205,7 +202,7 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
assertEquals("main.pbs", srcNode.children().get(1).displayName());
|
||||
}
|
||||
|
||||
private p.studio.vfs.VfsProjectContext projectContext() {
|
||||
return new p.studio.vfs.VfsProjectContext("Example", "pbs", tempDir);
|
||||
private VfsProjectContext projectContext() {
|
||||
return new VfsProjectContext("Example", "pbs", tempDir);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user