diff --git a/platform/lang-api/src/com/intellij/refactoring/util/CommonRefactoringUtil.java b/platform/lang-api/src/com/intellij/refactoring/util/CommonRefactoringUtil.java index f1fe5d61ec87..95891abbb0a6 100644 --- a/platform/lang-api/src/com/intellij/refactoring/util/CommonRefactoringUtil.java +++ b/platform/lang-api/src/com/intellij/refactoring/util/CommonRefactoringUtil.java @@ -50,6 +50,9 @@ public class CommonRefactoringUtil { dialog.show(); } + /** + * Fatal refactoring problem during unit test run. Corresponds to message of modal dialog shown during user driven refactoring. + */ public static class RefactoringErrorHintException extends RuntimeException { public RefactoringErrorHintException(String message) { super(message); diff --git a/platform/lang-impl/src/com/intellij/refactoring/rename/PsiElementRenameHandler.java b/platform/lang-impl/src/com/intellij/refactoring/rename/PsiElementRenameHandler.java index 338a8d3d8270..afcc869a141c 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/rename/PsiElementRenameHandler.java +++ b/platform/lang-impl/src/com/intellij/refactoring/rename/PsiElementRenameHandler.java @@ -84,47 +84,44 @@ public class PsiElementRenameHandler implements RenameHandler { rename(element, project, nameSuggestionContext, editor); } - static boolean canRename(Project project, Editor editor, PsiElement element) { - if (element == null) return false; - if (!(element instanceof PsiFile) && CollectHighlightsUtil.isOutsideSourceRootJavaFile(element.getContainingFile())) return false; - - if (!element.isWritable()) { - String message = RefactoringBundle.getCannotRefactorMessage("This element cannot be renamed."); - if (!ApplicationManager.getApplication().isUnitTestMode()) { - showErrorMessage(project, editor, message); - } + static boolean canRename(Project project, Editor editor, PsiElement element) throws CommonRefactoringUtil.RefactoringErrorHintException { + String message = renameabilityStatus(project, element); + if (message != null) { + if (message.length() > 0) showErrorMessage(project, editor, message); return false; } + return true; + } + + static @Nullable String renameabilityStatus(Project project, PsiElement element) { + if (element == null) return ""; + if (!(element instanceof PsiFile) && CollectHighlightsUtil.isOutsideSourceRootJavaFile(element.getContainingFile())) return ""; + + if (!element.isWritable()) { + return RefactoringBundle.getCannotRefactorMessage("This element cannot be renamed."); + } boolean hasRenameProcessor = RenamePsiElementProcessor.forElement(element) != RenamePsiElementProcessor.DEFAULT; boolean hasWritableMetaData = element instanceof PsiMetaOwner && ((PsiMetaOwner)element).getMetaData() instanceof PsiWritableMetaData; if (!hasRenameProcessor && !hasWritableMetaData && !(element instanceof PsiNamedElement)) { - String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.symbol.to.rename")); - if (!ApplicationManager.getApplication().isUnitTestMode()) { - showErrorMessage(project, editor, message); - } - return false; + return RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.symbol.to.rename")); } if (!PsiManager.getInstance(project).isInProject(element) && element.isPhysical()) { - String message = RefactoringBundle + return RefactoringBundle .getCannotRefactorMessage(RefactoringBundle.message("error.out.of.project.element", UsageViewUtil.getType(element))); - showErrorMessage(project, editor, message); - return false; } if (InjectedLanguageUtil.isInInjectedLanguagePrefixSuffix(element)) { - String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.in.injected.lang.prefix.suffix", UsageViewUtil.getType(element))); - showErrorMessage(project, editor, message); - return false; + return RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.in.injected.lang.prefix.suffix", UsageViewUtil.getType(element))); } - return true;//CommonRefactoringUtil.checkReadOnlyStatus(project, element); + return null; } - private static void showErrorMessage(Project project, @Nullable Editor editor, String message) { + static void showErrorMessage(Project project, @Nullable Editor editor, String message) { CommonRefactoringUtil.showErrorHint(project, editor, message, RefactoringBundle.message("rename.title"), null); } diff --git a/platform/lang-impl/src/com/intellij/refactoring/rename/RenameProcessor.java b/platform/lang-impl/src/com/intellij/refactoring/rename/RenameProcessor.java index 1038cabae474..421e1cd3085a 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/rename/RenameProcessor.java +++ b/platform/lang-impl/src/com/intellij/refactoring/rename/RenameProcessor.java @@ -405,14 +405,18 @@ public class RenameProcessor extends BaseRefactoringProcessor { } protected void prepareTestRun() { - try { - if (!PsiElementRenameHandler.canRename(myProject, null, myPrimaryElement)) return; - prepareRenaming(myPrimaryElement, myNewName, myAllRenames); - } - catch (CommonRefactoringUtil.RefactoringErrorHintException e) { // unit test mode + String s = PsiElementRenameHandler.renameabilityStatus(myProject, myPrimaryElement); + if (!StringUtil.isEmpty(s)) { + if (inTestsProduceExceptionOnBadRenameabilityStatus()) { + PsiElementRenameHandler.showErrorMessage(myProject, null, s); + } + return; } + prepareRenaming(myPrimaryElement, myNewName, myAllRenames); } + protected boolean inTestsProduceExceptionOnBadRenameabilityStatus() { return false; } + public Collection getNewNames() { return myAllRenames.values(); }