move PsiShortNamesCache to java-indexing

This commit is contained in:
Evgeny Pasynkov
2012-07-25 12:10:51 +02:00
parent 2f5d4b905d
commit 8cdaaee00d
11 changed files with 18 additions and 4 deletions
@@ -1,249 +0,0 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.psi.impl;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.HashSet;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
public class CompositeShortNamesCache extends PsiShortNamesCache {
private final List<PsiShortNamesCache> myCaches = new ArrayList<PsiShortNamesCache>();
private PsiShortNamesCache[] myCacheArray = new PsiShortNamesCache[0];
public CompositeShortNamesCache(Project project) {
if (!project.isDefault()) {
for (final PsiShortNamesCache cache : project.getExtensions(PsiShortNamesCache.EP_NAME)) {
addCache(cache);
}
}
}
public void addCache(PsiShortNamesCache cache) {
myCaches.add(cache);
myCacheArray = myCaches.toArray(new PsiShortNamesCache[myCaches.size()]);
}
@Override
@NotNull
public PsiFile[] getFilesByName(@NotNull String name) {
Merger<PsiFile> merger = null;
for (PsiShortNamesCache cache : myCacheArray) {
PsiFile[] classes = cache.getFilesByName(name);
if (classes.length != 0) {
if (merger == null) merger = new Merger<PsiFile>();
merger.add(classes);
}
}
PsiFile[] result = merger == null ? null : merger.getResult();
return result != null ? result : PsiFile.EMPTY_ARRAY;
}
@Override
@NotNull
public String[] getAllFileNames() {
Merger<String> merger = new Merger<String>();
for (PsiShortNamesCache cache : myCacheArray) {
merger.add(cache.getAllFileNames());
}
String[] result = merger.getResult();
return result != null ? result : ArrayUtil.EMPTY_STRING_ARRAY;
}
@Override
@NotNull
public PsiClass[] getClassesByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
Merger<PsiClass> merger = null;
for (PsiShortNamesCache cache : myCacheArray) {
PsiClass[] classes = cache.getClassesByName(name, scope);
if (classes.length != 0) {
if (merger == null) merger = new Merger<PsiClass>();
merger.add(classes);
}
}
PsiClass[] result = merger == null ? null : merger.getResult();
return result != null ? result : PsiClass.EMPTY_ARRAY;
}
@Override
@NotNull
public String[] getAllClassNames() {
Merger<String> merger = new Merger<String>();
for (PsiShortNamesCache cache : myCacheArray) {
merger.add(cache.getAllClassNames());
}
String[] result = merger.getResult();
return result != null ? result : ArrayUtil.EMPTY_STRING_ARRAY;
}
@Override
public void getAllClassNames(@NotNull HashSet<String> dest) {
for (PsiShortNamesCache cache : myCacheArray) {
cache.getAllClassNames(dest);
}
}
@Override
@NotNull
public PsiMethod[] getMethodsByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
Merger<PsiMethod> merger = null;
for (PsiShortNamesCache cache : myCacheArray) {
PsiMethod[] methods = cache.getMethodsByName(name, scope);
if (methods.length != 0) {
if (merger == null) merger = new Merger<PsiMethod>();
merger.add(methods);
}
}
PsiMethod[] result = merger == null ? null : merger.getResult();
return result == null ? PsiMethod.EMPTY_ARRAY : result;
}
@Override
@NotNull
public PsiMethod[] getMethodsByNameIfNotMoreThan(@NonNls @NotNull final String name, @NotNull final GlobalSearchScope scope, final int maxCount) {
Merger<PsiMethod> merger = null;
for (PsiShortNamesCache cache : myCacheArray) {
PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, maxCount);
if (methods.length == maxCount) return methods;
if (methods.length != 0) {
if (merger == null) merger = new Merger<PsiMethod>();
merger.add(methods);
}
}
PsiMethod[] result = merger == null ? null : merger.getResult();
return result == null ? PsiMethod.EMPTY_ARRAY : result;
}
@NotNull
@Override
public PsiField[] getFieldsByNameIfNotMoreThan(@NonNls @NotNull String name, @NotNull GlobalSearchScope scope, int maxCount) {
Merger<PsiField> merger = null;
for (PsiShortNamesCache cache : myCacheArray) {
PsiField[] fields = cache.getFieldsByNameIfNotMoreThan(name, scope, maxCount);
if (fields.length == maxCount) return fields;
if (fields.length != 0) {
if (merger == null) merger = new Merger<PsiField>();
merger.add(fields);
}
}
PsiField[] result = merger == null ? null : merger.getResult();
return result == null ? PsiField.EMPTY_ARRAY : result;
}
@Override
public boolean processMethodsWithName(@NonNls @NotNull String name,
@NotNull GlobalSearchScope scope,
@NotNull Processor<PsiMethod> processor) {
for (PsiShortNamesCache cache : myCacheArray) {
if (!cache.processMethodsWithName(name, scope, processor)) return false;
}
return true;
}
@Override
@NotNull
public String[] getAllMethodNames() {
Merger<String> merger = new Merger<String>();
for (PsiShortNamesCache cache : myCacheArray) {
merger.add(cache.getAllMethodNames());
}
String[] result = merger.getResult();
return result != null ? result : ArrayUtil.EMPTY_STRING_ARRAY;
}
@Override
public void getAllMethodNames(@NotNull HashSet<String> set) {
for (PsiShortNamesCache cache : myCacheArray) {
cache.getAllMethodNames(set);
}
}
@Override
@NotNull
public PsiField[] getFieldsByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
Merger<PsiField> merger = null;
for (PsiShortNamesCache cache : myCacheArray) {
PsiField[] classes = cache.getFieldsByName(name, scope);
if (classes.length != 0) {
if (merger == null) merger = new Merger<PsiField>();
merger.add(classes);
}
}
PsiField[] result = merger == null ? null : merger.getResult();
return result != null ? result : PsiField.EMPTY_ARRAY;
}
@Override
@NotNull
public String[] getAllFieldNames() {
Merger<String> merger = new Merger<String>();
for (PsiShortNamesCache cache : myCacheArray) {
merger.add(cache.getAllFieldNames());
}
String[] result = merger.getResult();
return result != null ? result : ArrayUtil.EMPTY_STRING_ARRAY;
}
@Override
public void getAllFieldNames(@NotNull HashSet<String> set) {
for (PsiShortNamesCache cache : myCacheArray) {
cache.getAllFieldNames(set);
}
}
private static class Merger<T> {
private T[] mySingleItem = null;
private Set<T> myAllItems = null;
public void add(@NotNull T[] items) {
if (items.length == 0) return;
if (mySingleItem == null) {
mySingleItem = items;
return;
}
if (myAllItems == null) {
myAllItems = new THashSet<T>(Arrays.asList(mySingleItem));
}
ContainerUtil.addAll(myAllItems, items);
}
public T[] getResult() {
if (myAllItems == null) return mySingleItem;
return myAllItems.toArray(mySingleItem);
}
}
@SuppressWarnings({"HardCodedStringLiteral"})
@Override
public String toString() {
return "Composite cache: " + myCaches;
}
}
@@ -1,237 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.psi.impl;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.index.JavaFieldNameIndex;
import com.intellij.psi.impl.java.stubs.index.JavaMethodNameIndex;
import com.intellij.psi.impl.java.stubs.index.JavaShortClassNameIndex;
import com.intellij.psi.impl.java.stubs.index.JavaStubIndexKeys;
import com.intellij.psi.impl.search.JavaSourceFilterScope;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.psi.stubs.StubIndex;
import com.intellij.util.ArrayUtil;
import com.intellij.util.CommonProcessors;
import com.intellij.util.Processor;
import com.intellij.util.SmartList;
import com.intellij.util.containers.HashSet;
import gnu.trove.THashSet;
import gnu.trove.TObjectHashingStrategy;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
class PsiShortNamesCacheImpl extends PsiShortNamesCache {
private final PsiManagerEx myManager;
public PsiShortNamesCacheImpl(PsiManagerEx manager) {
myManager = manager;
}
@Override
@NotNull
public PsiFile[] getFilesByName(@NotNull String name) {
return FilenameIndex.getFilesByName(myManager.getProject(), name, GlobalSearchScope.projectScope(myManager.getProject()));
}
@Override
@NotNull
public String[] getAllFileNames() {
return FilenameIndex.getAllFilenames(myManager.getProject());
}
@Override
@NotNull
public PsiClass[] getClassesByName(@NotNull String name, @NotNull final GlobalSearchScope scope) {
final Collection<PsiClass> classes = JavaShortClassNameIndex.getInstance().get(name, myManager.getProject(), scope);
if (classes.isEmpty()) return PsiClass.EMPTY_ARRAY;
ArrayList<PsiClass> list = new ArrayList<PsiClass>(classes.size());
OuterLoop:
for (PsiClass aClass : classes) {
VirtualFile vFile = aClass.getContainingFile().getVirtualFile();
if (!scope.contains(vFile)) continue;
for (int j = 0; j < list.size(); j++) {
PsiClass aClass1 = list.get(j);
String qName = aClass.getQualifiedName();
String qName1 = aClass1.getQualifiedName();
if (qName != null && qName1 != null && qName.equals(qName1)) {
VirtualFile vFile1 = aClass1.getContainingFile().getVirtualFile();
int res = scope.compare(vFile1, vFile);
if (res > 0) {
continue OuterLoop; // aClass1 hides aClass
}
else if (res < 0) {
list.remove(j);
//noinspection AssignmentToForLoopParameter
j--; // aClass hides aClass1
}
}
}
list.add(aClass);
}
return list.toArray(new PsiClass[list.size()]);
}
@Override
@NotNull
public String[] getAllClassNames() {
final Collection<String> names = JavaShortClassNameIndex.getInstance().getAllKeys(myManager.getProject());
return ArrayUtil.toStringArray(names);
}
@Override
public void getAllClassNames(@NotNull HashSet<String> set) {
set.addAll(JavaShortClassNameIndex.getInstance().getAllKeys(myManager.getProject()));
}
@Override
@NotNull
public PsiMethod[] getMethodsByName(@NotNull String name, @NotNull final GlobalSearchScope scope) {
Collection<PsiMethod> methods = StubIndex.getInstance().get(JavaStubIndexKeys.METHODS, name, myManager.getProject(), new JavaSourceFilterScope(scope));
if (methods.isEmpty()) return PsiMethod.EMPTY_ARRAY;
List<PsiMethod> list = filterMembers(methods, scope);
return list.toArray(new PsiMethod[list.size()]);
}
@Override
@NotNull
public PsiMethod[] getMethodsByNameIfNotMoreThan(@NonNls @NotNull final String name, @NotNull final GlobalSearchScope scope, final int maxCount) {
final List<PsiMethod> methods = new SmartList<PsiMethod>();
StubIndex.getInstance().process(JavaStubIndexKeys.METHODS, name, myManager.getProject(), scope, new CommonProcessors.CollectProcessor<PsiMethod>(methods){
@Override
public boolean process(PsiMethod method) {
return methods.size() != maxCount && super.process(method);
}
});
if (methods.isEmpty()) return PsiMethod.EMPTY_ARRAY;
List<PsiMethod> list = filterMembers(methods, scope);
return list.toArray(new PsiMethod[list.size()]);
}
@Override
public boolean processMethodsWithName(@NonNls @NotNull String name,
@NotNull GlobalSearchScope scope,
@NotNull Processor<PsiMethod> processor) {
return StubIndex.getInstance().process(JavaStubIndexKeys.METHODS, name, myManager.getProject(), scope, processor);
}
@Override
@NotNull
public String[] getAllMethodNames() {
final Collection<String> names = JavaMethodNameIndex.getInstance().getAllKeys(myManager.getProject());
return ArrayUtil.toStringArray(names);
}
@Override
public void getAllMethodNames(@NotNull HashSet<String> set) {
set.addAll(JavaMethodNameIndex.getInstance().getAllKeys(myManager.getProject()));
}
@Override
@NotNull
public PsiField[] getFieldsByNameIfNotMoreThan(@NotNull String name, @NotNull final GlobalSearchScope scope, final int maxCount) {
final List<PsiField> methods = new SmartList<PsiField>();
StubIndex.getInstance().process(JavaStubIndexKeys.FIELDS, name, myManager.getProject(), scope, new CommonProcessors.CollectProcessor<PsiField>(methods){
@Override
public boolean process(PsiField method) {
return methods.size() != maxCount && super.process(method);
}
});
if (methods.isEmpty()) return PsiField.EMPTY_ARRAY;
List<PsiField> list = filterMembers(methods, scope);
return list.toArray(new PsiField[list.size()]);
}
@NotNull
@Override
public PsiField[] getFieldsByName(@NotNull String name, @NotNull final GlobalSearchScope scope) {
final Collection<PsiField> fields = JavaFieldNameIndex.getInstance().get(name, myManager.getProject(), scope);
if (fields.isEmpty()) return PsiField.EMPTY_ARRAY;
List<PsiField> list = filterMembers(fields, scope);
return list.toArray(new PsiField[list.size()]);
}
@Override
@NotNull
public String[] getAllFieldNames() {
final Collection<String> names = JavaFieldNameIndex.getInstance().getAllKeys(myManager.getProject());
return ArrayUtil.toStringArray(names);
}
@Override
public void getAllFieldNames(@NotNull HashSet<String> set) {
set.addAll(JavaFieldNameIndex.getInstance().getAllKeys(myManager.getProject()));
}
private <T extends PsiMember> List<T> filterMembers(Collection<T> members, final GlobalSearchScope scope) {
List<T> result = new ArrayList<T>(members.size());
Set<PsiMember> set = new THashSet<PsiMember>(members.size(), new TObjectHashingStrategy<PsiMember>() {
@Override
public int computeHashCode(PsiMember member) {
int code = 0;
final PsiClass clazz = member.getContainingClass();
if (clazz != null) {
String name = clazz.getName();
if (name != null) {
code += name.hashCode();
}
else {
//anonymous classes are not equivalent
code += clazz.hashCode();
}
}
if (member instanceof PsiMethod) {
code += 37 * ((PsiMethod)member).getParameterList().getParametersCount();
}
return code;
}
@Override
public boolean equals(PsiMember object, PsiMember object1) {
return myManager.areElementsEquivalent(object, object1);
}
});
for (T member : members) {
ProgressManager.checkCanceled();
if (!scope.contains(member.getContainingFile().getVirtualFile())) continue;
if (!set.add(member)) continue;
result.add(member);
}
return result;
}
}
@@ -1,54 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.impl.java.stubs.index;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiField;
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;
public class JavaFieldNameIndex extends StringStubIndexExtension<PsiField> {
private static final JavaFieldNameIndex ourInstance = new JavaFieldNameIndex();
public static JavaFieldNameIndex getInstance() {
return ourInstance;
}
@NotNull
@Override
public StubIndexKey<String, PsiField> getKey() {
return JavaStubIndexKeys.FIELDS;
}
@Override
public Collection<PsiField> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
return super.get(s, project, new JavaSourceFilterScope(scope));
}
@Override
public boolean isKeyHighlySelective() {
return true;
}
}
@@ -1,54 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.impl.java.stubs.index;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiMethod;
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;
public class JavaMethodNameIndex extends StringStubIndexExtension<PsiMethod> {
private static final JavaMethodNameIndex ourInstance = new JavaMethodNameIndex();
public static JavaMethodNameIndex getInstance() {
return ourInstance;
}
@NotNull
@Override
public StubIndexKey<String, PsiMethod> getKey() {
return JavaStubIndexKeys.METHODS;
}
@Override
public Collection<PsiMethod> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
return super.get(s, project, new JavaSourceFilterScope(scope));
}
@Override
public boolean isKeyHighlySelective() {
return true;
}
}
@@ -1,54 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.impl.java.stubs.index;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
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;
public class JavaShortClassNameIndex extends StringStubIndexExtension<PsiClass> {
private static final JavaShortClassNameIndex ourInstance = new JavaShortClassNameIndex();
public static JavaShortClassNameIndex getInstance() {
return ourInstance;
}
@NotNull
@Override
public StubIndexKey<String, PsiClass> getKey() {
return JavaStubIndexKeys.CLASS_SHORT_NAMES;
}
@Override
public Collection<PsiClass> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
return super.get(s, project, new JavaSourceFilterScope(scope));
}
@Override
public boolean isKeyHighlySelective() {
return true;
}
}