implements PLN-0020

This commit is contained in:
2026-03-31 16:45:47 +01:00
parent 2b7aafaa5c
commit f9a47bbdbf
11 changed files with 382 additions and 27 deletions
@@ -12,8 +12,11 @@ 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.stream.Collectors;
@@ -21,6 +24,8 @@ import java.util.stream.Stream;
final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
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) {
@@ -64,26 +69,90 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
@Override
public VfsDocumentOpenResult openDocument(final Path path) {
try {
final var supportedDocument = requireSupportedDocument(path);
return openSupportedDocument(supportedDocument);
} catch (UnsupportedDocumentException unsupportedDocumentException) {
return new VfsUnsupportedDocument(
unsupportedDocumentException.path(),
unsupportedDocumentException.reason());
}
}
@Override
public VfsDocumentAccessContext accessContext(final Path path) {
return accessContextFor(requireSupportedDocument(path));
}
@Override
public VfsDocumentAccessContext updateAccessContext(final Path path, final Map<String, String> attributes) {
final var supportedDocument = requireSupportedDocument(path);
accessContextAttributes.put(supportedDocument.path(), Map.copyOf(Objects.requireNonNull(attributes, "attributes")));
return accessContextFor(supportedDocument);
}
@Override
public 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());
}
final var normalizedPath = supportedDocument.path();
final var snapshot = editableSnapshots.computeIfAbsent(
normalizedPath,
ignored -> new EditableDocumentSnapshot(
supportedDocument.content(),
supportedDocument.content(),
supportedDocument.lineSeparator()));
snapshot.content = Objects.requireNonNull(content, "content");
snapshot.lineSeparator = lineSeparator(snapshot.content);
return toVfsTextDocument(supportedDocument.withContent(snapshot.content, snapshot.lineSeparator), snapshot.isDirty(), accessContextFor(supportedDocument));
}
@Override
public VfsDocumentSaveResult saveDocument(final Path path) {
final var supportedDocument = requireSupportedDocument(path);
final var accessContext = accessContextFor(supportedDocument);
if (accessContext.accessMode() != VfsDocumentAccessMode.EDITABLE) {
return new VfsDocumentSaveResult(supportedDocument.path(), supportedDocument.typeId(), VfsDocumentSaveStatus.READ_ONLY);
}
final var snapshot = editableSnapshots.computeIfAbsent(
supportedDocument.path(),
ignored -> new EditableDocumentSnapshot(
supportedDocument.content(),
supportedDocument.content(),
supportedDocument.lineSeparator()));
if (!snapshot.isDirty()) {
return new VfsDocumentSaveResult(supportedDocument.path(), supportedDocument.typeId(), VfsDocumentSaveStatus.NO_CHANGES);
}
writeUtf8(supportedDocument.path(), snapshot.content);
snapshot.savedContent = snapshot.content;
snapshot.lineSeparator = lineSeparator(snapshot.content);
return new VfsDocumentSaveResult(supportedDocument.path(), supportedDocument.typeId(), VfsDocumentSaveStatus.SAVED);
}
@Override
public List<VfsDocumentSaveResult> saveAllDocuments() {
return editableSnapshots.keySet().stream()
.map(this::saveDocument)
.toList();
}
private SupportedDocument requireSupportedDocument(final Path path) {
final Path normalizedPath = normalize(path);
if (!normalizedPath.startsWith(projectContext.rootPath())) {
return new VfsUnsupportedDocument(normalizedPath, VfsUnsupportedReason.OUTSIDE_PROJECT);
throw new UnsupportedDocumentException(normalizedPath, VfsUnsupportedReason.OUTSIDE_PROJECT);
}
if (!Files.exists(normalizedPath)) {
return new VfsUnsupportedDocument(normalizedPath, VfsUnsupportedReason.NOT_FOUND);
throw new UnsupportedDocumentException(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);
throw new UnsupportedDocumentException(normalizedPath, VfsUnsupportedReason.NOT_A_FILE);
}
final byte[] bytes = readAllBytes(normalizedPath);
if (containsNulByte(bytes)) {
return new VfsUnsupportedDocument(normalizedPath, VfsUnsupportedReason.BINARY_CONTENT);
throw new UnsupportedDocumentException(normalizedPath, VfsUnsupportedReason.BINARY_CONTENT);
}
final String content;
@@ -94,17 +163,51 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
.decode(ByteBuffer.wrap(bytes))
.toString();
} catch (CharacterCodingException codingException) {
return new VfsUnsupportedDocument(normalizedPath, VfsUnsupportedReason.INVALID_UTF8);
throw new UnsupportedDocumentException(normalizedPath, VfsUnsupportedReason.INVALID_UTF8, codingException);
}
return new VfsTextDocument(
final DocumentKind documentKind = documentKind(normalizedPath, content);
return new SupportedDocument(
normalizedPath,
normalizedPath.getFileName().toString(),
documentTypeId(normalizedPath, content),
documentKind.typeId(),
documentKind.frontendDocument(),
documentKind.accessMode(),
content,
lineSeparator(content));
}
private VfsDocumentOpenResult openSupportedDocument(final SupportedDocument supportedDocument) {
final var accessContext = accessContextFor(supportedDocument);
if (supportedDocument.accessMode() == VfsDocumentAccessMode.EDITABLE) {
final var snapshot = editableSnapshots.computeIfAbsent(
supportedDocument.path(),
ignored -> new EditableDocumentSnapshot(
supportedDocument.content(),
supportedDocument.content(),
supportedDocument.lineSeparator()));
return toVfsTextDocument(
supportedDocument.withContent(snapshot.content, snapshot.lineSeparator),
snapshot.isDirty(),
accessContext);
}
return toVfsTextDocument(supportedDocument, false, accessContext);
}
private VfsTextDocument toVfsTextDocument(
final SupportedDocument supportedDocument,
final boolean dirty,
final VfsDocumentAccessContext accessContext) {
return new VfsTextDocument(
supportedDocument.path(),
supportedDocument.documentName(),
supportedDocument.typeId(),
supportedDocument.content(),
supportedDocument.lineSeparator(),
dirty,
accessContext);
}
private VfsProjectSnapshot buildSnapshot() {
return new VfsProjectSnapshot(
projectContext.rootPath(),
@@ -121,18 +224,21 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
.collect(Collectors.toSet());
}
private String documentTypeId(final Path path, final String content) {
private DocumentKind documentKind(final Path path, final String content) {
final String extension = extensionOf(path);
if ("json".equals(extension) || "ndjson".equals(extension)) {
return VfsDocumentTypeIds.JSON;
if ("json".equals(extension)) {
return new DocumentKind(VfsDocumentTypeIds.JSON, false, VfsDocumentAccessMode.EDITABLE);
}
if ("ndjson".equals(extension)) {
return new DocumentKind(VfsDocumentTypeIds.NDJSON, false, VfsDocumentAccessMode.EDITABLE);
}
if (isBashDocument(path, extension, content)) {
return VfsDocumentTypeIds.BASH;
return new DocumentKind(VfsDocumentTypeIds.BASH, false, VfsDocumentAccessMode.EDITABLE);
}
if (isFrontendSourceDocument(extension)) {
return projectContext.languageId();
return new DocumentKind(projectContext.languageId(), true, VfsDocumentAccessMode.READ_ONLY);
}
return VfsDocumentTypeIds.TEXT;
return new DocumentKind(VfsDocumentTypeIds.TEXT, false, VfsDocumentAccessMode.EDITABLE);
}
private VfsProjectNode buildNode(
@@ -256,4 +362,104 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
private String lineSeparator(final String content) {
return content.contains("\r\n") ? "CRLF" : "LF";
}
private byte[] readAllBytes(final Path path) {
try {
return Files.readAllBytes(path);
} catch (IOException ioException) {
throw new UncheckedIOException(ioException);
}
}
private void writeUtf8(final Path path, final String content) {
try {
Files.writeString(path, content, StandardCharsets.UTF_8);
} catch (IOException ioException) {
throw new UncheckedIOException(ioException);
}
}
private VfsDocumentAccessContext accessContextFor(final SupportedDocument supportedDocument) {
final var attributes = accessContextAttributes.computeIfAbsent(supportedDocument.path(), ignored -> Map.of());
return new VfsDocumentAccessContext(
supportedDocument.path(),
supportedDocument.typeId(),
supportedDocument.frontendDocument(),
supportedDocument.accessMode(),
attributes);
}
private record DocumentKind(
String typeId,
boolean frontendDocument,
VfsDocumentAccessMode accessMode) {
}
private record SupportedDocument(
Path path,
String documentName,
String typeId,
boolean frontendDocument,
VfsDocumentAccessMode accessMode,
String content,
String lineSeparator) {
private SupportedDocument withContent(final String updatedContent, final String updatedLineSeparator) {
return new SupportedDocument(
path,
documentName,
typeId,
frontendDocument,
accessMode,
updatedContent,
updatedLineSeparator);
}
}
private static final class EditableDocumentSnapshot {
private String savedContent;
private String content;
private String lineSeparator;
private EditableDocumentSnapshot(
final String savedContent,
final String content,
final String lineSeparator) {
this.savedContent = savedContent;
this.content = content;
this.lineSeparator = lineSeparator;
}
private boolean isDirty() {
return !Objects.equals(savedContent, content);
}
}
private static final class UnsupportedDocumentException extends IllegalArgumentException {
private final Path path;
private final VfsUnsupportedReason reason;
private UnsupportedDocumentException(final Path path, final VfsUnsupportedReason reason) {
super(reason.name() + ": " + path);
this.path = path;
this.reason = reason;
}
private UnsupportedDocumentException(
final Path path,
final VfsUnsupportedReason reason,
final Throwable cause) {
super(reason.name() + ": " + path, cause);
this.path = path;
this.reason = reason;
}
private Path path() {
return path;
}
private VfsUnsupportedReason reason() {
return reason;
}
}
}