diff --git a/platform/refactoring/src/com/intellij/refactoring/rename/PsiElementRenameHandler.java b/platform/refactoring/src/com/intellij/refactoring/rename/PsiElementRenameHandler.java index 880f6b1f176e..88a9f307e051 100644 --- a/platform/refactoring/src/com/intellij/refactoring/rename/PsiElementRenameHandler.java +++ b/platform/refactoring/src/com/intellij/refactoring/rename/PsiElementRenameHandler.java @@ -1,5 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. - +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.refactoring.rename; import com.intellij.ide.scratch.ScratchUtil; @@ -16,7 +15,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.MessageDialogBuilder; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.NlsContexts; -import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtilBase; @@ -129,49 +127,47 @@ public class PsiElementRenameHandler implements RenameHandler { rename(element, project, nameSuggestionContext, suggestedNames, editor, null, processor); } - public static boolean canRename(@NotNull Project project, Editor editor, PsiElement element) throws CommonRefactoringUtil.RefactoringErrorHintException { - String message = element == null ? null : renameabilityStatus(project, element); - if (StringUtil.isNotEmpty(message)) { - showErrorMessage(project, editor, message); + public static boolean canRename(@NotNull Project project, Editor editor, PsiElement element) { + if (element == null) return false; + String message = getErrorMessage(project, editor, element); + if (message != null) { + CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.getCannotRefactorMessage(message), + RefactoringBundle.message("rename.title"), null); return false; } + return true; } - private static @Nullable @NlsContexts.DialogMessage String renameabilityStatus(@NotNull Project project, @NotNull PsiElement element) { - boolean hasRenameProcessor = !(RenamePsiElementProcessorBase.forPsiElement(element) instanceof RenamePsiElementProcessorBase.DefaultRenamePsiElementProcessor); - boolean hasWritableMetaData = element instanceof PsiMetaOwner && ((PsiMetaOwner)element).getMetaData() instanceof PsiWritableMetaData; + private static @Nullable @NlsContexts.DialogMessage String getErrorMessage(@NotNull Project project, Editor editor, PsiElement element) { + boolean hasRenameProcessor = + !(RenamePsiElementProcessorBase.forPsiElement(element) instanceof RenamePsiElementProcessorBase.DefaultRenamePsiElementProcessor); + boolean hasWritableMetaData = element instanceof PsiMetaOwner o && o.getMetaData() instanceof PsiWritableMetaData; if (!hasRenameProcessor && !hasWritableMetaData && !(element instanceof PsiNamedElement)) { - return RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.symbol.to.rename")); + return editor == null + ? RefactoringBundle.message("error.cannot.be.renamed") + : RefactoringBundle.message("error.wrong.caret.position.symbol.to.rename"); } - - if (!PsiManager.getInstance(project).isInProject(element)) { - if (element.isPhysical()) { - VirtualFile virtualFile = PsiUtilCore.getVirtualFile(element); - if (!(virtualFile != null && NonProjectFileWritingAccessProvider.isWriteAccessAllowed(virtualFile, project))) { - String message = RefactoringBundle.message("error.out.of.project.element", RenameUtil.getUsageViewType(element)); - return RefactoringBundle.getCannotRefactorMessage(message); + else { + if (!PsiManager.getInstance(project).isInProject(element)) { + if (element.isPhysical()) { + VirtualFile virtualFile = PsiUtilCore.getVirtualFile(element); + if (virtualFile == null || !NonProjectFileWritingAccessProvider.isWriteAccessAllowed(virtualFile, project)) { + return RefactoringBundle.message("error.out.of.project.element", RenameUtil.getUsageViewType(element)); + } + } + if (!element.isWritable()) { + return RefactoringBundle.message("error.cannot.be.renamed"); } } - - if (!element.isWritable()) { - return RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.cannot.be.renamed")); + if (InjectedLanguageUtilBase.isInInjectedLanguagePrefixSuffix(element)) { + return RefactoringBundle.message("error.in.injected.lang.prefix.suffix", RenameUtil.getUsageViewType(element)); } } - - if (InjectedLanguageUtilBase.isInInjectedLanguagePrefixSuffix(element)) { - final String message = RefactoringBundle.message("error.in.injected.lang.prefix.suffix", RenameUtil.getUsageViewType(element)); - return RefactoringBundle.getCannotRefactorMessage(message); - } - return null; } - private static void showErrorMessage(@NotNull Project project, @Nullable Editor editor, @NotNull @NlsContexts.DialogMessage String message) { - CommonRefactoringUtil.showErrorHint(project, editor, message, RefactoringBundle.message("rename.title"), null); - } - public static void rename(@NotNull PsiElement element, @NotNull Project project, PsiElement nameSuggestionContext, Editor editor) { rename(element, project, nameSuggestionContext, editor, null); } @@ -234,11 +230,11 @@ public class PsiElementRenameHandler implements RenameHandler { public static boolean isVetoed(PsiElement element) { if (element == null || - element instanceof SyntheticElement || - element instanceof PsiNamedElement namedElement && namedElement.getName() == null) { + element instanceof SyntheticElement || + element instanceof PsiNamedElement namedElement && namedElement.getName() == null) { return true; } - for(Condition condition: VETO_RENAME_CONDITION_EP.getExtensionList()) { + for (Condition condition: VETO_RENAME_CONDITION_EP.getExtensionList()) { if (condition.value(element)) return true; } return false; @@ -247,9 +243,6 @@ public class PsiElementRenameHandler implements RenameHandler { public static @Nullable PsiElement getElement(@NotNull DataContext dataContext) { PsiElement[] elementArray = CommonRefactoringUtil.getPsiElementArray(dataContext); - if (elementArray.length != 1) { - return null; - } - return elementArray[0]; + return elementArray.length != 1 ? null : elementArray[0]; } }