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