mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
make DumbService.runReadActionInSmartMode work in read action in dumb mode, add some invocations
This commit is contained in:
+3
-3
@@ -22,6 +22,7 @@ package com.intellij.psi.impl.search;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.ProgressIndicatorProvider;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.psi.*;
|
||||
@@ -78,8 +79,7 @@ public class AllClassesSearchExecutor implements QueryExecutor<PsiClass, AllClas
|
||||
final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
|
||||
for (final String name : names) {
|
||||
ProgressIndicatorProvider.checkCanceled();
|
||||
final PsiClass[] classes = MethodUsagesSearcher.resolveInReadAction(project, () -> cache.getClassesByName(name, scope));
|
||||
for (PsiClass psiClass : classes) {
|
||||
for (PsiClass psiClass : DumbService.getInstance(project).runReadActionInSmartMode(() -> cache.getClassesByName(name, scope))) {
|
||||
ProgressIndicatorProvider.checkCanceled();
|
||||
if (!processor.process(psiClass)) {
|
||||
return false;
|
||||
@@ -92,7 +92,7 @@ public class AllClassesSearchExecutor implements QueryExecutor<PsiClass, AllClas
|
||||
public static Project processClassNames(final Project project, final GlobalSearchScope scope, final Consumer<String> consumer) {
|
||||
final ProgressIndicator indicator = ProgressIndicatorProvider.getGlobalProgressIndicator();
|
||||
|
||||
MethodUsagesSearcher.resolveInReadAction(project, new Computable<Void>() {
|
||||
DumbService.getInstance(project).runReadActionInSmartMode(new Computable<Void>() {
|
||||
@Override
|
||||
public Void compute() {
|
||||
PsiShortNamesCache.getInstance(project).processAllClassNames(new Processor<String>() {
|
||||
|
||||
+10
-8
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.psi.impl.search;
|
||||
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
@@ -59,7 +60,7 @@ public class ConstructorReferencesSearchHelper {
|
||||
final boolean[] isEnum = new boolean[1];
|
||||
final boolean[] isUnder18 = new boolean[1];
|
||||
|
||||
MethodUsagesSearcher.resolveInReadAction(project, new Computable<Void>() {
|
||||
DumbService.getInstance(project).runReadActionInSmartMode(new Computable<Void>() {
|
||||
@Override
|
||||
public Void compute() {
|
||||
constructorCanBeCalledImplicitly[0] = constructor.getParameterList().getParametersCount() == 0;
|
||||
@@ -103,10 +104,10 @@ public class ConstructorReferencesSearchHelper {
|
||||
}
|
||||
|
||||
// search usages like "this(..)"
|
||||
if (!MethodUsagesSearcher.resolveInReadAction(project,
|
||||
() -> processSuperOrThis(containingClass, constructor, constructorCanBeCalledImplicitly[0], searchScope, project,
|
||||
isStrictSignatureSearch,
|
||||
PsiKeyword.THIS, processor))) {
|
||||
if (!DumbService.getInstance(project).runReadActionInSmartMode(
|
||||
() -> processSuperOrThis(containingClass, constructor, constructorCanBeCalledImplicitly[0], searchScope, project,
|
||||
isStrictSignatureSearch,
|
||||
PsiKeyword.THIS, processor))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -127,7 +128,7 @@ public class ConstructorReferencesSearchHelper {
|
||||
@NotNull final PsiMethod constructor,
|
||||
@NotNull final Project project,
|
||||
@NotNull final PsiClass aClass) {
|
||||
return MethodUsagesSearcher.resolveInReadAction(project, () -> {
|
||||
return DumbService.getInstance(project).runReadActionInSmartMode(() -> {
|
||||
for (PsiField field : aClass.getFields()) {
|
||||
if (field instanceof PsiEnumConstant) {
|
||||
PsiReference reference = field.getReference();
|
||||
@@ -149,7 +150,7 @@ public class ConstructorReferencesSearchHelper {
|
||||
return ReferencesSearch.search(aClass, searchScope).forEach(reference -> {
|
||||
final PsiElement element = reference.getElement();
|
||||
if (element != null) {
|
||||
return MethodUsagesSearcher.resolveInReadAction(project, () -> {
|
||||
return DumbService.getInstance(project).runReadActionInSmartMode(() -> {
|
||||
final PsiElement parent = element.getParent();
|
||||
if (parent instanceof PsiMethodReferenceExpression &&
|
||||
((PsiMethodReferenceExpression)parent).getReferenceNameElement() instanceof PsiKeyword) {
|
||||
@@ -219,7 +220,8 @@ public class ConstructorReferencesSearchHelper {
|
||||
@NotNull final Project project,
|
||||
@NotNull final PsiClass containingClass) {
|
||||
if (containingClass instanceof PsiAnonymousClass) return true;
|
||||
boolean same = MethodUsagesSearcher.resolveInReadAction(project, () -> myManager.areElementsEquivalent(constructor.getContainingClass(), containingClass.getSuperClass()));
|
||||
boolean same = DumbService.getInstance(project).runReadActionInSmartMode(
|
||||
() -> myManager.areElementsEquivalent(constructor.getContainingClass(), containingClass.getSuperClass()));
|
||||
if (!same) {
|
||||
return true;
|
||||
}
|
||||
|
||||
+6
-5
@@ -17,9 +17,9 @@ package com.intellij.psi.impl.search;
|
||||
|
||||
import com.intellij.concurrency.JobLauncher;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.ReadAction;
|
||||
import com.intellij.openapi.progress.ProgressIndicatorProvider;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
@@ -176,9 +176,10 @@ public class JavaDirectInheritorsSearcher implements QueryExecutor<PsiClass, Dir
|
||||
@NotNull PsiClass baseClass,
|
||||
@NotNull String baseClassName,
|
||||
@NotNull SearchScope useScope) {
|
||||
GlobalSearchScope globalUseScope = ReadAction.compute(() -> StubHierarchyInheritorSearcher.restrictScope(toGlobal(useScope, project)));
|
||||
DumbService dumbService = DumbService.getInstance(project);
|
||||
GlobalSearchScope globalUseScope = dumbService.runReadActionInSmartMode(() -> StubHierarchyInheritorSearcher.restrictScope(toGlobal(useScope, project)));
|
||||
Collection<PsiReferenceList> candidates =
|
||||
MethodUsagesSearcher.resolveInReadAction(project, () -> JavaSuperClassNameOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
|
||||
dumbService.runReadActionInSmartMode(() -> JavaSuperClassNameOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
|
||||
|
||||
// memory/speed optimisation: it really is a map(string -> PsiClass or List<PsiClass>)
|
||||
final Map<String, Object> classes = new HashMap<>();
|
||||
@@ -227,11 +228,11 @@ public class JavaDirectInheritorsSearcher implements QueryExecutor<PsiClass, Dir
|
||||
}
|
||||
|
||||
Collection<PsiAnonymousClass> anonymousCandidates =
|
||||
MethodUsagesSearcher.resolveInReadAction(project, () -> JavaAnonymousClassBaseRefOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
|
||||
dumbService.runReadActionInSmartMode(() -> JavaAnonymousClassBaseRefOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
|
||||
|
||||
processConcurrentlyIfTooMany(anonymousCandidates,
|
||||
candidate-> {
|
||||
boolean isInheritor = MethodUsagesSearcher.resolveInReadAction(project, () -> candidate.isInheritor(baseClass, false));
|
||||
boolean isInheritor = dumbService.runReadActionInSmartMode(() -> candidate.isInheritor(baseClass, false));
|
||||
if (isInheritor) {
|
||||
synchronized (result) {
|
||||
result.add(candidate);
|
||||
|
||||
+17
-27
@@ -15,11 +15,8 @@
|
||||
*/
|
||||
package com.intellij.psi.impl.search;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.QueryExecutorBase;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
@@ -46,7 +43,7 @@ public class MethodUsagesSearcher extends QueryExecutorBase<PsiReference, Method
|
||||
final boolean[] needStrictSignatureSearch = new boolean[1];
|
||||
final boolean strictSignatureSearch = p.isStrictSignatureSearch();
|
||||
|
||||
final PsiClass aClass = resolveInReadAction(p.getProject(), () -> {
|
||||
final PsiClass aClass = DumbService.getInstance(p.getProject()).runReadActionInSmartMode(() -> {
|
||||
PsiClass aClass1 = method.getContainingClass();
|
||||
if (aClass1 == null) return null;
|
||||
isConstructor[0] = method.isConstructor();
|
||||
@@ -66,7 +63,7 @@ public class MethodUsagesSearcher extends QueryExecutorBase<PsiReference, Method
|
||||
|
||||
final SearchRequestCollector collector = p.getOptimizer();
|
||||
|
||||
final SearchScope searchScope = resolveInReadAction(p.getProject(), () -> p.getEffectiveSearchScope());
|
||||
final SearchScope searchScope = DumbService.getInstance(p.getProject()).runReadActionInSmartMode(() -> p.getEffectiveSearchScope());
|
||||
if (searchScope == GlobalSearchScope.EMPTY_SCOPE) {
|
||||
return;
|
||||
}
|
||||
@@ -90,30 +87,23 @@ public class MethodUsagesSearcher extends QueryExecutorBase<PsiReference, Method
|
||||
return;
|
||||
}
|
||||
|
||||
resolveInReadAction(p.getProject(), new Computable<Void>() {
|
||||
@Override
|
||||
public Void compute() {
|
||||
final PsiMethod[] methods = strictSignatureSearch ? new PsiMethod[]{method} : aClass.findMethodsByName(methodName[0], false);
|
||||
SearchScope accessScope = methods[0].getUseScope();
|
||||
for (int i = 1; i < methods.length; i++) {
|
||||
PsiMethod method1 = methods[i];
|
||||
accessScope = accessScope.union(method1.getUseScope());
|
||||
}
|
||||
|
||||
SearchScope restrictedByAccessScope = searchScope.intersectWith(accessScope);
|
||||
|
||||
short searchContext = UsageSearchContext.IN_CODE | UsageSearchContext.IN_COMMENTS | UsageSearchContext.IN_FOREIGN_LANGUAGES;
|
||||
collector.searchWord(methodName[0], restrictedByAccessScope, searchContext, true, method,
|
||||
getTextOccurrenceProcessor(methods, aClass, strictSignatureSearch));
|
||||
|
||||
SimpleAccessorReferenceSearcher.addPropertyAccessUsages(method, restrictedByAccessScope, collector);
|
||||
return null;
|
||||
DumbService.getInstance(p.getProject()).runReadActionInSmartMode(()-> {
|
||||
final PsiMethod[] methods = strictSignatureSearch ? new PsiMethod[]{method} : aClass.findMethodsByName(methodName[0], false);
|
||||
SearchScope accessScope = methods[0].getUseScope();
|
||||
for (int i = 1; i < methods.length; i++) {
|
||||
PsiMethod method1 = methods[i];
|
||||
accessScope = accessScope.union(method1.getUseScope());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static <T> T resolveInReadAction(@NotNull Project p, @NotNull Computable<T> computable) {
|
||||
return ApplicationManager.getApplication().isReadAccessAllowed() ? computable.compute() : DumbService.getInstance(p).runReadActionInSmartMode(computable);
|
||||
SearchScope restrictedByAccessScope = searchScope.intersectWith(accessScope);
|
||||
|
||||
short searchContext = UsageSearchContext.IN_CODE | UsageSearchContext.IN_COMMENTS | UsageSearchContext.IN_FOREIGN_LANGUAGES;
|
||||
collector.searchWord(methodName[0], restrictedByAccessScope, searchContext, true, method,
|
||||
getTextOccurrenceProcessor(methods, aClass, strictSignatureSearch));
|
||||
|
||||
SimpleAccessorReferenceSearcher.addPropertyAccessUsages(method, restrictedByAccessScope, collector);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
protected MethodTextOccurrenceProcessor getTextOccurrenceProcessor(PsiMethod[] methods, PsiClass aClass, boolean strictSignatureSearch) {
|
||||
|
||||
@@ -79,7 +79,8 @@ public abstract class DumbService {
|
||||
public abstract void waitForSmartMode();
|
||||
|
||||
/**
|
||||
* Pause the current thread until dumb mode ends, and then run the read action. Index is guaranteed to be available inside that read action.
|
||||
* Pause the current thread until dumb mode ends, and then run the read action. Index is guaranteed to be available inside that read action,
|
||||
* unless this method is already called with read access allowed.
|
||||
*/
|
||||
public <T> T runReadActionInSmartMode(@NotNull final Computable<T> r) {
|
||||
final Ref<T> result = new Ref<T>();
|
||||
@@ -111,9 +112,15 @@ public abstract class DumbService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause the current thread until dumb mode ends, and then run the read action. Index is guaranteed to be available inside that read action.
|
||||
* Pause the current thread until dumb mode ends, and then run the read action. Index is guaranteed to be available inside that read action,
|
||||
* unless this method is already called with read access allowed.
|
||||
*/
|
||||
public void runReadActionInSmartMode(@NotNull final Runnable r) {
|
||||
if (ApplicationManager.getApplication().isReadAccessAllowed()) {
|
||||
r.run();
|
||||
return;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
waitForSmartMode();
|
||||
boolean success = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
|
||||
|
||||
+10
-18
@@ -15,9 +15,8 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.psi.impl;
|
||||
|
||||
import com.intellij.openapi.application.AccessToken;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.impl.search.StubHierarchyInheritorSearcher;
|
||||
@@ -25,6 +24,7 @@ import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.psi.search.searches.DirectClassInheritorsSearch;
|
||||
import com.intellij.psi.stubs.StubIndex;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.QueryExecutor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -70,17 +70,15 @@ public class GroovyDirectInheritorsSearcher implements QueryExecutor<PsiClass, D
|
||||
public boolean execute(@NotNull final DirectClassInheritorsSearch.SearchParameters queryParameters, @NotNull final Processor<PsiClass> consumer) {
|
||||
final PsiClass clazz = queryParameters.getClassToProcess();
|
||||
final SearchScope scope = queryParameters.getScope();
|
||||
Project project = PsiUtilCore.getProjectInReadAction(clazz);
|
||||
if (scope instanceof GlobalSearchScope) {
|
||||
final List<PsiClass> candidates = ApplicationManager.getApplication().runReadAction(new Computable<List<PsiClass>>() {
|
||||
@Override
|
||||
public List<PsiClass> compute() {
|
||||
if (!clazz.isValid()) return Collections.emptyList();
|
||||
final List<PsiClass> candidates = DumbService.getInstance(project).runReadActionInSmartMode(() -> {
|
||||
if (!clazz.isValid()) return Collections.emptyList();
|
||||
GlobalSearchScope restrictedScope = StubHierarchyInheritorSearcher.restrictScope((GlobalSearchScope)scope);
|
||||
return getDerivingClassCandidates(clazz, restrictedScope, queryParameters.includeAnonymous());
|
||||
}
|
||||
});
|
||||
});
|
||||
for (final PsiClass candidate : candidates) {
|
||||
if (!queryParameters.isCheckInheritance() || isInheritor(clazz, candidate)) {
|
||||
if (!queryParameters.isCheckInheritance() || isInheritor(clazz, candidate, project)) {
|
||||
if (!consumer.process(candidate)) {
|
||||
return false;
|
||||
}
|
||||
@@ -93,13 +91,7 @@ public class GroovyDirectInheritorsSearcher implements QueryExecutor<PsiClass, D
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isInheritor(PsiClass clazz, PsiClass candidate) {
|
||||
AccessToken accessToken = ApplicationManager.getApplication().acquireReadActionLock();
|
||||
try {
|
||||
return candidate.isValid() && candidate.isInheritor(clazz, false);
|
||||
}
|
||||
finally {
|
||||
accessToken.finish();
|
||||
}
|
||||
private static boolean isInheritor(PsiClass clazz, PsiClass candidate, Project project) {
|
||||
return DumbService.getInstance(project).runReadActionInSmartMode(() -> candidate.isValid() && candidate.isInheritor(clazz, false));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user