Repository navigation
Conversation
Implements the three LSP call-hierarchy methods (prepareCallHierarchy, incomingCalls, outgoingCalls) via a new LanguageServerCallHierarchy extension: - CallHierarchyHandler: registers callHierarchyProvider capability and serves the three methods using Reflector, ReferenceFinder, AstProvider and DefinitionLocator. - OutgoingCallsWalker: collects method/function calls within the enclosing function body. - Unit tests for the handler. Depends on phpactor/language-server-protocol PR phpactor#30 (CallHierarchyParams and the three request interfaces).
Switch the Call Hierarchy handler and tests from the hand-written PrepareCallHierarchyRequest / CallHierarchyParams to the generated CallHierarchyPrepareRequest / CallHierarchyIncomingCallsParams / CallHierarchyOutgoingCallsParams, matching the regenerated language-server-protocol.
The import became unused after switching to ChainReferenceFinder. PHP-CS-Fixer flagged it; removing it clears the CI CS-Fixer failure.
- Bump phpactor/language-server-protocol to ^3.17.6 (Call Hierarchy request interfaces) - Introduce Call value object and use it in OutgoingCallsWalker/CallHierarchyHandler - Use typed $container->expect in LanguageServerCallHierarchyExtension - Add MethodCallReferenceFinder unit test for cross-document method call references - Fix CompletionWorseExtensionTest assertion (assert 0 completions for minimal input) - Remove stale phpstan baseline entries for Call Hierarchy extension
Address review comment asking to test via LanguageServerTester and avoid mocks, following the GotoImplementationHandlerTest pattern. Replace Prophecy mocks with a real Reflector (ReflectorBuilder), real reference finders (TolerantVariableReferenceFinder + MethodCallReferenceFinder) and a real WorseReflectionDefinitionLocator. Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
| try { | ||
| $lspDoc = $this->workspace->get($uri); | ||
| } catch (UnknownDocument) { | ||
| $range = RangeConverter::toLspRange($location->range(), ''); |
There was a problem hiding this comment.
is there a reason to catch this exception? converting a range with an empty string will resul in Range(0,0,0,0) unconditionally.
| try { | ||
| $targetText = $this->workspace->get($targetUri)->text; | ||
| } catch (UnknownDocument) { | ||
| $range = RangeConverter::toLspRange(ByteOffsetRange::fromInts($offset, $offset), $text); |
There was a problem hiding this comment.
similarly this will result in the wrong range which wouldn't be useful. Unless there is a reason I would let the exception bubble up (alternatively returning null and skipping)
| return new CallHierarchyItem($name, $kind === 'method' ? SymbolKind::METHOD : SymbolKind::FUNCTION, $targetUri, $range, $range); | ||
| } catch (CouldNotLocateDefinition) { | ||
| $range = RangeConverter::toLspRange(ByteOffsetRange::fromInts($offset, $offset), $text); | ||
| return new CallHierarchyItem($name, $kind === 'method' ? SymbolKind::METHOD : SymbolKind::FUNCTION, $uri, $range, $range); |
|
|
||
| /** | ||
| * Finds references to a method or function by searching all open documents | ||
| * in the workspace for matching call expressions. |
There was a problem hiding this comment.
is there a specific reason to only search opened documents vs. the entire workspace? is that what other implementations do?
Phpactor reference finding can be very slow accross the entire workspace so it's not necessarily a bad thing to do this but not sure it's what would be expected.
| return []; | ||
| } | ||
|
|
||
| $walker = new OutgoingCallsWalker($bodyRange); |
There was a problem hiding this comment.
using the Reflector to walk the AST will perform static analysis which can be very slow. It seems this just needs to collect method calls without resolving the types they are being called on - in which case no analysis of the AST is required.
You can simply call:
foreach ($node->getDescendantNodes() as $descendantNode) {
if (!$descendantNode instance of MemberAcessNode) {
continue;
}
// ...
}
|
looks generally good, some questions and suggestions.
|
|
ok, this PR can now be rebased on develop (the protocol is already updated there). |
Summary
Implements the LSP Call Hierarchy capability in phpactor's language server,
so clients (e.g. Qwen Code) can use
textDocument/prepareCallHierarchy,textDocument/callHierarchy/incomingCallsandtextDocument/callHierarchy/outgoingCalls.Changes
LanguageServerCallHierarchyextension registering aCallHierarchyHandleras an LSP method handler.CallHierarchyHandlerregisters thecallHierarchyProvidercapability andserves the three methods using
Reflector,ReferenceFinder,AstProviderandDefinitionLocator.OutgoingCallsWalkercollects method/function calls inside the enclosingfunction body.
lib/Phpactor.php.Dependency
This PR depends on phpactor/language-server-protocol#30 (adds
CallHierarchyParamsand the three request interfaces). Thephpactor/language-server-protocoldependency must be bumped to the taggedrelease containing those classes before this PR can pass
composer integrate(phpstan/phpunit).