feat(pbs): support quoted project imports and installed authoring contracts
This commit is contained in:
+9
-6
@@ -1,6 +1,7 @@
|
||||
package p.studio.compiler;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.AppMode;
|
||||
import p.studio.compiler.messages.FrontendPhaseContext;
|
||||
@@ -32,8 +33,9 @@ class PBSFrontendModuleCompletionTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void moduleRefCompletionListsKnownModulesAndMemberCompletionStays() throws IOException {
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"app", "zelda-1", "équipe-1"})
|
||||
void moduleRefCompletionListsKnownModulesAndMemberCompletionStays(String projectName) throws IOException {
|
||||
final Path projectRoot = tempDir.resolve("app");
|
||||
final Path sourceRoot = projectRoot.resolve("src");
|
||||
final Path moduleA = sourceRoot.resolve("a").resolve("source.pbs");
|
||||
@@ -47,6 +49,7 @@ class PBSFrontendModuleCompletionTest {
|
||||
Files.writeString(moduleA.getParent().resolve("mod.barrel"), "pub fn target() -> int;\n");
|
||||
final String caller = """
|
||||
import { Log } from @sdk:log;
|
||||
import { target } from @%s:a;
|
||||
|
||||
declare struct Vec() {
|
||||
fn blend(dx: int, dy: int) -> int { return dx; }
|
||||
@@ -55,7 +58,7 @@ class PBSFrontendModuleCompletionTest {
|
||||
fn frame(vec: Vec) -> void {
|
||||
vec.blend(1, 2);
|
||||
}
|
||||
""";
|
||||
""".formatted(p.studio.compiler.pbs.PbsSourceNames.project(projectName));
|
||||
Files.writeString(main, caller);
|
||||
Files.writeString(sourceRoot.resolve("mod.barrel"), """
|
||||
pub struct Vec();
|
||||
@@ -66,7 +69,7 @@ class PBSFrontendModuleCompletionTest {
|
||||
final var fileTable = new FileTable(1);
|
||||
final ProjectId projectId = projectTable.register(ProjectDescriptor.builder()
|
||||
.rootPath(projectRoot)
|
||||
.name("app")
|
||||
.name(projectName)
|
||||
.version("1.0.0")
|
||||
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
|
||||
.build());
|
||||
@@ -86,7 +89,7 @@ class PBSFrontendModuleCompletionTest {
|
||||
|
||||
final var moduleCompletion = service.completion(request, editorialContext, byteOffset(caller, "@sdk:log"));
|
||||
assertTrue(labels(moduleCompletion).contains("@sdk:log"));
|
||||
assertTrue(labels(moduleCompletion).contains("@app:a"));
|
||||
assertTrue(labels(moduleCompletion).contains("@" + p.studio.compiler.pbs.PbsSourceNames.project(projectName) + ":a"));
|
||||
assertTrue(moduleCompletion.stream().allMatch(candidate -> candidate.kind() == FrontendSymbolKind.MODULE));
|
||||
|
||||
final int afterDot = caller.indexOf("vec.") + "vec.".length();
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package p.studio.compiler.pbs.parser;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.pbs.PbsSourceNames;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PbsQuotedProjectTest {
|
||||
@Test void roundTripsProjectIdentityAndUtf8Spans() {
|
||||
for (var name : new String[]{"game", "zelda-1", "physics-2d", "fn", "équipe-1", "a\\n", "a\"b", "a\nb\rc\td", " padded "}) {
|
||||
for (var named : new boolean[]{true, false}) {
|
||||
var source = "// é\nimport " + (named ? "{ X as Y } from " : "") + "@" + PbsSourceNames.project(name) + ":world/items;";
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
var file = new FileId(0);
|
||||
var ast = PbsParser.parse(PbsLexer.lex(source, file, diagnostics), file, diagnostics);
|
||||
assertTrue(diagnostics.isEmpty(), name + ": " + diagnostics.asCollection());
|
||||
var ref = ast.imports().getFirst().moduleRef();
|
||||
assertEquals(name, ref.project());
|
||||
assertEquals(java.util.List.of("world", "items"), ref.pathSegments().asList());
|
||||
assertEquals(source.substring(0, source.indexOf('@')).getBytes(StandardCharsets.UTF_8).length, ref.span().getStart());
|
||||
assertEquals(source.substring(0, source.indexOf(';')).getBytes(StandardCharsets.UTF_8).length, ref.span().getEnd());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test void quotedIdentifierRetainsDecodedName() {
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
var file = new FileId(0);
|
||||
var ast = PbsParser.parse(PbsLexer.lex("import @\"game\":items;", file, diagnostics), file, diagnostics);
|
||||
assertTrue(diagnostics.isEmpty());
|
||||
assertEquals("game", ast.imports().getFirst().moduleRef().project());
|
||||
}
|
||||
|
||||
@Test void rejectsBlankMalformedAndUnquotedHyphenNamesAndQuotedPaths() {
|
||||
for (var source : new String[]{"import @\"\":src;", "import @\" \\t\":src;", "import @\"bad\\q\":src;",
|
||||
"import @\"unterminated:src;", "import @zelda-1:src;", "import @game:\"src\";",
|
||||
"import @\"\"\"game\"\"\":src;"}) {
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
var file = new FileId(0);
|
||||
PbsParser.parse(PbsLexer.lex(source, file, diagnostics), file, diagnostics);
|
||||
assertFalse(diagnostics.isEmpty(), source);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user