Programmatically perform refactoring without preview
已回答
I am trying to perform some refactoring operations programmatically. I successfully perform rename method and rename class when there is no preview. There is a case I've been working on where there is a refactoring preview because the UsageInfo.isNonCodeUsage is true. I have tried creating a copy of the UsageInfo with the field set to false but I've been unsuccessful. I am aware that if I don't do it programmatically, I can use a keyboard shortcut to accept the refactoring in the preview. It doesn't look like I can turn off the preview but maybe I missed something.
Do you have any suggestions for turning it off? If I cannot turn off the preview, is there any way I can programmatically perform the "Do Refactoring" button action?
请先登录再写评论。
You could try invoking com.intellij.refactoring.RefactoringFactory#createRename(com.intellij.psi.PsiElement, java.lang.String, boolean, boolean) with searchInNonJavaFiles=false.
Hi Yann,
I followed your suggestion and invoked com.intellij.refactoring.RefactoringFactory#createRename() instead of using the RenameProcessor and it runs without a preview! Thank you for the quick response and the suggestion.
I noticed that two of the code usages do not get renamed. One of them is a field inside of the inner class and the other is inside of the constructor. I figured I am doing something wrong so I tried refactoring the class using the editor and it does appear as a usage inside of the refacatoring prompt; however when I applied the refactoring the usage became INVALID and the usage was not renamed. Could this be a bug?
If you have a reproducible case (ideally a standalone minimal project), please file an issue and provide project https://youtrack.jetbrains.com/issues/IDEA
If I have a java file (say A.java) in the package (say "org") at the location /src/main/java/org/A.java......
If I want to rename refactor the java class file "A" to java class file "B", then how should I call the RefactoringFactory.getInstance(project).createRename(....). Am not sure how to point to the A.java PsiElement
I was able to do the successful refactoring programmatically, from the code snippet below, mentioned in " https://intellij-support.jetbrains.com/hc/en-us/community/posts/206116339-Refactoring-HowTo"
JavaRefactoringFactory factory = JavaRefactoringFactory.getInstance(project);
JavaRenameRefactoring rename = factory.createRename(psiElement, renameString);
UsageInfo[] usages = rename.findUsages();
rename.doRefactoring(usages);