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) {
}