Rename refactoring, PHP class does not renamed in XML

Answered

Hi guys,

What is the right way to implement rename refactoring of PHP classes in XML?

<someTag some-class="Some\Php\Class">

For now I am using DOM API, and implement MyConverter

public class MyConverter extends ResolvingConverter<PhpClass> implements CustomReferenceConverter 

with method

public PsiReference[] createReferences(GenericDomValue value, PsiElement element, ConvertContext context) {...

After that I can run Find Usages and my class presents in search results under section "Usages in XML description"

 

But when I do rename refactoring the class does not renamed in XML.

 

Then I implemented 

public class MyRenameProcessor extends RenamePsiElementProcessor {...

with method 

public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames) {...

and just add needed psiElements to the map allRenames - finally - rename works in XML

The question is: is it correct way? or maybe better (more convenient/official ) once exists?

 

P.S. By the way, when I declare PsiClass instead of PhpClass here

MyGenericAttributeValue<PhpClass>

Debugger shows an error - can not find PsiClass... is it correct or there is something misconfigured from my side?

8 comments
Comment actions Permalink
Official comment

 Please provide more details - 1. Can you please send whole code of all mentioned classes code or even the whole plugin? 2. version of Intellij SDK set up for project. 

Comment actions Permalink

PsiClass is an instance representing Java class or interface (see JavaDoc), so it's no PsiClass in PhpStorm's installation.

0
Comment actions Permalink

Here is a whole code https://github.com/voleye/magento-phpstorm-plugin/tree/rename-php-class-in-xml

I have updated Intellij and PhpStorm to 2017.2, version of SDK is: IntelliJ IDEA IU-172.3317.76 (java version 1.8.0_131)

0
Comment actions Permalink

Please, take a look http://www.jetbrains.org/intellij/sdk/docs/reference_guide/custom_language_support/rename_refactoring.html

In general you should provide PsiReference to PhpClass and implement handleElementRename() method.

 

 

0
Comment actions Permalink

Basically I have done the same, but if you look at PhpClassRenameProcessor you will see that all references filtered by "while" condition. So if my reference from xml is an PsiPolyVariantReferenceBase it will be removed from the list of references and as a result "handleElementRename" method will not be call.
Here is an implementation if any
https://github.com/voleye/magento2plugin

I have tried to provid my own rename processor but the method "findReferences" does not calls. 

0
Comment actions Permalink

Looks like we third-party class references are not supported in rename at the moment. I've created an issue https://youtrack.jetbrains.com/issue/WI-37530. Star it and you'll get all updates for this issue.

0
Comment actions Permalink

Thank you very much!

0
Comment actions Permalink

You're welcome! Let us know if you face other issues. 

0

Please sign in to leave a comment.