feat(pbs): resolve editorial chains and expose read-only external sources
Intrepid/Prometeu/Studio/pipeline/head Build started...
Test / Build skipped: 16, passed: 838
Intrepid/Prometeu/Studio/pipeline/pr-master This commit looks good

This commit is contained in:
2026-10-07 09:00:31 +01:00
parent d0737df0ac
commit 967b9ec598
38 changed files with 1856 additions and 42 deletions
@@ -6,7 +6,12 @@ import java.util.Objects;
public record FrontendDefinitionLocation(
Path path,
int startOffset,
int endOffset) {
int endOffset,
FrontendSourceDocument source) {
public FrontendDefinitionLocation(Path path, int startOffset, int endOffset) {
this(path, startOffset, endOffset, null);
}
public FrontendDefinitionLocation {
path = Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
@@ -16,5 +21,9 @@ public record FrontendDefinitionLocation(
if (endOffset < startOffset) {
throw new IllegalArgumentException("endOffset must not be before startOffset");
}
if (source != null && (!path.equals(source.path())
|| endOffset > source.text().getBytes(java.nio.charset.StandardCharsets.UTF_8).length)) {
throw new IllegalArgumentException("location must fit its source revision");
}
}
}
@@ -11,6 +11,13 @@ import java.util.Optional;
* Optional frontend-owned editor services.
*/
public interface FrontendLanguageService {
default boolean supportsExternalSources() { return false; }
default Optional<FrontendSourceDocument> sourceDocument(
FrontendDocumentRequest request, FrontendEditorialContext context, java.nio.file.Path path) {
return Optional.empty();
}
default Optional<FrontendEditorialContext> prepareEditorialContext(
final FrontendPhaseContext phaseContext) {
return Optional.empty();
@@ -0,0 +1,31 @@
package p.studio.compiler.services;
import java.nio.file.Path;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import java.util.Objects;
/** A compiler-known source and the semantic context for exactly that content. */
public record FrontendSourceDocument(
Path path, String text, String origin, String name, String languageId,
boolean readOnly, FrontendEditorialContext context) {
public FrontendSourceDocument {
path = Objects.requireNonNull(path).toAbsolutePath().normalize();
Objects.requireNonNull(text);
Objects.requireNonNull(origin);
Objects.requireNonNull(name);
Objects.requireNonNull(languageId);
Objects.requireNonNull(context);
}
public String revision() {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256")
.digest(text.getBytes(StandardCharsets.UTF_8)));
} catch (NoSuchAlgorithmException impossible) {
throw new IllegalStateException(impossible);
}
}
}
@@ -0,0 +1,21 @@
package p.studio.compiler.services;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class FrontendSourceDocumentTest {
@Test void locationCannotMixPathsOrExceedTheExactUtf8SourceRevision() {
final Path path = Path.of("/virtual/stdlib/sdk/main.pbs");
final String text = "// á 😀\r\nfn f() {}\r\n";
final var context = new FrontendEditorialContext(null, null, null);
final var source = new FrontendSourceDocument(path, text, "bundled", "sdk/main.pbs", "pbs", true, context);
final int bytes = text.getBytes(StandardCharsets.UTF_8).length;
assertDoesNotThrow(() -> new FrontendDefinitionLocation(path, 0, bytes, source));
assertThrows(IllegalArgumentException.class, () -> new FrontendDefinitionLocation(path, 0, bytes + 1, source));
assertThrows(IllegalArgumentException.class, () -> new FrontendDefinitionLocation(Path.of("/other.pbs"), 0, 1, source));
assertNotEquals(source.revision(), new FrontendSourceDocument(path, text.replace("\r\n", "\n"),
"bundled", "sdk/main.pbs", "pbs", true, context).revision());
}
}