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

Commit 3eae721

Browse files
committed
add the feature to DoctrineAnnotationCompletor
1 parent 010fe9d commit 3eae721

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

lib/Bridge/TolerantParser/WorseReflection/DoctrineAnnotationCompletor.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
use Microsoft\PhpParser\Node;
77
use Microsoft\PhpParser\Node\SourceFileNode;
88
use Microsoft\PhpParser\Parser;
9+
use Phpactor\Completion\Bridge\TolerantParser\ResolveAliasSuggestionsTrait;
910
use Phpactor\Completion\Core\Completor;
1011
use Phpactor\Completion\Core\Completor\NameSearcherCompletor;
1112
use Phpactor\Completion\Core\Suggestion;
1213
use Phpactor\Completion\Core\Util\OffsetHelper;
1314
use Phpactor\ReferenceFinder\NameSearcher;
14-
use Phpactor\ReferenceFinder\Search\NameSearchResult;
1515
use Phpactor\TextDocument\ByteOffset;
1616
use Phpactor\TextDocument\TextDocument;
1717
use Phpactor\WorseReflection\Core\Exception\NotFound;
1818
use Phpactor\WorseReflection\Reflector;
1919

2020
class DoctrineAnnotationCompletor extends NameSearcherCompletor implements Completor
2121
{
22+
use ResolveAliasSuggestionsTrait;
23+
2224
/**
2325
* @var Reflector
2426
*/
@@ -61,26 +63,23 @@ public function complete(TextDocument $source, ByteOffset $byteOffset): Generato
6163
return true;
6264
}
6365

66+
$importTable = $this->getClassImportTablesForNode($node);
6467
$suggestions = $this->completeName($annotation);
6568

6669
foreach ($suggestions as $suggestion) {
6770
if (!$this->isAnAnnotation($suggestion)) {
6871
continue;
6972
}
7073

71-
yield $suggestion;
74+
$resolvedSuggestions = $this->resolveAliasSuggestions($importTable, $suggestion);
75+
foreach ($resolvedSuggestions as $resolvedSuggestion) {
76+
yield $resolvedSuggestion->withSnippet($resolvedSuggestion->name().'($1)$0');
77+
}
7278
}
7379

7480
return $suggestions->getReturn();
7581
}
7682

77-
protected function createSuggestionOptions(NameSearchResult $result): array
78-
{
79-
return array_merge(parent::createSuggestionOptions($result), [
80-
'snippet' => (string) $result->name()->head() .'($1)$0',
81-
]);
82-
}
83-
8483
private function truncateSource(string $source, int $byteOffset): string
8584
{
8685
// truncate source at byte offset - we don't want the rest of the source

lib/Core/Suggestion.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ public function withoutNameImport(): self
146146
return $suggestion;
147147
}
148148

149+
public function withSnippet(string $snippet): self
150+
{
151+
$suggestion = clone $this;
152+
$suggestion->snippet = $snippet;
153+
154+
return $suggestion;
155+
}
156+
149157
public function toArray(): array
150158
{
151159
return [

tests/Integration/Bridge/TolerantParser/DoctrineAnnotationCompletorTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,45 @@ class Foo {}
104104
'type' => Suggestion::TYPE_CLASS,
105105
'name' => 'Entity',
106106
'short_description' => 'App\Annotation\Entity',
107-
'snippet' => 'Entity($1)$0'
107+
'snippet' => 'Entity($1)$0',
108+
'name_import' => 'App\Annotation\Entity',
109+
]
110+
]];
111+
112+
113+
yield 'in a namespace with an import' => [
114+
<<<'EOT'
115+
<?php
116+
117+
namespace App\Annotation;
118+
119+
/**
120+
* @Annotation
121+
*/
122+
class Entity {}
123+
124+
namespace App;
125+
126+
use App\Annotation as APP;
127+
128+
/**
129+
* @Ent<>
130+
*/
131+
class Foo {}
132+
EOT
133+
, [
134+
[
135+
'type' => Suggestion::TYPE_CLASS,
136+
'name' => 'APP\Entity',
137+
'short_description' => 'App\Annotation\Entity',
138+
'snippet' => 'APP\Entity($1)$0',
139+
'name_import' => null,
140+
], [
141+
'type' => Suggestion::TYPE_CLASS,
142+
'name' => 'Entity',
143+
'short_description' => 'App\Annotation\Entity',
144+
'snippet' => 'Entity($1)$0',
145+
'name_import' => 'App\Annotation\Entity',
108146
]
109147
]];
110148

tests/Unit/Core/SuggestionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,13 @@ public function testWithoutNameImport()
8989
)->withoutNameImport();
9090
$this->assertNull($suggestion->nameImport());
9191
}
92+
93+
public function testWithSnippet()
94+
{
95+
$suggestion = Suggestion::createWithOptions(
96+
'name',
97+
['snippet' => 'snippet'],
98+
)->withSnippet('test');
99+
$this->assertEquals('test', $suggestion->snippet());
100+
}
92101
}

0 commit comments

Comments
 (0)