implements PLN-0035 pbs inline type hint payload production
This commit is contained in:
@@ -14,11 +14,13 @@ import p.studio.compiler.source.diagnostics.Diagnostic;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.services.PBSFrontendPhaseService;
|
||||
import p.studio.compiler.services.PbsInlineHintSurface;
|
||||
import p.studio.compiler.workspaces.AssetSurfaceContextLoader;
|
||||
import p.studio.compiler.workspaces.PipelineStage;
|
||||
import p.studio.compiler.workspaces.stages.LoadSourcesPipelineStage;
|
||||
import p.studio.compiler.workspaces.stages.ResolveDepsPipelineStage;
|
||||
import p.studio.lsp.dtos.LspDiagnosticDTO;
|
||||
import p.studio.lsp.dtos.LspInlineHintDTO;
|
||||
import p.studio.lsp.dtos.LspRangeDTO;
|
||||
import p.studio.lsp.dtos.LspSemanticPresentationDTO;
|
||||
import p.studio.lsp.messages.*;
|
||||
@@ -154,6 +156,10 @@ final class LspSemanticReadPhase {
|
||||
final Map<FileId, List<PbsAst.TopDecl>> importedSupplementalTopDeclsByFile = importedSupplementalTopDeclsByFile(
|
||||
snapshot.frontendSpec(),
|
||||
context);
|
||||
final Map<Path, List<LspInlineHintDTO>> inlineHintsByDocument = inlineHintsByDocument(
|
||||
snapshot,
|
||||
snapshot.frontendSpec(),
|
||||
context);
|
||||
final List<IndexedDocument> indexedDocuments = new ArrayList<>();
|
||||
for (final FileId fileId : snapshot.fileTable()) {
|
||||
final SourceHandle sourceHandle = snapshot.fileTable().get(fileId);
|
||||
@@ -181,7 +187,7 @@ final class LspSemanticReadPhase {
|
||||
semanticPresentation(snapshot.frontendSpec()),
|
||||
diagnosticsByDocument,
|
||||
semanticIndex.semanticHighlightsByDocument(),
|
||||
Map.of(),
|
||||
inlineHintsByDocument,
|
||||
semanticIndex.documentSymbolsByDocument(),
|
||||
semanticIndex.structuralAnchorsByDocument(),
|
||||
semanticIndex.workspaceSymbols(),
|
||||
@@ -192,9 +198,42 @@ final class LspSemanticReadPhase {
|
||||
private static Map<FileId, List<PbsAst.TopDecl>> importedSupplementalTopDeclsByFile(
|
||||
final FrontendSpec frontendSpec,
|
||||
final BuilderPipelineContext context) {
|
||||
if (context.resolvedWorkspace == null || context.fileTable == null || !"pbs".equals(frontendSpec.getLanguageId())) {
|
||||
final var surface = semanticReadSurface(frontendSpec, context);
|
||||
final Map<FileId, List<PbsAst.TopDecl>> byFile = new LinkedHashMap<>();
|
||||
for (final var entry : surface.supplementalTopDeclsByFile().entrySet()) {
|
||||
byFile.put(entry.getKey(), entry.getValue().asList());
|
||||
}
|
||||
return Map.copyOf(byFile);
|
||||
}
|
||||
|
||||
private static Map<Path, List<LspInlineHintDTO>> inlineHintsByDocument(
|
||||
final AnalysisSnapshot snapshot,
|
||||
final FrontendSpec frontendSpec,
|
||||
final BuilderPipelineContext context) {
|
||||
if (snapshot.fileTable() == null) {
|
||||
return Map.of();
|
||||
}
|
||||
final var surface = semanticReadSurface(frontendSpec, context);
|
||||
final Map<Path, List<LspInlineHintDTO>> byDocument = new LinkedHashMap<>();
|
||||
for (final var entry : surface.inlineHintsByFile().entrySet()) {
|
||||
final SourceHandle sourceHandle = snapshot.fileTable().get(entry.getKey());
|
||||
final Path documentPath = sourceHandle.getCanonPath().toAbsolutePath().normalize();
|
||||
final var hints = entry.getValue().asList().stream()
|
||||
.map(LspSemanticReadPhase::toInlineHintDTO)
|
||||
.toList();
|
||||
if (!hints.isEmpty()) {
|
||||
byDocument.put(documentPath, hints);
|
||||
}
|
||||
}
|
||||
return Map.copyOf(byDocument);
|
||||
}
|
||||
|
||||
private static PBSFrontendPhaseService.PbsSemanticReadSurface semanticReadSurface(
|
||||
final FrontendSpec frontendSpec,
|
||||
final BuilderPipelineContext context) {
|
||||
if (context.resolvedWorkspace == null || context.fileTable == null || !"pbs".equals(frontendSpec.getLanguageId())) {
|
||||
return new PBSFrontendPhaseService.PbsSemanticReadSurface(Map.of(), Map.of());
|
||||
}
|
||||
final FESurfaceContext feSurfaceContext = new AssetSurfaceContextLoader().load(context.resolvedWorkspace.mainProject().getRootPath());
|
||||
final FrontendPhaseContext frontendPhaseContext = new FrontendPhaseContext(
|
||||
context.resolvedWorkspace.graph().projectTable(),
|
||||
@@ -205,15 +244,19 @@ final class LspSemanticReadPhase {
|
||||
feSurfaceContext);
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var issues = BuildingIssueSink.empty();
|
||||
final var supplementalTopDecls = PBSFrontendPhaseService.importedSupplementalTopDeclsByFile(
|
||||
return PBSFrontendPhaseService.semanticReadSurface(
|
||||
frontendPhaseContext,
|
||||
diagnostics,
|
||||
issues);
|
||||
final Map<FileId, List<PbsAst.TopDecl>> byFile = new LinkedHashMap<>();
|
||||
for (final var entry : supplementalTopDecls.entrySet()) {
|
||||
byFile.put(entry.getKey(), entry.getValue().asList());
|
||||
}
|
||||
return Map.copyOf(byFile);
|
||||
}
|
||||
|
||||
private static LspInlineHintDTO toInlineHintDTO(final PbsInlineHintSurface hint) {
|
||||
return new LspInlineHintDTO(
|
||||
new LspRangeDTO(
|
||||
(int) hint.anchorSpan().getStart(),
|
||||
(int) hint.anchorSpan().getEnd()),
|
||||
hint.label(),
|
||||
hint.category());
|
||||
}
|
||||
|
||||
private record IndexedDocument(
|
||||
|
||||
@@ -89,7 +89,7 @@ final class LspServiceImplTest {
|
||||
assertEquals("pbs-function", semanticKeyForLexeme(analysis, OVERLAY_SOURCE, "helper"));
|
||||
assertEquals(List.of("/themes/pbs/semantic-highlighting.css"), analysis.semanticPresentation().resources());
|
||||
assertTrue(analysis.semanticPresentation().semanticKeys().contains("pbs-function"));
|
||||
assertTrue(analysis.inlineHints().isEmpty());
|
||||
assertFalse(analysis.inlineHints().isEmpty(), analysis.toString());
|
||||
|
||||
assertTrue(
|
||||
analysis.documentSymbols().stream().anyMatch(symbol -> symbol.name().equals("helper_call")),
|
||||
@@ -209,7 +209,8 @@ final class LspServiceImplTest {
|
||||
void analyzeDocumentReturnsDedicatedInlineHintTransportSurface() throws Exception {
|
||||
final Path projectRoot = createProject();
|
||||
final Path mainFile = projectRoot.resolve("src/main.pbs");
|
||||
Files.writeString(mainFile, "fn main() -> void { let value = 1; }\n");
|
||||
final String source = "fn main() -> void { let value = 1; }\n";
|
||||
Files.writeString(mainFile, source);
|
||||
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext(projectRoot));
|
||||
final LspService service = new LspServiceImpl(
|
||||
@@ -218,7 +219,39 @@ final class LspServiceImplTest {
|
||||
|
||||
final var analysis = service.analyzeDocument(new LspAnalyzeDocumentRequest(mainFile));
|
||||
|
||||
assertTrue(analysis.inlineHints().isEmpty(), analysis.toString());
|
||||
assertEquals(1, analysis.inlineHints().size(), analysis.toString());
|
||||
assertEquals("int", analysis.inlineHints().getFirst().label());
|
||||
assertEquals("type", analysis.inlineHints().getFirst().category());
|
||||
assertEquals("value", sourceSlice(source, analysis.inlineHints().getFirst().anchor()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyzeDocumentPreservesValidInlineHintsUnderPartialDegradation() throws Exception {
|
||||
final Path projectRoot = createProject();
|
||||
final Path mainFile = projectRoot.resolve("src/main.pbs");
|
||||
final String source = """
|
||||
fn ok() -> void {
|
||||
let value = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
fn broken( -> void {
|
||||
let missing = 2;
|
||||
}
|
||||
""";
|
||||
Files.writeString(mainFile, source);
|
||||
|
||||
final VfsProjectDocument vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext(projectRoot));
|
||||
final LspService service = new LspServiceImpl(
|
||||
new LspProjectContext("Example", "pbs", projectRoot),
|
||||
vfs);
|
||||
|
||||
final var analysis = service.analyzeDocument(new LspAnalyzeDocumentRequest(mainFile));
|
||||
|
||||
assertFalse(analysis.diagnostics().isEmpty(), analysis.toString());
|
||||
assertEquals(1, analysis.inlineHints().size(), analysis.toString());
|
||||
assertEquals("int", analysis.inlineHints().getFirst().label());
|
||||
assertEquals("value", sourceSlice(source, analysis.inlineHints().getFirst().anchor()));
|
||||
}
|
||||
|
||||
private Path createProject() throws Exception {
|
||||
@@ -315,6 +348,12 @@ final class LspServiceImplTest {
|
||||
return source.substring(start, end);
|
||||
}
|
||||
|
||||
private static String sourceSlice(
|
||||
final String source,
|
||||
final p.studio.lsp.dtos.LspRangeDTO range) {
|
||||
return spanContent(source, range.startOffset(), range.endOffset());
|
||||
}
|
||||
|
||||
private static final class OverlayVfsProjectDocument implements VfsProjectDocument {
|
||||
private final VfsProjectDocument delegate;
|
||||
private final Path overlayPath;
|
||||
|
||||
Reference in New Issue
Block a user