feat(pbs): resolve editorial chains and expose read-only external sources
This commit is contained in:
+10
-1
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -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();
|
||||
|
||||
+31
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user