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;
}
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);
}
}
Aggregations