prometeu-vsf

This commit is contained in:
2026-03-31 08:22:28 +01:00
parent 983cbd5fe8
commit a13d0a54c2
19 changed files with 29 additions and 2 deletions
@@ -0,0 +1,209 @@
package p.studio.vfs;
import p.studio.compiler.FrontendRegistryService;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
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.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
private final VfsProjectContext projectContext;
private VfsProjectSnapshot snapshot;
FilesystemProjectDocumentVfs(final VfsProjectContext projectContext) {
this.projectContext = Objects.requireNonNull(projectContext, "projectContext");
this.snapshot = buildSnapshot();
}
@Override
public VfsProjectContext projectContext() {
return projectContext;
}
@Override
public VfsProjectSnapshot snapshot() {
return snapshot;
}
@Override
public VfsProjectSnapshot refresh() {
snapshot = buildSnapshot();
return snapshot;
}
@Override
public VfsProjectSnapshot refresh(final VfsRefreshRequest request) {
final Path refreshTarget = refreshRootFor(Objects.requireNonNull(request, "request").targetPath());
if (refreshTarget.equals(projectContext.rootPath())) {
return refresh();
}
final Set<Path> taggedRoots = taggedSourceRoots();
final String displayName = refreshTarget.getFileName() == null
? projectContext.projectName()
: refreshTarget.getFileName().toString();
final VfsProjectNode refreshedNode = buildNode(refreshTarget, displayName, taggedRoots);
snapshot = new VfsProjectSnapshot(
projectContext.rootPath(),
replaceNode(snapshot.rootNode(), refreshedNode));
return snapshot;
}
@Override
public VfsDocumentOpenResult openDocument(final Path path) {
final Path normalizedPath = normalize(path);
if (!normalizedPath.startsWith(projectContext.rootPath())) {
return new VfsUnsupportedDocument(normalizedPath, VfsUnsupportedReason.OUTSIDE_PROJECT);
}
if (!Files.exists(normalizedPath)) {
return new VfsUnsupportedDocument(normalizedPath, VfsUnsupportedReason.NOT_FOUND);
}
if (!Files.isRegularFile(normalizedPath)) {
return new VfsUnsupportedDocument(normalizedPath, VfsUnsupportedReason.NOT_A_FILE);
}
final byte[] bytes;
try {
bytes = Files.readAllBytes(normalizedPath);
} catch (IOException ioException) {
throw new UncheckedIOException(ioException);
}
if (containsNulByte(bytes)) {
return new VfsUnsupportedDocument(normalizedPath, VfsUnsupportedReason.BINARY_CONTENT);
}
final String content;
try {
content = StandardCharsets.UTF_8.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT)
.decode(ByteBuffer.wrap(bytes))
.toString();
} catch (CharacterCodingException codingException) {
return new VfsUnsupportedDocument(normalizedPath, VfsUnsupportedReason.INVALID_UTF8);
}
return new VfsTextDocument(
normalizedPath,
normalizedPath.getFileName().toString(),
content,
lineSeparator(content));
}
private VfsProjectSnapshot buildSnapshot() {
return new VfsProjectSnapshot(
projectContext.rootPath(),
buildNode(projectContext.rootPath(), projectContext.projectName(), taggedSourceRoots()));
}
private Set<Path> taggedSourceRoots() {
return FrontendRegistryService.getFrontendSpec(projectContext.languageId())
.stream()
.flatMap(frontendSpec -> frontendSpec.getSourceRoots().stream())
.map(projectContext.rootPath()::resolve)
.map(Path::toAbsolutePath)
.map(Path::normalize)
.collect(Collectors.toSet());
}
private VfsProjectNode buildNode(
final Path path,
final String displayName,
final Set<Path> taggedRoots) {
if (!Files.isDirectory(path)) {
return new VfsProjectNode(path, displayName, false, taggedRoots.contains(path), List.of());
}
final List<VfsProjectNode> children;
try (Stream<Path> stream = Files.list(path)) {
children = stream
.sorted(nodeComparator())
.map(child -> buildNode(child, child.getFileName().toString(), taggedRoots))
.toList();
} catch (IOException ioException) {
throw new UncheckedIOException(ioException);
}
return new VfsProjectNode(path, displayName, true, taggedRoots.contains(path), children);
}
private VfsProjectNode replaceNode(final VfsProjectNode currentNode, final VfsProjectNode replacementNode) {
if (currentNode.path().equals(replacementNode.path())) {
return replacementNode;
}
if (!replacementNode.path().startsWith(currentNode.path())) {
return currentNode;
}
if (!currentNode.directory()) {
return currentNode;
}
final List<VfsProjectNode> replacedChildren = currentNode.children()
.stream()
.map(child -> replaceNode(child, replacementNode))
.sorted(projectNodeComparator())
.toList();
return new VfsProjectNode(
currentNode.path(),
currentNode.displayName(),
true,
currentNode.taggedSourceRoot(),
replacedChildren);
}
private Path refreshRootFor(final Path targetPath) {
final Path normalizedTarget = normalize(targetPath);
if (!normalizedTarget.startsWith(projectContext.rootPath())) {
throw new IllegalArgumentException("Refresh target must stay within the project root.");
}
if (Files.exists(normalizedTarget)) {
return normalizedTarget;
}
final Path parent = normalizedTarget.getParent();
if (parent == null || !parent.startsWith(projectContext.rootPath())) {
return projectContext.rootPath();
}
return parent;
}
private Comparator<Path> nodeComparator() {
return Comparator
.comparing((Path path) -> !Files.isDirectory(path))
.thenComparing(path -> path.getFileName().toString(), String.CASE_INSENSITIVE_ORDER);
}
private Comparator<VfsProjectNode> projectNodeComparator() {
return Comparator
.comparing((VfsProjectNode node) -> !node.directory())
.thenComparing(VfsProjectNode::displayName, String.CASE_INSENSITIVE_ORDER);
}
private Path normalize(final Path path) {
return Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
}
private boolean containsNulByte(final byte[] bytes) {
for (final byte value : bytes) {
if (value == 0) {
return true;
}
}
return false;
}
private String lineSeparator(final String content) {
return content.contains("\r\n") ? "CRLF" : "LF";
}
}
@@ -0,0 +1,10 @@
package p.studio.vfs;
import java.util.Objects;
public final class FilesystemProjectDocumentVfsFactory implements ProjectDocumentVfsFactory {
@Override
public ProjectDocumentVfs open(final VfsProjectContext projectContext) {
return new FilesystemProjectDocumentVfs(Objects.requireNonNull(projectContext, "projectContext"));
}
}