diff --git a/platform/core-api/src/com/intellij/psi/PsiNamedElementWithCustomPresentation.java b/platform/core-api/src/com/intellij/psi/PsiNamedElementWithCustomPresentation.java new file mode 100644 index 000000000000..666d0e95ebb4 --- /dev/null +++ b/platform/core-api/src/com/intellij/psi/PsiNamedElementWithCustomPresentation.java @@ -0,0 +1,28 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.psi; + +import com.intellij.openapi.util.NlsSafe; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +/** + * A named element whose user-visible name may differ from internal name returned from {@link #getName()}. + * Some elements have {@link #getName()} returning something different from displayed to the user. For example, + * some JVM languages implement PsiClass interface and have to return from {@link #getName()} the JVM class name, + * to be reusable from other languages, rather than the class name like it's displayed in its original source code. + *
+ * If a named element implements this interface, UI-facing code may prefer the name returned from + * {@link #getPresentationName()} to display it to user. + *
+ */ +@ApiStatus.Experimental +public interface PsiNamedElementWithCustomPresentation extends PsiNamedElement { + /** + * @return the name of the named element, like it's displayed in the source code; + * null if a given element has no user-visible name. + */ + @NlsSafe + default @Nullable String getPresentationName() { + return getName(); + } +} \ No newline at end of file diff --git a/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java b/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java index 09f59f1234b0..0c43b19b5017 100644 --- a/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java +++ b/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java @@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Iconable; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiNamedElement; +import com.intellij.psi.PsiNamedElementWithCustomPresentation; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.rename.RenameUtil; import com.intellij.spellchecker.SpellCheckerManager; @@ -29,7 +30,7 @@ public class RenameTo extends PsiUpdateModCommandQuickFix implements Iconable { protected void applyFix(@NotNull Project project, @NotNull PsiElement psiElement, @NotNull ModPsiUpdater updater) { PsiNamedElement named = PsiTreeUtil.getNonStrictParentOfType(psiElement, PsiNamedElement.class); if (named == null) return; - String name = named.getName(); + String name = named instanceof PsiNamedElementWithCustomPresentation custom ? custom.getPresentationName() : named.getName(); if (name == null) return; List