implements PLN-0132
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 62.51% (18192/29102)
* Branch Coverage: 53.15% (7093/13345)
* Lines of Code: 29102
* Cyclomatic Complexity: 11715
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 663
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 62.51% (18192/29102)
* Branch Coverage: 53.15% (7093/13345)
* Lines of Code: 29102
* Cyclomatic Complexity: 11715
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 663
Add PBS find-references as the request-local inverse of definition identity on FrontendLanguageService. Usage sites are identifier tokens whose definition locations intersect the cursor set. Empty results stay honest when definition is empty, including virtual stdlib paths. Honor includeDeclaration in the LSP bridge. Housekeep DSC-0040 with LSN-0069.
This commit is contained in:
+123
-1
@@ -11,6 +11,8 @@ import p.studio.compiler.pbs.semantics.PbsEditorialResolvedSymbol;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSignatureHelp;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSymbolKind;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSupportService;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialSupportService.EditorialDocument;
|
||||
import p.studio.compiler.services.PBSFrontendPhaseService.PbsSemanticReadSurface;
|
||||
import p.studio.compiler.services.FrontendCompletionCandidate;
|
||||
import p.studio.compiler.services.FrontendDefinitionLocation;
|
||||
import p.studio.compiler.services.FrontendDocumentRequest;
|
||||
@@ -31,6 +33,7 @@ import p.studio.utilities.structures.ReadOnlyList;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@@ -69,7 +72,8 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
|
||||
ast,
|
||||
semanticReadSurface.supplementalTopDeclsByFile().getOrDefault(fileId, ReadOnlyList.empty()),
|
||||
phaseContext.feSurfaceContext(),
|
||||
phaseContext.fileTable));
|
||||
phaseContext.fileTable,
|
||||
semanticReadSurface));
|
||||
}
|
||||
|
||||
public List<PbsEditorialCompletionCandidate> completion(
|
||||
@@ -213,6 +217,56 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
|
||||
return List.copyOf(locations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FrontendDefinitionLocation> references(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext,
|
||||
final int offset) {
|
||||
if (request == null
|
||||
|| editorialContext == null
|
||||
|| !(editorialContext.syntaxTree() instanceof PbsAst.File ast)
|
||||
|| !(editorialContext.supplementalDeclarations() instanceof ReadOnlyList<?> supplementalTopDecls)
|
||||
|| !(editorialContext.semanticContext() instanceof FESurfaceContext feSurfaceContext)
|
||||
|| !(editorialContext.sourceFiles() instanceof FileTableReader fileTable)) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
final ReadOnlyList<PbsAst.TopDecl> typedSupplementalTopDecls =
|
||||
(ReadOnlyList<PbsAst.TopDecl>) supplementalTopDecls;
|
||||
final List<PbsEditorialLocation> editorialIdentity = definition(
|
||||
request.documentText(),
|
||||
ast,
|
||||
typedSupplementalTopDecls,
|
||||
feSurfaceContext,
|
||||
offset);
|
||||
final ArrayList<PbsEditorialLocation> physicalIdentity = new ArrayList<>();
|
||||
for (final PbsEditorialLocation location : editorialIdentity) {
|
||||
if (toFrontendLocation(location, fileTable).isPresent()) {
|
||||
physicalIdentity.add(location);
|
||||
}
|
||||
}
|
||||
if (physicalIdentity.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
final EditorialDocument cursorDocument = new EditorialDocument(
|
||||
request.documentText(),
|
||||
ast,
|
||||
typedSupplementalTopDecls,
|
||||
feSurfaceContext);
|
||||
final List<PbsEditorialLocation> usages = editorialSupportService.references(
|
||||
cursorDocument,
|
||||
offset,
|
||||
projectDocuments(request, editorialContext, cursorDocument, fileTable));
|
||||
final LinkedHashMap<String, FrontendDefinitionLocation> mapped = new LinkedHashMap<>();
|
||||
for (final PbsEditorialLocation usage : usages) {
|
||||
toFrontendLocation(usage, fileTable).ifPresent(location ->
|
||||
mapped.putIfAbsent(frontendLocationKey(location), location));
|
||||
}
|
||||
return List.copyOf(mapped.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FrontendSemanticToken> semanticTokens(final FrontendDocumentRequest request) {
|
||||
return semanticTokens(request == null ? "" : request.documentText());
|
||||
@@ -260,6 +314,74 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
|
||||
return FrontendSymbolKind.valueOf(kind.name());
|
||||
}
|
||||
|
||||
private List<EditorialDocument> projectDocuments(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext,
|
||||
final EditorialDocument cursorDocument,
|
||||
final FileTableReader fileTable) {
|
||||
if (!(editorialContext.projectSurface() instanceof PbsSemanticReadSurface surface)
|
||||
|| surface.astByFile() == null
|
||||
|| surface.astByFile().isEmpty()) {
|
||||
return List.of(cursorDocument);
|
||||
}
|
||||
final ArrayList<EditorialDocument> documents = new ArrayList<>();
|
||||
for (final var entry : surface.astByFile().entrySet()) {
|
||||
if (!isRegularSource(entry.getKey(), fileTable)) {
|
||||
continue;
|
||||
}
|
||||
final PbsAst.File fileAst = entry.getValue();
|
||||
if (fileAst == null) {
|
||||
continue;
|
||||
}
|
||||
final ReadOnlyList<PbsAst.TopDecl> fileSupplementals = fileAst == cursorDocument.ast()
|
||||
? cursorDocument.supplementalTopDecls()
|
||||
: surface.supplementalTopDeclsByFile().getOrDefault(entry.getKey(), ReadOnlyList.empty());
|
||||
documents.add(new EditorialDocument(
|
||||
textForFile(entry.getKey(), fileTable, request),
|
||||
fileAst,
|
||||
fileSupplementals,
|
||||
cursorDocument.feSurfaceContext()));
|
||||
}
|
||||
if (documents.isEmpty()) {
|
||||
return List.of(cursorDocument);
|
||||
}
|
||||
return List.copyOf(documents);
|
||||
}
|
||||
|
||||
private boolean isRegularSource(
|
||||
final FileId fileId,
|
||||
final FileTableReader fileTable) {
|
||||
if (fileId == null || fileId.isNone() || fileTable == null) {
|
||||
return false;
|
||||
}
|
||||
final var sourceHandle = fileTable.get(fileId);
|
||||
if (sourceHandle == null || sourceHandle.getCanonPath() == null) {
|
||||
return false;
|
||||
}
|
||||
return Files.isRegularFile(sourceHandle.getCanonPath().toAbsolutePath().normalize());
|
||||
}
|
||||
|
||||
private String textForFile(
|
||||
final FileId fileId,
|
||||
final FileTableReader fileTable,
|
||||
final FrontendDocumentRequest request) {
|
||||
final var sourceHandle = fileTable.get(fileId);
|
||||
if (sourceHandle == null || sourceHandle.getCanonPath() == null) {
|
||||
return "";
|
||||
}
|
||||
final Path path = sourceHandle.getCanonPath().toAbsolutePath().normalize();
|
||||
if (request != null
|
||||
&& request.documentPath() != null
|
||||
&& path.equals(request.documentPath().toAbsolutePath().normalize())) {
|
||||
return request.documentText() == null ? "" : request.documentText();
|
||||
}
|
||||
return sourceHandle.readUtf8().orElse("");
|
||||
}
|
||||
|
||||
private static String frontendLocationKey(final FrontendDefinitionLocation location) {
|
||||
return location.path() + ":" + location.startOffset() + ":" + location.endOffset();
|
||||
}
|
||||
|
||||
private Optional<FrontendDefinitionLocation> toFrontendLocation(
|
||||
final PbsEditorialLocation location,
|
||||
final FileTableReader fileTable) {
|
||||
|
||||
+122
@@ -91,6 +91,73 @@ public final class PbsEditorialSupportService {
|
||||
return buildContext(sourceText, ast, supplementalTopDecls, feSurfaceContext).definitionAt(offset);
|
||||
}
|
||||
|
||||
public List<PbsEditorialLocation> references(
|
||||
final String sourceText,
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
final int offset) {
|
||||
return references(sourceText, ast, supplementalTopDecls, FESurfaceContext.empty(), offset);
|
||||
}
|
||||
|
||||
public List<PbsEditorialLocation> references(
|
||||
final String sourceText,
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
final FESurfaceContext feSurfaceContext,
|
||||
final int offset) {
|
||||
final EditorialDocument cursor = new EditorialDocument(sourceText, ast, supplementalTopDecls, feSurfaceContext);
|
||||
return references(cursor, offset, List.of(cursor));
|
||||
}
|
||||
|
||||
public List<PbsEditorialLocation> references(
|
||||
final EditorialDocument cursorDocument,
|
||||
final int offset,
|
||||
final List<EditorialDocument> projectDocuments) {
|
||||
if (cursorDocument == null) {
|
||||
return List.of();
|
||||
}
|
||||
final List<PbsEditorialLocation> identity = definition(
|
||||
cursorDocument.sourceText(),
|
||||
cursorDocument.ast(),
|
||||
cursorDocument.supplementalTopDecls(),
|
||||
cursorDocument.feSurfaceContext(),
|
||||
offset);
|
||||
if (identity.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
final Set<String> identityKeys = locationKeys(identity);
|
||||
final LinkedHashMap<String, PbsEditorialLocation> usages = new LinkedHashMap<>();
|
||||
final List<EditorialDocument> documents = projectDocuments == null || projectDocuments.isEmpty()
|
||||
? List.of(cursorDocument)
|
||||
: projectDocuments;
|
||||
for (final EditorialDocument document : documents) {
|
||||
if (document == null || document.ast() == null) {
|
||||
continue;
|
||||
}
|
||||
for (final PbsEditorialLocation usage : buildContext(
|
||||
document.sourceText(),
|
||||
document.ast(),
|
||||
document.supplementalTopDecls(),
|
||||
document.feSurfaceContext()).usagesMatching(identityKeys)) {
|
||||
usages.putIfAbsent(locationKey(usage.span()), usage);
|
||||
}
|
||||
}
|
||||
return List.copyOf(usages.values());
|
||||
}
|
||||
|
||||
public record EditorialDocument(
|
||||
String sourceText,
|
||||
PbsAst.File ast,
|
||||
ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
FESurfaceContext feSurfaceContext) {
|
||||
public EditorialDocument {
|
||||
sourceText = sourceText == null ? "" : sourceText;
|
||||
Objects.requireNonNull(ast, "ast");
|
||||
supplementalTopDecls = supplementalTopDecls == null ? ReadOnlyList.empty() : supplementalTopDecls;
|
||||
feSurfaceContext = feSurfaceContext == null ? FESurfaceContext.empty() : feSurfaceContext;
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<PbsEditorialSignatureHelp> signatureHelp(
|
||||
final String sourceText,
|
||||
final PbsAst.File ast,
|
||||
@@ -133,6 +200,25 @@ public final class PbsEditorialSupportService {
|
||||
return new DocumentContext(sourceText, ast, supplementalTopDecls, feSurfaceContext);
|
||||
}
|
||||
|
||||
private static Set<String> locationKeys(final List<PbsEditorialLocation> locations) {
|
||||
final LinkedHashSet<String> keys = new LinkedHashSet<>();
|
||||
if (locations == null) {
|
||||
return Set.of();
|
||||
}
|
||||
for (final PbsEditorialLocation location : locations) {
|
||||
if (location != null && location.span() != null) {
|
||||
keys.add(locationKey(location.span()));
|
||||
}
|
||||
}
|
||||
return Set.copyOf(keys);
|
||||
}
|
||||
|
||||
private static String locationKey(final Span span) {
|
||||
final FileId fileId = span.getFileId();
|
||||
final int fileNumber = fileId == null ? -1 : fileId.getId();
|
||||
return fileNumber + ":" + span.getStart() + ":" + span.getEnd();
|
||||
}
|
||||
|
||||
private static final class DocumentContext {
|
||||
private final String sourceText;
|
||||
private final PbsAst.File ast;
|
||||
@@ -642,6 +728,42 @@ public final class PbsEditorialSupportService {
|
||||
return resolveSymbolDefinition(token.lexeme(), tokenIndex, scopeState, offset);
|
||||
}
|
||||
|
||||
private List<PbsEditorialLocation> usagesMatching(final Set<String> identityKeys) {
|
||||
if (identityKeys == null || identityKeys.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
final FileId fileId = ast.span() == null ? FileId.none() : ast.span().getFileId();
|
||||
final LinkedHashMap<String, PbsEditorialLocation> usages = new LinkedHashMap<>();
|
||||
for (final PbsToken token : tokens) {
|
||||
if (token.kind() != PbsTokenKind.IDENTIFIER) {
|
||||
continue;
|
||||
}
|
||||
if (!intersects(definitionAt(token.start()), identityKeys)) {
|
||||
continue;
|
||||
}
|
||||
final Span usageSpan = new Span(fileId, token.start(), token.end());
|
||||
if (!hasPhysicalSpan(usageSpan)) {
|
||||
continue;
|
||||
}
|
||||
usages.putIfAbsent(locationKey(usageSpan), new PbsEditorialLocation(usageSpan));
|
||||
}
|
||||
return List.copyOf(usages.values());
|
||||
}
|
||||
|
||||
private boolean intersects(
|
||||
final List<PbsEditorialLocation> locations,
|
||||
final Set<String> identityKeys) {
|
||||
if (locations == null || locations.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (final PbsEditorialLocation location : locations) {
|
||||
if (location != null && location.span() != null && identityKeys.contains(locationKey(location.span()))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<PbsEditorialLocation> resolveSymbolDefinition(
|
||||
final String symbolName,
|
||||
final int tokenIndex,
|
||||
|
||||
+88
@@ -334,6 +334,94 @@ final class PbsEditorialSupportServiceTest {
|
||||
assertTrue(locations.get(1).span().contains(source.indexOf("fn blend(dx: int, dy: int)")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCollectSameFileReferencesForPhysicalSymbolsAndSkipTrivia() {
|
||||
final var source = """
|
||||
fn helper(value: int) -> int {
|
||||
let total = value;
|
||||
return total;
|
||||
}
|
||||
|
||||
fn frame() -> void {
|
||||
helper(1);
|
||||
helper(2);
|
||||
// helper
|
||||
let text = "helper";
|
||||
missingName;
|
||||
}
|
||||
""";
|
||||
final var ast = parseOrdinary(source);
|
||||
|
||||
final var helperRefs = service.references(source, ast, ReadOnlyList.empty(), source.indexOf("helper(1)"));
|
||||
assertEquals(3, helperRefs.size(), helperRefs.toString());
|
||||
assertTrue(helperRefs.stream().anyMatch(location -> location.span().contains(source.indexOf("helper(value"))));
|
||||
assertTrue(helperRefs.stream().anyMatch(location -> location.span().contains(source.indexOf("helper(1)"))));
|
||||
assertTrue(helperRefs.stream().anyMatch(location -> location.span().contains(source.indexOf("helper(2)"))));
|
||||
assertTrue(helperRefs.stream().noneMatch(location -> location.span().contains(source.indexOf("// helper"))));
|
||||
assertTrue(helperRefs.stream().noneMatch(location -> location.span().contains(source.indexOf("\"helper\""))));
|
||||
|
||||
final var parameterRefs = service.references(source, ast, ReadOnlyList.empty(), source.indexOf("value;"));
|
||||
assertEquals(2, parameterRefs.size());
|
||||
assertTrue(parameterRefs.stream().anyMatch(location -> location.span().contains(source.indexOf("value: int"))));
|
||||
assertTrue(parameterRefs.stream().anyMatch(location -> location.span().contains(source.indexOf("value;"))));
|
||||
|
||||
final var localRefs = service.references(source, ast, ReadOnlyList.empty(), source.indexOf("total;"));
|
||||
assertEquals(1, localRefs.size());
|
||||
assertTrue(localRefs.getFirst().span().contains(source.indexOf("total;")));
|
||||
assertTrue(service.definition(source, ast, ReadOnlyList.empty(), source.indexOf("total = value")).isEmpty());
|
||||
|
||||
assertTrue(service.references(source, ast, ReadOnlyList.empty(), source.indexOf("fn helper")).isEmpty());
|
||||
assertTrue(service.references(source, ast, ReadOnlyList.empty(), source.indexOf("int")).isEmpty());
|
||||
assertTrue(service.references(source, ast, ReadOnlyList.empty(), source.indexOf("missingName")).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCollectImportedAndOverloadedReferencesByDefinitionIntersection() {
|
||||
final var importedSource = """
|
||||
declare service Log {
|
||||
fn info(message: str) -> void {
|
||||
return;
|
||||
}
|
||||
}
|
||||
""";
|
||||
final var importedAst = parseInterface(importedSource, new FileId(100));
|
||||
final var source = """
|
||||
import { Log as Logger } from @sdk:log;
|
||||
|
||||
declare struct Vec() {
|
||||
fn blend(dx: int) -> int { return dx; }
|
||||
fn blend(dx: int, dy: int) -> int { return dx; }
|
||||
}
|
||||
|
||||
fn frame(vec: Vec) -> void {
|
||||
Logger.info("a");
|
||||
Logger.info("b");
|
||||
vec.blend(1, 2);
|
||||
}
|
||||
""";
|
||||
final var ast = parseOrdinary(source);
|
||||
final var supplementalTopDecls = importedAst.topDecls();
|
||||
|
||||
final var infoRefs = service.references(
|
||||
new PbsEditorialSupportService.EditorialDocument(source, ast, supplementalTopDecls, null),
|
||||
source.indexOf("info(\"a\")"),
|
||||
List.of(
|
||||
new PbsEditorialSupportService.EditorialDocument(source, ast, supplementalTopDecls, null),
|
||||
new PbsEditorialSupportService.EditorialDocument(
|
||||
importedSource,
|
||||
importedAst,
|
||||
ReadOnlyList.empty(),
|
||||
null)));
|
||||
assertEquals(2, infoRefs.stream().filter(location -> location.span().getFileId().getId() == 0).count());
|
||||
assertTrue(infoRefs.stream().anyMatch(location -> location.span().contains(source.indexOf("info(\"a\")"))));
|
||||
assertTrue(infoRefs.stream().anyMatch(location -> location.span().contains(source.indexOf("info(\"b\")"))));
|
||||
|
||||
final var blendRefs = service.references(source, ast, supplementalTopDecls, source.indexOf("blend(1"));
|
||||
assertTrue(blendRefs.stream().anyMatch(location -> location.span().contains(source.indexOf("blend(1"))));
|
||||
final var blendIdentity = service.definition(source, ast, supplementalTopDecls, source.indexOf("blend(1"));
|
||||
assertEquals(2, blendIdentity.size());
|
||||
}
|
||||
|
||||
private List<PbsEditorialLocation> requireDefinition(
|
||||
final String source,
|
||||
final PbsAst.File ast,
|
||||
|
||||
Reference in New Issue
Block a user