use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class GroovyOverrideImplementExploreUtil method getMapToOverrideImplement.
@NotNull
public static Map<MethodSignature, CandidateInfo> getMapToOverrideImplement(GrTypeDefinition aClass, boolean toImplement, boolean skipImplemented) {
Map<MethodSignature, PsiMethod> abstracts = ContainerUtil.newLinkedHashMap();
Map<MethodSignature, PsiMethod> finals = ContainerUtil.newLinkedHashMap();
Map<MethodSignature, PsiMethod> concretes = ContainerUtil.newLinkedHashMap();
PsiUtilCore.ensureValid(aClass);
Collection<HierarchicalMethodSignature> allMethodSignatures = aClass.getVisibleSignatures();
PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
for (HierarchicalMethodSignature signature : allMethodSignatures) {
PsiMethod method = signature.getMethod();
if (method instanceof GrTraitMethod) {
for (HierarchicalMethodSignature superSignature : signature.getSuperSignatures()) {
processMethod(aClass, skipImplemented, abstracts, finals, concretes, resolveHelper, superSignature, superSignature.getMethod());
}
} else {
processMethod(aClass, skipImplemented, abstracts, finals, concretes, resolveHelper, signature, method);
}
}
final Map<MethodSignature, CandidateInfo> result = new TreeMap<>(new OverrideImplementExploreUtil.MethodSignatureComparator());
if (toImplement || aClass.isInterface()) {
collectMethodsToImplement(aClass, abstracts, finals, concretes, result);
} else {
for (Map.Entry<MethodSignature, PsiMethod> entry : concretes.entrySet()) {
MethodSignature signature = entry.getKey();
PsiMethod concrete = entry.getValue();
if (finals.get(signature) == null) {
PsiMethod abstractOne = abstracts.get(signature);
if (abstractOne == null || !abstractOne.getContainingClass().isInheritor(concrete.getContainingClass(), true) || CommonClassNames.JAVA_LANG_OBJECT.equals(concrete.getContainingClass().getQualifiedName())) {
PsiSubstitutor subst = OverrideImplementExploreUtil.correctSubstitutor(concrete, signature.getSubstitutor());
CandidateInfo info = new CandidateInfo(concrete, subst);
result.put(signature, info);
}
}
}
}
return result;
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class ReplaceAbstractClassInstanceByMapIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement psiElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
PsiDocumentManager.getInstance(project).commitAllDocuments();
GrCodeReferenceElement ref = (GrCodeReferenceElement) psiElement;
final GrAnonymousClassDefinition anonymous = (GrAnonymousClassDefinition) ref.getParent();
final GrNewExpression newExpr = (GrNewExpression) anonymous.getParent();
final PsiElement resolved = ref.resolve();
// && ((PsiClass)resolved).isInterface();
assert resolved instanceof PsiClass;
GrTypeDefinitionBody body = anonymous.getBody();
assert body != null;
List<Pair<PsiMethod, GrOpenBlock>> methods = new ArrayList<>();
for (GrMethod method : body.getMethods()) {
methods.add(new Pair<>(method, method.getBlock()));
}
final PsiClass iface = (PsiClass) resolved;
final Collection<CandidateInfo> collection = OverrideImplementExploreUtil.getMethodsToOverrideImplement(anonymous, true);
for (CandidateInfo info : collection) {
methods.add(new Pair<>((PsiMethod) info.getElement(), null));
}
StringBuilder buffer = new StringBuilder();
if (methods.size() == 1) {
final Pair<PsiMethod, GrOpenBlock> pair = methods.get(0);
appendClosureTextByMethod(pair.getFirst(), buffer, pair.getSecond(), newExpr);
if (!GroovyConfigUtils.getInstance().isVersionAtLeast(psiElement, GroovyConfigUtils.GROOVY2_2)) {
buffer.append(" as ").append(iface.getQualifiedName());
}
} else {
buffer.append("[");
buffer.append("\n");
for (Pair<PsiMethod, GrOpenBlock> pair : methods) {
final PsiMethod method = pair.getFirst();
final GrOpenBlock block = pair.getSecond();
buffer.append(method.getName()).append(": ");
appendClosureTextByMethod(method, buffer, block, newExpr);
buffer.append(",\n");
}
if (!methods.isEmpty()) {
buffer.delete(buffer.length() - 2, buffer.length());
buffer.append('\n');
}
buffer.append("]");
buffer.append(" as ").append(iface.getQualifiedName());
}
createAndAdjustNewExpression(project, newExpr, buffer);
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class GrMethodOverrideCompletionProvider method addSuperMethods.
private static void addSuperMethods(final GrTypeDefinition psiClass, CompletionResultSet completionResultSet, boolean toImplement) {
final Collection<CandidateInfo> candidates = GroovyOverrideImplementExploreUtil.getMethodsToOverrideImplement(psiClass, toImplement);
for (CandidateInfo candidateInfo : candidates) {
final PsiMethod method = (PsiMethod) candidateInfo.getElement();
if (method.isConstructor())
continue;
RowIcon icon = new RowIcon(2);
icon.setIcon(method.getIcon(0), 0);
icon.setIcon(toImplement ? AllIcons.Gutter.ImplementingMethod : AllIcons.Gutter.OverridingMethod, 1);
PsiSubstitutor substitutor = candidateInfo.getSubstitutor();
String parameters = PsiFormatUtil.formatMethod(method, substitutor, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_NAME);
String visibility = VisibilityUtil.getVisibilityModifier(method.getModifierList());
String modifiers = (visibility == PsiModifier.PACKAGE_LOCAL ? "" : visibility + " ");
PsiType type = substitutor.substitute(method.getReturnType());
String parentClassName = psiClass == null ? "" : psiClass.getName();
String signature = modifiers + (type == null ? "" : type.getPresentableText() + " ") + method.getName();
LookupElementBuilder lookupElement = LookupElementBuilder.create(method, signature).appendTailText(parameters, false).appendTailText("{...}", true).withTypeText(parentClassName).withIcon(icon).withInsertHandler(new GroovyMethodOverrideHandler(psiClass));
completionResultSet.addElement(lookupElement);
}
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class JavaGenerateMemberCompletionContributor method addSuperSignatureElements.
private static void addSuperSignatureElements(final PsiClass parent, boolean implemented, CompletionResultSet result, Set<MethodSignature> addedSignatures) {
for (CandidateInfo candidate : OverrideImplementExploreUtil.getMethodsToOverrideImplement(parent, implemented)) {
PsiMethod baseMethod = (PsiMethod) candidate.getElement();
PsiClass baseClass = baseMethod.getContainingClass();
PsiSubstitutor substitutor = candidate.getSubstitutor();
if (!baseMethod.isConstructor() && baseClass != null && addedSignatures.add(baseMethod.getSignature(substitutor))) {
result.addElement(createOverridingLookupElement(implemented, baseMethod, baseClass, substitutor));
}
}
}
use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.
the class CanonicalPsiTypeConverterImpl method getReferences.
public PsiReference[] getReferences(@Nullable PsiType type, String typeText, int startOffsetInText, @NotNull final PsiElement element) {
final ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
assert manipulator != null;
String trimmed = typeText.trim();
int offset = manipulator.getRangeInElement(element).getStartOffset() + startOffsetInText + typeText.indexOf(trimmed);
if (trimmed.startsWith(ARRAY_PREFIX)) {
offset += ARRAY_PREFIX.length();
if (trimmed.endsWith(";")) {
trimmed = trimmed.substring(ARRAY_PREFIX.length(), trimmed.length() - 1);
} else {
trimmed = trimmed.substring(ARRAY_PREFIX.length());
}
}
if (type != null) {
type = type.getDeepComponentType();
}
final boolean isPrimitiveType = type instanceof PsiPrimitiveType;
return new JavaClassReferenceSet(trimmed, element, offset, false, CLASS_REFERENCE_PROVIDER) {
@Override
@NotNull
protected JavaClassReference createReference(int refIndex, @NotNull String subRefText, @NotNull TextRange textRange, boolean staticImport) {
return new JavaClassReference(this, textRange, refIndex, subRefText, staticImport) {
@Override
public boolean isSoft() {
return true;
}
@Override
@NotNull
public JavaResolveResult advancedResolve(final boolean incompleteCode) {
if (isPrimitiveType) {
return new CandidateInfo(element, PsiSubstitutor.EMPTY, false, false, element);
}
return super.advancedResolve(incompleteCode);
}
@Override
public void processVariants(@NotNull final PsiScopeProcessor processor) {
if (processor instanceof JavaCompletionProcessor) {
((JavaCompletionProcessor) processor).setCompletionElements(getVariants());
} else {
super.processVariants(processor);
}
}
@Override
@NotNull
public Object[] getVariants() {
final Object[] variants = super.getVariants();
if (myIndex == 0) {
return ArrayUtil.mergeArrays(variants, PRIMITIVES, ArrayUtil.OBJECT_ARRAY_FACTORY);
}
return variants;
}
};
}
}.getAllReferences();
}
Aggregations