diff --git a/src/CompletionProvider.php b/src/CompletionProvider.php index 261bc1d..5d65ea3 100644 --- a/src/CompletionProvider.php +++ b/src/CompletionProvider.php @@ -4,12 +4,10 @@ declare(strict_types = 1); namespace LanguageServer; use PhpParser\Node; -use phpDocumentor\Reflection\Types; use LanguageServer\Protocol\{ TextEdit, Range, Position, - SymbolKind, CompletionList, CompletionItem, CompletionItemKind @@ -134,30 +132,25 @@ class CompletionProvider || $node instanceof Node\Expr\StaticPropertyFetch || $node instanceof Node\Expr\ClassConstFetch ) { - if (!is_string($node->name)) { - // If the name is an Error node, just filter by the class - if ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\PropertyFetch) { - // For instances, resolve the variable type - $prefixes = DefinitionResolver::getFqnsFromType( - $this->definitionResolver->resolveExpressionNodeToType($node->var) - ); - } else { - $prefixes = [$node->class instanceof Node\Name ? (string)$node->class : '']; - } - // If we are just filtering by the class, add the appropiate operator to the prefix - // to filter the type of symbol - foreach ($prefixes as &$prefix) { - if ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\PropertyFetch) { - $prefix .= '->'; - } else if ($node instanceof Node\Expr\StaticCall || $node instanceof Node\Expr\ClassConstFetch) { - $prefix .= '::'; - } else if ($node instanceof Node\Expr\StaticPropertyFetch) { - $prefix .= '::$'; - } - } + // If the name is an Error node, just filter by the class + if ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\PropertyFetch) { + // For instances, resolve the variable type + $prefixes = DefinitionResolver::getFqnsFromType( + $this->definitionResolver->resolveExpressionNodeToType($node->var) + ); } else { - $fqn = $this->definitionResolver->resolveReferenceNodeToFqn($node); - $prefixes = $fqn !== null ? [$fqn] : []; + $prefixes = [$node->class instanceof Node\Name ? (string)$node->class : '']; + } + // If we are just filtering by the class, add the appropiate operator to the prefix + // to filter the type of symbol + foreach ($prefixes as &$prefix) { + if ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\PropertyFetch) { + $prefix .= '->'; + } else if ($node instanceof Node\Expr\StaticCall || $node instanceof Node\Expr\ClassConstFetch) { + $prefix .= '::'; + } else if ($node instanceof Node\Expr\StaticPropertyFetch) { + $prefix .= '::$'; + } } foreach ($this->project->getDefinitions() as $fqn => $def) { diff --git a/tests/Server/TextDocument/CompletionTest.php b/tests/Server/TextDocument/CompletionTest.php index 7b5dd0a..f32503c 100644 --- a/tests/Server/TextDocument/CompletionTest.php +++ b/tests/Server/TextDocument/CompletionTest.php @@ -254,10 +254,25 @@ class CompletionTest extends TestCase new Position(2, 13) )->wait(); $this->assertEquals(new CompletionList([ + new CompletionItem( + 'TEST_CLASS_CONST', + CompletionItemKind::VARIABLE, + 'int', + 'Anim labore veniam consectetur laboris minim quis aute aute esse nulla ad.' + ), + new CompletionItem( + 'staticTestProperty', + CompletionItemKind::PROPERTY, + '\TestClass[]', + 'Lorem excepteur officia sit anim velit veniam enim.', + null, + null, + '$staticTestProperty' + ), new CompletionItem( 'staticTestMethod', CompletionItemKind::METHOD, - 'mixed', // Method return type + 'mixed', 'Do magna consequat veniam minim proident eiusmod incididunt aute proident.' ) ], true), $items); @@ -277,6 +292,21 @@ class CompletionTest extends TestCase CompletionItemKind::VARIABLE, 'int', 'Anim labore veniam consectetur laboris minim quis aute aute esse nulla ad.' + ), + new CompletionItem( + 'staticTestProperty', + CompletionItemKind::PROPERTY, + '\TestClass[]', + 'Lorem excepteur officia sit anim velit veniam enim.', + null, + null, + '$staticTestProperty' + ), + new CompletionItem( + 'staticTestMethod', + CompletionItemKind::METHOD, + 'mixed', + 'Do magna consequat veniam minim proident eiusmod incididunt aute proident.' ) ], true), $items); }