|
2 | 2 |
|
3 | 3 | import com.intellij.patterns.XmlPatterns;
|
4 | 4 | import com.intellij.psi.*;
|
5 |
| -import com.intellij.psi.xml.XmlAttributeValue; |
6 |
| -import com.intellij.psi.xml.XmlText; |
7 |
| -import com.intellij.psi.xml.XmlToken; |
| 5 | +import com.intellij.psi.xml.*; |
8 | 6 | import com.intellij.util.ProcessingContext;
|
9 | 7 | import com.intellij.util.containers.ContainerUtil;
|
10 | 8 | import com.jetbrains.php.lang.psi.elements.Method;
|
|
19 | 17 | import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
|
20 | 18 | import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
|
21 | 19 | import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
|
| 20 | +import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper; |
22 | 21 | import org.apache.commons.lang.StringUtils;
|
23 | 22 | import org.jetbrains.annotations.NotNull;
|
24 | 23 |
|
@@ -74,6 +73,13 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
|
74 | 73 | new ClassPsiReferenceProvider()
|
75 | 74 | );
|
76 | 75 |
|
| 76 | + // Symfoyn 3.3 shortcut |
| 77 | + // <service id="Class\Name"> |
| 78 | + registrar.registerReferenceProvider( |
| 79 | + XmlHelper.getAttributePattern("id"), |
| 80 | + new ClassAsIdPsiReferenceProvider() |
| 81 | + ); |
| 82 | + |
77 | 83 | // <parameter key="fos_user.user_manager.class">FOS\UserBundle\Doctrine\UserManager</parameter>
|
78 | 84 | registrar.registerReferenceProvider(
|
79 | 85 | XmlHelper.getParameterClassValuePattern(),
|
@@ -259,8 +265,38 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
|
259 | 265 | return new PsiReference[0];
|
260 | 266 | }
|
261 | 267 |
|
262 |
| - return new PsiReference[]{ new ServiceIdReference( |
263 |
| - (XmlAttributeValue) psiElement) |
| 268 | + return new PsiReference[] { |
| 269 | + new ServiceIdReference((XmlAttributeValue) psiElement) |
| 270 | + }; |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + /** |
| 275 | + * Shortcut for service tag without class attribute |
| 276 | + * |
| 277 | + * <service id="Foobar\Foobar"/> |
| 278 | + */ |
| 279 | + private static class ClassAsIdPsiReferenceProvider extends PsiReferenceProvider { |
| 280 | + @NotNull |
| 281 | + @Override |
| 282 | + public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) { |
| 283 | + if(!Symfony2ProjectComponent.isEnabled(psiElement) || !(psiElement instanceof XmlAttributeValue)) { |
| 284 | + return new PsiReference[0]; |
| 285 | + } |
| 286 | + |
| 287 | + PsiElement parent = psiElement.getParent(); |
| 288 | + if(!(parent instanceof XmlAttribute) && YamlHelper.isClassServiceId(parent.getText())) { |
| 289 | + return new PsiReference[0]; |
| 290 | + } |
| 291 | + |
| 292 | + // invalidate on class attribute |
| 293 | + PsiElement xmlTag = parent.getParent(); |
| 294 | + if(!(xmlTag instanceof XmlTag) || ((XmlTag) xmlTag).getAttribute("class") != null) { |
| 295 | + return new PsiReference[0]; |
| 296 | + } |
| 297 | + |
| 298 | + return new PsiReference[] { |
| 299 | + new ServiceIdWithoutParameterReference((XmlAttributeValue) psiElement) |
264 | 300 | };
|
265 | 301 | }
|
266 | 302 | }
|
@@ -355,6 +391,33 @@ public Object[] getVariants() {
|
355 | 391 |
|
356 | 392 | }
|
357 | 393 |
|
| 394 | + /** |
| 395 | + * <service id="Foobar\Foo"/> |
| 396 | + */ |
| 397 | + private static class ServiceIdWithoutParameterReference extends PsiPolyVariantReferenceBase<PsiElement> { |
| 398 | + |
| 399 | + @NotNull |
| 400 | + private final XmlAttributeValue psiElement; |
| 401 | + |
| 402 | + private ServiceIdWithoutParameterReference(@NotNull XmlAttributeValue psiElement) { |
| 403 | + super(psiElement); |
| 404 | + this.psiElement = psiElement; |
| 405 | + } |
| 406 | + |
| 407 | + @NotNull |
| 408 | + @Override |
| 409 | + public ResolveResult[] multiResolve(boolean b) { |
| 410 | + String value = this.psiElement.getValue(); |
| 411 | + return PsiElementResolveResult.createResults(ServiceUtil.getServiceClassTargets(getElement().getProject(), value)); |
| 412 | + } |
| 413 | + |
| 414 | + @NotNull |
| 415 | + @Override |
| 416 | + public Object[] getVariants() { |
| 417 | + return new Object[0]; |
| 418 | + } |
| 419 | + } |
| 420 | + |
358 | 421 | /**
|
359 | 422 | * <factory class="FooBar" method="cre<caret>ate"/>
|
360 | 423 | */
|
|
0 commit comments