Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

Commit

Permalink
remove return of class imports in use context (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 authored Jul 1, 2021
1 parent f0a080d commit e4486dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
18 changes: 15 additions & 3 deletions lib/Core/Completor/NameSearcherCompletor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Generator;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\NamespaceUseClause;
use Phpactor\Completion\Core\DocumentPrioritizer\DefaultResultPrioritizer;
use Phpactor\Completion\Core\DocumentPrioritizer\DocumentPrioritizer;
use Phpactor\Completion\Core\Suggestion;
Expand Down Expand Up @@ -49,6 +50,10 @@ protected function createSuggestion(NameSearchResult $result, ?Node $node = null
{
$options = array_merge($this->createSuggestionOptions($result, null, $node), $options);

if ($node !== null && $node->getParent() instanceof NamespaceUseClause) {
return Suggestion::createWithOptions($result->name()->__toString(), $options);
}

return Suggestion::createWithOptions($result->name()->head(), $options);
}

Expand All @@ -57,13 +62,20 @@ protected function createSuggestionOptions(
?TextDocumentUri $sourceUri = null,
?Node $node = null
): array {
return [
$options = [
'short_description' => $result->name()->__toString(),
'type' => $this->suggestionType($result),
'class_import' => $this->classImport($result),
'name_import' => $result->name()->__toString(),
'class_import' => null,
'name_import' => null,
'priority' => $this->prioritizer->priority($result->uri(), $sourceUri)
];

if ($node === null || !($node->getParent() instanceof NamespaceUseClause)) {
$options['class_import'] = $this->classImport($result);
$options['name_import'] = $result->name()->__toString();
}

return $options;
}

protected function suggestionType(NameSearchResult $result): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ public function provideComplete(): Generator
]
]
];
yield 'import in use context' => [
'<?php namespace Foo; class Bar {} namespace Bar; use b<>', [
[
'type' => Suggestion::TYPE_CLASS,
'name' => 'Foo\\Bar',
'short_description' => 'Foo\\Bar',
'snippet' => null,
]
]
];
}

protected function createTolerantCompletor(TextDocument $source): TolerantCompletor
Expand All @@ -82,17 +92,14 @@ protected function createTolerantCompletor(TextDocument $source): TolerantComple
$searcher->search('Foo')->willYield([
NameSearchResult::create('class', 'Foobar')
]);
$searcher->search('Foo')->willYield([
NameSearchResult::create('class', 'Foobar')
]);
$searcher->search('Fo')->willYield([
NameSearchResult::create('class', 'Foobar')
]);
$searcher->search('ba')->willYield([
NameSearchResult::create('function', 'bar'),
]);
$searcher->search('ba')->willYield([
NameSearchResult::create('function', 'bar'),
$searcher->search('b')->willYield([
NameSearchResult::create('class', 'Foo\\Bar'),
]);

$reflector = ReflectorBuilder::create()->addSource($source)->build();
Expand Down

0 comments on commit e4486dc

Please sign in to comment.