mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
support searching for annotated members in local scope (EA-30366)
This commit is contained in:
@@ -25,6 +25,7 @@ import com.intellij.psi.impl.search.JavaSourceFilterScope;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.stubs.StringStubIndexExtension;
|
||||
import com.intellij.psi.stubs.StubIndexKey;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -39,7 +40,7 @@ public class JavaAnnotationIndex extends StringStubIndexExtension<PsiAnnotation>
|
||||
return JavaStubIndexKeys.ANNOTATIONS;
|
||||
}
|
||||
|
||||
public Collection<PsiAnnotation> get(final String s, final Project project, final GlobalSearchScope scope) {
|
||||
public Collection<PsiAnnotation> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
|
||||
return super.get(s, project, new JavaSourceFilterScope(scope));
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -25,6 +25,7 @@ import com.intellij.psi.impl.search.JavaSourceFilterScope;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.stubs.StringStubIndexExtension;
|
||||
import com.intellij.psi.stubs.StubIndexKey;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -40,7 +41,7 @@ public class JavaAnonymousClassBaseRefOccurenceIndex extends StringStubIndexExte
|
||||
return JavaStubIndexKeys.ANONYMOUS_BASEREF;
|
||||
}
|
||||
|
||||
public Collection<PsiAnonymousClass> get(final String s, final Project project, final GlobalSearchScope scope) {
|
||||
public Collection<PsiAnonymousClass> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
|
||||
return super.get(s, project, new JavaSourceFilterScope(scope));
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import com.intellij.psi.impl.search.JavaSourceFilterScope;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.stubs.StringStubIndexExtension;
|
||||
import com.intellij.psi.stubs.StubIndexKey;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -39,7 +40,7 @@ public class JavaFieldNameIndex extends StringStubIndexExtension<PsiField> {
|
||||
return JavaStubIndexKeys.FIELDS;
|
||||
}
|
||||
|
||||
public Collection<PsiField> get(final String s, final Project project, final GlobalSearchScope scope) {
|
||||
public Collection<PsiField> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
|
||||
return super.get(s, project, new JavaSourceFilterScope(scope));
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -25,6 +25,7 @@ import com.intellij.psi.impl.search.JavaSourceFilterScope;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.stubs.IntStubIndexExtension;
|
||||
import com.intellij.psi.stubs.StubIndexKey;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -38,7 +39,7 @@ public class JavaFullClassNameIndex extends IntStubIndexExtension<PsiClass> {
|
||||
return JavaStubIndexKeys.CLASS_FQN;
|
||||
}
|
||||
|
||||
public Collection<PsiClass> get(final Integer integer, final Project project, final GlobalSearchScope scope) {
|
||||
public Collection<PsiClass> get(final Integer integer, final Project project, @NotNull final GlobalSearchScope scope) {
|
||||
return super.get(integer, project, new JavaSourceFilterScope(scope));
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import com.intellij.psi.impl.search.JavaSourceFilterScope;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.stubs.StringStubIndexExtension;
|
||||
import com.intellij.psi.stubs.StubIndexKey;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -39,7 +40,7 @@ public class JavaMethodNameIndex extends StringStubIndexExtension<PsiMethod> {
|
||||
return JavaStubIndexKeys.METHODS;
|
||||
}
|
||||
|
||||
public Collection<PsiMethod> get(final String s, final Project project, final GlobalSearchScope scope) {
|
||||
public Collection<PsiMethod> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
|
||||
return super.get(s, project, new JavaSourceFilterScope(scope));
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -25,6 +25,7 @@ import com.intellij.psi.impl.search.JavaSourceFilterScope;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.stubs.StringStubIndexExtension;
|
||||
import com.intellij.psi.stubs.StubIndexKey;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -39,7 +40,7 @@ public class JavaShortClassNameIndex extends StringStubIndexExtension<PsiClass>
|
||||
return JavaStubIndexKeys.CLASS_SHORT_NAMES;
|
||||
}
|
||||
|
||||
public Collection<PsiClass> get(final String s, final Project project, final GlobalSearchScope scope) {
|
||||
public Collection<PsiClass> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
|
||||
return super.get(s, project, new JavaSourceFilterScope(scope));
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -25,6 +25,7 @@ import com.intellij.psi.impl.search.JavaSourceFilterScope;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.stubs.StringStubIndexExtension;
|
||||
import com.intellij.psi.stubs.StubIndexKey;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -40,7 +41,7 @@ public class JavaSuperClassNameOccurenceIndex extends StringStubIndexExtension<P
|
||||
return JavaStubIndexKeys.SUPER_CLASSES;
|
||||
}
|
||||
|
||||
public Collection<PsiReferenceList> get(final String s, final Project project, final GlobalSearchScope scope) {
|
||||
public Collection<PsiReferenceList> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
|
||||
return super.get(s, project, new JavaSourceFilterScope(scope));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.intellij.psi.impl.search;
|
||||
|
||||
import com.intellij.openapi.application.AccessToken;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.ReadAction;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
@@ -8,6 +10,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.PsiManagerImpl;
|
||||
import com.intellij.psi.impl.java.stubs.index.JavaAnnotationIndex;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.psi.search.searches.AnnotatedElementsSearch;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
@@ -16,6 +19,7 @@ import com.intellij.util.QueryExecutor;
|
||||
import com.intellij.util.indexing.FileBasedIndex;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
@@ -36,15 +40,7 @@ public class AnnotatedElementsSearcher implements QueryExecutor<PsiModifierListO
|
||||
final SearchScope useScope = p.getScope();
|
||||
Class<? extends PsiModifierListOwner>[] types = p.getTypes();
|
||||
|
||||
final GlobalSearchScope scope = useScope instanceof GlobalSearchScope ? (GlobalSearchScope)useScope : null;
|
||||
final Collection<? extends PsiElement> annotations = ApplicationManager.getApplication().runReadAction(
|
||||
new Computable<Collection<? extends PsiElement>>() {
|
||||
@Override
|
||||
public Collection<? extends PsiElement> compute() {
|
||||
return JavaAnnotationIndex.getInstance().get(annClass.getName(), annClass.getProject(), scope);
|
||||
}
|
||||
});
|
||||
for (PsiElement elt : annotations) {
|
||||
for (PsiElement elt : getAnnotationCandidates(annClass, useScope)) {
|
||||
if (notAnnotation(elt)) continue;
|
||||
|
||||
final PsiAnnotation ann = (PsiAnnotation)elt;
|
||||
@@ -87,6 +83,30 @@ public class AnnotatedElementsSearcher implements QueryExecutor<PsiModifierListO
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Collection<? extends PsiElement> getAnnotationCandidates(final PsiClass annClass, SearchScope useScope) {
|
||||
AccessToken token = ReadAction.start();
|
||||
try {
|
||||
if (useScope instanceof GlobalSearchScope) {
|
||||
return JavaAnnotationIndex.getInstance().get(annClass.getName(), annClass.getProject(), (GlobalSearchScope)useScope);
|
||||
}
|
||||
final ArrayList<PsiAnnotation> result = new ArrayList<PsiAnnotation>();
|
||||
for (PsiElement element : ((LocalSearchScope)useScope).getScope()) {
|
||||
element.accept(new PsiRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitElement(PsiElement element) {
|
||||
if (element instanceof PsiAnnotation) {
|
||||
result.add((PsiAnnotation)element);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
finally {
|
||||
token.finish();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isInstanceof(PsiElement owner, Class<? extends PsiModifierListOwner>[] types) {
|
||||
for (Class<? extends PsiModifierListOwner> type : types) {
|
||||
if(type.isInstance(owner)) return true;
|
||||
|
||||
@@ -25,11 +25,12 @@ import com.intellij.openapi.roots.ProjectRootManager;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.search.DelegatingGlobalSearchScope;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class JavaSourceFilterScope extends DelegatingGlobalSearchScope {
|
||||
private final ProjectFileIndex myIndex;
|
||||
|
||||
public JavaSourceFilterScope(final GlobalSearchScope delegate) {
|
||||
public JavaSourceFilterScope(@NotNull final GlobalSearchScope delegate) {
|
||||
super(delegate);
|
||||
myIndex = ProjectRootManager.getInstance(getProject()).getFileIndex();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user