use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class CreateFromUsageUtils method guessExpectedTypes.
@NotNull
static ExpectedTypeInfo[] guessExpectedTypes(@NotNull PsiExpression expression, boolean allowVoidType) {
PsiManager manager = expression.getManager();
GlobalSearchScope resolveScope = expression.getResolveScope();
List<ExpectedTypeInfo[]> typesList = new ArrayList<>();
List<String> expectedMethodNames = new ArrayList<>();
List<String> expectedFieldNames = new ArrayList<>();
getExpectedInformation(expression, typesList, expectedMethodNames, expectedFieldNames);
if (typesList.size() == 1 && (!expectedFieldNames.isEmpty() || !expectedMethodNames.isEmpty())) {
ExpectedTypeInfo[] infos = typesList.get(0);
if (infos.length == 1 && infos[0].getKind() == ExpectedTypeInfo.TYPE_OR_SUBTYPE && infos[0].getType().equals(PsiType.getJavaLangObject(manager, resolveScope))) {
typesList.clear();
}
}
if (typesList.isEmpty()) {
final JavaPsiFacade facade = JavaPsiFacade.getInstance(expression.getProject());
final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(expression.getProject());
PsiElementFactory factory = facade.getElementFactory();
for (String fieldName : expectedFieldNames) {
PsiField[] fields = cache.getFieldsByNameIfNotMoreThan(fieldName, resolveScope, MAX_RAW_GUESSED_MEMBERS_COUNT);
addMemberInfo(fields, expression, typesList, factory);
}
for (String methodName : expectedMethodNames) {
PsiMethod[] projectMethods = cache.getMethodsByNameIfNotMoreThan(methodName, resolveScope.intersectWith(GlobalSearchScope.projectScope(manager.getProject())), MAX_RAW_GUESSED_MEMBERS_COUNT);
PsiMethod[] libraryMethods = cache.getMethodsByNameIfNotMoreThan(methodName, resolveScope.intersectWith(GlobalSearchScope.notScope(GlobalSearchScope.projectScope(manager.getProject()))), MAX_RAW_GUESSED_MEMBERS_COUNT);
PsiMethod[] methods = ArrayUtil.mergeArrays(projectMethods, libraryMethods);
addMemberInfo(methods, expression, typesList, factory);
}
}
ExpectedTypeInfo[] expectedTypes = ExpectedTypeUtil.intersect(typesList);
if (expectedTypes.length == 0 && !typesList.isEmpty()) {
List<ExpectedTypeInfo> union = new ArrayList<>();
for (ExpectedTypeInfo[] aTypesList : typesList) {
ContainerUtil.addAll(union, (ExpectedTypeInfo[]) aTypesList);
}
expectedTypes = union.toArray(new ExpectedTypeInfo[union.size()]);
}
if (expectedTypes.length == 0) {
PsiType t = allowVoidType ? PsiType.VOID : PsiType.getJavaLangObject(manager, resolveScope);
expectedTypes = new ExpectedTypeInfo[] { ExpectedTypesProvider.createInfo(t, ExpectedTypeInfo.TYPE_OR_SUBTYPE, t, TailType.NONE) };
}
return expectedTypes;
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class DefaultSymbolNavigationContributor method getNames.
@Override
@NotNull
public String[] getNames(Project project, boolean includeNonProjectItems) {
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
HashSet<String> set = new HashSet<>();
cache.getAllMethodNames(set);
cache.getAllFieldNames(set);
cache.getAllClassNames(set);
return ArrayUtil.toStringArray(set);
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class DefaultSymbolNavigationContributor method processElementsWithName.
@Override
public void processElementsWithName(@NotNull String name, @NotNull final Processor<NavigationItem> processor, @NotNull final FindSymbolParameters parameters) {
GlobalSearchScope scope = parameters.getSearchScope();
IdFilter filter = parameters.getIdFilter();
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(scope.getProject());
String completePattern = parameters.getCompletePattern();
final Condition<PsiMember> qualifiedMatcher = getQualifiedNameMatcher(completePattern);
//noinspection UnusedDeclaration
final Set<PsiMethod> collectedMethods = new THashSet<>();
boolean success = cache.processFieldsWithName(name, field -> {
if (isOpenable(field) && qualifiedMatcher.value(field))
return processor.process(field);
return true;
}, scope, filter) && cache.processClassesWithName(name, aClass -> {
if (isOpenable(aClass) && qualifiedMatcher.value(aClass))
return processor.process(aClass);
return true;
}, scope, filter) && cache.processMethodsWithName(name, method -> {
if (!method.isConstructor() && isOpenable(method) && qualifiedMatcher.value(method)) {
collectedMethods.add(method);
}
return true;
}, scope, filter);
if (success) {
// hashSuperMethod accesses index and can not be invoked without risk of the deadlock in processMethodsWithName
Iterator<PsiMethod> iterator = collectedMethods.iterator();
while (iterator.hasNext()) {
PsiMethod method = iterator.next();
if (!hasSuperMethod(method, scope, qualifiedMatcher) && !processor.process(method))
return;
ProgressManager.checkCanceled();
iterator.remove();
}
}
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class DefaultSymbolNavigationContributor method processNames.
public void processNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter) {
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(scope.getProject());
cache.processAllClassNames(processor, scope, filter);
cache.processAllFieldNames(processor, scope, filter);
cache.processAllMethodNames(processor, scope, filter);
}
use of com.intellij.psi.search.PsiShortNamesCache in project intellij-community by JetBrains.
the class DefaultSymbolNavigationContributor method getItemsByName.
@Override
@NotNull
public NavigationItem[] getItemsByName(String name, final String pattern, Project project, boolean includeNonProjectItems) {
GlobalSearchScope scope = includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project);
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
Condition<PsiMember> qualifiedMatcher = getQualifiedNameMatcher(pattern);
List<PsiMember> result = new ArrayList<>();
for (PsiMethod method : cache.getMethodsByName(name, scope)) {
if (!method.isConstructor() && isOpenable(method) && !hasSuperMethod(method, scope, qualifiedMatcher)) {
result.add(method);
}
}
for (PsiField field : cache.getFieldsByName(name, scope)) {
if (isOpenable(field)) {
result.add(field);
}
}
for (PsiClass aClass : cache.getClassesByName(name, scope)) {
if (isOpenable(aClass)) {
result.add(aClass);
}
}
PsiMember[] array = result.toArray(new PsiMember[result.size()]);
Arrays.sort(array, MyComparator.INSTANCE);
return array;
}
Aggregations