implements PLN-0137 (7/7) call-hierarchy

Call hierarchy walks CallExpr nodes for functions and methods. It does
not use the references index. A cursor that is not on a callable is
empty, and type hierarchy stays unadvertised.

Housekeep DSC-0066 with LSN-0074.
This commit is contained in:
2026-09-22 09:20:28 +01:00
parent e456bb8d7a
commit 1fcecdb04d
24 changed files with 995 additions and 333 deletions
@@ -16,12 +16,15 @@ import p.studio.compiler.pbs.semantics.PbsEditorialSupportService.EditorialDocum
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.pbs.semantics.PbsEditorialRename;
import p.studio.compiler.pbs.semantics.PbsEditorialWorkspaceSymbol;
import p.studio.compiler.pbs.PbsCallHierarchy;
import p.studio.compiler.pbs.PbsDocumentFormatter;
import p.studio.compiler.pbs.PbsSemanticTokenOverlay;
import p.studio.compiler.pbs.PbsStructureRanges;
import p.studio.compiler.pbs.semantics.PbsQuickFix;
import p.studio.compiler.pbs.semantics.PbsQuickFixCollector;
import p.studio.compiler.services.PBSFrontendPhaseService.PbsSemanticReadSurface;
import p.studio.compiler.services.FrontendCallHierarchyItem;
import p.studio.compiler.services.FrontendCallSite;
import p.studio.compiler.services.FrontendCodeAction;
import p.studio.compiler.services.FrontendCompletionCandidate;
import p.studio.compiler.services.FrontendSymbolKind;
@@ -482,6 +485,77 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
offsets);
}
@Override
public boolean callHierarchySupported() {
return true;
}
@Override
public List<FrontendCallHierarchyItem> prepareCallHierarchy(
final FrontendDocumentRequest request,
final FrontendEditorialContext editorialContext,
final int offset) {
final PbsAst.File ast = callHierarchyAst(editorialContext);
if (ast == null) {
return List.of();
}
return PbsCallHierarchy.prepare(ast, offset).stream().map(this::toCallItem).toList();
}
@Override
public List<FrontendCallSite> incomingCalls(
final FrontendDocumentRequest request,
final FrontendEditorialContext editorialContext,
final FrontendCallHierarchyItem target) {
return callSites(callHierarchyAst(editorialContext), target, true);
}
@Override
public List<FrontendCallSite> outgoingCalls(
final FrontendDocumentRequest request,
final FrontendEditorialContext editorialContext,
final FrontendCallHierarchyItem target) {
return callSites(callHierarchyAst(editorialContext), target, false);
}
private List<FrontendCallSite> callSites(
final PbsAst.File ast,
final FrontendCallHierarchyItem target,
final boolean incoming) {
if (ast == null || target == null) {
return List.of();
}
final PbsCallHierarchy.Callable callable = new PbsCallHierarchy.Callable(
target.name(),
target.method(),
target.startOffset(),
target.endOffset());
final var calls = incoming
? PbsCallHierarchy.incomingCalls(ast, callable)
: PbsCallHierarchy.outgoingCalls(ast, callable);
return calls.stream()
.map(call -> new FrontendCallSite(
toCallItem(incoming ? call.caller() : call.callee()),
call.startOffset(),
call.endOffset()))
.toList();
}
private static PbsAst.File callHierarchyAst(final FrontendEditorialContext editorialContext) {
if (editorialContext == null || !(editorialContext.syntaxTree() instanceof PbsAst.File ast)) {
return null;
}
return ast;
}
private FrontendCallHierarchyItem toCallItem(final PbsCallHierarchy.Callable callable) {
return new FrontendCallHierarchyItem(
callable.name(),
callable.method(),
callable.startOffset(),
callable.endOffset());
}
@Override
public boolean formattingSupported() {
return true;
@@ -0,0 +1,335 @@
package p.studio.compiler.pbs;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.source.Span;
import java.util.ArrayList;
import java.util.List;
public final class PbsCallHierarchy {
private PbsCallHierarchy() {
}
public record Callable(
String name,
boolean method,
int startOffset,
int endOffset) {
}
public record CallSite(
Callable caller,
Callable callee,
int startOffset,
int endOffset) {
}
public static List<Callable> prepare(final PbsAst.File ast, final int offset) {
Callable best = null;
for (final Callable callable : callables(ast)) {
if (callable.startOffset() > offset || offset >= callable.endOffset()) {
continue;
}
if (best == null || callable.endOffset() - callable.startOffset() < best.endOffset() - best.startOffset()) {
best = callable;
}
}
return best == null ? List.of() : List.of(best);
}
public static List<CallSite> incomingCalls(final PbsAst.File ast, final Callable target) {
if (target == null) {
return List.of();
}
final ArrayList<CallSite> calls = new ArrayList<>();
for (final CallSite call : calls(ast)) {
if (sameCallable(call.callee(), target)) {
calls.add(call);
}
}
return List.copyOf(calls);
}
public static List<CallSite> outgoingCalls(final PbsAst.File ast, final Callable target) {
if (target == null) {
return List.of();
}
final ArrayList<CallSite> calls = new ArrayList<>();
for (final CallSite call : calls(ast)) {
if (sameCallable(call.caller(), target)) {
calls.add(call);
}
}
return List.copyOf(calls);
}
private static boolean sameCallable(final Callable left, final Callable right) {
return left != null
&& right != null
&& left.method() == right.method()
&& left.name().equals(right.name())
&& left.startOffset() == right.startOffset()
&& left.endOffset() == right.endOffset();
}
private static List<Callable> callables(final PbsAst.File ast) {
final ArrayList<Callable> callables = new ArrayList<>();
if (ast == null || ast.topDecls() == null) {
return callables;
}
for (final PbsAst.TopDecl topDecl : ast.topDecls()) {
collectCallables(topDecl, callables);
}
return callables;
}
private static void collectCallables(final PbsAst.TopDecl topDecl, final List<Callable> callables) {
switch (topDecl) {
case null -> {
}
case PbsAst.FunctionDecl functionDecl -> callables.add(callable(functionDecl.name(), false, functionDecl.span()));
case PbsAst.StructDecl structDecl -> {
if (structDecl.methods() != null) {
for (final PbsAst.FunctionDecl method : structDecl.methods()) {
if (method != null) {
callables.add(callable(method.name(), true, method.span()));
}
}
}
if (structDecl.ctors() != null) {
for (final PbsAst.CtorDecl ctor : structDecl.ctors()) {
if (ctor != null) {
callables.add(callable(ctor.name(), true, ctor.span()));
}
}
}
}
case PbsAst.ServiceDecl serviceDecl -> {
if (serviceDecl.methods() != null) {
for (final PbsAst.FunctionDecl method : serviceDecl.methods()) {
if (method != null) {
callables.add(callable(method.name(), true, method.span()));
}
}
}
}
default -> {
}
}
}
private static List<CallSite> calls(final PbsAst.File ast) {
final List<Callable> callables = callables(ast);
final ArrayList<CallSite> calls = new ArrayList<>();
for (final Callable caller : callables) {
final PbsAst.Block body = body(ast, caller);
if (body == null) {
continue;
}
final ArrayList<PbsAst.CallExpr> found = new ArrayList<>();
collectCalls(body, found);
for (final PbsAst.CallExpr call : found) {
final Callable callee = resolveCallee(call, callables);
final int[] span = offsets(call.span());
if (callee == null || span == null) {
continue;
}
calls.add(new CallSite(caller, callee, span[0], span[1]));
}
}
return calls;
}
private static Callable resolveCallee(final PbsAst.CallExpr call, final List<Callable> callables) {
if (call == null || call.callee() == null) {
return null;
}
final boolean method = call.callee() instanceof PbsAst.MemberExpr;
final String name = switch (call.callee()) {
case PbsAst.IdentifierExpr identifier -> identifier.name();
case PbsAst.MemberExpr member -> member.memberName();
default -> null;
};
if (name == null) {
return null;
}
for (final Callable callable : callables) {
if (callable.method() == method && callable.name().equals(name)) {
return callable;
}
}
return null;
}
private static PbsAst.Block body(final PbsAst.File ast, final Callable caller) {
for (final Callable callable : callables(ast)) {
if (!sameCallable(callable, caller)) {
continue;
}
}
return findBody(ast, caller);
}
private static PbsAst.Block findBody(final PbsAst.File ast, final Callable caller) {
if (ast.topDecls() == null) {
return null;
}
for (final PbsAst.TopDecl topDecl : ast.topDecls()) {
final PbsAst.Block body = findBody(topDecl, caller);
if (body != null) {
return body;
}
}
return null;
}
private static PbsAst.Block findBody(final PbsAst.TopDecl topDecl, final Callable caller) {
switch (topDecl) {
case null -> {
return null;
}
case PbsAst.FunctionDecl functionDecl -> {
if (matches(functionDecl.name(), false, functionDecl.span(), caller)) {
return functionDecl.body();
}
return null;
}
case PbsAst.StructDecl structDecl -> {
if (structDecl.methods() != null) {
for (final PbsAst.FunctionDecl method : structDecl.methods()) {
if (method != null && matches(method.name(), true, method.span(), caller)) {
return method.body();
}
}
}
if (structDecl.ctors() != null) {
for (final PbsAst.CtorDecl ctor : structDecl.ctors()) {
if (ctor != null && matches(ctor.name(), true, ctor.span(), caller)) {
return ctor.body();
}
}
}
return null;
}
case PbsAst.ServiceDecl serviceDecl -> {
if (serviceDecl.methods() == null) {
return null;
}
for (final PbsAst.FunctionDecl method : serviceDecl.methods()) {
if (method != null && matches(method.name(), true, method.span(), caller)) {
return method.body();
}
}
return null;
}
default -> {
return null;
}
}
}
private static void collectCalls(final PbsAst.Block block, final List<PbsAst.CallExpr> calls) {
if (block == null || block.statements() == null) {
return;
}
for (final PbsAst.Statement statement : block.statements()) {
collectCalls(statement, calls);
}
collectCalls(block.tailExpression(), calls);
}
private static void collectCalls(final PbsAst.Statement statement, final List<PbsAst.CallExpr> calls) {
switch (statement) {
case null -> {
}
case PbsAst.LetStatement letStatement -> collectCalls(letStatement.initializer(), calls);
case PbsAst.ReturnStatement returnStatement -> collectCalls(returnStatement.value(), calls);
case PbsAst.AssignStatement assignStatement -> collectCalls(assignStatement.value(), calls);
case PbsAst.ExpressionStatement expressionStatement -> collectCalls(expressionStatement.expression(), calls);
case PbsAst.IfStatement ifStatement -> {
collectCalls(ifStatement.condition(), calls);
collectCalls(ifStatement.thenBlock(), calls);
collectCalls(ifStatement.elseBlock(), calls);
collectCalls(ifStatement.elseIf(), calls);
}
case PbsAst.ForStatement forStatement -> {
collectCalls(forStatement.fromExpression(), calls);
collectCalls(forStatement.untilExpression(), calls);
collectCalls(forStatement.stepExpression(), calls);
collectCalls(forStatement.body(), calls);
}
case PbsAst.WhileStatement whileStatement -> {
collectCalls(whileStatement.condition(), calls);
collectCalls(whileStatement.body(), calls);
}
default -> {
}
}
}
private static void collectCalls(final PbsAst.Expression expression, final List<PbsAst.CallExpr> calls) {
switch (expression) {
case null -> {
}
case PbsAst.CallExpr callExpr -> {
calls.add(callExpr);
collectCalls(callExpr.callee(), calls);
if (callExpr.arguments() != null) {
for (final PbsAst.Expression argument : callExpr.arguments()) {
collectCalls(argument, calls);
}
}
}
case PbsAst.MemberExpr memberExpr -> collectCalls(memberExpr.receiver(), calls);
case PbsAst.BinaryExpr binaryExpr -> {
collectCalls(binaryExpr.left(), calls);
collectCalls(binaryExpr.right(), calls);
}
case PbsAst.UnaryExpr unaryExpr -> collectCalls(unaryExpr.expression(), calls);
case PbsAst.GroupExpr groupExpr -> collectCalls(groupExpr.expression(), calls);
case PbsAst.IfExpr ifExpr -> {
collectCalls(ifExpr.condition(), calls);
collectCalls(ifExpr.thenBlock(), calls);
collectCalls(ifExpr.elseExpression(), calls);
}
case PbsAst.BlockExpr blockExpr -> collectCalls(blockExpr.block(), calls);
case PbsAst.ApplyExpr applyExpr -> {
collectCalls(applyExpr.callee(), calls);
collectCalls(applyExpr.argument(), calls);
}
default -> {
}
}
}
private static boolean matches(
final String name,
final boolean method,
final Span span,
final Callable caller) {
final Callable callable = callable(name, method, span);
return callable != null && sameCallable(callable, caller);
}
private static Callable callable(final String name, final boolean method, final Span span) {
final int[] offsets = offsets(span);
if (name == null || name.isBlank() || offsets == null) {
return null;
}
return new Callable(name, method, offsets[0], offsets[1]);
}
private static int[] offsets(final Span span) {
if (span == null || span.getEnd() <= span.getStart()) {
return null;
}
return new int[] {toOffset(span.getStart()), toOffset(span.getEnd())};
}
private static int toOffset(final long value) {
if (value <= 0L) {
return 0;
}
return (int) Math.min(Integer.MAX_VALUE, value);
}
}
@@ -0,0 +1,73 @@
package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PbsCallHierarchyTest {
private static final String SOURCE = """
fn helper() -> int {
return 1;
}
declare struct Box() {
fn blend(dx: int) -> int {
return dx;
}
}
fn main(box: Box) -> int {
box.blend(1);
return helper();
}
""";
@Test
void functionAndMethodCallsComeFromTheAst() {
final PbsAst.File ast = parse(SOURCE);
final int mainOffset = byteOffset(SOURCE, "fn main");
final var prepared = PbsCallHierarchy.prepare(ast, mainOffset);
assertEquals(1, prepared.size());
assertEquals("main", prepared.getFirst().name());
assertEquals(false, prepared.getFirst().method());
final var outgoing = PbsCallHierarchy.outgoingCalls(ast, prepared.getFirst());
assertTrue(outgoing.stream().anyMatch(call -> call.callee().name().equals("helper") && !call.callee().method()));
assertTrue(outgoing.stream().anyMatch(call -> call.callee().name().equals("blend") && call.callee().method()));
final int helperOffset = byteOffset(SOURCE, "fn helper");
final var helper = PbsCallHierarchy.prepare(ast, helperOffset).getFirst();
final var incoming = PbsCallHierarchy.incomingCalls(ast, helper);
assertEquals(1, incoming.size());
assertEquals("main", incoming.getFirst().caller().name());
}
@Test
void aStructNameIsNotACallable() {
final PbsAst.File ast = parse(SOURCE);
assertTrue(PbsCallHierarchy.prepare(ast, byteOffset(SOURCE, "struct Box")).isEmpty());
}
private static PbsAst.File parse(final String source) {
return PbsParser.parse(
PbsLexer.lex(source, FileId.none(), DiagnosticSink.empty()),
FileId.none(),
DiagnosticSink.empty());
}
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;
}
}
@@ -0,0 +1,20 @@
package p.studio.compiler.services;
import java.util.Objects;
public record FrontendCallHierarchyItem(
String name,
boolean method,
int startOffset,
int endOffset) {
public FrontendCallHierarchyItem {
name = Objects.requireNonNull(name, "name").trim();
if (name.isEmpty()) {
throw new IllegalArgumentException("name must not be blank");
}
if (startOffset < 0 || endOffset < startOffset) {
throw new IllegalArgumentException("call hierarchy range is invalid");
}
}
}
@@ -0,0 +1,16 @@
package p.studio.compiler.services;
import java.util.Objects;
public record FrontendCallSite(
FrontendCallHierarchyItem item,
int startOffset,
int endOffset) {
public FrontendCallSite {
item = Objects.requireNonNull(item, "item");
if (startOffset < 0 || endOffset < startOffset) {
throw new IllegalArgumentException("call site range is invalid");
}
}
}
@@ -190,6 +190,31 @@ public interface FrontendLanguageService {
return selectionRanges(request, offsets);
}
default boolean callHierarchySupported() {
return false;
}
default java.util.List<FrontendCallHierarchyItem> prepareCallHierarchy(
final FrontendDocumentRequest request,
final FrontendEditorialContext editorialContext,
final int offset) {
return java.util.List.of();
}
default java.util.List<FrontendCallSite> incomingCalls(
final FrontendDocumentRequest request,
final FrontendEditorialContext editorialContext,
final FrontendCallHierarchyItem target) {
return java.util.List.of();
}
default java.util.List<FrontendCallSite> outgoingCalls(
final FrontendDocumentRequest request,
final FrontendEditorialContext editorialContext,
final FrontendCallHierarchyItem target) {
return java.util.List.of();
}
default boolean formattingSupported() {
return false;
}
@@ -36,6 +36,8 @@ class FrontendLanguageServiceTest {
assertFalse(service.foldingRangesSupported());
assertTrue(service.foldingRanges(request).isEmpty());
assertTrue(service.foldingRanges(request, null).isEmpty());
assertFalse(service.callHierarchySupported());
assertTrue(service.prepareCallHierarchy(request, null, 0).isEmpty());
assertFalse(service.formattingSupported());
assertTrue(service.formatDocument(request).isEmpty());
assertFalse(service.selectionRangesSupported());