use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class InlineOptionsDialog method initOccurrencesNumber.
protected static int initOccurrencesNumber(PsiNameIdentifierOwner nameIdentifierOwner) {
final ProgressManager progressManager = ProgressManager.getInstance();
final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(nameIdentifierOwner.getProject());
final GlobalSearchScope scope = GlobalSearchScope.projectScope(nameIdentifierOwner.getProject());
final String name = nameIdentifierOwner.getName();
final boolean isCheapToSearch = name != null && searchHelper.isCheapEnoughToSearch(name, scope, null, progressManager.getProgressIndicator()) != PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES;
return isCheapToSearch ? ReferencesSearch.search(nameIdentifierOwner, scope).findAll().size() : -1;
}
use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class UnusedSymbolUtil method processUsages.
// return false if can't process usages (weird member of too may usages) or processor returned false
public static boolean processUsages(@NotNull Project project, @NotNull PsiFile containingFile, @NotNull PsiMember member, @NotNull ProgressIndicator progress, @Nullable PsiFile ignoreFile, @NotNull Processor<UsageInfo> usageInfoProcessor) {
String name = member.getName();
if (name == null) {
log("* " + member.getName() + " no name; false");
return false;
}
SearchScope useScope = member.getUseScope();
PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
if (useScope instanceof GlobalSearchScope) {
// some classes may have references from within XML outside dependent modules, e.g. our actions
if (member instanceof PsiClass) {
useScope = GlobalSearchScope.projectScope(project).uniteWith((GlobalSearchScope) useScope);
}
// if we've resolved all references, find usages will be fast
PsiSearchHelper.SearchCostResult cheapEnough = RefResolveService.ENABLED && RefResolveService.getInstance(project).isUpToDate() ? PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES : searchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope) useScope, ignoreFile, progress);
if (cheapEnough == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) {
log("* " + member.getName() + " too many usages; false");
return false;
}
//if count is 0 there is no usages since we've called myRefCountHolder.isReferenced() before
if (cheapEnough == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES && !canBeReferencedViaWeirdNames(member, containingFile)) {
log("* " + member.getName() + " 0 usages; true");
return true;
}
if (member instanceof PsiMethod) {
String propertyName = PropertyUtil.getPropertyName(member);
if (propertyName != null) {
SearchScope fileScope = containingFile.getUseScope();
if (fileScope instanceof GlobalSearchScope && searchHelper.isCheapEnoughToSearch(propertyName, (GlobalSearchScope) fileScope, ignoreFile, progress) == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) {
log("* " + member.getName() + " too many prop usages; false");
return false;
}
}
}
}
FindUsagesOptions options;
if (member instanceof PsiPackage) {
options = new JavaPackageFindUsagesOptions(useScope);
options.isSearchForTextOccurrences = true;
} else if (member instanceof PsiClass) {
options = new JavaClassFindUsagesOptions(useScope);
options.isSearchForTextOccurrences = true;
} else if (member instanceof PsiMethod) {
PsiMethod method = (PsiMethod) member;
options = new JavaMethodFindUsagesOptions(useScope);
options.isSearchForTextOccurrences = method.isConstructor();
} else if (member instanceof PsiVariable) {
options = new JavaVariableFindUsagesOptions(useScope);
options.isSearchForTextOccurrences = false;
} else {
options = new FindUsagesOptions(useScope);
options.isSearchForTextOccurrences = true;
}
options.isUsages = true;
return JavaFindUsagesHelper.processElementUsages(member, options, usageInfoProcessor);
}
use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class DeprecatedIsStillUsedInspection method checkMember.
private static void checkMember(@NotNull PsiMember member, @NotNull PsiIdentifier identifier, @NotNull ProblemsHolder holder) {
if (!(member instanceof PsiDocCommentOwner) || !isDeprecated((PsiDocCommentOwner) member)) {
return;
}
PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(member.getProject());
String name = member.getName();
if (name != null && hasUsages(member, name, searchHelper, member.getResolveScope())) {
holder.registerProblem(identifier, "Deprecated member '" + name + "' is still used");
}
}
use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class DfaVariableValue method isCheapEnoughToSearch.
private static boolean isCheapEnoughToSearch(PsiField field, String name) {
SearchScope scope = field.getUseScope();
if (!(scope instanceof GlobalSearchScope))
return true;
PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(field.getProject());
PsiSearchHelper.SearchCostResult result = helper.isCheapEnoughToSearch(name, (GlobalSearchScope) scope, field.getContainingFile(), null);
return result != PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES;
}
use of com.intellij.psi.search.PsiSearchHelper in project intellij-community by JetBrains.
the class UnusedDeclarationInspectionBase method runInspection.
@Override
public void runInspection(@NotNull final AnalysisScope scope, @NotNull InspectionManager manager, @NotNull final GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {
globalContext.getRefManager().iterate(new RefJavaVisitor() {
@Override
public void visitElement(@NotNull final RefEntity refEntity) {
if (refEntity instanceof RefElementImpl) {
final RefElementImpl refElement = (RefElementImpl) refEntity;
if (!refElement.isSuspicious())
return;
PsiFile file = refElement.getContainingFile();
if (file == null)
return;
final boolean isSuppressed = refElement.isSuppressed(getShortName(), ALTERNATIVE_ID);
if (isSuppressed || !((GlobalInspectionContextBase) globalContext).isToCheckFile(file, UnusedDeclarationInspectionBase.this)) {
if (isSuppressed || !scope.contains(file)) {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
}
}
}
}
});
if (isAddNonJavaUsedEnabled()) {
checkForReachableRefs(globalContext);
final StrictUnreferencedFilter strictUnreferencedFilter = new StrictUnreferencedFilter(this, globalContext);
ProgressManager.getInstance().runProcess(new Runnable() {
@Override
public void run() {
final RefManager refManager = globalContext.getRefManager();
final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(refManager.getProject());
refManager.iterate(new RefJavaVisitor() {
@Override
public void visitElement(@NotNull final RefEntity refEntity) {
if (refEntity instanceof RefClass && strictUnreferencedFilter.accepts((RefClass) refEntity)) {
findExternalClassReferences((RefClass) refEntity);
} else if (refEntity instanceof RefMethod) {
RefMethod refMethod = (RefMethod) refEntity;
if (refMethod.isConstructor() && strictUnreferencedFilter.accepts(refMethod)) {
findExternalClassReferences(refMethod.getOwnerClass());
}
}
}
private void findExternalClassReferences(final RefClass refElement) {
final PsiClass psiClass = refElement.getElement();
String qualifiedName = psiClass != null ? psiClass.getQualifiedName() : null;
if (qualifiedName != null) {
final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(globalContext.getProject());
final PsiNonJavaFileReferenceProcessor processor = (file, startOffset, endOffset) -> {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
return false;
};
final DelegatingGlobalSearchScope globalSearchScope = new DelegatingGlobalSearchScope(projectScope) {
@Override
public boolean contains(@NotNull VirtualFile file) {
return file.getFileType() != JavaFileType.INSTANCE && super.contains(file);
}
};
if (helper.processUsagesInNonJavaFiles(qualifiedName, processor, globalSearchScope)) {
final PsiReference reference = ReferencesSearch.search(psiClass, globalSearchScope).findFirst();
if (reference != null) {
getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
for (PsiMethod method : psiClass.getMethods()) {
final RefElement refMethod = refManager.getReference(method);
if (refMethod != null) {
getEntryPointsManager(globalContext).addEntryPoint(refMethod, false);
}
}
}
}
}
}
});
}
}, null);
}
myProcessedSuspicious = new HashSet<>();
myPhase = 1;
}
Aggregations