public static void doRename( final PsiElement element, String newName, UsageInfo[] usages, final Project project, @Nullable final RefactoringElementListener listener) throws IncorrectOperationException { final RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(element); final String fqn = element instanceof PsiFile ? ((PsiFile) element).getVirtualFile().getPath() : CopyReferenceAction.elementToFqn(element); if (fqn != null) { UndoableAction action = new BasicUndoableAction() { public void undo() throws UnexpectedUndoException { if (listener instanceof UndoRefactoringElementListener) { ((UndoRefactoringElementListener) listener).undoElementMovedOrRenamed(element, fqn); } } @Override public void redo() throws UnexpectedUndoException {} }; UndoManager.getInstance(project).undoableActionPerformed(action); } processor.renameElement(element, newName, usages, listener); }
private static String getStringToReplace( PsiElement element, String newName, boolean nonJava, final RenamePsiElementProcessor theProcessor) { if (element instanceof PsiMetaOwner) { final PsiMetaOwner psiMetaOwner = (PsiMetaOwner) element; final PsiMetaData metaData = psiMetaOwner.getMetaData(); if (metaData != null) { return metaData.getName(); } } if (theProcessor != null) { String result = theProcessor.getQualifiedNameAfterRename(element, newName, nonJava); if (result != null) { return result; } } if (element instanceof PsiNamedElement) { return newName; } else { LOG.error("Unknown element type"); return null; } }
@NotNull public static UsageInfo[] findUsages( final PsiElement element, final String newName, boolean searchInStringsAndComments, boolean searchForTextOccurrences, Map<? extends PsiElement, String> allRenames) { final List<UsageInfo> result = Collections.synchronizedList(new ArrayList<UsageInfo>()); PsiManager manager = element.getManager(); GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject()); RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(element); Collection<PsiReference> refs = processor.findReferences(element, searchInStringsAndComments); for (final PsiReference ref : refs) { if (ref == null) { LOG.error("null reference from processor " + processor); continue; } PsiElement referenceElement = ref.getElement(); result.add( new MoveRenameUsageInfo( referenceElement, ref, ref.getRangeInElement().getStartOffset(), ref.getRangeInElement().getEndOffset(), element, ref.resolve() == null)); } processor.findCollisions(element, newName, allRenames, result); final PsiElement searchForInComments = processor.getElementToSearchInStringsAndComments(element); if (searchInStringsAndComments && searchForInComments != null) { String stringToSearch = ElementDescriptionUtil.getElementDescription( searchForInComments, NonCodeSearchDescriptionLocation.STRINGS_AND_COMMENTS); if (stringToSearch.length() > 0) { final String stringToReplace = getStringToReplace(element, newName, false, processor); TextOccurrencesUtil.UsageInfoFactory factory = new NonCodeUsageInfoFactory(searchForInComments, stringToReplace); TextOccurrencesUtil.addUsagesInStringsAndComments( searchForInComments, stringToSearch, result, factory); } } if (searchForTextOccurrences && searchForInComments != null) { String stringToSearch = ElementDescriptionUtil.getElementDescription( searchForInComments, NonCodeSearchDescriptionLocation.NON_JAVA); if (stringToSearch.length() > 0) { final String stringToReplace = getStringToReplace(element, newName, true, processor); addTextOccurrence( searchForInComments, result, projectScope, stringToSearch, stringToReplace); } final Pair<String, String> additionalStringToSearch = processor.getTextOccurrenceSearchStrings(searchForInComments, newName); if (additionalStringToSearch != null && additionalStringToSearch.first.length() > 0) { addTextOccurrence( searchForInComments, result, projectScope, additionalStringToSearch.first, additionalStringToSearch.second); } } return result.toArray(new UsageInfo[result.size()]); }