Search in sources :

Example 1 with PsiMetaOwner

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

the class PsiElement2UsageTargetAdapter method update.

private void update(PsiElement element) {
    if (element != null && element.isValid()) {
        final ItemPresentation presentation = ((NavigationItem) element).getPresentation();
        myIconOpen = presentation == null ? null : ComputableIcon.create(presentation, true);
        myIconClosed = presentation == null ? null : ComputableIcon.create(presentation, false);
        myPresentableText = presentation == null ? UsageViewUtil.createNodeText(element) : presentation.getPresentableText();
        if (myIconOpen == null || myIconClosed == null) {
            if (element instanceof PsiMetaOwner) {
                final PsiMetaOwner psiMetaOwner = (PsiMetaOwner) element;
                final PsiMetaData metaData = psiMetaOwner.getMetaData();
                if (metaData instanceof PsiPresentableMetaData) {
                    final PsiPresentableMetaData psiPresentableMetaData = (PsiPresentableMetaData) metaData;
                    if (myIconOpen == null)
                        myIconOpen = ComputableIcon.create(psiPresentableMetaData);
                    if (myIconClosed == null)
                        myIconClosed = ComputableIcon.create(psiPresentableMetaData);
                }
            } else if (element instanceof PsiFile) {
                final PsiFile psiFile = (PsiFile) element;
                final VirtualFile virtualFile = psiFile.getVirtualFile();
                if (virtualFile != null) {
                    myIconOpen = ComputableIcon.create(virtualFile);
                    myIconClosed = ComputableIcon.create(virtualFile);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) NavigationItem(com.intellij.navigation.NavigationItem) PsiElementNavigationItem(com.intellij.navigation.PsiElementNavigationItem) PsiMetaData(com.intellij.psi.meta.PsiMetaData) ItemPresentation(com.intellij.navigation.ItemPresentation) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner)

Example 2 with PsiMetaOwner

use of com.intellij.psi.meta.PsiMetaOwner 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 3 with PsiMetaOwner

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

the class JavaFindUsagesHelper method getElementNames.

@NotNull
public static Set<String> getElementNames(@NotNull final PsiElement element) {
    if (element instanceof PsiDirectory) {
        // normalize a directory to a corresponding package
        PsiPackage aPackage = ReadAction.compute(() -> JavaDirectoryService.getInstance().getPackage((PsiDirectory) element));
        return aPackage == null ? Collections.emptySet() : getElementNames(aPackage);
    }
    final Set<String> result = new HashSet<>();
    ApplicationManager.getApplication().runReadAction(() -> {
        if (element instanceof PsiPackage) {
            ContainerUtil.addIfNotNull(result, ((PsiPackage) element).getQualifiedName());
        } else if (element instanceof PsiClass) {
            final String qname = ((PsiClass) element).getQualifiedName();
            if (qname != null) {
                result.add(qname);
                PsiClass topLevelClass = PsiUtil.getTopLevelClass(element);
                if (topLevelClass != null && !(topLevelClass instanceof PsiSyntheticClass)) {
                    String topName = topLevelClass.getQualifiedName();
                    assert topName != null : "topLevelClass : " + topLevelClass + "; element: " + element + " (" + qname + ") top level file: " + InjectedLanguageManager.getInstance(element.getProject()).getTopLevelFile(element);
                    if (qname.length() > topName.length()) {
                        result.add(topName + qname.substring(topName.length()).replace('.', '$'));
                    }
                }
            }
        } else if (element instanceof PsiMethod) {
            ContainerUtil.addIfNotNull(result, ((PsiMethod) element).getName());
        } else if (element instanceof PsiVariable) {
            ContainerUtil.addIfNotNull(result, ((PsiVariable) element).getName());
        } else if (element instanceof PsiMetaOwner) {
            final PsiMetaData metaData = ((PsiMetaOwner) element).getMetaData();
            if (metaData != null) {
                ContainerUtil.addIfNotNull(result, metaData.getName());
            }
        } else if (element instanceof PsiNamedElement) {
            ContainerUtil.addIfNotNull(result, ((PsiNamedElement) element).getName());
        } else if (element instanceof XmlAttributeValue) {
            ContainerUtil.addIfNotNull(result, ((XmlAttributeValue) element).getValue());
        } else {
            LOG.error("Unknown element type: " + element);
        }
    });
    return result;
}
Also used : XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiMetaData(com.intellij.psi.meta.PsiMetaData) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PsiMetaOwner

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

the class DescriptiveNameUtil method getDescriptiveName.

@NotNull
public static String getDescriptiveName(@NotNull PsiElement psiElement) {
    LOG.assertTrue(psiElement.isValid());
    if (psiElement instanceof PsiMetaOwner) {
        final PsiMetaOwner psiMetaOwner = (PsiMetaOwner) psiElement;
        final PsiMetaData metaData = psiMetaOwner.getMetaData();
        if (metaData != null)
            return getMetaDataName(metaData);
    }
    if (psiElement instanceof PsiFile) {
        return ((PsiFile) psiElement).getName();
    }
    final Language lang = psiElement.getLanguage();
    final FindUsagesProvider provider = LanguageFindUsages.INSTANCE.forLanguage(lang);
    assert provider != null : lang;
    return provider.getDescriptiveName(psiElement);
}
Also used : Language(com.intellij.lang.Language) PsiMetaData(com.intellij.psi.meta.PsiMetaData) PsiFile(com.intellij.psi.PsiFile) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiMetaOwner

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

the class AbstractQualifiedReference method bindToElement.

@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    CheckUtil.checkWritable(this);
    if (isReferenceTo(element))
        return this;
    if (element instanceof PsiMethod) {
        final PsiMethod method = (PsiMethod) element;
        final String methodName = method.getName();
        if (isDirectlyVisible(method))
            return replaceReference(methodName);
        final AbstractQualifiedReference result = replaceReference(method.getContainingClass().getQualifiedName() + "." + methodName);
        final AbstractQualifiedReference qualifier = result.getQualifier();
        assert qualifier != null;
        qualifier.shortenReferences();
        return result;
    }
    if (element instanceof PsiClass) {
        return replaceReference(((PsiClass) element).getQualifiedName()).shortenReferences();
    }
    if (element instanceof PsiPackage) {
        return replaceReference(((PsiPackage) element).getQualifiedName());
    }
    if (element instanceof PsiMetaOwner) {
        final PsiMetaData metaData = ((PsiMetaOwner) element).getMetaData();
        if (metaData != null) {
            final String name = metaData.getName(this);
            if (name != null) {
                return replaceReference(name);
            }
        }
    }
    return this;
}
Also used : PsiMetaData(com.intellij.psi.meta.PsiMetaData) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner)

Aggregations

PsiMetaOwner (com.intellij.psi.meta.PsiMetaOwner)12 PsiMetaData (com.intellij.psi.meta.PsiMetaData)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 NotNull (org.jetbrains.annotations.NotNull)3 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 PsiPresentableMetaData (com.intellij.psi.meta.PsiPresentableMetaData)2 PsiWritableMetaData (com.intellij.psi.meta.PsiWritableMetaData)2 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)2 XmlTag (com.intellij.psi.xml.XmlTag)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 Nullable (org.jetbrains.annotations.Nullable)2 EmptyResolveMessageProvider (com.intellij.codeInsight.daemon.EmptyResolveMessageProvider)1 LookupValueFactory (com.intellij.codeInsight.lookup.LookupValueFactory)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 LocalQuickFixProvider (com.intellij.codeInspection.LocalQuickFixProvider)1 Language (com.intellij.lang.Language)1 ItemPresentation (com.intellij.navigation.ItemPresentation)1 NavigationItem (com.intellij.navigation.NavigationItem)1 PsiElementNavigationItem (com.intellij.navigation.PsiElementNavigationItem)1