Search in sources :

Example 1 with PsiElementUsage

use of com.intellij.usages.rules.PsiElementUsage in project intellij-community by JetBrains.

the class GrImportFilteringRule method isVisible.

@Override
public boolean isVisible(@NotNull Usage usage) {
    if (usage instanceof PsiElementUsage) {
        final PsiElement psiElement = ((PsiElementUsage) usage).getElement();
        final PsiFile containingFile = psiElement.getContainingFile();
        if (containingFile instanceof GroovyFile) {
            // check whether the element is in the import list
            return PsiTreeUtil.getParentOfType(psiElement, GrImportStatement.class, true) == null;
        }
    }
    return true;
}
Also used : PsiFile(com.intellij.psi.PsiFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage)

Example 2 with PsiElementUsage

use of com.intellij.usages.rules.PsiElementUsage in project intellij-community by JetBrains.

the class PyTestCase method findUsage.

/**
   * Finds all usages of element. Works much like method in {@link com.intellij.testFramework.fixtures.CodeInsightTestFixture#findUsages(com.intellij.psi.PsiElement)},
   * but supports {@link com.intellij.find.findUsages.CustomUsageSearcher} and {@link com.intellij.psi.search.searches.ReferencesSearch} as well
   *
   * @param element what to find
   * @return usages
   */
@NotNull
protected Collection<PsiElement> findUsage(@NotNull final PsiElement element) {
    final Collection<PsiElement> result = new ArrayList<>();
    final CollectProcessor<Usage> usageCollector = new CollectProcessor<>();
    for (final CustomUsageSearcher searcher : CustomUsageSearcher.EP_NAME.getExtensions()) {
        searcher.processElementUsages(element, usageCollector, new FindUsagesOptions(myFixture.getProject()));
    }
    for (final Usage usage : usageCollector.getResults()) {
        if (usage instanceof PsiElementUsage) {
            result.add(((PsiElementUsage) usage).getElement());
        }
    }
    for (final PsiReference reference : ReferencesSearch.search(element).findAll()) {
        result.add(reference.getElement());
    }
    for (final UsageInfo info : myFixture.findUsages(element)) {
        result.add(info.getElement());
    }
    return result;
}
Also used : PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) Usage(com.intellij.usages.Usage) CustomUsageSearcher(com.intellij.find.findUsages.CustomUsageSearcher) FindUsagesOptions(com.intellij.find.findUsages.FindUsagesOptions) UsageInfo(com.intellij.usageView.UsageInfo) CollectProcessor(com.intellij.util.CommonProcessors.CollectProcessor) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PsiElementUsage

use of com.intellij.usages.rules.PsiElementUsage in project intellij-community by JetBrains.

the class UsageViewManagerImpl method isInScope.

public static boolean isInScope(@NotNull Usage usage, @NotNull SearchScope searchScope) {
    PsiElement element = null;
    VirtualFile file = usage instanceof UsageInFile ? ((UsageInFile) usage).getFile() : usage instanceof PsiElementUsage ? PsiUtilCore.getVirtualFile(element = ((PsiElementUsage) usage).getElement()) : null;
    if (file != null) {
        return isFileInScope(file, searchScope);
    }
    return element != null && (searchScope instanceof EverythingGlobalScope || searchScope instanceof ProjectScopeImpl || searchScope instanceof ProjectAndLibrariesScope);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UsageInFile(com.intellij.usages.rules.UsageInFile) PsiElement(com.intellij.psi.PsiElement) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage)

Example 4 with PsiElementUsage

use of com.intellij.usages.rules.PsiElementUsage in project intellij-community by JetBrains.

the class BaseRefactoringProcessor method createPresentation.

@NotNull
private static UsageViewPresentation createPresentation(@NotNull UsageViewDescriptor descriptor, @NotNull Usage[] usages) {
    UsageViewPresentation presentation = new UsageViewPresentation();
    presentation.setTabText(RefactoringBundle.message("usageView.tabText"));
    presentation.setTargetsNodeText(descriptor.getProcessedElementsHeader());
    presentation.setShowReadOnlyStatusAsRed(true);
    presentation.setShowCancelButton(true);
    presentation.setUsagesString(RefactoringBundle.message("usageView.usagesText"));
    int codeUsageCount = 0;
    int nonCodeUsageCount = 0;
    int dynamicUsagesCount = 0;
    Set<PsiFile> codeFiles = new HashSet<>();
    Set<PsiFile> nonCodeFiles = new HashSet<>();
    Set<PsiFile> dynamicUsagesCodeFiles = new HashSet<>();
    for (Usage usage : usages) {
        if (usage instanceof PsiElementUsage) {
            final PsiElementUsage elementUsage = (PsiElementUsage) usage;
            final PsiElement element = elementUsage.getElement();
            if (element == null)
                continue;
            final PsiFile containingFile = element.getContainingFile();
            if (elementUsage.isNonCodeUsage()) {
                nonCodeUsageCount++;
                nonCodeFiles.add(containingFile);
            } else {
                codeUsageCount++;
                codeFiles.add(containingFile);
            }
            if (usage instanceof UsageInfo2UsageAdapter) {
                final UsageInfo usageInfo = ((UsageInfo2UsageAdapter) usage).getUsageInfo();
                if (usageInfo instanceof MoveRenameUsageInfo && usageInfo.isDynamicUsage()) {
                    dynamicUsagesCount++;
                    dynamicUsagesCodeFiles.add(containingFile);
                }
            }
        }
    }
    codeFiles.remove(null);
    nonCodeFiles.remove(null);
    dynamicUsagesCodeFiles.remove(null);
    String codeReferencesText = descriptor.getCodeReferencesText(codeUsageCount, codeFiles.size());
    presentation.setCodeUsagesString(codeReferencesText);
    final String commentReferencesText = descriptor.getCommentReferencesText(nonCodeUsageCount, nonCodeFiles.size());
    if (commentReferencesText != null) {
        presentation.setNonCodeUsagesString(commentReferencesText);
    }
    presentation.setDynamicUsagesString("Dynamic " + StringUtil.decapitalize(descriptor.getCodeReferencesText(dynamicUsagesCount, dynamicUsagesCodeFiles.size())));
    String generatedCodeString;
    if (codeReferencesText.contains("in code")) {
        generatedCodeString = StringUtil.replace(codeReferencesText, "in code", "in generated code");
    } else {
        generatedCodeString = codeReferencesText + " in generated code";
    }
    presentation.setUsagesInGeneratedCodeString(generatedCodeString);
    return presentation;
}
Also used : PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiElementUsage

use of com.intellij.usages.rules.PsiElementUsage in project intellij-community by JetBrains.

the class ClassGroupingRule method groupUsage.

@Override
public UsageGroup groupUsage(@NotNull Usage usage) {
    if (!(usage instanceof PsiElementUsage)) {
        return null;
    }
    final PsiElement psiElement = ((PsiElementUsage) usage).getElement();
    final PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile == null)
        return null;
    PsiFile topLevelFile = InjectedLanguageManager.getInstance(containingFile.getProject()).getTopLevelFile(containingFile);
    if (!(topLevelFile instanceof PsiJavaFile) || topLevelFile instanceof ServerPageFile) {
        return null;
    }
    PsiElement containingClass = topLevelFile == containingFile ? psiElement : InjectedLanguageManager.getInstance(containingFile.getProject()).getInjectionHost(containingFile);
    do {
        containingClass = PsiTreeUtil.getParentOfType(containingClass, PsiClass.class, true);
        if (containingClass == null || ((PsiClass) containingClass).getQualifiedName() != null)
            break;
    } while (true);
    if (containingClass == null) {
        // check whether the element is in the import list
        PsiImportList importList = PsiTreeUtil.getParentOfType(psiElement, PsiImportList.class, true);
        if (importList != null) {
            final String fileName = getFileNameWithoutExtension(topLevelFile);
            final PsiClass[] classes = ((PsiJavaFile) topLevelFile).getClasses();
            for (final PsiClass aClass : classes) {
                if (fileName.equals(aClass.getName())) {
                    containingClass = aClass;
                    break;
                }
            }
        }
    } else {
        // skip JspClass synthetic classes.
        if (containingClass.getParent() instanceof PsiFile && FileTypeUtils.isInServerPageFile(containingClass)) {
            containingClass = null;
        }
    }
    if (containingClass != null) {
        return new ClassUsageGroup((PsiClass) containingClass);
    }
    final VirtualFile virtualFile = topLevelFile.getVirtualFile();
    if (virtualFile != null) {
        return new FileGroupingRule.FileUsageGroup(topLevelFile.getProject(), virtualFile);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage)

Aggregations

PsiElementUsage (com.intellij.usages.rules.PsiElementUsage)11 PsiElement (com.intellij.psi.PsiElement)8 PsiFile (com.intellij.psi.PsiFile)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 NotNull (org.jetbrains.annotations.NotNull)3 UsageInfo (com.intellij.usageView.UsageInfo)2 Usage (com.intellij.usages.Usage)2 CustomUsageSearcher (com.intellij.find.findUsages.CustomUsageSearcher)1 FindUsagesOptions (com.intellij.find.findUsages.FindUsagesOptions)1 FavoritesManager (com.intellij.ide.favoritesTreeView.FavoritesManager)1 AbstractUrl (com.intellij.ide.projectView.impl.AbstractUrl)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 JSImportStatement (com.intellij.lang.javascript.psi.ecmal4.JSImportStatement)1 Editor (com.intellij.openapi.editor.Editor)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 Project (com.intellij.openapi.project.Project)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)1