prometeu-vsf
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package p.studio.vfs;
|
||||
|
||||
public final class VfsDocumentTypeIds {
|
||||
public static final String TEXT = "text";
|
||||
public static final String JSON = "json";
|
||||
public static final String BASH = "bash";
|
||||
|
||||
private VfsDocumentTypeIds() {
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,14 @@ import java.util.Objects;
|
||||
public record VfsTextDocument(
|
||||
Path path,
|
||||
String documentName,
|
||||
String typeId,
|
||||
String content,
|
||||
String lineSeparator) 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");
|
||||
}
|
||||
|
||||
+50
@@ -1,6 +1,7 @@
|
||||
package p.studio.vfs;
|
||||
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.compiler.models.FrontendSpec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
@@ -12,6 +13,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -98,6 +100,7 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
|
||||
return new VfsTextDocument(
|
||||
normalizedPath,
|
||||
normalizedPath.getFileName().toString(),
|
||||
documentTypeId(normalizedPath, content),
|
||||
content,
|
||||
lineSeparator(content));
|
||||
}
|
||||
@@ -118,6 +121,20 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private String documentTypeId(final Path path, final String content) {
|
||||
final String extension = extensionOf(path);
|
||||
if ("json".equals(extension) || "ndjson".equals(extension)) {
|
||||
return VfsDocumentTypeIds.JSON;
|
||||
}
|
||||
if (isBashDocument(path, extension, content)) {
|
||||
return VfsDocumentTypeIds.BASH;
|
||||
}
|
||||
if (isFrontendSourceDocument(extension)) {
|
||||
return projectContext.languageId();
|
||||
}
|
||||
return VfsDocumentTypeIds.TEXT;
|
||||
}
|
||||
|
||||
private VfsProjectNode buildNode(
|
||||
final Path path,
|
||||
final String displayName,
|
||||
@@ -194,6 +211,39 @@ final class FilesystemProjectDocumentVfs implements ProjectDocumentVfs {
|
||||
return Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
|
||||
}
|
||||
|
||||
private boolean isFrontendSourceDocument(final String extension) {
|
||||
if (extension.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
return FrontendRegistryService.getFrontendSpec(projectContext.languageId())
|
||||
.map(FrontendSpec::getAllowedExtensions)
|
||||
.stream()
|
||||
.flatMap(allowedExtensions -> allowedExtensions.stream())
|
||||
.anyMatch(allowedExtension -> allowedExtension.equalsIgnoreCase(extension));
|
||||
}
|
||||
|
||||
private boolean isBashDocument(final Path path, final String extension, final String content) {
|
||||
if (Set.of("sh", "bash", "bashrc", "bash_profile").contains(extension)) {
|
||||
return true;
|
||||
}
|
||||
final String fileName = path.getFileName().toString().toLowerCase(Locale.ROOT);
|
||||
if (".bashrc".equals(fileName) || ".bash_profile".equals(fileName)) {
|
||||
return true;
|
||||
}
|
||||
final int firstLineBreak = content.indexOf('\n');
|
||||
final String firstLine = firstLineBreak >= 0 ? content.substring(0, firstLineBreak) : content;
|
||||
return firstLine.startsWith("#!") && (firstLine.contains("bash") || firstLine.contains("/sh"));
|
||||
}
|
||||
|
||||
private String extensionOf(final Path path) {
|
||||
final String fileName = path.getFileName().toString();
|
||||
final int dot = fileName.lastIndexOf('.');
|
||||
if (dot < 0 || dot == fileName.length() - 1) {
|
||||
return "";
|
||||
}
|
||||
return fileName.substring(dot + 1).toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private boolean containsNulByte(final byte[] bytes) {
|
||||
for (final byte value : bytes) {
|
||||
if (value == 0) {
|
||||
|
||||
+40
@@ -43,10 +43,50 @@ final class FilesystemProjectDocumentVfsTest {
|
||||
final VfsTextDocument document = assertInstanceOf(VfsTextDocument.class, result);
|
||||
|
||||
assertEquals("main.pbs", document.documentName());
|
||||
assertEquals("pbs", document.typeId());
|
||||
assertEquals("LF", document.lineSeparator());
|
||||
assertTrue(document.content().contains("fn main()"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void openDocumentClassifiesJsonFilesWithJsonTypeIdentifier() throws Exception {
|
||||
final Path file = tempDir.resolve("prometeu.json");
|
||||
Files.writeString(file, "{\n \"name\": \"Example\"\n}\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsDocumentOpenResult result = vfs.openDocument(file);
|
||||
final VfsTextDocument document = assertInstanceOf(VfsTextDocument.class, result);
|
||||
|
||||
assertEquals(VfsDocumentTypeIds.JSON, document.typeId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void openDocumentClassifiesNdjsonFilesWithJsonTypeIdentifier() throws Exception {
|
||||
final Path file = tempDir.resolve("events.ndjson");
|
||||
Files.writeString(file, "{\"event\":\"start\"}\n{\"event\":\"done\"}\n");
|
||||
|
||||
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext());
|
||||
|
||||
final VfsDocumentOpenResult result = vfs.openDocument(file);
|
||||
final VfsTextDocument document = assertInstanceOf(VfsTextDocument.class, result);
|
||||
|
||||
assertEquals(VfsDocumentTypeIds.JSON, document.typeId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void openDocumentClassifiesBashScriptsWithBashTypeIdentifier() throws Exception {
|
||||
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 VfsDocumentOpenResult result = vfs.openDocument(file);
|
||||
final VfsTextDocument document = assertInstanceOf(VfsTextDocument.class, result);
|
||||
|
||||
assertEquals(VfsDocumentTypeIds.BASH, document.typeId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void openDocumentRejectsBinaryLikeFiles() throws Exception {
|
||||
final Path file = tempDir.resolve("sprite.bin");
|
||||
|
||||
Reference in New Issue
Block a user