|
| 1 | +package nl.jrdie.idea.springql.references; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.completion.PrioritizedLookupElement; |
| 4 | +import com.intellij.codeInsight.lookup.LookupElement; |
| 5 | +import com.intellij.codeInsight.lookup.LookupElementBuilder; |
| 6 | +import com.intellij.lang.jsgraphql.types.language.FieldDefinition; |
| 7 | +import com.intellij.lang.jsgraphql.types.language.ObjectTypeDefinition; |
| 8 | +import com.intellij.psi.PsiElement; |
| 9 | +import com.intellij.psi.PsiElementResolveResult; |
| 10 | +import com.intellij.psi.PsiPolyVariantReferenceBase; |
| 11 | +import com.intellij.psi.ResolveResult; |
| 12 | +import nl.jrdie.idea.springql.icons.QLIcons; |
| 13 | +import nl.jrdie.idea.springql.index.entry.SchemaMappingIndexEntry; |
| 14 | +import nl.jrdie.idea.springql.svc.QLIdeService; |
| 15 | +import nl.jrdie.idea.springql.utils.QLIdeUtil; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | +import org.jetbrains.uast.UAnnotation; |
| 19 | +import org.jetbrains.uast.UMethod; |
| 20 | +import org.jetbrains.uast.UastContextKt; |
| 21 | + |
| 22 | +import javax.swing.Icon; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +public class QLFieldPolyReference extends PsiPolyVariantReferenceBase<PsiElement> { |
| 28 | + |
| 29 | + public QLFieldPolyReference(@NotNull PsiElement psiElement) { |
| 30 | + super(Objects.requireNonNull(psiElement, "psiElement")); |
| 31 | + } |
| 32 | + |
| 33 | + @NotNull |
| 34 | + @Override |
| 35 | + public ResolveResult[] multiResolve(boolean incompleteCode) { |
| 36 | + final QLIdeService svc = myElement.getProject().getService(QLIdeService.class); |
| 37 | + if (!svc.isApplicableProject(myElement.getProject())) { |
| 38 | + return ResolveResult.EMPTY_ARRAY; |
| 39 | + } |
| 40 | + |
| 41 | + final UAnnotation parentAnnotation = UastContextKt.getUastParentOfType(myElement, UAnnotation.class); |
| 42 | + if (parentAnnotation == null) { |
| 43 | + return ResolveResult.EMPTY_ARRAY; |
| 44 | + } |
| 45 | + |
| 46 | + final Set<SchemaMappingIndexEntry> annotations = svc.getIndex().schemaMappingByAnnotation(parentAnnotation); |
| 47 | + |
| 48 | + return annotations |
| 49 | + .stream() |
| 50 | + .map(SchemaMappingIndexEntry::getSchemaPsi) |
| 51 | + .flatMap(List::stream) |
| 52 | + .map(PsiElementResolveResult::new) |
| 53 | + .toArray(ResolveResult[]::new); |
| 54 | + } |
| 55 | + |
| 56 | + @NotNull |
| 57 | + @Override |
| 58 | + public Object[] getVariants() { |
| 59 | + final QLIdeService svc = myElement.getProject().getService(QLIdeService.class); |
| 60 | + |
| 61 | + return svc |
| 62 | + .getSchemaRegistry() |
| 63 | + .getFieldDefinitions() |
| 64 | + .stream() |
| 65 | + .map(fieldDefinition -> createLookupElement(svc, fieldDefinition)) |
| 66 | + .filter(Objects::nonNull) |
| 67 | + .toArray(LookupElement[]::new); |
| 68 | + } |
| 69 | + |
| 70 | + @Nullable |
| 71 | + private LookupElement createLookupElement(@NotNull QLIdeService svc, |
| 72 | + @NotNull FieldDefinition fieldDefinition) { |
| 73 | + Objects.requireNonNull(svc, "svc"); |
| 74 | + Objects.requireNonNull(fieldDefinition, "fieldDefinition"); |
| 75 | + |
| 76 | + final PsiElement targetElement = fieldDefinition.getElement(); |
| 77 | + if (targetElement == null) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + final UAnnotation nearestAnnotation = svc.findNearestSchemaMappingAnnotations(UastContextKt.toUElement(myElement)) |
| 82 | + .stream() |
| 83 | + .findFirst() |
| 84 | + .orElse(null); |
| 85 | + |
| 86 | + ObjectTypeDefinition parentTypeByAnnotation = null; |
| 87 | + if (nearestAnnotation != null) { |
| 88 | + final String parentTypeName = svc.findApplicableParentTypeName(nearestAnnotation); |
| 89 | + if (parentTypeName == null) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + parentTypeByAnnotation = svc.getSchemaRegistry().getObjectTypeDefinition(parentTypeName); |
| 94 | + } |
| 95 | + |
| 96 | + final boolean isChildOfCurrentAnnotationParentType = parentTypeByAnnotation != null && |
| 97 | + parentTypeByAnnotation |
| 98 | + .getFieldDefinitions() |
| 99 | + .contains(fieldDefinition); |
| 100 | + |
| 101 | + String correctDisplayName = null; |
| 102 | + if (isChildOfCurrentAnnotationParentType) { |
| 103 | + correctDisplayName = fieldDefinition.getName(); |
| 104 | + } else { |
| 105 | + final ObjectTypeDefinition parentTypeDefinition = svc.getSchemaRegistry().getParentType(fieldDefinition); |
| 106 | + if (parentTypeDefinition != null) { |
| 107 | + correctDisplayName = parentTypeDefinition.getName() + "."; |
| 108 | + } |
| 109 | + correctDisplayName += fieldDefinition.getName(); |
| 110 | + } |
| 111 | + |
| 112 | + final String typeText = QLIdeUtil.printTypeVal(fieldDefinition.getType()); |
| 113 | + |
| 114 | + final Icon icon = getLookupIcon(svc, fieldDefinition); |
| 115 | + |
| 116 | + final LookupElement intermediateLookupElement = LookupElementBuilder |
| 117 | + .create(fieldDefinition.getName()) |
| 118 | + .withPresentableText(correctDisplayName) |
| 119 | + .withPsiElement(fieldDefinition.getElement()) |
| 120 | + .withIcon(icon) |
| 121 | + .withBoldness(isChildOfCurrentAnnotationParentType) |
| 122 | + .withStrikeoutness(fieldDefinition.hasDirective("deprecated")) |
| 123 | + .withTypeText(typeText); |
| 124 | + |
| 125 | + return PrioritizedLookupElement |
| 126 | + .withPriority(intermediateLookupElement, determinePriority(fieldDefinition, parentTypeByAnnotation, svc)); |
| 127 | + } |
| 128 | + |
| 129 | + private Icon getLookupIcon(@NotNull QLIdeService svc, |
| 130 | + @NotNull FieldDefinition fieldDefinition) { |
| 131 | + Objects.requireNonNull(svc, "svc"); |
| 132 | + Objects.requireNonNull(fieldDefinition, "fieldDefinition"); |
| 133 | + |
| 134 | + if (svc.isApolloFederationNode(fieldDefinition)) { |
| 135 | + return QLIcons.INSTANCE.getApollo(); |
| 136 | + } |
| 137 | + if (svc.isIntrospectionNode(fieldDefinition)) { |
| 138 | + return QLIcons.INSTANCE.getIntrospectionFieldType(); |
| 139 | + } |
| 140 | + return QLIcons.INSTANCE.getField(); |
| 141 | + } |
| 142 | + |
| 143 | + private double determinePriority(@NotNull FieldDefinition fieldDefinition, |
| 144 | + @Nullable ObjectTypeDefinition parentType, |
| 145 | + @NotNull QLIdeService svc) { |
| 146 | + Objects.requireNonNull(fieldDefinition, "fieldDefinition"); |
| 147 | + Objects.requireNonNull(svc, "svc"); |
| 148 | + |
| 149 | + if (svc.isIntrospectionNode(fieldDefinition)) { |
| 150 | + return 0.0; |
| 151 | + } |
| 152 | + |
| 153 | + if (fieldDefinition.hasDirective("deprecated")) { |
| 154 | + return 7.0; |
| 155 | + } |
| 156 | + |
| 157 | + UMethod nearestMethod = UastContextKt.getUastParentOfType(myElement, UMethod.class); |
| 158 | + if (nearestMethod != null) { |
| 159 | + if (fieldDefinition.getName().equalsIgnoreCase(nearestMethod.getName())) { |
| 160 | + return 10.0; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if (parentType != null && parentType.getFieldDefinitions().contains(fieldDefinition)) { |
| 165 | + return 8.0; |
| 166 | + } |
| 167 | + |
| 168 | + return 5.0; |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments