implements PLN-0035 pbs inline type hint payload production
This commit is contained in:
+23
@@ -194,6 +194,29 @@ class PbsParserTest {
|
||||
assertFalse(diagnostics.isEmpty(), "Parser should reject reserved keyword 'error' as callable/member name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTrackLetNameSpanSeparatelyFromWholeStatementSpan() {
|
||||
final var source = """
|
||||
fn main() -> void {
|
||||
let inferred = 1;
|
||||
return;
|
||||
}
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var fileId = new FileId(0);
|
||||
|
||||
final PbsAst.File ast = PbsParser.parse(PbsLexer.lex(source, fileId, diagnostics), fileId, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
|
||||
final var functionDecl = assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().getFirst());
|
||||
final var letStatement = assertInstanceOf(PbsAst.LetStatement.class, functionDecl.body().statements().getFirst());
|
||||
assertEquals("inferred", letStatement.name());
|
||||
assertEquals(source.indexOf("inferred"), letStatement.nameSpan().getStart());
|
||||
assertEquals(source.indexOf("inferred") + "inferred".length(), letStatement.nameSpan().getEnd());
|
||||
assertTrue(letStatement.span().getStart() < letStatement.nameSpan().getStart());
|
||||
assertTrue(letStatement.span().getEnd() > letStatement.nameSpan().getEnd());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAcceptStructFieldTrailingComma() {
|
||||
final var source = "declare struct S(a: int,);";
|
||||
|
||||
+117
-2
@@ -20,6 +20,7 @@ import p.studio.compiler.pbs.stdlib.StdlibEnvironment;
|
||||
import p.studio.compiler.pbs.stdlib.StdlibEnvironmentResolver;
|
||||
import p.studio.compiler.pbs.stdlib.StdlibModuleSource;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.compiler.source.tables.FileTable;
|
||||
import p.studio.compiler.source.tables.ProjectTable;
|
||||
@@ -1868,13 +1869,127 @@ class PBSFrontendPhaseServiceTest {
|
||||
allowedDiagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
|
||||
}
|
||||
|
||||
private void registerFile(
|
||||
@Test
|
||||
void shouldProduceInlineHintsForEligibleInferredLetBindings() throws IOException {
|
||||
final var projectRoot = tempDir.resolve("project-inline-hints");
|
||||
final var sourceRoot = projectRoot.resolve("src");
|
||||
Files.createDirectories(sourceRoot);
|
||||
|
||||
final var sourceFile = sourceRoot.resolve("main.pbs");
|
||||
final var modBarrel = sourceRoot.resolve("mod.barrel");
|
||||
Files.writeString(sourceFile, """
|
||||
declare struct Player(score: int);
|
||||
declare error Failure {
|
||||
Boom;
|
||||
}
|
||||
|
||||
fn fetch_pair() -> result<Failure> (left: int, right: int) {
|
||||
return ok((left: 1, right: 2));
|
||||
}
|
||||
|
||||
fn main() -> void {
|
||||
let scalar = 1;
|
||||
let player = new Player(1);
|
||||
let maybe = some(1);
|
||||
let pair = (left: 1, right: 2);
|
||||
let outcome = fetch_pair();
|
||||
return;
|
||||
}
|
||||
""");
|
||||
Files.writeString(modBarrel, "pub fn main() -> void;");
|
||||
|
||||
final var projectTable = new ProjectTable();
|
||||
final var fileTable = new FileTable(1);
|
||||
final var projectId = projectTable.register(ProjectDescriptor.builder()
|
||||
.rootPath(projectRoot)
|
||||
.name("app")
|
||||
.version("1.0.0")
|
||||
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
|
||||
.build());
|
||||
|
||||
final var sourceFileId = registerFile(projectId, projectRoot, sourceFile, fileTable);
|
||||
registerFile(projectId, projectRoot, modBarrel, fileTable);
|
||||
|
||||
final var ctx = new FrontendPhaseContext(
|
||||
projectTable,
|
||||
fileTable,
|
||||
new BuildStack(ReadOnlyList.wrap(List.of(projectId))));
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
final var inlineHintsByFile = PBSFrontendPhaseService.inlineHintsByFile(
|
||||
ctx,
|
||||
diagnostics,
|
||||
BuildingIssueSink.empty());
|
||||
final var sourceHints = inlineHintsByFile.get(sourceFileId);
|
||||
assertNotNull(sourceHints, () -> "diagnostics="
|
||||
+ diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList()
|
||||
+ ", keys=" + inlineHintsByFile.keySet());
|
||||
final var labels = sourceHints.asList().stream()
|
||||
.map(PbsInlineHintSurface::label)
|
||||
.toList();
|
||||
|
||||
assertEquals(List.of(
|
||||
"int",
|
||||
"Player",
|
||||
"optional int",
|
||||
"(left: int, right: int)",
|
||||
"result<Failure> (left: int, right: int)"),
|
||||
labels);
|
||||
assertEquals(0, diagnostics.errorCount(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSuppressInlineHintsForExplicitlyTypedLetBindings() throws IOException {
|
||||
final var projectRoot = tempDir.resolve("project-inline-hints-explicit");
|
||||
final var sourceRoot = projectRoot.resolve("src");
|
||||
Files.createDirectories(sourceRoot);
|
||||
|
||||
final var sourceFile = sourceRoot.resolve("main.pbs");
|
||||
final var modBarrel = sourceRoot.resolve("mod.barrel");
|
||||
Files.writeString(sourceFile, """
|
||||
fn main() -> void {
|
||||
let inferred = 1;
|
||||
let explicit: int = 2;
|
||||
return;
|
||||
}
|
||||
""");
|
||||
Files.writeString(modBarrel, "pub fn main() -> void;");
|
||||
|
||||
final var projectTable = new ProjectTable();
|
||||
final var fileTable = new FileTable(1);
|
||||
final var projectId = projectTable.register(ProjectDescriptor.builder()
|
||||
.rootPath(projectRoot)
|
||||
.name("app")
|
||||
.version("1.0.0")
|
||||
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
|
||||
.build());
|
||||
|
||||
final var sourceFileId = registerFile(projectId, projectRoot, sourceFile, fileTable);
|
||||
registerFile(projectId, projectRoot, modBarrel, fileTable);
|
||||
|
||||
final var ctx = new FrontendPhaseContext(
|
||||
projectTable,
|
||||
fileTable,
|
||||
new BuildStack(ReadOnlyList.wrap(List.of(projectId))));
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
final var inlineHints = PBSFrontendPhaseService.inlineHintsByFile(
|
||||
ctx,
|
||||
diagnostics,
|
||||
BuildingIssueSink.empty()).get(sourceFileId);
|
||||
|
||||
assertEquals(1, inlineHints.size());
|
||||
assertEquals("int", inlineHints.getFirst().label());
|
||||
assertEquals("type", inlineHints.getFirst().category());
|
||||
}
|
||||
|
||||
private FileId registerFile(
|
||||
final ProjectId projectId,
|
||||
final Path projectRoot,
|
||||
final Path file,
|
||||
final FileTable fileTable) throws IOException {
|
||||
final BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class);
|
||||
fileTable.register(new SourceHandle(
|
||||
return fileTable.register(new SourceHandle(
|
||||
projectId,
|
||||
projectRoot.relativize(file),
|
||||
file,
|
||||
|
||||
Reference in New Issue
Block a user