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
@@ -32,6 +32,7 @@ import p.studio.compiler.services.FrontendDocumentLink;
import p.studio.compiler.services.FrontendFoldingRange;
import p.studio.compiler.services.FrontendSelectionRange;
import p.studio.compiler.services.FrontendDefinitionLocation;
import p.studio.compiler.services.FrontendSourceDocument;
import p.studio.compiler.services.FrontendDocumentRequest;
import p.studio.compiler.services.FrontendDocumentSymbol;
import p.studio.compiler.services.FrontendRenameEdit;
@@ -63,6 +64,38 @@ import java.util.Objects;
import java.util.Optional;
public final class PBSFrontendLanguageService implements FrontendLanguageService {
@Override
public boolean supportsExternalSources() { return true; }
@Override
public Optional<FrontendSourceDocument> sourceDocument(
final FrontendDocumentRequest request,
final FrontendEditorialContext context,
final Path path) {
if (!(context.projectSurface() instanceof PbsSemanticReadSurface surface)
|| !(context.sourceFiles() instanceof FileTableReader files)) return Optional.empty();
FrontendSourceDocument projectSource = null;
for (final var entry : surface.astByFile().entrySet()) {
final var handle = files.get(entry.getKey());
if (handle == null || !handle.getCanonPath().toAbsolutePath().normalize().equals(path.toAbsolutePath().normalize())) continue;
final var text = handle.readUtf8();
if (text.isEmpty()) return Optional.empty();
final boolean bundled = surface.sourceKindByFile().get(entry.getKey()) == SourceKind.SDK_INTERFACE;
final Path owner = surface.ownerRootByFile().get(entry.getKey());
final boolean external = bundled || owner == null || !owner.toAbsolutePath().normalize().equals(request.projectRoot().toAbsolutePath().normalize());
final var source = new FrontendSourceDocument(path, text.orElseThrow(),
bundled ? "bundled" : external ? "dependency" : "project",
handle.getRelativePath().toString(), "pbs", external,
new FrontendEditorialContext(entry.getValue(),
surface.supplementalTopDeclsByFile().getOrDefault(entry.getKey(), ReadOnlyList.empty()),
context.semanticContext(), files, surface));
// A dependency may physically overlap a project source root. External
// ownership wins even if another file-table entry treats it as local.
if (external) return Optional.of(source);
projectSource = source;
}
return Optional.ofNullable(projectSource);
}
private final PbsEditorialSupportService editorialSupportService;
private final PBSSemanticTokenProvider semanticTokenProvider;
@@ -257,7 +290,12 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
typedSupplementalTopDecls,
feSurfaceContext,
offset)) {
toFrontendLocation(location, fileTable).ifPresent(locations::add);
final var handle = fileTable.get(location.span().getFileId());
if (handle != null) {
sourceDocument(request, editorialContext, handle.getCanonPath()).ifPresent(source ->
locations.add(new FrontendDefinitionLocation(source.path(),
toOffset(location.span().getStart()), toOffset(location.span().getEnd()), source)));
}
}
return List.copyOf(locations);
}
@@ -401,14 +439,16 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
}
final LinkedHashMap<String, FrontendRenameEdit> edits = new LinkedHashMap<>();
for (final PbsEditorialLocation site : planned.sites()) {
toFrontendLocation(site, fileTable).ifPresent(location ->
edits.putIfAbsent(
final var mapped = toFrontendLocation(site, fileTable);
if (mapped.isEmpty()) return FrontendRenameResult.refused("rename requires a non-editable source");
final var location = mapped.orElseThrow();
edits.putIfAbsent(
frontendLocationKey(location),
new FrontendRenameEdit(
location.path(),
location.startOffset(),
location.endOffset(),
newName)));
newName));
}
if (edits.isEmpty()) {
return FrontendRenameResult.refused("not renameable");
@@ -19,6 +19,7 @@ public final class PbsParser {
private final PbsTokenCursor cursor;
private final PbsTopLevelParser topLevelParser;
private final PbsBlockParser blockParser;
private final PbsExprParser expressionParser;
private PbsParser(
final ReadOnlyList<PbsToken> tokens,
@@ -29,6 +30,7 @@ public final class PbsParser {
this.cursor = context.cursor();
final var exprParser = new PbsExprParser(context, this::parseExpressionSurfaceBlock);
this.expressionParser = exprParser;
final var attributeParser = new PbsAttributeParser(context, this::isTopLevelRestartToken);
final var typeParser = new PbsTypeParser(context);
@@ -64,6 +66,16 @@ public final class PbsParser {
return topLevelParser.parseFile();
}
/** Parses a complete expression fragment using the same grammar as a source file. */
public static PbsAst.Expression parseExpression(
final ReadOnlyList<PbsToken> tokens,
final FileId fileId,
final p.studio.compiler.source.diagnostics.DiagnosticSink diagnostics) {
final var parser = new PbsParser(tokens, fileId, diagnostics, ParseMode.ORDINARY);
final var expression = parser.expressionParser.parseExpression();
return parser.cursor.isAtEnd() && !diagnostics.hasErrors() ? expression : null;
}
private PbsAst.Block parseExpressionSurfaceBlock(final String message) {
return blockParser.parseBlock(message);
}
@@ -5,6 +5,7 @@ import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.lexer.PbsToken;
import p.studio.compiler.pbs.lexer.PbsTokenKind;
import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.source.Span;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
@@ -1396,6 +1397,11 @@ public final class PbsEditorialSupportService {
private TypeView resolveReceiverTypeBeforeDot(
final int dotTokenIndex,
final ScopeState scopeState) {
final PbsAst.Expression receiver = receiverExpressionBefore(dotTokenIndex);
if (receiver != null) {
return analyzeExpressionType(receiver, scopeState.scope(), null,
scopeState.returnType(), scopeState.resultErrorName(), scopeState.receiverType());
}
final List<String> segments = resolveChainSegmentsEndingAt(dotTokenIndex - 1);
if (segments.isEmpty()) {
return TypeView.unknown();
@@ -1407,6 +1413,34 @@ public final class PbsEditorialSupportService {
return current;
}
private PbsAst.Expression receiverExpressionBefore(final int dotTokenIndex) {
// Locate only the fragment boundary here; grammar and overload selection
// remain in the existing PBS parser and flow expression analyzer.
int start = dotTokenIndex;
int depth = 0;
for (int index = dotTokenIndex - 1; index >= 0; index--) {
final var kind = token(index).kind();
if (kind == PbsTokenKind.COMMENT) { start = index; continue; }
if (kind == PbsTokenKind.RIGHT_PAREN) {
depth++;
} else if (kind == PbsTokenKind.LEFT_PAREN) {
if (depth == 0) break;
depth--;
} else if (depth == 0 && kind != PbsTokenKind.IDENTIFIER
&& kind != PbsTokenKind.DOT && kind != PbsTokenKind.THIS
&& kind != PbsTokenKind.NEW) {
break;
}
start = index;
}
if (start == dotTokenIndex || depth != 0) return null;
final var fragment = new ArrayList<PbsToken>();
for (int index = start; index < dotTokenIndex; index++) fragment.add(token(index));
final int end = token(dotTokenIndex).start();
fragment.add(new PbsToken(PbsTokenKind.EOF, "", end, end));
return PbsParser.parseExpression(ReadOnlyList.wrap(fragment), ast.span().getFileId(), DiagnosticSink.empty());
}
private TypeView resolveBaseChainType(
final String baseName,
final ScopeState scopeState) {
@@ -132,11 +132,16 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
final Map<FileId, ReadOnlyList<p.studio.compiler.pbs.ast.PbsAst.TopDecl>> supplementalTopDeclsByFile = new LinkedHashMap<>();
final Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile = new LinkedHashMap<>();
final Map<FileId, SourceKind> sourceKindByFile = new LinkedHashMap<>();
final Map<FileId, Path> ownerRootByFile = new LinkedHashMap<>();
for (final var entry : importedSemanticContexts.entrySet()) {
supplementalTopDeclsByFile.put(entry.getKey(), entry.getValue().supplementalTopDecls());
}
for (final var parsedSourceFile : assembly.parsedSourceFiles()) {
astByFile.put(parsedSourceFile.fileId(), parsedSourceFile.ast());
final var sourceHandle = ctx.fileTable.get(parsedSourceFile.fileId());
if (sourceHandle != null && parsedSourceFile.sourceKind() != SourceKind.SDK_INTERFACE) {
ownerRootByFile.put(parsedSourceFile.fileId(), ctx.projectTable.get(sourceHandle.getProjectId()).getRootPath());
}
sourceKindByFile.put(
parsedSourceFile.fileId(),
parsedSourceFile.sourceKind() == null ? SourceKind.PROJECT : parsedSourceFile.sourceKind());
@@ -158,6 +163,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
Map.copyOf(supplementalTopDeclsByFile),
Map.copyOf(inlineHintsByFile),
Map.copyOf(sourceKindByFile),
Map.copyOf(ownerRootByFile),
physicalModuleFiles,
moduleCompletionLabels(ctx, physicalModuleFiles));
}
@@ -225,6 +231,7 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
Map<FileId, ReadOnlyList<p.studio.compiler.pbs.ast.PbsAst.TopDecl>> supplementalTopDeclsByFile,
Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile,
Map<FileId, SourceKind> sourceKindByFile,
Map<FileId, Path> ownerRootByFile,
Map<ModuleReference, Path> physicalModuleFileByReference,
List<String> moduleCompletionLabels) {
}
@@ -16,6 +16,79 @@ import static org.junit.jupiter.api.Assertions.*;
final class PbsEditorialSupportServiceTest {
private final PbsEditorialSupportService service = new PbsEditorialSupportService();
@Test
void receiverMatrixHandlesNestedCallsVariablesParenthesesAndRecovery() {
final String declarations = """
declare struct Button() { fn down(value: int) -> bool { return true; } }
declare struct Pad() { fn left() -> Button { return new Button(); } }
fn pad(value: int) -> Pad { return new Pad(); }
fn number() -> int { return 1; }
""";
for (final String receiver : List.of("pad(number()).left()", "(pad(1).left())", "typed", "inferred")) {
for (final String suffix : List.of("down(1);", "down(", "")) {
final String source = declarations + """
fn main() -> void {
let typed: Button = new Button();
let inferred = pad(1).left();
""" + receiver + "." + suffix + "\n}\ndeclare ;";
final var ast = parseAllowingErrors(source);
final int member = source.lastIndexOf(receiver + ".") + receiver.length() + 1;
assertTrue(service.completion(source, ast, ReadOnlyList.empty(), member).stream()
.anyMatch(item -> item.label().equals("down")), receiver + "/" + suffix);
if (!suffix.isEmpty()) {
assertTrue(service.hover(source, ast, ReadOnlyList.empty(), member).isPresent(), receiver);
assertFalse(service.definition(source, ast, ReadOnlyList.empty(), member).isEmpty(), receiver);
assertTrue(service.signatureHelp(source, ast, ReadOnlyList.empty(), member + 5).isPresent(), receiver);
}
}
}
}
@Test
void ambiguousAndUnknownReceiverDoNotInventMembers() {
final String source = """
declare struct A() { fn onlyA() -> int { return 1; } }
declare struct B() { fn onlyB() -> int { return 2; } }
fn choose(value: int) -> A { return new A(); }
fn choose(value: int) -> B { return new B(); }
fn main() -> void { choose(1).onlyA(); missing().onlyA(); }
""";
final var ast = parseOrdinary(source);
for (final String receiver : List.of("choose(1).", "missing().")) {
final int offset = source.indexOf(receiver) + receiver.length();
assertTrue(service.hover(source, ast, ReadOnlyList.empty(), offset).isEmpty());
assertTrue(service.definition(source, ast, ReadOnlyList.empty(), offset).isEmpty());
assertTrue(service.completion(source, ast, ReadOnlyList.empty(), offset).isEmpty());
assertTrue(service.signatureHelp(source, ast, ReadOnlyList.empty(), offset + 6).isEmpty());
}
}
@Test
void chainedReceiversShareTypeResolutionAcrossEditorialQueries() {
final String source = """
import { Input } from @sdk:input;
fn main() -> void {
Input.pad().left().down();
}
""";
final var ast = parseOrdinary(source);
final var supplemental = supplementalTopDecls("""
declare host Input { fn pad() -> Pad; }
declare builtin type Pad(pub id: int) { fn left() -> Button; }
declare builtin type Button(pub id: int) { fn down() -> bool; }
""");
final int offset = source.indexOf("down");
assertAll(
() -> assertEquals("Button", service.hover(source, ast, supplemental, offset)
.orElseThrow().origin()),
() -> assertEquals(100, service.definition(source, ast, supplemental, offset)
.getFirst().span().getFileId().getId()),
() -> assertTrue(service.completion(source, ast, supplemental, offset).stream()
.anyMatch(candidate -> candidate.label().equals("down"))),
() -> assertEquals("down() -> bool", service.signatureHelp(source, ast, supplemental, offset + 5)
.orElseThrow().signatures().getFirst().label()));
}
@Test
void shouldResolveGeneralCompletionAndHoverAcrossLocalAndImportedSymbols() {
final var source = """
@@ -26,6 +26,7 @@ final class SourceCrawler extends SimpleFileVisitor<Path> {
private final ReadOnlySet<String> allowedExtensions;
private final List<Path> paths;
private Path traversalRoot;
public SourceCrawler(
final ReadOnlySet<String> allowedExtensions,
@@ -36,7 +37,12 @@ final class SourceCrawler extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult preVisitDirectory(final Path path, final BasicFileAttributes attrs) {
for (final var directory : path) {
if (traversalRoot == null) {
// The caller already admitted this source root. Ignore only descendants,
// not ancestors such as .workspace containing an explicitly resolved library.
traversalRoot = path;
}
for (final var directory : traversalRoot.relativize(path)) {
if (!isAllowedPath(directory)) {
return FileVisitResult.SKIP_SUBTREE;
}
@@ -83,4 +89,4 @@ final class SourceCrawler extends SimpleFileVisitor<Path> {
return allowedExtensions.contains(extension);
}
}
}
@@ -0,0 +1,64 @@
package p.studio.compiler.workspaces.stages;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.utilities.structures.ReadOnlySet;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SourceCrawlerTest {
@TempDir Path project;
@Test
void resolvedWorkspaceLibraryEntersFileTable() throws Exception {
final Path library = Files.createDirectories(project.resolve(".workspace/repos/lib/src")).getParent();
Files.createDirectories(project.resolve("src"));
Files.writeString(project.resolve("prometeu.json"), """
{"name":"app","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game",
"dependencies":[{"path":".workspace/repos/lib"}]}
""");
Files.writeString(library.resolve("prometeu.json"), """
{"name":"lib","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game",
"dependencies":[]}
""");
Files.writeString(project.resolve(".workspace/repos/registry.json"), "{}");
final Path source = Files.writeString(library.resolve("src/main.pbs"), "fn value() -> int { return 1; }");
Files.writeString(library.resolve("src/mod.barrel"), "export { value };\n");
final var context = p.studio.compiler.models.BuilderPipelineContext.fromConfig(
new p.studio.compiler.messages.BuilderPipelineConfig(false, project.toString()));
final var logs = p.studio.utilities.logs.LogAggregator.empty();
org.junit.jupiter.api.Assertions.assertFalse(new ResolveDepsPipelineStage().run(context, logs).hasErrors());
org.junit.jupiter.api.Assertions.assertFalse(new LoadSourcesPipelineStage().run(context, logs).hasErrors());
final var loaded = new ArrayList<Path>();
for (int i = 0; i < context.fileTable.size(); i++) {
loaded.add(context.fileTable.get(new p.studio.compiler.source.identifiers.FileId(i)).getCanonPath());
}
org.junit.jupiter.api.Assertions.assertTrue(loaded.contains(source.toRealPath()), loaded.toString());
}
@Test
void explicitlyAdmittedDependencyRootInsideWorkspaceIsTraversed() throws Exception {
final Path sourceRoot = Files.createDirectories(project.resolve(".workspace/repos/lib/src"));
final Path source = Files.writeString(sourceRoot.resolve("main.pbs"), "fn value() -> int { return 1; }");
final Path incidental = Files.createDirectories(sourceRoot.resolve(".workspace/incidental"));
Files.writeString(incidental.resolve("hidden.pbs"), "fn hidden() -> int { return 2; }");
final var paths = new ArrayList<Path>();
Files.walkFileTree(sourceRoot, new SourceCrawler(ReadOnlySet.wrap(Set.of("pbs")), paths));
assertEquals(java.util.List.of(source), paths);
}
@Test
void ordinaryRootDoesNotCollectIncidentalWorkspaceSources() throws Exception {
final Path source = Files.writeString(project.resolve("main.pbs"), "fn value() -> int { return 1; }");
final Path hidden = Files.createDirectories(project.resolve(".workspace/repos/lib"));
Files.writeString(hidden.resolve("hidden.pbs"), "fn hidden() -> int { return 2; }");
final var paths = new ArrayList<Path>();
Files.walkFileTree(project, new SourceCrawler(ReadOnlySet.wrap(Set.of("pbs")), paths));
assertEquals(java.util.List.of(source), paths);
}
}
@@ -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());
}
}