Skip to content

Complete class renaming#1379

Merged
dantleech merged 15 commits intophpactor:masterfrom
przepompownia:complete-renaming
Mar 27, 2022
Merged

Complete class renaming#1379
dantleech merged 15 commits intophpactor:masterfrom
przepompownia:complete-renaming

Conversation

@przepompownia
Copy link
Contributor

@przepompownia przepompownia commented Mar 24, 2022

Closes #1366

@przepompownia
Copy link
Contributor Author

What do you think about this approach?

I started with yielding RenameFile directly but that approach required changing existing objects, creating some new complex or using instanceof.

No unit tests are provided yet.

use Microsoft\PhpParser\ResolvedName;
use Phpactor\ClassMover\ClassMover;
use Phpactor\ClassMover\Domain\Name\QualifiedName;
use Phpactor\Extension\Core\Application\Helper\ClassFileNormalizer;
Copy link
Collaborator

@dantleech dantleech Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better not to couple to this class here (most anything here is legacy). Instead you can use/update the adapter in this package: ClassToFileUriToNameConverter

public function __construct(TextDocumentUri $documentUri, TextEdit $textEdit, ?TextDocumentUri $newDocumentUri)
{
$this->documentUri = $documentUri;
$this->newDocumentUri = $newDocumentUri;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is bending the resposibility of this class a bit...

@dantleech
Copy link
Collaborator

dantleech commented Mar 25, 2022

Some other approaches:

  • Return a RenameResult class from Renamer#rename. $result->locatedEdits(); $result->renameOperations()
  • Return a RenameResult from the Renamer#rename Generator. foreach ($g as $r) { /.. } $result = $g->getReturn();

Pros/cons:

  • A dedicated return type is clean but we lose the benefit of the Generator (which is lost in LSP anyway, so we don't lose anything now)
  • If we were using the Generator in a useful way (and due to LSP we are not) Generator#getReturn() would mean getting the "rename" operation after the text edits had been applied.

- pass returned value from RenameHandler to LocatedTextEditConverter
@przepompownia
Copy link
Contributor Author

Indeed, we don't need to modify LocatedTextEdit(s), we can pass rename object(s) directly into the converter.

Maybe LocatedTextEditConverter should be renamed to something more appropriate since it gets Rename.

)
);
}
$documentEdits[] = new RenameFile(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be conditional i think, not all renames involve a file rename

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(if rename old URL != new URL?)

Copy link
Contributor Author

@przepompownia przepompownia Mar 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking for this case I also added quick (currently silent i.e. no exception is thrown to catch by the handler and notify the client) return when the original name equals the new.
https://github.com/phpactor/phpactor/pull/1379/files#diff-44acedae8f3be2f84c97421b00bde9016131dd4a65c4909c475420683c235b52R85-R89

@dantleech
Copy link
Collaborator

nice! are you happy to add tests?

@przepompownia
Copy link
Contributor Author

przepompownia commented Mar 27, 2022

Please look at (currently anonymous) NameToUriConverter implementation inside ClassRenamerTest if it is enough or if it's better to use real ClassToFileNameToUriConverter and Workspace.

@dantleech dantleech merged commit 4744163 into phpactor:master Mar 27, 2022
@dantleech
Copy link
Collaborator

seems to work great, thanks!

@przepompownia
Copy link
Contributor Author

Thanks for support and merging!

@przepompownia przepompownia deleted the complete-renaming branch March 27, 2022 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LSP rename: missing RenameFile operation in documentChanges

2 participants