[indexing] Remove progress indicator from PsiSearchHelper.isCheapEnoughToSearch and at use sites

Was unused

GitOrigin-RevId: 98f5e2b1f48f54fc15d54cbbf73ff549f96fca6a
This commit is contained in:
Tagir Valeev
2024-03-19 18:45:40 +01:00
committed by intellij-monorepo-bot
parent 310d7e251c
commit 5428775b85
33 changed files with 99 additions and 84 deletions

View File

@@ -51,7 +51,7 @@ final class JavaTelescope {
ProgressIndicator progress = ObjectUtils.notNull(ProgressIndicatorProvider.getGlobalProgressIndicator(), /*todo remove*/new EmptyProgressIndicator());
AtomicInteger totalUsageCount = new AtomicInteger();
JobLauncher.getInstance().invokeConcurrentlyUnderProgress(members, progress, member -> {
int count = usagesCount(project, file, member, scope, progress);
int count = usagesCount(project, file, member, scope);
int newCount = totalUsageCount.updateAndGet(old -> count == TOO_MANY_USAGES ? TOO_MANY_USAGES : old + count);
return newCount != TOO_MANY_USAGES;
});
@@ -61,11 +61,10 @@ final class JavaTelescope {
private static int usagesCount(@NotNull Project project,
@NotNull PsiFile containingFile,
@NotNull final PsiMember member,
@NotNull SearchScope scope,
@NotNull ProgressIndicator progress) {
@NotNull SearchScope scope) {
SearchScope searchScope = getSearchScope(project, member, scope);
AtomicInteger count = new AtomicInteger();
boolean ok = UnusedSymbolUtil.processUsages(project, containingFile, searchScope, member, progress, null, info -> {
boolean ok = UnusedSymbolUtil.processUsages(project, containingFile, searchScope, member, null, info -> {
PsiFile psiFile = info.getFile();
if (psiFile == null) {
return true;

View File

@@ -119,21 +119,19 @@ public final class UnusedSymbolUtil {
public static boolean isFieldUsed(@NotNull Project project,
@NotNull PsiFile containingFile,
@NotNull PsiField field,
@NotNull ProgressIndicator progress,
@NotNull GlobalUsageHelper helper) {
if (helper.isLocallyUsed(field)) {
return true;
}
if (field instanceof PsiEnumConstant && isEnumValuesMethodUsed(project, containingFile, field, progress, helper)) {
if (field instanceof PsiEnumConstant && isEnumValuesMethodUsed(project, containingFile, field, helper)) {
return true;
}
return !weAreSureThereAreNoUsages(project, containingFile, field, progress, helper);
return !weAreSureThereAreNoUsages(project, containingFile, field, helper);
}
public static boolean isMethodUsed(@NotNull Project project,
@NotNull PsiFile containingFile,
@NotNull PsiMethod method,
@NotNull ProgressIndicator progress,
@NotNull GlobalUsageHelper helper) {
if (helper.isLocallyUsed(method)) return true;
@@ -148,13 +146,13 @@ public final class UnusedSymbolUtil {
return true;
}
if (!helper.isCurrentFileAlreadyChecked()) {
return !weAreSureThereAreNoUsages(project, containingFile, method, progress, helper);
return !weAreSureThereAreNoUsages(project, containingFile, method, helper);
}
}
else {
//class maybe used in some weird way, e.g. from XML, therefore the only constructor is used too
if (isTheOnlyConstructor(method, containingClass) &&
isClassUsed(project, containingFile, containingClass, progress, helper)) {
isClassUsed(project, containingFile, containingClass, helper)) {
return true;
}
if (isImplicitUsage(project, method)) return true;
@@ -162,7 +160,7 @@ public final class UnusedSymbolUtil {
if (!method.isConstructor() && FindSuperElementsHelper.findSuperElements(method).length != 0) {
return true;
}
return !weAreSureThereAreNoUsages(project, containingFile, method, progress, helper);
return !weAreSureThereAreNoUsages(project, containingFile, method, helper);
}
return false;
}
@@ -174,7 +172,6 @@ public final class UnusedSymbolUtil {
private static boolean weAreSureThereAreNoUsages(@NotNull Project project,
@NotNull PsiFile containingFile,
@NotNull final PsiMember member,
@NotNull ProgressIndicator progress,
@NotNull GlobalUsageHelper helper) {
log("* " + member.getName() + ": call wearesure");
if (!helper.shouldCheckUsages(member)) {
@@ -184,7 +181,7 @@ public final class UnusedSymbolUtil {
final PsiFile ignoreFile = helper.isCurrentFileAlreadyChecked() ? containingFile : null;
boolean sure = processUsages(project, containingFile, member, progress, ignoreFile, info -> {
boolean sure = processUsages(project, containingFile, member, ignoreFile, info -> {
PsiFile psiFile = info.getFile();
if (psiFile == ignoreFile || psiFile == null) {
return true; // ignore usages in containingFile because isLocallyUsed() method would have caught that
@@ -217,21 +214,33 @@ public final class UnusedSymbolUtil {
return useScope;
}
// return false if can't process usages (weird member of too may usages) or processor returned false
/**
* @deprecated use {@link #processUsages(Project, PsiFile, SearchScope, PsiMember, PsiFile, Processor)}
*/
@SuppressWarnings("unused")
@Deprecated
public static boolean processUsages(@NotNull Project project,
@NotNull PsiFile containingFile,
@NotNull PsiMember member,
@NotNull ProgressIndicator progress,
@Nullable PsiFile ignoreFile,
@NotNull Processor<? super UsageInfo> usageInfoProcessor) {
return processUsages(project, containingFile, getUseScope(member), member, progress, ignoreFile, usageInfoProcessor);
return processUsages(project, containingFile, member, ignoreFile, usageInfoProcessor);
}
// 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,
@Nullable PsiFile ignoreFile,
@NotNull Processor<? super UsageInfo> usageInfoProcessor) {
return processUsages(project, containingFile, getUseScope(member), member, ignoreFile, usageInfoProcessor);
}
public static boolean processUsages(@NotNull Project project,
@NotNull PsiFile containingFile,
@NotNull final SearchScope useScope,
@NotNull PsiMember member,
@NotNull ProgressIndicator progress,
@Nullable PsiFile ignoreFile,
@NotNull Processor<? super UsageInfo> usageInfoProcessor) {
String name = member.getName();
@@ -242,7 +251,7 @@ public final class UnusedSymbolUtil {
PsiSearchHelper searchHelper = PsiSearchHelper.getInstance(project);
if (useScope instanceof GlobalSearchScope) {
PsiSearchHelper.SearchCostResult cheapEnough = searchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope)useScope, ignoreFile, progress);
PsiSearchHelper.SearchCostResult cheapEnough = searchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope)useScope, ignoreFile);
if (cheapEnough == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES
// try to search for private and package-private members unconditionally - they are unlikely to have millions of usages
&& (member.hasModifierProperty(PsiModifier.PUBLIC) || member.hasModifierProperty(PsiModifier.PROTECTED))) {
@@ -262,7 +271,7 @@ public final class UnusedSymbolUtil {
if (propertyName != null) {
SearchScope fileScope = containingFile.getUseScope();
if (fileScope instanceof GlobalSearchScope &&
searchHelper.isCheapEnoughToSearch(propertyName, (GlobalSearchScope)fileScope, ignoreFile, progress) ==
searchHelper.isCheapEnoughToSearch(propertyName, (GlobalSearchScope)fileScope, ignoreFile) ==
PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) {
log("* "+member.getName()+" too many prop usages; false");
return false;
@@ -300,12 +309,11 @@ public final class UnusedSymbolUtil {
private static boolean isEnumValuesMethodUsed(@NotNull Project project,
@NotNull PsiFile containingFile,
@NotNull PsiMember member,
@NotNull ProgressIndicator progress,
@NotNull GlobalUsageHelper helper) {
final PsiClass containingClass = member.getContainingClass();
if (!(containingClass instanceof PsiClassImpl)) return true;
final PsiMethod valuesMethod = ((PsiClassImpl)containingClass).getValuesMethod();
return valuesMethod == null || isMethodUsed(project, containingFile, valuesMethod, progress, helper);
return valuesMethod == null || isMethodUsed(project, containingFile, valuesMethod, helper);
}
private static boolean canBeReferencedViaWeirdNames(@NotNull PsiMember member, @NotNull PsiFile containingFile) {
@@ -321,11 +329,10 @@ public final class UnusedSymbolUtil {
public static boolean isClassUsed(@NotNull Project project,
@NotNull PsiFile containingFile,
@NotNull PsiClass aClass,
@NotNull ProgressIndicator progress,
@NotNull GlobalUsageHelper helper) {
Boolean result = helper.unusedClassCache.get(aClass);
if (result == null) {
result = isReallyUsed(project, containingFile, aClass, progress, helper);
result = isReallyUsed(project, containingFile, aClass, helper);
helper.unusedClassCache.put(aClass, result);
}
return result;
@@ -334,7 +341,6 @@ public final class UnusedSymbolUtil {
private static boolean isReallyUsed(@NotNull Project project,
@NotNull PsiFile containingFile,
@NotNull PsiClass aClass,
@NotNull ProgressIndicator progress,
@NotNull GlobalUsageHelper helper) {
if (isImplicitUsage(project, aClass) || helper.isLocallyUsed(aClass)) return true;
if (helper.isCurrentFileAlreadyChecked()) {
@@ -342,7 +348,7 @@ public final class UnusedSymbolUtil {
aClass.getParent() instanceof PsiDeclarationStatement ||
aClass instanceof PsiTypeParameter) return false;
}
return !weAreSureThereAreNoUsages(project, containingFile, aClass, progress, helper);
return !weAreSureThereAreNoUsages(project, containingFile, aClass, helper);
}
private static boolean isIntentionalPrivateConstructor(@NotNull PsiMethod method, PsiClass containingClass) {

View File

@@ -27,7 +27,6 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicatorProvider;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
@@ -333,7 +332,7 @@ class PostHighlightingVisitor extends JavaElementVisitor {
quickFixes.add(QuickFixFactory.getInstance().createAddToImplicitlyWrittenFieldsFix(project, annoName)));
}
}
else if (!UnusedSymbolUtil.isFieldUsed(myProject, myFile, field, ProgressIndicatorProvider.getGlobalProgressIndicator(), myGlobalUsageHelper)) {
else if (!UnusedSymbolUtil.isFieldUsed(myProject, myFile, field, myGlobalUsageHelper)) {
if (UnusedSymbolUtil.isImplicitWrite(myProject, field)) {
message = getNotUsedForReadingMessage(field);
quickFixes.add(QuickFixFactory.getInstance().createSafeDeleteFix(field));
@@ -471,7 +470,7 @@ class PostHighlightingVisitor extends JavaElementVisitor {
}
private void processMethod(@NotNull Project project, @NotNull PsiMethod method) {
if (UnusedSymbolUtil.isMethodUsed(myProject, myFile, method, ProgressIndicatorProvider.getGlobalProgressIndicator(), myGlobalUsageHelper)) {
if (UnusedSymbolUtil.isMethodUsed(myProject, myFile, method, myGlobalUsageHelper)) {
return;
}
String key;
@@ -494,7 +493,7 @@ class PostHighlightingVisitor extends JavaElementVisitor {
}
private void processClass(@NotNull Project project, @NotNull PsiClass aClass) {
if (UnusedSymbolUtil.isClassUsed(project, myFile, aClass, ProgressIndicatorProvider.getGlobalProgressIndicator(), myGlobalUsageHelper)) {
if (UnusedSymbolUtil.isClassUsed(project, myFile, aClass, myGlobalUsageHelper)) {
return;
}

View File

@@ -79,7 +79,7 @@ public final class DeprecatedIsStillUsedInspection extends LocalInspectionTool {
@NotNull SearchScope searchScope) {
PsiSearchHelper.SearchCostResult cheapEnough
= searchScope instanceof GlobalSearchScope ?
psiSearchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope)searchScope, null, null) : null;
psiSearchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope)searchScope, null) : null;
if (cheapEnough == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES) {
return ThreeState.NO;
}

View File

@@ -323,7 +323,6 @@ public final class RedundantThrowsDeclarationLocalInspection extends AbstractBas
final PsiSearchHelper searchHelper = PsiSearchHelper.getInstance(method.getProject());
final PsiSearchHelper.SearchCostResult search = searchHelper.isCheapEnoughToSearch(method.getName(),
(GlobalSearchScope)method.getUseScope(),
null,
null);
if (search == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES) return false;
if (search == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) return true;

View File

@@ -10,7 +10,6 @@ import com.intellij.codeInspection.inheritance.ImplicitSubclassProvider;
import com.intellij.java.analysis.JavaAnalysisBundle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Comparing;
@@ -214,7 +213,7 @@ public class AccessCanBeTightenedInspection extends AbstractBaseJavaLocalInspect
int minLevel = Math.max(PsiUtil.ACCESS_LEVEL_PRIVATE, level);
AtomicInteger maxLevel = new AtomicInteger(minLevel);
AtomicBoolean foundUsage = new AtomicBoolean();
boolean proceed = UnusedSymbolUtil.processUsages(project, memberFile, member, new EmptyProgressIndicator(), null, info -> {
boolean proceed = UnusedSymbolUtil.processUsages(project, memberFile, member, null, info -> {
PsiElement element = info.getElement();
if (element == null) return true;
PsiFile psiFile = info.getFile();

View File

@@ -321,7 +321,7 @@ public final class RedundantMethodOverrideInspection extends BaseInspection {
if (isOnTheFly()) {
final PsiSearchHelper searchHelper = PsiSearchHelper.getInstance(method.getProject());
final PsiSearchHelper.SearchCostResult cost =
searchHelper.isCheapEnoughToSearch(method.getName(), scope, null, null);
searchHelper.isCheapEnoughToSearch(method.getName(), scope, null);
if (cost == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES) {
return true;
}

View File

@@ -15,7 +15,6 @@
*/
package com.siyeh.ig.psiutils;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.psi.*;
import com.intellij.psi.controlFlow.DefUseUtil;
import com.intellij.psi.search.GlobalSearchScope;
@@ -77,8 +76,7 @@ public final class DeclarationSearchUtils {
return false;
}
final PsiSearchHelper searchHelper = PsiSearchHelper.getInstance(element.getProject());
final PsiSearchHelper.SearchCostResult cost =
searchHelper.isCheapEnoughToSearch(name, globalSearchScope, null, ProgressManager.getInstance().getProgressIndicator());
final PsiSearchHelper.SearchCostResult cost = searchHelper.isCheapEnoughToSearch(name, globalSearchScope, null);
return cost == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES
? zeroResult
: cost == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES;

View File

@@ -126,7 +126,7 @@ public final class ExplicitToImplicitClassMigrationInspection extends AbstractBa
if (isOnTheFly) {
final PsiSearchHelper searchHelper = PsiSearchHelper.getInstance(project);
final PsiSearchHelper.SearchCostResult cost =
searchHelper.isCheapEnoughToSearch(className, scope, null, null);
searchHelper.isCheapEnoughToSearch(className, scope, null);
if (cost == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) {
return;
}

View File

@@ -7,7 +7,6 @@ import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase;
import com.intellij.codeInspection.reference.RefUtil;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.psi.*;
import com.intellij.psi.util.AccessModifier;
import com.intellij.psi.util.PropertyUtilBase;
@@ -16,8 +15,6 @@ import com.siyeh.ig.psiutils.MethodUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
@SuppressWarnings("InspectionDescriptionNotFoundInspection") // via UnusedReturnValue
public class UnusedReturnValueLocalInspection extends AbstractBaseJavaLocalInspectionTool {
private final UnusedReturnValue myGlobal;
@@ -56,7 +53,7 @@ public class UnusedReturnValueLocalInspection extends AbstractBaseJavaLocalInspe
}
final boolean[] atLeastOneUsageExists = new boolean[]{false};
if (UnusedSymbolUtil.processUsages(manager.getProject(), method.getContainingFile(), method, new EmptyProgressIndicator(), null, u -> {
if (UnusedSymbolUtil.processUsages(manager.getProject(), method.getContainingFile(), method, null, u -> {
if (!atLeastOneUsageExists[0]) atLeastOneUsageExists[0] = true;
PsiElement element = u.getElement();
if (element instanceof PsiReferenceExpression) {

View File

@@ -176,7 +176,7 @@ public class SafeDeleteFix extends LocalQuickFixAndIntentionActionOnPsiElement {
final String name = namedElement.getName();
if (name != null &&
PsiSearchHelper.getInstance(explored.getProject())
.isCheapEnoughToSearch(name, GlobalSearchScope.projectScope(explored.getProject()), null, null) ==
.isCheapEnoughToSearch(name, GlobalSearchScope.projectScope(explored.getProject()), null) ==
PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) {
return false;
}

View File

@@ -6,7 +6,6 @@ import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.reference.RefUtil;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ObjectUtils;
@@ -39,7 +38,7 @@ public class BooleanMethodIsAlwaysInvertedLocalInspection extends AbstractBaseJa
if (!PsiTypes.booleanType().equals(method.getReturnType()) || MethodUtils.hasSuper(method) || RefUtil.isImplicitRead(method)) return null;
int[] usageCount = {0};
if (!UnusedSymbolUtil.processUsages(manager.getProject(), method.getContainingFile(), method, new EmptyProgressIndicator(), null, u -> {
if (!UnusedSymbolUtil.processUsages(manager.getProject(), method.getContainingFile(), method, null, u -> {
PsiElement element = u.getElement();
if (!(element instanceof PsiReferenceExpression)) return false;
PsiMethodCallExpression methodCallExpression = ObjectUtils.tryCast(element.getParent(), PsiMethodCallExpression.class);

View File

@@ -13,7 +13,6 @@ import com.intellij.codeInspection.reference.*;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
@@ -414,7 +413,7 @@ public final class SameParameterValueInspection extends GlobalJavaBatchInspectio
Arrays.fill(paramValues, VALUE_UNDEFINED);
int[] usageCount = {0};
if (UnusedSymbolUtil.processUsages(holder.getProject(), holder.getFile(), javaMethod, new EmptyProgressIndicator(), null, info -> {
if (UnusedSymbolUtil.processUsages(holder.getProject(), holder.getFile(), javaMethod, null, info -> {
PsiElement element = info.getElement();
usageCount[0]++;
UElement uElement = UastContextKt.toUElement(element);

View File

@@ -126,7 +126,7 @@ public final class AtomicReferenceImplicitUsageProvider implements ImplicitUsage
PsiSearchHelper searchHelper = PsiSearchHelper.getInstance(project);
if (scope instanceof GlobalSearchScope &&
searchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope)scope, null, null) == FEW_OCCURRENCES) {
searchHelper.isCheapEnoughToSearch(name, (GlobalSearchScope)scope, null) == FEW_OCCURRENCES) {
return scope;
}
return null;

View File

@@ -221,7 +221,7 @@ class ProjectProblemsViewPropertyTest : BaseUnivocityTest() {
val module = ModuleUtilCore.findModuleForPsiElement(member) ?: return false
val scope = GlobalSearchScope.moduleScope(module)
val memberFile = member.containingFile
return PsiSearchHelper.getInstance(myProject).isCheapEnoughToSearch(name, scope, memberFile, null) != TOO_MANY_OCCURRENCES
return PsiSearchHelper.getInstance(myProject).isCheapEnoughToSearch(name, scope, memberFile) != TOO_MANY_OCCURRENCES
}
private fun isOverride(possibleOverride: PsiMethod, target: PsiMember): Boolean {

View File

@@ -3,6 +3,7 @@ package com.intellij.openapi.progress;
import com.intellij.openapi.application.ModalityState;
import org.jetbrains.annotations.ApiStatus.Obsolete;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -16,6 +17,7 @@ public class EmptyProgressIndicator extends EmptyProgressIndicatorBase implement
private volatile boolean myIsCanceled;
@Obsolete
@Contract(pure = true)
public EmptyProgressIndicator() { }
@Obsolete

View File

@@ -2,6 +2,7 @@
package com.intellij.openapi.progress;
import org.jetbrains.annotations.ApiStatus.Obsolete;
import org.jetbrains.annotations.Contract;
public abstract class ProgressIndicatorProvider {
public static ProgressIndicatorProvider getInstance() {
@@ -29,6 +30,7 @@ public abstract class ProgressIndicatorProvider {
* or null if this code is running outside any progress.
*/
@Obsolete
@Contract(pure = true)
public static ProgressIndicator getGlobalProgressIndicator() {
ProgressManager instance = ProgressManager.getInstanceOrNull();
return instance == null ? null : instance.getProgressIndicator();

View File

@@ -207,12 +207,23 @@ public interface PsiSearchHelper {
boolean caseSensitive);
/**
* @deprecated use {@link #isCheapEnoughToSearch(String, GlobalSearchScope, PsiFile)}
*/
@Deprecated
@NotNull
SearchCostResult isCheapEnoughToSearch(@NotNull String name,
@NotNull GlobalSearchScope scope,
@Nullable PsiFile fileToIgnoreOccurrencesIn,
@Nullable ProgressIndicator progress);
@NotNull
default SearchCostResult isCheapEnoughToSearch(@NotNull String name,
@NotNull GlobalSearchScope scope,
@Nullable PsiFile fileToIgnoreOccurrencesIn) {
return isCheapEnoughToSearch(name, scope, fileToIgnoreOccurrencesIn, null);
}
enum SearchCostResult {
ZERO_OCCURRENCES, FEW_OCCURRENCES, TOO_MANY_OCCURRENCES
}

View File

@@ -305,6 +305,13 @@ public class PsiSearchHelperImpl implements PsiSearchHelper {
@NotNull GlobalSearchScope scope,
@Nullable PsiFile fileToIgnoreOccurrencesIn,
@Nullable ProgressIndicator progress) {
return isCheapEnoughToSearch(name, scope, fileToIgnoreOccurrencesIn);
}
@Override
public @NotNull SearchCostResult isCheapEnoughToSearch(@NotNull String name,
@NotNull GlobalSearchScope scope,
@Nullable PsiFile fileToIgnoreOccurrencesIn) {
if (!ReadAction.compute(() -> scope.getUnloadedModulesBelongingToScope().isEmpty())) {
return SearchCostResult.TOO_MANY_OCCURRENCES;
}

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.refactoring.inline;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts.BorderTitle;
import com.intellij.openapi.util.NlsContexts.Label;
@@ -191,12 +190,11 @@ public abstract class InlineOptionsDialog extends RefactoringDialog implements I
protected static int getNumberOfOccurrences(PsiNameIdentifierOwner nameIdentifierOwner,
Predicate<? super PsiReference> ignoreOccurrence,
Function<? super GlobalSearchScope, ? extends Query<PsiReference>> searcher) {
final ProgressManager progressManager = ProgressManager.getInstance();
final PsiSearchHelper searchHelper = PsiSearchHelper.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;
name != null && searchHelper.isCheapEnoughToSearch(name, scope, null) != PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES;
return isCheapToSearch ? (int)searcher.apply(scope).findAll().stream().filter(ignoreOccurrence).count() : - 1;
}

View File

@@ -53,7 +53,7 @@ class ComponentModuleRegistrationChecker(private val moduleToModuleSet: Synchron
val psiSearchHelper = PsiSearchHelper.getInstance(project)
val scope = GlobalSearchScope.projectScope(project)
if (psiSearchHelper.isCheapEnoughToSearch(shortName, scope, null, null) == PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES) {
if (psiSearchHelper.isCheapEnoughToSearch(shortName, scope, null) == PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES) {
var extensionPointClass: PsiClass? = null
psiSearchHelper.processElementsWithWord(
{ element, _ ->

View File

@@ -102,7 +102,7 @@ public class GroovyPostHighlightingPass extends TextEditorHighlightingPass {
String name = ((GrNamedElement)element).getName();
if (element instanceof GrTypeDefinition && !UnusedSymbolUtil.isClassUsed(myProject,
element.getContainingFile(), (GrTypeDefinition)element,
progress, usageHelper
usageHelper
)) {
HighlightInfo.Builder builder = UnusedSymbolUtil
.createUnusedSymbolInfoBuilder(nameId, GroovyBundle.message("text.class.0.is.unused", name), HighlightInfoType.UNUSED_SYMBOL, GroovyUnusedDeclarationInspection.SHORT_NAME);
@@ -115,7 +115,7 @@ public class GroovyPostHighlightingPass extends TextEditorHighlightingPass {
usageHelper.shouldCheckContributors = false;
}
try {
if (!UnusedSymbolUtil.isMethodUsed(method.getProject(), method.getContainingFile(), method, progress, usageHelper)) {
if (!UnusedSymbolUtil.isMethodUsed(method.getProject(), method.getContainingFile(), method, usageHelper)) {
String message;
if (method.isConstructor()) {
message = GroovyBundle.message("text.constructor.0.is.unused", name);
@@ -132,7 +132,7 @@ public class GroovyPostHighlightingPass extends TextEditorHighlightingPass {
usageHelper.shouldCheckContributors = true;
}
}
else if (element instanceof GrField && isFieldUnused((GrField)element, progress, usageHelper)) {
else if (element instanceof GrField && isFieldUnused((GrField)element, usageHelper)) {
HighlightInfo.Builder builder =
UnusedSymbolUtil.createUnusedSymbolInfoBuilder(nameId, GroovyBundle.message("text.property.0.is.unused", name), HighlightInfoType.UNUSED_SYMBOL, GroovyUnusedDeclarationInspection.SHORT_NAME);
IntentionAction action = QuickFixFactory.getInstance().createSafeDeleteFix(element);
@@ -196,8 +196,8 @@ public class GroovyPostHighlightingPass extends TextEditorHighlightingPass {
!PsiClassImplUtil.isMainOrPremainMethod(method);
}
private static boolean isFieldUnused(GrField field, ProgressIndicator progress, GlobalUsageHelper usageHelper) {
if (UnusedSymbolUtil.isFieldUsed(field.getProject(), field.getContainingFile(), field, progress, usageHelper)) return false;
private static boolean isFieldUnused(GrField field, GlobalUsageHelper usageHelper) {
if (UnusedSymbolUtil.isFieldUsed(field.getProject(), field.getContainingFile(), field, usageHelper)) return false;
final GrAccessorMethod[] getters = field.getGetters();
final GrAccessorMethod setter = field.getSetter();

View File

@@ -48,7 +48,7 @@ final class JavaFxImplicitUsageProvider implements ImplicitUsageProvider {
if (name == null) return false;
final Project project = member.getProject();
final PsiSearchHelper searchHelper = PsiSearchHelper.getInstance(project);
final PsiSearchHelper.SearchCostResult searchCost = searchHelper.isCheapEnoughToSearch(name, scope, null, null);
final PsiSearchHelper.SearchCostResult searchCost = searchHelper.isCheapEnoughToSearch(name, scope, null);
if (searchCost == PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES) {
final Query<PsiReference> query = ReferencesSearch.search(member, scope);
return query.findFirst() != null;

View File

@@ -36,10 +36,10 @@ private fun enumReferenceIsUsedByParameterizedTest(element: PsiEnumConstant): Bo
fun isCheapEnough(psiClass: PsiClass, name: String, useScope: SearchScope): Boolean {
if (useScope is LocalSearchScope) return true
val searchHelper = PsiSearchHelper.getInstance(psiClass.project)
if (SearchCostResult.ZERO_OCCURRENCES == searchHelper.isCheapEnoughToSearch(ORG_JUNIT_JUPITER_PARAMS_ENUM_SOURCE_SHORT, (useScope as GlobalSearchScope), null, null)) {
if (SearchCostResult.ZERO_OCCURRENCES == searchHelper.isCheapEnoughToSearch(ORG_JUNIT_JUPITER_PARAMS_ENUM_SOURCE_SHORT, (useScope as GlobalSearchScope), null)) {
return false
}
val cheapEnough = searchHelper.isCheapEnoughToSearch(name, useScope, null, null)
val cheapEnough = searchHelper.isCheapEnoughToSearch(name, useScope, null)
return !(cheapEnough == SearchCostResult.ZERO_OCCURRENCES || cheapEnough == SearchCostResult.TOO_MANY_OCCURRENCES)
}

View File

@@ -2,7 +2,6 @@
package org.jetbrains.kotlin.idea.search
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
@@ -18,9 +17,10 @@ import com.intellij.psi.search.SearchScope
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.util.Processor
import com.intellij.util.indexing.FileBasedIndex
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.kotlin.idea.base.util.*
import org.jetbrains.kotlin.idea.base.util.codeUsageScope
import org.jetbrains.kotlin.idea.base.util.projectScope
import org.jetbrains.kotlin.idea.base.util.restrictToKotlinSources
import org.jetbrains.kotlin.idea.base.util.useScope
import org.jetbrains.kotlin.idea.search.KotlinSearchUsagesSupport.SearchUtils.scriptDefinitionExists
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtFile
@@ -96,14 +96,13 @@ fun PsiElement.codeUsageScopeRestrictedToKotlinSources(): SearchScope = codeUsag
fun PsiSearchHelper.isCheapEnoughToSearchConsideringOperators(
name: String,
scope: GlobalSearchScope,
fileToIgnoreOccurrencesIn: PsiFile?,
progress: ProgressIndicator?
fileToIgnoreOccurrencesIn: PsiFile?
): PsiSearchHelper.SearchCostResult {
if (OperatorConventions.isConventionName(Name.identifier(name))) {
return PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES
}
return isCheapEnoughToSearch(name, scope, fileToIgnoreOccurrencesIn, progress)
return isCheapEnoughToSearch(name, scope, fileToIgnoreOccurrencesIn)
}
fun findScriptsWithUsages(declaration: KtNamedDeclaration, processor: (KtFile) -> Boolean): Boolean {

View File

@@ -188,7 +188,7 @@ object KotlinUnusedSymbolUtil {
listOfNotNull(declaration.getClassNameForCompanionObject())
for (name in list) {
if (name == null) continue
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null, null)) {
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null)) {
PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES -> {
} // go on, check other names
PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES -> zeroOccurrences = false

View File

@@ -17,9 +17,9 @@ import com.intellij.psi.search.PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES
import com.intellij.psi.search.PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.base.projectStructure.scope.KotlinSourceFilterScope
import org.jetbrains.kotlin.util.match
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKotlinInspection
import org.jetbrains.kotlin.idea.quickfix.RemoveValVarFromParameterFix
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.idea.search.isCheapEnoughToSearchConsideringOperators
@@ -29,8 +29,8 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKotlinInspection
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.util.match
internal val CONSTRUCTOR_VAL_VAR_MODIFIERS = listOf(
OPEN_KEYWORD, FINAL_KEYWORD, OVERRIDE_KEYWORD,
@@ -87,7 +87,7 @@ class CanBeParameterInspection : AbstractKotlinInspection() {
val restrictedScope = if (useScope is GlobalSearchScope) {
val psiSearchHelper = PsiSearchHelper.getInstance(parameter.project)
for (accessorName in parameter.getAccessorNames()) {
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(accessorName, useScope, null, null)) {
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(accessorName, useScope, null)) {
ZERO_OCCURRENCES -> {
} // go on
else -> return // accessor in use: should remain a property
@@ -95,7 +95,7 @@ class CanBeParameterInspection : AbstractKotlinInspection() {
}
// TOO_MANY_OCCURRENCES: too expensive
// ZERO_OCCURRENCES: unused at all, reported elsewhere
if (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null, null) != FEW_OCCURRENCES) return
if (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null) != FEW_OCCURRENCES) return
KotlinSourceFilterScope.projectSources(useScope, parameter.project)
} else useScope
// Find all references and check them

View File

@@ -96,7 +96,7 @@ class MemberVisibilityCanBePrivateInspection : AbstractKotlinInspection() {
val useScope = declaration.useScope
val name = declaration.name ?: return false
val restrictedScope = if (useScope is GlobalSearchScope) {
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null, null)) {
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null)) {
PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES -> return false
PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES -> return false
PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES -> KotlinSourceFilterScope.projectSourcesAndResources(useScope, declaration.project)

View File

@@ -53,7 +53,9 @@ import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKotlinInspection
import org.jetbrains.kotlin.idea.codeinsight.utils.*
import org.jetbrains.kotlin.idea.codeinsight.utils.ENUM_STATIC_METHOD_NAMES_WITH_ENTRIES_IN_JAVA
import org.jetbrains.kotlin.idea.codeinsight.utils.canBeReferenceToBuiltInEnumFunction
import org.jetbrains.kotlin.idea.codeinsight.utils.findExistingEditor
import org.jetbrains.kotlin.idea.completion.KotlinIdeaCompletionBundle
import org.jetbrains.kotlin.idea.core.isInheritable
import org.jetbrains.kotlin.idea.core.script.configuration.DefaultScriptingSupport
@@ -158,7 +160,7 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
listOfNotNull(declaration.getClassNameForCompanionObject())
for (name in list) {
if (name == null) continue
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null, null)) {
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null)) {
ZERO_OCCURRENCES -> {
} // go on, check other names
FEW_OCCURRENCES -> zeroOccurrences = false

View File

@@ -19,8 +19,8 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.core.CollectingNameValidator
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.CollectingNameValidator
import org.jetbrains.kotlin.idea.intentions.reflectToRegularFunctionType
import org.jetbrains.kotlin.idea.refactoring.changeSignature.*
import org.jetbrains.kotlin.idea.util.getDataFlowAwareTypes
@@ -193,7 +193,7 @@ class AddFunctionParametersFix(
val project = runReadAction { it.project }
val psiSearchHelper = PsiSearchHelper.getInstance(project)
val globalSearchScope = GlobalSearchScope.projectScope(project)
val cheapEnoughToSearch = psiSearchHelper.isCheapEnoughToSearch(name, globalSearchScope, null, null)
val cheapEnoughToSearch = psiSearchHelper.isCheapEnoughToSearch(name, globalSearchScope, null)
if (cheapEnoughToSearch == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) return false
}

View File

@@ -182,11 +182,11 @@ public final class UnusedPropertyInspection extends PropertiesInspectionBase {
if (name == null) return true;
PsiSearchHelper searchHelper = helper.getSearchHelper();
if (mayHaveUsages(property, name, searchHelper, helper.getOwnUseScope(), isOnTheFly, original)) return true;
if (mayHaveUsages(property, name, searchHelper, helper.getOwnUseScope(), isOnTheFly)) return true;
final GlobalSearchScope widerScope = isOnTheFly ? getWidestUseScope(property.getKey(), property.getProject(), helper.getModule())
: GlobalSearchScope.projectScope(property.getProject());
if (widerScope != null && mayHaveUsages(property, name, searchHelper, widerScope, isOnTheFly, original)) return true;
if (widerScope != null && mayHaveUsages(property, name, searchHelper, widerScope, isOnTheFly)) return true;
return false;
}
@@ -194,13 +194,12 @@ public final class UnusedPropertyInspection extends PropertiesInspectionBase {
@NotNull String name,
@NotNull PsiSearchHelper psiSearchHelper,
@NotNull GlobalSearchScope searchScope,
boolean onTheFly,
@Nullable ProgressIndicator indicator) {
boolean onTheFly) {
GlobalSearchScope exceptPropertyFiles = createExceptPropertyFilesScope(searchScope);
GlobalSearchScope newScope = searchScope.intersectWith(exceptPropertyFiles);
if (onTheFly) {
PsiSearchHelper.SearchCostResult cheapEnough = psiSearchHelper.isCheapEnoughToSearch(name, newScope, null, indicator);
PsiSearchHelper.SearchCostResult cheapEnough = psiSearchHelper.isCheapEnoughToSearch(name, newScope, null);
if (cheapEnough == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES) return false;
if (cheapEnough == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) return true;
}

View File

@@ -54,7 +54,7 @@ class PyReferencesCodeVisionProvider : ReferencesCodeVisionProvider() {
val scope = GlobalSearchScope.projectScope(element.project)
val costSearchOutsideCurrentFile =
PsiSearchHelper.getInstance(element.project).isCheapEnoughToSearch(elementName, scope, file, null)
PsiSearchHelper.getInstance(element.project).isCheapEnoughToSearch(elementName, scope, file)
if (costSearchOutsideCurrentFile == SearchCostResult.TOO_MANY_OCCURRENCES) return null
val usagesCount = AtomicInteger()

View File

@@ -59,7 +59,7 @@ private fun findAllDirectVariableUsages(variablePsi: PsiElement): Iterable<PsiEl
val uastScope = getUastScope(module.moduleScope)
val searchHelper = PsiSearchHelper.getInstance(module.project)
if (searchHelper.isCheapEnoughToSearch(variableName, uastScope, currentFile, null) != PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES) {
if (searchHelper.isCheapEnoughToSearch(variableName, uastScope, currentFile) != PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES) {
return localUsages
}