From 1fd51c0cc983b7551108d0f5177c728e44fbfb1e Mon Sep 17 00:00:00 2001 From: Daniel Leech Date: Sat, 6 Apr 2024 14:09:06 +0100 Subject: [PATCH] GH-2603: Do not filter static method invocations --- .../Completor/ContextSensitiveCompletor.php | 16 ++++++++++++---- .../Completor/ContextSensitiveCompletorTest.php | 9 +++++---- lib/Indexer/Model/IndexJob.php | 4 ---- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/Completion/Bridge/WorseReflection/Completor/ContextSensitiveCompletor.php b/lib/Completion/Bridge/WorseReflection/Completor/ContextSensitiveCompletor.php index f4f5e08f8..4583d9006 100644 --- a/lib/Completion/Bridge/WorseReflection/Completor/ContextSensitiveCompletor.php +++ b/lib/Completion/Bridge/WorseReflection/Completor/ContextSensitiveCompletor.php @@ -35,6 +35,13 @@ public function __construct(private TolerantCompletor $inner, private Reflector public function complete(Node $node, TextDocument $source, ByteOffset $offset): Generator { $generator = $this->inner->complete($node, $source, $offset); + if ( + !$node instanceof CallExpression && + ($node instanceof QualifiedName && !$node->parent instanceof ObjectCreationExpression) + ) { + yield from $generator; + return $generator->getReturn(); + } $type = $this->resolveFilterableType($node, $source, $offset); if (null === $type) { @@ -77,6 +84,7 @@ private function resolveFilterableType(Node $node, TextDocument $source, ByteOff { $argumentNb = 0; $memberAccessOrObjectCreation = $node; + $node = NodeUtil::firstDescendantNodeBeforeOffset($node, $offset->toInt()); if ($node instanceof QualifiedName) { $memberAccessOrObjectCreation = null; $argumentExpression = $node->getFirstAncestor(ArgumentExpression::class); @@ -89,9 +97,10 @@ private function resolveFilterableType(Node $node, TextDocument $source, ByteOff $memberAccessOrObjectCreation = $list->parent; } } - if ($node instanceof ArgumentExpressionList) { - $argumentNb = count(iterator_to_array($node->getValues())); - $memberAccessOrObjectCreation = $node->parent; + $argumentList = $node->getFirstAncestor(ArgumentExpressionList::class); + if ($argumentList instanceof ArgumentExpressionList) { + $argumentNb = max(0, count(iterator_to_array($argumentList->getValues())) - 1); + $memberAccessOrObjectCreation = $argumentList->parent; } if (!$memberAccessOrObjectCreation instanceof CallExpression && !$memberAccessOrObjectCreation instanceof ObjectCreationExpression) { @@ -103,7 +112,6 @@ private function resolveFilterableType(Node $node, TextDocument $source, ByteOff return null; } try { - ; $memberAccessOrObjectCreation = $this->reflector->reflectOffset($source, $offset)->nodeContext(); } catch (NotFound $e) { return null; diff --git a/lib/Completion/Tests/Unit/Bridge/WorseReflection/Completor/ContextSensitiveCompletorTest.php b/lib/Completion/Tests/Unit/Bridge/WorseReflection/Completor/ContextSensitiveCompletorTest.php index f72e2bb9f..367747add 100644 --- a/lib/Completion/Tests/Unit/Bridge/WorseReflection/Completor/ContextSensitiveCompletorTest.php +++ b/lib/Completion/Tests/Unit/Bridge/WorseReflection/Completor/ContextSensitiveCompletorTest.php @@ -78,7 +78,7 @@ class Obj {} class Foo { public function bar(Obj $obj){}} $f = new Foo(); - $f->bar(<>) + $f->bar(F<>) EOT, [ 'Bar\Foo', @@ -97,7 +97,7 @@ class Obj {} class Foo { public function bar(Obj $obj){}} $f = new Foo(); - $f->bar(new <> + $f->bar(new <>) EOT, [ 'Bar\Obj', @@ -114,7 +114,7 @@ class Obj {} class Foo { public function bar(Obj $obj){}} $f = new Foo(); - $f->bar(new <> + $f->bar(new <>) EOT, [ 'Obj', @@ -167,12 +167,13 @@ class Baz {} class Foo { public function bar(Obj $obj, Baz $baz){}} $f = new Foo(); - $f->bar(Obj::new(),new <> + $f->bar(Obj::new(), new <>) EOT, [ 'Baz', ], ]; + yield '2nd arg partial' => [ [ 'Obj', diff --git a/lib/Indexer/Model/IndexJob.php b/lib/Indexer/Model/IndexJob.php index 35838985d..d714a2948 100644 --- a/lib/Indexer/Model/IndexJob.php +++ b/lib/Indexer/Model/IndexJob.php @@ -3,12 +3,8 @@ namespace Phpactor\Indexer\Model; use Generator; -use Phar; -use PharFileInfo; use Phpactor\TextDocument\TextDocumentBuilder; -use RecursiveIteratorIterator; use SplFileInfo; -use UnexpectedValueException; class IndexJob {