1
0
Fork 0

CompletionItemFactory and imported missing names in SymbolInformationFactory

pull/661/head
dantleech 2018-07-29 15:37:14 +01:00 committed by Daniel Leech
parent 641b1c2907
commit d05d02ce8a
4 changed files with 46 additions and 7 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace LanguageServer; namespace LanguageServer;
use LanguageServer\Index\ReadableIndex; use LanguageServer\Index\ReadableIndex;
use LanguageServer\ProtocolBridge\CompletionItemFactory;
use LanguageServer\Protocol\{ use LanguageServer\Protocol\{
TextEdit, TextEdit,
Range, Range,
@ -246,7 +247,7 @@ class CompletionProvider
foreach ($this->index->getDefinitions() as $fqn => $def) { foreach ($this->index->getDefinitions() as $fqn => $def) {
foreach ($prefixes as $prefix) { foreach ($prefixes as $prefix) {
if (substr($fqn, 0, strlen($prefix)) === $prefix && $def->isMember) { if (substr($fqn, 0, strlen($prefix)) === $prefix && $def->isMember) {
$list->items[] = CompletionItem::fromDefinition($def); $list->items[] = CompletionItemFactory::fromDefinition($def);
} }
} }
} }
@ -279,7 +280,7 @@ class CompletionProvider
foreach ($this->index->getDefinitions() as $fqn => $def) { foreach ($this->index->getDefinitions() as $fqn => $def) {
foreach ($prefixes as $prefix) { foreach ($prefixes as $prefix) {
if (substr(strtolower($fqn), 0, strlen($prefix)) === strtolower($prefix) && $def->isMember) { if (substr(strtolower($fqn), 0, strlen($prefix)) === strtolower($prefix) && $def->isMember) {
$list->items[] = CompletionItem::fromDefinition($def); $list->items[] = CompletionItemFactory::fromDefinition($def);
} }
} }
} }
@ -337,7 +338,7 @@ class CompletionProvider
foreach ($aliases as $alias => $fqn) { foreach ($aliases as $alias => $fqn) {
// Suggest symbols that have been `use`d and match the prefix // Suggest symbols that have been `use`d and match the prefix
if (substr($alias, 0, $prefixLen) === $prefix && ($def = $this->index->getDefinition($fqn))) { if (substr($alias, 0, $prefixLen) === $prefix && ($def = $this->index->getDefinition($fqn))) {
$list->items[] = CompletionItem::fromDefinition($def); $list->items[] = CompletionItemFactory::fromDefinition($def);
} }
} }
} }
@ -370,7 +371,7 @@ class CompletionProvider
// Only suggest classes for `new` // Only suggest classes for `new`
&& (!isset($creation) || $def->canBeInstantiated) && (!isset($creation) || $def->canBeInstantiated)
) { ) {
$item = CompletionItem::fromDefinition($def); $item = CompletionItemFactory::fromDefinition($def);
// Find the shortest name to reference the symbol // Find the shortest name to reference the symbol
if ($namespaceNode && ($alias = array_search($fqn, $aliases, true)) !== false) { if ($namespaceNode && ($alias = array_search($fqn, $aliases, true)) !== false) {
// $alias is the name under which this definition is aliased in the current namespace // $alias is the name under which this definition is aliased in the current namespace

View File

@ -0,0 +1,36 @@
<?php
namespace LanguageServer\ProtocolBridge;
use LanguageServer\Definition;
use LanguageServer\Protocol\CompletionItem;
use LanguageServer\Protocol\CompletionItemKind;
use LanguageServer\Protocol\SymbolKind;
class CompletionItemFactory
{
/**
* Creates a CompletionItem for a Definition
*
* @param Definition $def
* @return CompletionItem|null
*/
public static function fromDefinition(Definition $def)
{
$item = new CompletionItem;
$item->label = $def->symbolInformation->name;
$item->kind = CompletionItemKind::fromSymbolKind($def->symbolInformation->kind);
if ($def->type) {
$item->detail = (string)$def->type;
} else if ($def->symbolInformation->containerName) {
$item->detail = $def->symbolInformation->containerName;
}
if ($def->documentation) {
$item->documentation = $def->documentation;
}
if ($def->isStatic && $def->symbolInformation->kind === SymbolKind::PROPERTY) {
$item->insertText = '$' . $def->symbolInformation->name;
}
return $item;
}
}

View File

@ -5,6 +5,8 @@ namespace LanguageServer\ProtocolBridge;
use LanguageServer\Protocol\Location; use LanguageServer\Protocol\Location;
use LanguageServer\Protocol\SymbolInformation; use LanguageServer\Protocol\SymbolInformation;
use LanguageServer\Protocol\SymbolKind; use LanguageServer\Protocol\SymbolKind;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\ResolvedName;
class SymbolInformationFactory class SymbolInformationFactory
{ {
@ -15,7 +17,7 @@ class SymbolInformationFactory
* @param string $fqn If given, $containerName will be extracted from it * @param string $fqn If given, $containerName will be extracted from it
* @return SymbolInformation|null * @return SymbolInformation|null
*/ */
public function fromNode($node, string $fqn = null) public function fromNode($node, string $fqn = null):? SymbolInformation
{ {
$symbol = new SymbolInformation(); $symbol = new SymbolInformation();
if ($node instanceof Node\Statement\ClassDeclaration) { if ($node instanceof Node\Statement\ClassDeclaration) {
@ -67,7 +69,7 @@ class SymbolInformationFactory
$symbol->name = $node->getName(); $symbol->name = $node->getName();
} else if (isset($node->name)) { } else if (isset($node->name)) {
if ($node->name instanceof Node\QualifiedName) { if ($node->name instanceof Node\QualifiedName) {
$symbol->name = (string)PhpParser\ResolvedName::buildName($node->name->nameParts, $node->getFileContents()); $symbol->name = (string)ResolvedName::buildName($node->name->nameParts, $node->getFileContents());
} else { } else {
$symbol->name = ltrim((string)$node->name->getText($node->getFileContents()), "$"); $symbol->name = ltrim((string)$node->name->getText($node->getFileContents()), "$");
} }

View File

@ -5,8 +5,8 @@ namespace LanguageServer\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use LanguageServer\LanguageServer; use LanguageServer\LanguageServer;
use LanguageServer\ProtocolBridge\Message;
use LanguageServer\Protocol\{ use LanguageServer\Protocol\{
Message,
ClientCapabilities, ClientCapabilities,
TextDocumentSyncKind, TextDocumentSyncKind,
MessageType, MessageType,