implements PLN-0137 (6/7) module-ref-completion

Completion inside a ModuleRef lists project and stdlib modules the
current resolver can already name. Completion after a dot stays the
existing member list. Items remain eager, resolveProvider stays false,
and there is no snippet or ranking score.
This commit is contained in:
2026-09-22 09:13:40 +01:00
parent 202c785f94
commit e456bb8d7a
10 changed files with 286 additions and 4 deletions
@@ -0,0 +1,127 @@
package p.studio.compiler;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.AppMode;
import p.studio.compiler.messages.FrontendPhaseContext;
import p.studio.compiler.models.BuildStack;
import p.studio.compiler.models.ProjectDescriptor;
import p.studio.compiler.models.SourceHandle;
import p.studio.compiler.services.FrontendCompletionCandidate;
import p.studio.compiler.services.FrontendDocumentRequest;
import p.studio.compiler.services.FrontendSymbolKind;
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;
import p.studio.compiler.utilities.SourceProviderFactory;
import p.studio.utilities.structures.ReadOnlyList;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PBSFrontendModuleCompletionTest {
@TempDir
Path tempDir;
@Test
void moduleRefCompletionListsKnownModulesAndMemberCompletionStays() throws IOException {
final Path projectRoot = tempDir.resolve("app");
final Path sourceRoot = projectRoot.resolve("src");
final Path moduleA = sourceRoot.resolve("a").resolve("source.pbs");
final Path main = sourceRoot.resolve("main.pbs");
Files.createDirectories(moduleA.getParent());
Files.writeString(moduleA, """
fn target() -> int {
return 1;
}
""");
Files.writeString(moduleA.getParent().resolve("mod.barrel"), "pub fn target() -> int;\n");
final String caller = """
import { Log } from @sdk:log;
declare struct Vec() {
fn blend(dx: int, dy: int) -> int { return dx; }
}
fn frame(vec: Vec) -> void {
vec.blend(1, 2);
}
""";
Files.writeString(main, caller);
Files.writeString(sourceRoot.resolve("mod.barrel"), """
pub struct Vec();
pub fn frame(vec: Vec) -> void;
""");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final ProjectId projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("app")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.build());
register(projectId, projectRoot, moduleA, fileTable);
register(projectId, projectRoot, moduleA.getParent().resolve("mod.barrel"), fileTable);
final FileId mainId = register(projectId, projectRoot, main, fileTable);
register(projectId, projectRoot, sourceRoot.resolve("mod.barrel"), fileTable);
final var phaseContext = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))),
AppMode.Game);
final var service = new PBSFrontendLanguageService();
final var editorialContext = service.prepareEditorialContext(phaseContext, mainId).orElseThrow();
final var request = new FrontendDocumentRequest(projectRoot, main, caller);
final var moduleCompletion = service.completion(request, editorialContext, byteOffset(caller, "@sdk:log"));
assertTrue(labels(moduleCompletion).contains("@sdk:log"));
assertTrue(labels(moduleCompletion).contains("@app:a"));
assertTrue(moduleCompletion.stream().allMatch(candidate -> candidate.kind() == FrontendSymbolKind.MODULE));
final int afterDot = caller.indexOf("vec.") + "vec.".length();
final var memberCompletion = service.completion(
request,
editorialContext,
caller.substring(0, afterDot).getBytes(StandardCharsets.UTF_8).length);
assertTrue(labels(memberCompletion).contains("blend"), labels(memberCompletion).toString());
assertFalse(labels(memberCompletion).contains("@sdk:log"));
}
private static List<String> labels(final List<FrontendCompletionCandidate> candidates) {
return candidates.stream().map(FrontendCompletionCandidate::label).toList();
}
private static int byteOffset(final String source, final String marker) {
final int index = source.indexOf(marker);
if (index < 0) {
throw new IllegalArgumentException(marker);
}
return source.substring(0, index + 1).getBytes(StandardCharsets.UTF_8).length;
}
private static FileId register(
final ProjectId projectId,
final Path projectRoot,
final Path file,
final FileTable fileTable) throws IOException {
final BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class);
return fileTable.register(new SourceHandle(
projectId,
projectRoot.relativize(file),
file,
attributes.size(),
attributes.lastModifiedTime().toMillis(),
SourceProviderFactory.filesystem()));
}
}