use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ResolveUtil method filterSameSignatureCandidates.
/**
* The point is that we do not want to see repeating methods in completion.
* Candidates can have multiple toString() methods (e.g. from Object and from some inheritor) and we want to show only one.
*/
public static GroovyResolveResult[] filterSameSignatureCandidates(Collection<? extends GroovyResolveResult> candidates) {
if (candidates.size() == 0)
return GroovyResolveResult.EMPTY_ARRAY;
if (candidates.size() == 1)
return candidates.toArray(new GroovyResolveResult[candidates.size()]);
final List<GroovyResolveResult> result = new ArrayList<>();
final Iterator<? extends GroovyResolveResult> allIterator = candidates.iterator();
result.add(allIterator.next());
Outer: while (allIterator.hasNext()) {
final GroovyResolveResult currentResult = allIterator.next();
final PsiMethod currentMethod;
final PsiSubstitutor currentSubstitutor;
if (currentResult instanceof GroovyMethodResult) {
final GroovyMethodResult currentMethodResult = (GroovyMethodResult) currentResult;
currentMethod = currentMethodResult.getElement();
currentSubstitutor = currentMethodResult.getSubstitutor(false);
} else if (currentResult.getElement() instanceof PsiMethod) {
currentMethod = (PsiMethod) currentResult.getElement();
currentSubstitutor = currentResult.getSubstitutor();
} else {
result.add(currentResult);
continue;
}
Inner: for (Iterator<GroovyResolveResult> resultIterator = result.iterator(); resultIterator.hasNext(); ) {
final GroovyResolveResult otherResult = resultIterator.next();
final PsiMethod otherMethod;
final PsiSubstitutor otherSubstitutor;
if (otherResult instanceof GroovyMethodResult) {
final GroovyMethodResult otherMethodResult = (GroovyMethodResult) otherResult;
otherMethod = otherMethodResult.getElement();
otherSubstitutor = otherMethodResult.getSubstitutor(false);
} else if (otherResult.getElement() instanceof PsiMethod) {
otherMethod = (PsiMethod) otherResult.getElement();
otherSubstitutor = otherResult.getSubstitutor();
} else {
continue Inner;
}
if (dominated(currentMethod, currentSubstitutor, otherMethod, otherSubstitutor)) {
// then do not add current method to result and skip rest other methods
continue Outer;
} else if (dominated(otherMethod, otherSubstitutor, currentMethod, currentSubstitutor)) {
// if other method is dominated by current method
// then remove other from result
resultIterator.remove();
}
}
result.add(currentResult);
}
return result.toArray(new GroovyResolveResult[result.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ResolveUtil method resolveExistingElement.
@Nullable
public static <T> T resolveExistingElement(PsiElement place, ResolverProcessor processor, Class<? extends T>... classes) {
treeWalkUp(place, processor, true);
final GroovyResolveResult[] candidates = processor.getCandidates();
for (GroovyResolveResult candidate : candidates) {
final PsiElement element = candidate.getElement();
if (element == place)
continue;
for (Class<? extends T> clazz : classes) {
if (//noinspection unchecked
clazz.isInstance(element))
return (T) element;
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ResolveUtil method getMethodCandidates.
@NotNull
public static GroovyResolveResult[] getMethodCandidates(@NotNull PsiType thisType, @Nullable String methodName, @NotNull PsiElement place, boolean resolveClosures, boolean allVariants, @Nullable PsiType... argumentTypes) {
if (methodName == null)
return GroovyResolveResult.EMPTY_ARRAY;
thisType = TypesUtil.boxPrimitiveType(thisType, place.getManager(), place.getResolveScope());
MethodResolverProcessor processor = new MethodResolverProcessor(methodName, place, false, thisType, argumentTypes, PsiType.EMPTY_ARRAY, allVariants);
final ResolveState state = ResolveState.initial().put(ClassHint.RESOLVE_CONTEXT, place);
processAllDeclarations(thisType, processor, state, place);
boolean hasApplicableMethods = processor.hasApplicableCandidates();
final GroovyResolveResult[] methodCandidates = processor.getCandidates();
if (hasApplicableMethods && methodCandidates.length == 1)
return methodCandidates;
final GroovyResolveResult[] allPropertyCandidates;
if (resolveClosures) {
PropertyResolverProcessor propertyResolver = new PropertyResolverProcessor(methodName, place);
processAllDeclarations(thisType, propertyResolver, state, place);
allPropertyCandidates = propertyResolver.getCandidates();
} else {
allPropertyCandidates = GroovyResolveResult.EMPTY_ARRAY;
}
List<GroovyResolveResult> propertyCandidates = new ArrayList<>(allPropertyCandidates.length);
for (GroovyResolveResult candidate : allPropertyCandidates) {
final PsiElement resolved = candidate.getElement();
if (!(resolved instanceof GrField))
continue;
final PsiType type = ((GrField) resolved).getTypeGroovy();
if (isApplicableClosureType(type, argumentTypes, place)) {
propertyCandidates.add(candidate);
}
}
for (GroovyResolveResult candidate : propertyCandidates) {
final PsiElement element = candidate.getElement();
if (element instanceof GrField) {
final PsiClass containingClass = ((PsiField) element).getContainingClass();
if (containingClass != null && PsiTreeUtil.isContextAncestor(containingClass, place, true)) {
return new GroovyResolveResult[] { candidate };
}
}
}
List<GroovyResolveResult> allCandidates = new ArrayList<>();
if (hasApplicableMethods) {
ContainerUtil.addAll(allCandidates, methodCandidates);
}
ContainerUtil.addAll(allCandidates, propertyCandidates);
//search for getters
for (String getterName : GroovyPropertyUtils.suggestGettersName(methodName)) {
AccessorResolverProcessor getterResolver = new AccessorResolverProcessor(getterName, methodName, place, true, thisType, PsiType.EMPTY_ARRAY);
processAllDeclarations(thisType, getterResolver, state, place);
//can be only one candidate
final GroovyResolveResult[] candidates = getterResolver.getCandidates();
final List<GroovyResolveResult> applicable = new ArrayList<>();
for (GroovyResolveResult candidate : candidates) {
PsiMethod method = (PsiMethod) candidate.getElement();
assert method != null;
final PsiType type = org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.getSmartReturnType(method);
if (isApplicableClosureType(type, argumentTypes, place)) {
applicable.add(candidate);
}
}
if (applicable.size() == 1) {
return applicable.toArray(new GroovyResolveResult[applicable.size()]);
}
ContainerUtil.addAll(allCandidates, applicable);
}
if (!allCandidates.isEmpty()) {
return allCandidates.toArray(new GroovyResolveResult[allCandidates.size()]);
} else if (!hasApplicableMethods) {
return methodCandidates;
}
return GroovyResolveResult.EMPTY_ARRAY;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ResolveUtil method getCallVariants.
public static GroovyResolveResult[] getCallVariants(GroovyPsiElement place) {
final PsiElement parent = place.getParent();
GroovyResolveResult[] variants = GroovyResolveResult.EMPTY_ARRAY;
if (parent instanceof GrCallExpression) {
variants = ((GrCallExpression) parent).getCallVariants(place instanceof GrExpression ? (GrExpression) place : null);
} else if (parent instanceof GrConstructorInvocation) {
final PsiClass clazz = ((GrConstructorInvocation) parent).getDelegatedClass();
if (clazz != null) {
final PsiMethod[] constructors = clazz.getConstructors();
variants = getConstructorResolveResult(constructors, place);
}
} else if (parent instanceof GrAnonymousClassDefinition) {
final PsiElement element = ((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy().resolve();
if (element instanceof PsiClass) {
final PsiMethod[] constructors = ((PsiClass) element).getConstructors();
variants = getConstructorResolveResult(constructors, place);
}
} else if (place instanceof GrReferenceExpression) {
variants = ((GrReferenceExpression) place).getSameNameVariants();
}
return variants;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ResolveUtil method getConstructorResolveResult.
private static GroovyResolveResult[] getConstructorResolveResult(PsiMethod[] constructors, PsiElement place) {
GroovyResolveResult[] variants = new GroovyResolveResult[constructors.length];
for (int i = 0; i < constructors.length; i++) {
final boolean isAccessible = PsiUtil.isAccessible(constructors[i], place, null);
variants[i] = new GroovyResolveResultImpl(constructors[i], isAccessible);
}
return variants;
}
Aggregations