use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class GrClassImplUtil method getAllMethodsAndTheirSubstitutors.
@NotNull
public static List<Pair<PsiMethod, PsiSubstitutor>> getAllMethodsAndTheirSubstitutors(GrTypeDefinition grType) {
final Map<String, List<CandidateInfo>> allMethodsMap = CollectClassMembersUtil.getAllMethods(grType, true);
List<Pair<PsiMethod, PsiSubstitutor>> result = new ArrayList<>();
for (List<CandidateInfo> infos : allMethodsMap.values()) {
for (CandidateInfo info : infos) {
result.add(Pair.create((PsiMethod) info.getElement(), info.getSubstitutor()));
}
}
return result;
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class GrClassImplUtil method findMethodsAndTheirSubstitutorsByName.
@NotNull
public static List<Pair<PsiMethod, PsiSubstitutor>> findMethodsAndTheirSubstitutorsByName(GrTypeDefinition grType, String name, boolean checkBases) {
final ArrayList<Pair<PsiMethod, PsiSubstitutor>> result = new ArrayList<>();
if (!checkBases) {
final PsiMethod[] methods = grType.findMethodsByName(name, false);
for (PsiMethod method : methods) {
result.add(Pair.create(method, PsiSubstitutor.EMPTY));
}
} else {
final Map<String, List<CandidateInfo>> map = CollectClassMembersUtil.getAllMethods(grType, true);
final List<CandidateInfo> candidateInfos = map.get(name);
if (candidateInfos != null) {
for (CandidateInfo info : candidateInfos) {
final PsiElement element = info.getElement();
result.add(Pair.create((PsiMethod) element, info.getSubstitutor()));
}
}
}
return result;
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class ReferenceExpressionCompletionContributor method completeFinalReference.
static Set<LookupElement> completeFinalReference(final PsiElement element, PsiJavaCodeReferenceElement reference, ElementFilter filter, final JavaSmartCompletionParameters parameters) {
final Set<PsiField> used = parameters.getParameters().getInvocationCount() < 2 ? findConstantsUsedInSwitch(element) : Collections.<PsiField>emptySet();
final Set<LookupElement> elements = JavaSmartCompletionContributor.completeReference(element, reference, new AndFilter(filter, new ElementFilter() {
@Override
public boolean isAcceptable(Object o, PsiElement context) {
if (o instanceof CandidateInfo) {
final CandidateInfo info = (CandidateInfo) o;
final PsiElement member = info.getElement();
final PsiType expectedType = parameters.getExpectedType();
if (expectedType.equals(PsiType.VOID)) {
return member instanceof PsiMethod;
}
//noinspection SuspiciousMethodCalls
if (member instanceof PsiEnumConstant && used.contains(CompletionUtil.getOriginalOrSelf(member))) {
return false;
}
return AssignableFromFilter.isAcceptable(member, element, expectedType, info.getSubstitutor());
}
return false;
}
@Override
public boolean isClassAcceptable(Class hintClass) {
return true;
}
}), false, true, parameters.getParameters(), PrefixMatcher.ALWAYS_TRUE);
for (LookupElement lookupElement : elements) {
if (lookupElement.getObject() instanceof PsiMethod) {
final JavaMethodCallElement item = lookupElement.as(JavaMethodCallElement.CLASS_CONDITION_KEY);
if (item != null) {
final PsiMethod method = (PsiMethod) lookupElement.getObject();
if (SmartCompletionDecorator.hasUnboundTypeParams(method, parameters.getExpectedType())) {
item.setInferenceSubstitutor(SmartCompletionDecorator.calculateMethodReturnTypeSubstitutor(method, parameters.getExpectedType()), element);
}
}
}
}
return elements;
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class PsiDocParamRef method getReference.
@Override
public PsiReference getReference() {
final PsiDocComment comment = PsiTreeUtil.getParentOfType(this, PsiDocComment.class);
if (comment == null)
return null;
final PsiJavaDocumentedElement owner = comment.getOwner();
if (!(owner instanceof PsiMethod) && !(owner instanceof PsiClass))
return null;
final ASTNode valueToken = findChildByType(JavaDocTokenType.DOC_TAG_VALUE_TOKEN);
if (valueToken == null)
return null;
final String name = valueToken.getText();
PsiElement reference = null;
final PsiElement firstChild = getFirstChild();
if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
final PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
for (PsiTypeParameter typeParameter : typeParameters) {
if (typeParameter.getName().equals(name)) {
reference = typeParameter;
}
}
} else if (owner instanceof PsiMethod) {
final PsiParameter[] parameters = ((PsiMethod) owner).getParameterList().getParameters();
for (PsiParameter parameter : parameters) {
if (parameter.getName().equals(name)) {
reference = parameter;
}
}
}
final PsiElement resultReference = reference;
return new PsiJavaReference() {
@Override
public PsiElement resolve() {
return resultReference;
}
@Override
@NotNull
public String getCanonicalText() {
return valueToken.getText();
}
@Override
public PsiElement handleElementRename(String newElementName) {
final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(getNode());
LeafElement newElement = Factory.createSingleLeafElement(JavaDocTokenType.DOC_TAG_VALUE_TOKEN, newElementName, charTableByTree, getManager());
replaceChild(valueToken, newElement);
return PsiDocParamRef.this;
}
@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
if (isReferenceTo(element))
return PsiDocParamRef.this;
if (!(element instanceof PsiParameter)) {
throw new IncorrectOperationException("Unsupported operation");
}
return handleElementRename(((PsiParameter) element).getName());
}
@Override
public boolean isReferenceTo(PsiElement element) {
if (!(element instanceof PsiNamedElement))
return false;
PsiNamedElement namedElement = (PsiNamedElement) element;
if (!getCanonicalText().equals(namedElement.getName()))
return false;
return getManager().areElementsEquivalent(resolve(), element);
}
@Override
@NotNull
public PsiElement[] getVariants() {
final PsiElement firstChild = getFirstChild();
Set<String> usedNames = new HashSet<>();
for (PsiDocTag tag : comment.getTags()) {
if (tag.getName().equals("param")) {
PsiDocTagValue valueElement = tag.getValueElement();
if (valueElement != null) {
usedNames.add(valueElement.getText());
}
}
}
PsiNamedElement[] result = PsiNamedElement.EMPTY_ARRAY;
if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
result = ((PsiTypeParameterListOwner) owner).getTypeParameters();
} else if (owner instanceof PsiMethod) {
result = ((PsiMethod) owner).getParameterList().getParameters();
}
List<PsiElement> filtered = new ArrayList<>();
for (PsiNamedElement namedElement : result) {
if (!usedNames.contains(namedElement.getName())) {
filtered.add(namedElement);
}
}
return filtered.toArray(new PsiElement[filtered.size()]);
}
@Override
public boolean isSoft() {
return false;
}
@Override
public TextRange getRangeInElement() {
final int startOffsetInParent = valueToken.getPsi().getStartOffsetInParent();
return new TextRange(startOffsetInParent, startOffsetInParent + valueToken.getTextLength());
}
@Override
public PsiElement getElement() {
return PsiDocParamRef.this;
}
@Override
public void processVariants(@NotNull PsiScopeProcessor processor) {
for (final PsiElement element : getVariants()) {
if (!processor.execute(element, ResolveState.initial())) {
return;
}
}
}
@Override
@NotNull
public JavaResolveResult advancedResolve(boolean incompleteCode) {
return resultReference == null ? JavaResolveResult.EMPTY : new CandidateInfo(resultReference, PsiSubstitutor.EMPTY);
}
@Override
@NotNull
public JavaResolveResult[] multiResolve(boolean incompleteCode) {
return resultReference == null ? JavaResolveResult.EMPTY_ARRAY : new JavaResolveResult[] { new CandidateInfo(resultReference, PsiSubstitutor.EMPTY) };
}
};
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class MethodCallFixer method getUnambiguousParameterCount.
@Nullable
private static Integer getUnambiguousParameterCount(PsiCallExpression call) {
int argCount = -1;
for (CandidateInfo candidate : PsiResolveHelper.SERVICE.getInstance(call.getProject()).getReferencedMethodCandidates(call, false)) {
PsiElement element = candidate.getElement();
if (!(element instanceof PsiMethod))
return null;
if (((PsiMethod) element).isVarArgs())
return null;
int count = ((PsiMethod) element).getParameterList().getParametersCount();
if (argCount == -1) {
argCount = count;
} else if (argCount != count) {
return null;
}
}
return argCount;
}
Aggregations