prometeu-vsf
This commit is contained in:
+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) {
|
||||
|
||||
Reference in New Issue
Block a user