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:
+41
@@ -24,6 +24,7 @@ import p.studio.compiler.pbs.semantics.PbsQuickFixCollector;
|
||||
import p.studio.compiler.services.PBSFrontendPhaseService.PbsSemanticReadSurface;
|
||||
import p.studio.compiler.services.FrontendCodeAction;
|
||||
import p.studio.compiler.services.FrontendCompletionCandidate;
|
||||
import p.studio.compiler.services.FrontendSymbolKind;
|
||||
import p.studio.compiler.services.FrontendDocumentLink;
|
||||
import p.studio.compiler.services.FrontendFoldingRange;
|
||||
import p.studio.compiler.services.FrontendSelectionRange;
|
||||
@@ -135,6 +136,10 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
|
||||
return List.of();
|
||||
}
|
||||
|
||||
if (insideModuleRef(ast, offset)
|
||||
&& editorialContext.projectSurface() instanceof PbsSemanticReadSurface surface) {
|
||||
return moduleCompletions(surface.moduleCompletionLabels());
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
final ReadOnlyList<PbsAst.TopDecl> typedSupplementalTopDecls =
|
||||
(ReadOnlyList<PbsAst.TopDecl>) supplementalTopDecls;
|
||||
@@ -595,6 +600,42 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
|
||||
return semanticTokenProvider.tokenize(documentText == null ? "" : documentText);
|
||||
}
|
||||
|
||||
private static boolean insideModuleRef(final PbsAst.File ast, final int offset) {
|
||||
if (ast == null || ast.imports() == null || offset < 0) {
|
||||
return false;
|
||||
}
|
||||
for (final PbsAst.ImportDecl importDecl : ast.imports()) {
|
||||
if (importDecl == null || importDecl.moduleRef() == null || importDecl.moduleRef().span() == null) {
|
||||
continue;
|
||||
}
|
||||
final long start = importDecl.moduleRef().span().getStart();
|
||||
final long end = importDecl.moduleRef().span().getEnd();
|
||||
if (end > start && start <= offset && offset <= end) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static List<FrontendCompletionCandidate> moduleCompletions(final List<String> labels) {
|
||||
if (labels == null || labels.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
final ArrayList<FrontendCompletionCandidate> candidates = new ArrayList<>();
|
||||
for (final String label : labels) {
|
||||
if (label == null || label.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
candidates.add(new FrontendCompletionCandidate(
|
||||
label,
|
||||
FrontendSymbolKind.MODULE,
|
||||
"module",
|
||||
"",
|
||||
""));
|
||||
}
|
||||
return List.copyOf(candidates);
|
||||
}
|
||||
|
||||
private FrontendCompletionCandidate toFrontendCompletionCandidate(final PbsEditorialCompletionCandidate candidate) {
|
||||
return new FrontendCompletionCandidate(
|
||||
candidate.label(),
|
||||
|
||||
+73
@@ -5,9 +5,15 @@ import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.JarURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.jar.JarEntry;
|
||||
|
||||
public final class ResourceStdlibEnvironmentResolver implements StdlibEnvironmentResolver {
|
||||
private static final String STDLIB_ROOT = "stdlib";
|
||||
@@ -37,6 +43,73 @@ public final class ResourceStdlibEnvironmentResolver implements StdlibEnvironmen
|
||||
barrelSource.get()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyList<StdlibModuleSource> modules() {
|
||||
final String root = target.exportReferenceName().toLowerCase()
|
||||
+ "/" + STDLIB_ROOT + "/" + stdlibVersion;
|
||||
final ClassLoader classLoader = ResourceStdlibEnvironmentResolver.class.getClassLoader();
|
||||
if (classLoader == null) {
|
||||
return ReadOnlyList.empty();
|
||||
}
|
||||
final URL url = classLoader.getResource(root);
|
||||
if (url == null) {
|
||||
return ReadOnlyList.empty();
|
||||
}
|
||||
try {
|
||||
final List<List<String>> modulePaths = "jar".equals(url.getProtocol())
|
||||
? jarModulePaths(url, root)
|
||||
: fileModulePaths(url);
|
||||
final ArrayList<StdlibModuleSource> modules = new ArrayList<>();
|
||||
for (final List<String> modulePath : modulePaths) {
|
||||
if (modulePath.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
final String project = modulePath.getFirst();
|
||||
final ReadOnlyList<String> segments = ReadOnlyList.wrap(modulePath.subList(1, modulePath.size()));
|
||||
resolveModule(project, segments).ifPresent(modules::add);
|
||||
}
|
||||
return ReadOnlyList.wrap(modules);
|
||||
} catch (final Exception ignored) {
|
||||
return ReadOnlyList.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private List<List<String>> fileModulePaths(final URL url) throws Exception {
|
||||
final Path rootPath = Path.of(url.toURI());
|
||||
final ArrayList<List<String>> modulePaths = new ArrayList<>();
|
||||
try (var walk = Files.walk(rootPath)) {
|
||||
walk.filter(path -> "main.pbs".equals(String.valueOf(path.getFileName()))
|
||||
&& Files.isRegularFile(path.getParent().resolve("mod.barrel")))
|
||||
.forEach(path -> modulePaths.add(segments(rootPath.relativize(path.getParent()))));
|
||||
}
|
||||
return modulePaths;
|
||||
}
|
||||
|
||||
private List<List<String>> jarModulePaths(final URL url, final String root) throws IOException {
|
||||
final JarURLConnection connection = (JarURLConnection) url.openConnection();
|
||||
final var jarFile = connection.getJarFile();
|
||||
final String prefix = root + "/";
|
||||
final ArrayList<List<String>> modulePaths = new ArrayList<>();
|
||||
jarFile.stream()
|
||||
.map(JarEntry::getName)
|
||||
.filter(name -> name.startsWith(prefix) && name.endsWith("/main.pbs"))
|
||||
.forEach(name -> {
|
||||
final String directory = name.substring(prefix.length(), name.length() - "/main.pbs".length());
|
||||
if (!directory.isBlank() && jarFile.getEntry(prefix + directory + "/mod.barrel") != null) {
|
||||
modulePaths.add(List.of(directory.split("/")));
|
||||
}
|
||||
});
|
||||
return modulePaths;
|
||||
}
|
||||
|
||||
private static List<String> segments(final Path relative) {
|
||||
final ArrayList<String> segments = new ArrayList<>();
|
||||
for (final Path segment : relative) {
|
||||
segments.add(segment.toString());
|
||||
}
|
||||
return segments;
|
||||
}
|
||||
|
||||
private String moduleBasePath(
|
||||
final String project,
|
||||
final ReadOnlyList<String> pathSegments) {
|
||||
|
||||
+4
@@ -6,5 +6,9 @@ import java.util.Optional;
|
||||
|
||||
public interface StdlibEnvironment {
|
||||
Optional<StdlibModuleSource> resolveModule(String project, ReadOnlyList<String> pathSegments);
|
||||
|
||||
default ReadOnlyList<StdlibModuleSource> modules() {
|
||||
return ReadOnlyList.empty();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+33
-2
@@ -152,12 +152,14 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
inlineHintsByFile.put(parsedSourceFile.fileId(), inlineHints);
|
||||
}
|
||||
}
|
||||
final Map<ModuleReference, Path> physicalModuleFiles = physicalModuleFiles(ctx, assembly);
|
||||
return new PbsSemanticReadSurface(
|
||||
Map.copyOf(astByFile),
|
||||
Map.copyOf(supplementalTopDeclsByFile),
|
||||
Map.copyOf(inlineHintsByFile),
|
||||
Map.copyOf(sourceKindByFile),
|
||||
physicalModuleFiles(ctx, assembly));
|
||||
physicalModuleFiles,
|
||||
moduleCompletionLabels(ctx, physicalModuleFiles));
|
||||
}
|
||||
|
||||
private static Map<ModuleReference, Path> physicalModuleFiles(
|
||||
@@ -190,12 +192,41 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
return Map.copyOf(files);
|
||||
}
|
||||
|
||||
private static List<String> moduleCompletionLabels(
|
||||
final FrontendPhaseContext ctx,
|
||||
final Map<ModuleReference, Path> physicalModuleFiles) {
|
||||
final var labels = new java.util.TreeSet<String>();
|
||||
for (final ModuleReference reference : physicalModuleFiles.keySet()) {
|
||||
labels.add(moduleLabel(reference.project(), reference.pathSegments()));
|
||||
}
|
||||
if (ctx != null) {
|
||||
final var environment = new ResourceStdlibEnvironmentResolver().resolve(ctx.stdlibVersion(), ctx.target());
|
||||
for (final var module : environment.modules()) {
|
||||
labels.add(moduleLabel(module.project(), module.pathSegments()));
|
||||
}
|
||||
}
|
||||
return List.copyOf(labels);
|
||||
}
|
||||
|
||||
private static String moduleLabel(
|
||||
final String project,
|
||||
final ReadOnlyList<String> pathSegments) {
|
||||
if (project == null || project.isBlank()) {
|
||||
return "";
|
||||
}
|
||||
if (pathSegments == null || pathSegments.isEmpty()) {
|
||||
return "@" + project;
|
||||
}
|
||||
return "@" + project + ":" + String.join("/", pathSegments.asList());
|
||||
}
|
||||
|
||||
public record PbsSemanticReadSurface(
|
||||
Map<FileId, p.studio.compiler.pbs.ast.PbsAst.File> astByFile,
|
||||
Map<FileId, ReadOnlyList<p.studio.compiler.pbs.ast.PbsAst.TopDecl>> supplementalTopDeclsByFile,
|
||||
Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile,
|
||||
Map<FileId, SourceKind> sourceKindByFile,
|
||||
Map<ModuleReference, Path> physicalModuleFileByReference) {
|
||||
Map<ModuleReference, Path> physicalModuleFileByReference,
|
||||
List<String> moduleCompletionLabels) {
|
||||
}
|
||||
|
||||
private IRBackend mergeCompiledSources(
|
||||
|
||||
Reference in New Issue
Block a user