This commit is contained in:
Alexey Kudravtsev
2018-06-22 13:03:00 +03:00
parent 7416536541
commit 4f3cd1db55
6 changed files with 58 additions and 50 deletions
@@ -90,11 +90,11 @@ public abstract class PsiShortNamesCache {
@NotNull
public abstract String[] getAllClassNames();
public boolean processAllClassNames(Processor<String> processor) {
public boolean processAllClassNames(@NotNull Processor<String> processor) {
return ContainerUtil.process(getAllClassNames(), processor);
}
public boolean processAllClassNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public boolean processAllClassNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter) {
return ContainerUtil.process(getAllClassNames(), processor);
}
@@ -132,11 +132,11 @@ public abstract class PsiShortNamesCache {
return processMethodsWithName(name, scope, method -> processor.process(method));
}
public boolean processAllMethodNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public boolean processAllMethodNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter) {
return ContainerUtil.process(getAllMethodNames(), processor);
}
public boolean processAllFieldNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public boolean processAllFieldNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter) {
return ContainerUtil.process(getAllFieldNames(), processor);
}
@@ -96,7 +96,7 @@ public class CompositeShortNamesCache extends PsiShortNamesCache {
}
@Override
public boolean processAllClassNames(Processor<String> processor) {
public boolean processAllClassNames(@NotNull Processor<String> processor) {
CommonProcessors.UniqueProcessor<String> uniqueProcessor = new CommonProcessors.UniqueProcessor<>(processor);
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processAllClassNames(uniqueProcessor)) {
@@ -107,7 +107,7 @@ public class CompositeShortNamesCache extends PsiShortNamesCache {
}
@Override
public boolean processAllClassNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public boolean processAllClassNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processAllClassNames(processor, scope, filter)) {
return false;
@@ -117,7 +117,7 @@ public class CompositeShortNamesCache extends PsiShortNamesCache {
}
@Override
public boolean processAllMethodNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public boolean processAllMethodNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processAllMethodNames(processor, scope, filter)) {
return false;
@@ -127,7 +127,7 @@ public class CompositeShortNamesCache extends PsiShortNamesCache {
}
@Override
public boolean processAllFieldNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public boolean processAllFieldNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processAllFieldNames(processor, scope, filter)) {
return false;
@@ -16,6 +16,7 @@
package com.intellij.psi.impl;
import com.intellij.openapi.progress.ProgressIndicatorProvider;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.index.JavaFieldNameIndex;
@@ -41,28 +42,28 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
public class PsiShortNamesCacheImpl extends PsiShortNamesCache {
private final PsiManagerEx myManager;
private final Project myProject;
public PsiShortNamesCacheImpl(PsiManagerEx manager) {
myManager = manager;
public PsiShortNamesCacheImpl(Project project) {
myProject = project;
}
@Override
@NotNull
public PsiFile[] getFilesByName(@NotNull String name) {
return FilenameIndex.getFilesByName(myManager.getProject(), name, GlobalSearchScope.projectScope(myManager.getProject()));
return FilenameIndex.getFilesByName(myProject, name, GlobalSearchScope.projectScope(myProject));
}
@Override
@NotNull
public String[] getAllFileNames() {
return FilenameIndex.getAllFilenames(myManager.getProject());
return FilenameIndex.getAllFilenames(myProject);
}
@Override
@NotNull
public PsiClass[] getClassesByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
Collection<PsiClass> classes = JavaShortClassNameIndex.getInstance().get(name, myManager.getProject(), scope);
Collection<PsiClass> classes = JavaShortClassNameIndex.getInstance().get(name, myProject, scope);
if (classes.isEmpty()) return PsiClass.EMPTY_ARRAY;
List<PsiClass> result = new ArrayList<>(classes.size());
@@ -115,34 +116,33 @@ public class PsiShortNamesCacheImpl extends PsiShortNamesCache {
@Override
@NotNull
public String[] getAllClassNames() {
return ArrayUtil.toStringArray(JavaShortClassNameIndex.getInstance().getAllKeys(myManager.getProject()));
return ArrayUtil.toStringArray(JavaShortClassNameIndex.getInstance().getAllKeys(myProject));
}
@Override
public boolean processAllClassNames(Processor<String> processor) {
return JavaShortClassNameIndex.getInstance().processAllKeys(myManager.getProject(), processor);
public boolean processAllClassNames(@NotNull Processor<String> processor) {
return JavaShortClassNameIndex.getInstance().processAllKeys(myProject, processor);
}
@Override
public boolean processAllClassNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public boolean processAllClassNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
return StubIndex.getInstance().processAllKeys(JavaStubIndexKeys.CLASS_SHORT_NAMES, processor, scope, filter);
}
@Override
public boolean processAllMethodNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public boolean processAllMethodNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
return StubIndex.getInstance().processAllKeys(JavaStubIndexKeys.METHODS, processor, scope, filter);
}
@Override
public boolean processAllFieldNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public boolean processAllFieldNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
return StubIndex.getInstance().processAllKeys(JavaStubIndexKeys.FIELDS, processor, scope, filter);
}
@Override
@NotNull
public PsiMethod[] getMethodsByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
Collection<PsiMethod> methods = StubIndex.getElements(
JavaStubIndexKeys.METHODS, name, myManager.getProject(), new JavaSourceFilterScope(scope), PsiMethod.class);
Collection<PsiMethod> methods = JavaMethodNameIndex.getInstance().get(name, myProject, scope);
return filterMembers(methods, scope, PsiMethod.EMPTY_ARRAY);
}
@@ -156,19 +156,19 @@ public class PsiShortNamesCacheImpl extends PsiShortNamesCache {
return methods.size() != maxCount && super.process(method);
}
};
StubIndex.getInstance().processElements(JavaStubIndexKeys.METHODS, name, myManager.getProject(), scope, PsiMethod.class, processor);
StubIndex.getInstance().processElements(JavaStubIndexKeys.METHODS, name, myProject, scope, PsiMethod.class, processor);
return filterMembers(methods, scope, PsiMethod.EMPTY_ARRAY);
}
@Override
public boolean processMethodsWithName(@NotNull String name, @NotNull GlobalSearchScope scope, @NotNull Processor<PsiMethod> processor) {
return StubIndex.getInstance().processElements(JavaStubIndexKeys.METHODS, name, myManager.getProject(), scope, PsiMethod.class, processor);
return StubIndex.getInstance().processElements(JavaStubIndexKeys.METHODS, name, myProject, scope, PsiMethod.class, processor);
}
@Override
@NotNull
public String[] getAllMethodNames() {
return ArrayUtil.toStringArray(JavaMethodNameIndex.getInstance().getAllKeys(myManager.getProject()));
return ArrayUtil.toStringArray(JavaMethodNameIndex.getInstance().getAllKeys(myProject));
}
@Override
@@ -181,21 +181,21 @@ public class PsiShortNamesCacheImpl extends PsiShortNamesCache {
return fields.size() != maxCount && super.process(method);
}
};
StubIndex.getInstance().processElements(JavaStubIndexKeys.FIELDS, name, myManager.getProject(), scope, PsiField.class, processor);
StubIndex.getInstance().processElements(JavaStubIndexKeys.FIELDS, name, myProject, scope, PsiField.class, processor);
return filterMembers(fields, scope, PsiField.EMPTY_ARRAY);
}
@NotNull
@Override
public PsiField[] getFieldsByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
Collection<PsiField> fields = JavaFieldNameIndex.getInstance().get(name, myManager.getProject(), scope);
Collection<PsiField> fields = JavaFieldNameIndex.getInstance().get(name, myProject, scope);
return filterMembers(fields, scope, PsiField.EMPTY_ARRAY);
}
@Override
@NotNull
public String[] getAllFieldNames() {
return ArrayUtil.toStringArray(JavaFieldNameIndex.getInstance().getAllKeys(myManager.getProject()));
return ArrayUtil.toStringArray(JavaFieldNameIndex.getInstance().getAllKeys(myProject));
}
@Override
@@ -204,7 +204,7 @@ public class PsiShortNamesCacheImpl extends PsiShortNamesCache {
@NotNull GlobalSearchScope scope,
@Nullable IdFilter filter) {
return StubIndex.getInstance().processElements(
JavaStubIndexKeys.FIELDS, name, myManager.getProject(), new JavaSourceFilterScope(scope), filter, PsiField.class, processor);
JavaStubIndexKeys.FIELDS, name, myProject, new JavaSourceFilterScope(scope), filter, PsiField.class, processor);
}
@Override
@@ -213,7 +213,7 @@ public class PsiShortNamesCacheImpl extends PsiShortNamesCache {
@NotNull GlobalSearchScope scope,
@Nullable IdFilter filter) {
return StubIndex.getInstance().processElements(
JavaStubIndexKeys.METHODS, name, myManager.getProject(), new JavaSourceFilterScope(scope), filter, PsiMethod.class, processor);
JavaStubIndexKeys.METHODS, name, myProject, new JavaSourceFilterScope(scope), filter, PsiMethod.class, processor);
}
@Override
@@ -222,14 +222,16 @@ public class PsiShortNamesCacheImpl extends PsiShortNamesCache {
@NotNull GlobalSearchScope scope,
@Nullable IdFilter filter) {
return StubIndex.getInstance().processElements(
JavaStubIndexKeys.CLASS_SHORT_NAMES, name, myManager.getProject(), new JavaSourceFilterScope(scope), filter, PsiClass.class, processor);
JavaStubIndexKeys.CLASS_SHORT_NAMES, name, myProject, new JavaSourceFilterScope(scope), filter, PsiClass.class, processor);
}
private <T extends PsiMember> T[] filterMembers(Collection<T> members, GlobalSearchScope scope, T[] emptyArray) {
@NotNull
private <T extends PsiMember> T[] filterMembers(@NotNull Collection<T> members, @NotNull GlobalSearchScope scope, @NotNull T[] emptyArray) {
if (members.isEmpty()) {
return emptyArray;
}
PsiManager myManager = PsiManager.getInstance(myProject);
Set<PsiMember> set = new THashSet<>(members.size(), new TObjectHashingStrategy<PsiMember>() {
@Override
public int computeHashCode(PsiMember member) {
@@ -20,16 +20,19 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Processor;
import com.intellij.util.indexing.IdFilter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
interface FileNameIndexService {
Collection<VirtualFile> getVirtualFilesByName(Project project, String name, GlobalSearchScope scope, @Nullable IdFilter idFilter);
@NotNull
Collection<VirtualFile> getVirtualFilesByName(Project project, @NotNull String name, @NotNull GlobalSearchScope scope, @Nullable IdFilter idFilter);
void processAllFileNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter);
void processAllFileNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter);
Collection<VirtualFile> getFilesWithFileType(FileType type, GlobalSearchScope scope);
@NotNull
Collection<VirtualFile> getFilesWithFileType(@NotNull FileType type, @NotNull GlobalSearchScope scope);
boolean processFilesWithFileType(FileType type, Processor<VirtualFile> processor, GlobalSearchScope scope);
boolean processFilesWithFileType(@NotNull FileType type, @NotNull Processor<VirtualFile> processor, @NotNull GlobalSearchScope scope);
}
@@ -44,11 +44,10 @@ public class FilenameIndex {
/**
* @deprecated Not to be used.
*/
@Deprecated
public @NonNls static final ID<String, Void> NAME = ID.create("FilenameIndex");
@NonNls @Deprecated public static final ID<String, Void> NAME = ID.create("FilenameIndex");
@NotNull
public static String[] getAllFilenames(Project project) {
public static String[] getAllFilenames(@Nullable Project project) {
Set<String> names = new THashSet<>();
getService().processAllFileNames((String s) -> {
names.add(s);
@@ -57,24 +56,26 @@ public class FilenameIndex {
return ArrayUtil.toStringArray(names);
}
public static void processAllFileNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public static void processAllFileNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter) {
getService().processAllFileNames(processor, scope, filter);
}
public static Collection<VirtualFile> getVirtualFilesByName(final Project project, final String name, final GlobalSearchScope scope) {
@NotNull
public static Collection<VirtualFile> getVirtualFilesByName(final Project project, @NotNull String name, @NotNull GlobalSearchScope scope) {
return getService().getVirtualFilesByName(project, name, scope, null);
}
@NotNull
public static Collection<VirtualFile> getVirtualFilesByName(final Project project,
final String name,
@NotNull String name,
boolean caseSensitively,
final GlobalSearchScope scope) {
@NotNull GlobalSearchScope scope) {
if (caseSensitively) return getVirtualFilesByName(project, name, scope);
return getVirtualFilesByNameIgnoringCase(name, scope, project, null);
}
@NotNull
public static PsiFile[] getFilesByName(final Project project, final String name, final GlobalSearchScope scope) {
public static PsiFile[] getFilesByName(@NotNull Project project, @NotNull String name, @NotNull GlobalSearchScope scope) {
return (PsiFile[])getFilesByName(project, name, scope, false);
}
@@ -149,8 +150,8 @@ public class FilenameIndex {
}
@NotNull
public static PsiFileSystemItem[] getFilesByName(final Project project,
final String name,
public static PsiFileSystemItem[] getFilesByName(@NotNull Project project,
@NotNull String name,
@NotNull final GlobalSearchScope scope,
boolean includeDirs) {
SmartList<PsiFileSystemItem> result = new SmartList<>();
@@ -34,8 +34,9 @@ public class FileNameIndexServiceImpl implements FileNameIndexService {
myIndex = index;
}
@NotNull
@Override
public Collection<VirtualFile> getVirtualFilesByName(Project project, String name, GlobalSearchScope scope, IdFilter filter) {
public Collection<VirtualFile> getVirtualFilesByName(Project project, @NotNull String name, @NotNull GlobalSearchScope scope, IdFilter filter) {
Set<VirtualFile> files = new THashSet<>();
myIndex.processValues(FilenameIndexImpl.NAME, name, null, (file, value) -> {
files.add(file);
@@ -45,17 +46,18 @@ public class FileNameIndexServiceImpl implements FileNameIndexService {
}
@Override
public void processAllFileNames(Processor<String> processor, GlobalSearchScope scope, IdFilter filter) {
public void processAllFileNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
myIndex.processAllKeys(FilenameIndexImpl.NAME, processor, scope, filter);
}
@NotNull
@Override
public Collection<VirtualFile> getFilesWithFileType(FileType fileType, GlobalSearchScope scope) {
public Collection<VirtualFile> getFilesWithFileType(@NotNull FileType fileType, @NotNull GlobalSearchScope scope) {
return myIndex.getContainingFiles(FileTypeIndexImpl.NAME, fileType, scope);
}
@Override
public boolean processFilesWithFileType(FileType fileType, Processor<VirtualFile> processor, GlobalSearchScope scope) {
public boolean processFilesWithFileType(@NotNull FileType fileType, @NotNull Processor<VirtualFile> processor, @NotNull GlobalSearchScope scope) {
return myIndex.processValues(FileTypeIndexImpl.NAME, fileType, null, (file, value) -> processor.process(file), scope);
}
}