Skip to content

Commit 95a9ba0

Browse files
committed
Fix willRename behavior
Rename only the original reference as described in WillRenameFiles LSP specifications
1 parent 8a2779d commit 95a9ba0

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

lib/Rename/Adapter/ClassMover/FileRenamer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function renameFile(TextDocumentUri $from, TextDocumentUri $to): Promise
4343
$references = $this->client->class()->referencesTo($fromClass);
4444

4545
// rename class definition
46-
$locatedEdits = $this->replaceDefinition($to, $fromClass, $toClass);
46+
$locatedEdits = $this->replaceDefinition($from, $fromClass, $toClass);
4747

4848
$edits = TextEdits::none();
4949
$seen = [];

lib/Rename/Tests/Adapter/ClassMover/FileRenamerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,29 @@ public function testRenameReferencesInTwoFiles(): void
5252
self::assertCount(3, $edits->toLocatedTextEdits(), 'Locates two references');
5353
}
5454

55+
/**
56+
* Tests that the definition is renamed on the original file as described
57+
* in the LSP spec. Note that we use the "Simple" file-to-class name
58+
* strategy which scans the contents of the destination file to get the
59+
* class name. Therefore, despite having to save the renamed file, we do
60+
* not add it to the InMemoryDocumentLocator to simulate the behavior when
61+
* using Composer that has no reference to the new file, only the old one.
62+
*/
63+
public function testRenameReferencesToNewFile(): void
64+
{
65+
$document1 = $this->createDocument('One.php', '<?php class One{}');
66+
$document2 = $this->createDocument('Two.php', '<?php class Two{}; One::class;');
67+
68+
file_put_contents($document2->uri()->path(), $document2->__toString());
69+
$renamer = $this->createRenamer([$document1], [(new ClassRecord('One'))->setType('class')]);
70+
71+
$edits = wait($renamer->renameFile($document1->uri(), $document2->uri()));
72+
73+
self::assertInstanceOf(LocatedTextEditsMap::class, $edits);
74+
assert($edits instanceof LocatedTextEditsMap);
75+
self::assertCount(1, $edits->toLocatedTextEdits(), 'Locates one reference');
76+
}
77+
5578
/**
5679
* @param TextDocument[] $textDocuments
5780
* @param Record[] $records

0 commit comments

Comments
 (0)