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

Commit 745568c

Browse files
authored
Merge pull request #5 from elythyr/fix-order-references
Fix order references
2 parents d5c6c4d + 2ce40e2 commit 745568c

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

lib/LanguageServerReferenceFinder/Handler/ReferencesHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ public function references(
161161
*/
162162
private function toLocations(array $locations): array
163163
{
164-
return $this->locationConverter->toLspLocations(new Locations($locations));
164+
return $this->locationConverter->toLspLocations(
165+
(new Locations($locations))->sorted()
166+
);
165167
}
166168

167169
public function registerCapabiltiies(ServerCapabilities $capabilities): void

tests/LanguageServerReferenceFinder/Unit/Handler/ReferencesHandlerTest.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Phpactor\Extension\LanguageServerReferenceFinder\Tests\Unit\Handler;
44

55
use Phpactor\LanguageServerProtocol\Location as LspLocation;
6+
use Phpactor\LanguageServerProtocol\Position;
7+
use Phpactor\LanguageServerProtocol\Range;
68
use Phpactor\LanguageServerProtocol\ReferenceContext;
79
use Phpactor\LanguageServerProtocol\ReferencesRequest;
810
use Phpactor\Extension\LanguageServerBridge\Converter\LocationConverter;
@@ -21,19 +23,20 @@
2123
use Phpactor\TextDocument\ByteOffset;
2224
use Phpactor\TextDocument\Location;
2325
use Phpactor\TextDocument\TextDocumentBuilder;
26+
use Prophecy\Prophecy\ObjectProphecy;
2427

2528
class ReferencesHandlerTest extends TestCase
2629
{
2730
const EXAMPLE_URI = 'file:///test';
2831
const EXAMPLE_TEXT = 'hello';
2932

3033
/**
31-
* @var ObjectProphecy|ReferenceFinder
34+
* @var ObjectProphecy<ReferenceFinder>
3235
*/
3336
private $finder;
3437

3538
/**
36-
* @var ObjectProphecy
39+
* @var ObjectProphecy<DefinitionLocator>
3740
*/
3841
private $locator;
3942

@@ -51,14 +54,23 @@ public function testFindsReferences(): void
5154
->build()
5255
;
5356

57+
$document2 = TextDocumentBuilder::create(self::EXAMPLE_TEXT)
58+
->language('php')
59+
->uri(self::EXAMPLE_URI.'2')
60+
->build()
61+
;
62+
5463
$this->finder->findReferences(
5564
$document,
5665
ByteOffset::fromInt(0)
5766
)->willYield([
58-
PotentialLocation::surely(new Location($document->uri(), ByteOffset::fromInt(2)))
67+
PotentialLocation::surely(new Location($document->uri(), ByteOffset::fromInt(2))),
68+
PotentialLocation::surely(new Location($document2->uri(), ByteOffset::fromInt(3))),
69+
PotentialLocation::surely(new Location($document->uri(), ByteOffset::fromInt(5))),
5970
])->shouldBeCalled();
6071

6172
$tester = $this->createTester();
73+
$tester->textDocument()->open(self::EXAMPLE_URI.'2', self::EXAMPLE_TEXT);
6274

6375
$response = $tester->requestAndWait(ReferencesRequest::METHOD, [
6476
'textDocument' => ProtocolFactory::textDocumentIdentifier(self::EXAMPLE_URI),
@@ -68,9 +80,20 @@ public function testFindsReferences(): void
6880

6981
$locations = $response->result;
7082
$this->assertIsArray($locations);
71-
$this->assertCount(1, $locations);
72-
$lspLocation = reset($locations);
73-
$this->assertInstanceOf(LspLocation::class, $lspLocation);
83+
$this->assertEquals([
84+
new LspLocation(
85+
(string) $document->uri(),
86+
new Range(new Position(0, 2), new Position(0, 2)),
87+
),
88+
new LspLocation(
89+
(string) $document->uri(),
90+
new Range(new Position(0, 5), new Position(0, 5)),
91+
),
92+
new LspLocation(
93+
(string) $document2->uri(),
94+
new Range(new Position(0, 3), new Position(0, 3)),
95+
),
96+
], $locations);
7497
}
7598

7699
public function testFindsReferencesIncludingDeclaration(): void
@@ -93,7 +116,7 @@ public function testFindsReferencesIncludingDeclaration(): void
93116
ByteOffset::fromInt(0)
94117
)->willReturn(new DefinitionLocation($document->uri(), ByteOffset::fromInt(2)))->shouldBeCalled();
95118

96-
$response = $this->createTester(true)->requestAndWait(ReferencesRequest::METHOD, [
119+
$response = $this->createTester()->requestAndWait(ReferencesRequest::METHOD, [
97120
'textDocument' => ProtocolFactory::textDocumentIdentifier(self::EXAMPLE_URI),
98121
'position' => ProtocolFactory::position(0, 0),
99122
'context' => new ReferenceContext(true),
@@ -105,7 +128,7 @@ public function testFindsReferencesIncludingDeclaration(): void
105128
$this->assertInstanceOf(LspLocation::class, $lspLocation);
106129
}
107130

108-
public function testFindsReferencesIncludingDeclarationWhenDeclarationNotFound()
131+
public function testFindsReferencesIncludingDeclarationWhenDeclarationNotFound(): void
109132
{
110133
$document = TextDocumentBuilder::create(self::EXAMPLE_TEXT)
111134
->language('php')

0 commit comments

Comments
 (0)