Search in sources :

Example 1 with PsiWritableMetaData

use of com.intellij.psi.meta.PsiWritableMetaData in project intellij-community by JetBrains.

the class PsiElementRenameHandler method renameabilityStatus.

@Nullable
static String renameabilityStatus(Project project, PsiElement element) {
    if (element == null)
        return "";
    boolean hasRenameProcessor = RenamePsiElementProcessor.forElement(element) != RenamePsiElementProcessor.DEFAULT;
    boolean hasWritableMetaData = element instanceof PsiMetaOwner && ((PsiMetaOwner) element).getMetaData() instanceof PsiWritableMetaData;
    if (!hasRenameProcessor && !hasWritableMetaData && !(element instanceof PsiNamedElement)) {
        return RefactoringBundle.getCannotRefactorMessage(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", UsageViewUtil.getType(element));
                return RefactoringBundle.getCannotRefactorMessage(message);
            }
        }
        if (!element.isWritable()) {
            return RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.cannot.be.renamed"));
        }
    }
    if (InjectedLanguageUtil.isInInjectedLanguagePrefixSuffix(element)) {
        final String message = RefactoringBundle.message("error.in.injected.lang.prefix.suffix", UsageViewUtil.getType(element));
        return RefactoringBundle.getCannotRefactorMessage(message);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiWritableMetaData(com.intellij.psi.meta.PsiWritableMetaData) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PsiWritableMetaData

use of com.intellij.psi.meta.PsiWritableMetaData in project intellij-community by JetBrains.

the class RenameUtil method doRenameGenericNamedElement.

public static void doRenameGenericNamedElement(@NotNull PsiElement namedElement, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    PsiWritableMetaData writableMetaData = null;
    if (namedElement instanceof PsiMetaOwner) {
        final PsiMetaData metaData = ((PsiMetaOwner) namedElement).getMetaData();
        if (metaData instanceof PsiWritableMetaData) {
            writableMetaData = (PsiWritableMetaData) metaData;
        }
    }
    if (writableMetaData == null && !(namedElement instanceof PsiNamedElement)) {
        LOG.error("Unknown element type:" + namedElement);
    }
    boolean hasBindables = false;
    for (UsageInfo usage : usages) {
        if (!(usage.getReference() instanceof BindablePsiReference)) {
            rename(usage, newName);
        } else {
            hasBindables = true;
        }
    }
    if (writableMetaData != null) {
        writableMetaData.setName(newName);
    } else {
        PsiElement namedElementAfterRename = ((PsiNamedElement) namedElement).setName(newName);
        if (namedElementAfterRename != null)
            namedElement = namedElementAfterRename;
    }
    if (hasBindables) {
        for (UsageInfo usage : usages) {
            final PsiReference ref = usage.getReference();
            if (ref instanceof BindablePsiReference) {
                boolean fallback = true;
                if (!(ref instanceof FragmentaryPsiReference && ((FragmentaryPsiReference) ref).isFragmentOnlyRename())) {
                    try {
                        ref.bindToElement(namedElement);
                        fallback = false;
                    } catch (IncorrectOperationException ignored) {
                    }
                }
                if (fallback) {
                    //fall back to old scheme
                    ref.handleElementRename(newName);
                }
            }
        }
    }
    if (listener != null) {
        listener.elementRenamed(namedElement);
    }
}
Also used : PsiMetaData(com.intellij.psi.meta.PsiMetaData) PsiWritableMetaData(com.intellij.psi.meta.PsiWritableMetaData) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) UsageInfo(com.intellij.usageView.UsageInfo) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement)

Aggregations

PsiMetaOwner (com.intellij.psi.meta.PsiMetaOwner)2 PsiWritableMetaData (com.intellij.psi.meta.PsiWritableMetaData)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PomTargetPsiElement (com.intellij.pom.PomTargetPsiElement)1 PsiMetaData (com.intellij.psi.meta.PsiMetaData)1 UsageInfo (com.intellij.usageView.UsageInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 Nullable (org.jetbrains.annotations.Nullable)1