|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.dic; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.hints.HintInfo; |
| 4 | +import com.intellij.codeInsight.hints.InlayInfo; |
| 5 | +import com.intellij.codeInsight.hints.InlayParameterHintsProvider; |
| 6 | +import com.intellij.openapi.util.Pair; |
| 7 | +import com.intellij.psi.PsiElement; |
| 8 | +import com.intellij.psi.xml.XmlAttribute; |
| 9 | +import com.intellij.psi.xml.XmlAttributeValue; |
| 10 | +import com.intellij.psi.xml.XmlTag; |
| 11 | +import com.intellij.psi.xml.XmlText; |
| 12 | +import com.jetbrains.php.lang.psi.elements.Function; |
| 13 | +import com.jetbrains.php.lang.psi.elements.Method; |
| 14 | +import com.jetbrains.php.lang.psi.elements.Parameter; |
| 15 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 16 | +import fr.adrienbrault.idea.symfony2plugin.config.xml.XmlServiceContainerAnnotator; |
| 17 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 18 | +import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil; |
| 19 | +import org.apache.commons.lang.StringUtils; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | +import org.jetbrains.annotations.Nullable; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 30 | + */ |
| 31 | +public class ServiceArgumentParameterHintsProvider implements InlayParameterHintsProvider { |
| 32 | + @NotNull |
| 33 | + @Override |
| 34 | + public List<InlayInfo> getParameterHints(PsiElement psiElement) { |
| 35 | + List<InlayInfo> inlays = new ArrayList<>(); |
| 36 | + |
| 37 | + Match typeHint = getTypeHint(psiElement); |
| 38 | + if(typeHint != null) { |
| 39 | + inlays.add(new InlayInfo(typeHint.parameter, typeHint.targetOffset)); |
| 40 | + } |
| 41 | + |
| 42 | + return inlays; |
| 43 | + } |
| 44 | + |
| 45 | + @Nullable |
| 46 | + @Override |
| 47 | + public HintInfo getHintInfo(PsiElement psiElement) { |
| 48 | + Match typeHint = getTypeHint(psiElement); |
| 49 | + if(typeHint == null) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + return new HintInfo.MethodInfo(typeHint.method != null ? typeHint.method.getFQN() : typeHint.parameter, Collections.emptyList()); |
| 54 | + } |
| 55 | + |
| 56 | + @NotNull |
| 57 | + @Override |
| 58 | + public Set<String> getDefaultBlackList() { |
| 59 | + return Collections.emptySet(); |
| 60 | + } |
| 61 | + |
| 62 | + @Nullable |
| 63 | + private Match getTypeHint(@NotNull PsiElement psiElement) { |
| 64 | + if(psiElement instanceof XmlAttributeValue) { |
| 65 | + // <service><argument type="service" id="a<caret>a"></service> |
| 66 | + PsiElement xmlAttribute = psiElement.getParent(); |
| 67 | + if(xmlAttribute instanceof XmlAttribute && "id".equalsIgnoreCase(((XmlAttribute) xmlAttribute).getName())) { |
| 68 | + PsiElement argumentTag = xmlAttribute.getParent(); |
| 69 | + |
| 70 | + if (argumentTag instanceof XmlTag) { |
| 71 | + if ("service".equalsIgnoreCase(((XmlTag) argumentTag).getName())) { |
| 72 | + // <service type="alias" id="a<caret>a"/> |
| 73 | + String alias = ((XmlTag) argumentTag).getAttributeValue("alias"); |
| 74 | + if (alias != null && StringUtils.isNotBlank(alias)) { |
| 75 | + PhpClass serviceClass = ServiceUtil.getServiceClass(psiElement.getProject(), alias); |
| 76 | + if (serviceClass != null) { |
| 77 | + PsiElement firstChild = psiElement.getFirstChild(); |
| 78 | + if (firstChild != null) { |
| 79 | + return new Match(serviceClass.getName(), null, firstChild.getTextRange().getEndOffset()); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } else if ("argument".equalsIgnoreCase(((XmlTag) argumentTag).getName())) { |
| 84 | + // <service><argument type="service" id="a<caret>a"></service> |
| 85 | + PsiElement serviceTag = argumentTag.getParent(); |
| 86 | + if (serviceTag instanceof XmlTag && "service".equals(((XmlTag) serviceTag).getName())) { |
| 87 | + Pair<String, Method> parameterHint = findMethodParameterHint((XmlTag) argumentTag); |
| 88 | + if (parameterHint != null) { |
| 89 | + PsiElement firstChild = psiElement.getFirstChild(); |
| 90 | + if (firstChild != null) { |
| 91 | + return new Match(parameterHint.getFirst(), parameterHint.getSecond(), firstChild.getTextRange().getEndOffset()); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } else if(psiElement instanceof XmlText) { |
| 99 | + // <service><argument>%a<caret>a%</argument></service> |
| 100 | + PsiElement argumentTag = psiElement.getParent(); |
| 101 | + |
| 102 | + // match only: <argument>%a<caret>a%</argument> |
| 103 | + // ignore: <argument>\n<caret><argument></argument></argument> |
| 104 | + if(argumentTag instanceof XmlTag && "argument".equals(((XmlTag) argumentTag).getName()) && ((XmlTag) argumentTag).getSubTags().length == 0) { |
| 105 | + PsiElement serviceTag = argumentTag.getParent(); |
| 106 | + if(serviceTag instanceof XmlTag && "service".equals(((XmlTag) serviceTag).getName())) { |
| 107 | + Pair<String, Method> parameterHint = findMethodParameterHint((XmlTag) argumentTag); |
| 108 | + if(parameterHint != null) { |
| 109 | + PsiElement firstChild = psiElement.getFirstChild(); |
| 110 | + if(firstChild != null) { |
| 111 | + return new Match(parameterHint.getFirst(), parameterHint.getSecond(), firstChild.getTextRange().getStartOffset()); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + @Nullable |
| 122 | + private Pair<String, Method> findMethodParameterHint(@NotNull XmlTag argumentTag) { |
| 123 | + PsiElement serviceTag = argumentTag.getParent(); |
| 124 | + if("service".equalsIgnoreCase(((XmlTag) serviceTag).getName())) { |
| 125 | + String aClass = ((XmlTag) serviceTag).getAttributeValue("class"); |
| 126 | + if(StringUtils.isNotBlank(aClass)) { |
| 127 | + PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(serviceTag.getProject(), aClass); |
| 128 | + if(phpClass != null) { |
| 129 | + Method constructor = phpClass.getConstructor(); |
| 130 | + if(constructor != null) { |
| 131 | + int argumentIndex = XmlServiceContainerAnnotator.getArgumentIndex(argumentTag); |
| 132 | + |
| 133 | + String s = attachMethodInstances(constructor, argumentIndex); |
| 134 | + if(s == null) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + return Pair.create(s, constructor); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + @Nullable |
| 148 | + private String attachMethodInstances(@NotNull Function function, int parameterIndex) { |
| 149 | + Parameter[] constructorParameter = function.getParameters(); |
| 150 | + if(parameterIndex >= constructorParameter.length) { |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + Parameter parameter = constructorParameter[parameterIndex]; |
| 155 | + |
| 156 | + String className = parameter.getDeclaredType().toString(); |
| 157 | + PhpClass expectedClass = PhpElementsUtil.getClassInterface(function.getProject(), className); |
| 158 | + if(expectedClass != null) { |
| 159 | + return expectedClass.getName(); |
| 160 | + } |
| 161 | + |
| 162 | + return parameter.getName(); |
| 163 | + } |
| 164 | + |
| 165 | + private static class Match { |
| 166 | + @NotNull |
| 167 | + private final String parameter; |
| 168 | + |
| 169 | + @Nullable |
| 170 | + private final Method method; |
| 171 | + |
| 172 | + private final int targetOffset; |
| 173 | + |
| 174 | + Match(@NotNull String parameter, @Nullable Method method, int targetOffset) { |
| 175 | + this.parameter = parameter; |
| 176 | + this.method = method; |
| 177 | + this.targetOffset = targetOffset; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments