Skip to content

Commit 0bf21b1

Browse files
committed
#1984 support parameters inside "Autowire" attribute
1 parent a91c959 commit 0bf21b1

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/dic/registrar/DicGotoCompletionRegistrar.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import com.intellij.codeInsight.lookup.LookupElement;
44
import com.intellij.patterns.PlatformPatterns;
55
import com.intellij.psi.PsiElement;
6+
import com.intellij.psi.util.PsiTreeUtil;
7+
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
8+
import com.jetbrains.php.lang.psi.elements.ParameterList;
9+
import com.jetbrains.php.lang.psi.elements.PhpAttribute;
610
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
711
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider;
812
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrar;
@@ -26,6 +30,8 @@
2630
*/
2731
public class DicGotoCompletionRegistrar implements GotoCompletionRegistrar {
2832

33+
public static final String AUTOWIRE_ATTRIBUTE_CLASS = "\\Symfony\\Component\\DependencyInjection\\Attribute\\Autowire";
34+
2935
@Override
3036
public void register(@NotNull GotoCompletionRegistrarParameter registrar) {
3137
// getParameter('FOO')
@@ -65,6 +71,40 @@ public void register(@NotNull GotoCompletionRegistrarParameter registrar) {
6571
return new ParameterContributor((StringLiteralExpression) context);
6672
}
6773
);
74+
75+
// #[Autowire('<caret>')]
76+
registrar.register(
77+
PlatformPatterns.psiElement(), psiElement -> {
78+
PsiElement context = psiElement.getContext();
79+
if (!(context instanceof StringLiteralExpression)) {
80+
return null;
81+
}
82+
83+
PsiElement prevSibling = context.getPrevSibling();
84+
if (prevSibling == null) {
85+
PsiElement parent = context.getParent();
86+
if (parent instanceof ParameterList) {
87+
PsiElement parent2 = parent.getParent();
88+
if (parent2 instanceof PhpAttribute) {
89+
String fqn = ((PhpAttribute) parent2).getFQN();
90+
if (fqn != null && PhpElementsUtil.isInstanceOf(psiElement.getProject(), fqn, AUTOWIRE_ATTRIBUTE_CLASS)) {
91+
return new ParameterContributor((StringLiteralExpression) context);
92+
}
93+
}
94+
}
95+
} else {
96+
PsiElement psiElement1 = PsiTreeUtil.prevVisibleLeaf(prevSibling);
97+
if (psiElement1 != null && psiElement1.getNode().getElementType() == PhpTokenTypes.opCOLON) {
98+
PsiElement psiElement2 = PsiTreeUtil.prevVisibleLeaf(psiElement1);
99+
if (psiElement2 != null && psiElement2.getNode().getElementType() == PhpTokenTypes.IDENTIFIER && "value".equals(psiElement2.getText())) {
100+
return new ParameterContributor((StringLiteralExpression) context);
101+
}
102+
}
103+
}
104+
105+
return null;
106+
}
107+
);
68108
}
69109

70110
private static class ParameterContributor extends GotoCompletionProvider {

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/registrar/DicGotoCompletionRegistrarTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,60 @@ public void testParameterContributorFor() {
6363
PlatformPatterns.psiElement()
6464
);
6565
}
66+
67+
public void testParameterContributorForDefaultAttribute() {
68+
assertCompletionContains(PhpFileType.INSTANCE, "<?php\n" +
69+
"use Symfony\\Component\\DependencyInjection\\Attribute\\Autowire;\n" +
70+
"\n" +
71+
"class MyService\n" +
72+
"{\n" +
73+
" public function __construct(\n" +
74+
" #[Autowire('<caret>')]\n" +
75+
" private $parameter2" +
76+
" ) {}\n" +
77+
"}",
78+
"foo"
79+
);
80+
81+
assertNavigationMatch(PhpFileType.INSTANCE, "<?php\n" +
82+
"use Symfony\\Component\\DependencyInjection\\Attribute\\Autowire;\n" +
83+
"\n" +
84+
"class MyService\n" +
85+
"{\n" +
86+
" public function __construct(\n" +
87+
" #[Autowire('fo<caret>o')]\n" +
88+
" private $parameter2" +
89+
" ) {}\n" +
90+
"}",
91+
PlatformPatterns.psiElement()
92+
);
93+
}
94+
95+
public void testParameterContributorForNamedAttribute() {
96+
assertCompletionContains(PhpFileType.INSTANCE, "<?php\n" +
97+
"use Symfony\\Component\\DependencyInjection\\Attribute\\Autowire;\n" +
98+
"\n" +
99+
"class MyService\n" +
100+
"{\n" +
101+
" public function __construct(\n" +
102+
" #[Autowire(value: '<caret>')]\n" +
103+
" private $parameter2" +
104+
" ) {}\n" +
105+
"}",
106+
"foo"
107+
);
108+
109+
assertNavigationMatch(PhpFileType.INSTANCE, "<?php\n" +
110+
"use Symfony\\Component\\DependencyInjection\\Attribute\\Autowire;\n" +
111+
"\n" +
112+
"class MyService\n" +
113+
"{\n" +
114+
" public function __construct(\n" +
115+
" #[Autowire(value: 'fo<caret>o')]\n" +
116+
" private $parameter2" +
117+
" ) {}\n" +
118+
"}",
119+
PlatformPatterns.psiElement()
120+
);
121+
}
66122
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/registrar/fixtures/classes.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ function hasParameter($parameter);
99
}
1010
}
1111

12+
namespace Symfony\Component\DependencyInjection\Attribute
13+
{
14+
class Autowire
15+
{
16+
/**
17+
* @param string|null $value Parameter value (ie "%kernel.project_dir%/some/path")
18+
* @param string|null $service Service ID (ie "some.service")
19+
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
20+
*/
21+
public function __construct(
22+
string $value = null,
23+
string $service = null,
24+
string $expression = null,
25+
) {}
26+
}
27+
}
28+
1229
namespace
1330
{
1431
class Foo

0 commit comments

Comments
 (0)