11package fr .adrienbrault .idea .symfony2plugin .config .php ;
22
3+ import com .intellij .codeInsight .lookup .LookupElement ;
34import com .intellij .patterns .PlatformPatterns ;
45import com .intellij .psi .PsiElement ;
56import com .jetbrains .php .lang .PhpLanguage ;
67import com .jetbrains .php .lang .psi .elements .StringLiteralExpression ;
78import fr .adrienbrault .idea .symfony2plugin .codeInsight .GotoCompletionProvider ;
89import fr .adrienbrault .idea .symfony2plugin .codeInsight .GotoCompletionRegistrar ;
910import fr .adrienbrault .idea .symfony2plugin .codeInsight .GotoCompletionRegistrarParameter ;
11+ import fr .adrienbrault .idea .symfony2plugin .codeInsight .utils .GotoCompletionUtil ;
1012import fr .adrienbrault .idea .symfony2plugin .completion .DecoratedServiceCompletionProvider ;
13+ import fr .adrienbrault .idea .symfony2plugin .config .component .parser .ParameterLookupPercentElement ;
14+ import fr .adrienbrault .idea .symfony2plugin .dic .ContainerParameter ;
15+ import fr .adrienbrault .idea .symfony2plugin .stubs .ContainerCollectionResolver ;
16+ import fr .adrienbrault .idea .symfony2plugin .util .dict .ServiceUtil ;
17+ import fr .adrienbrault .idea .symfony2plugin .util .yaml .YamlHelper ;
1118import org .jetbrains .annotations .NotNull ;
1219import org .jetbrains .annotations .Nullable ;
1320
21+ import java .util .ArrayList ;
22+ import java .util .Collection ;
23+ import java .util .Collections ;
24+ import java .util .List ;
25+
1426/**
15- * Registers service-id completion for PHP array `decorates` and `parent` values, eg:
16- * `'decorates' => 'mailer'`
17- * `'parent' => 'app.abstract_mailer'`
27+ * Registers completion/navigation for PHP array service config values.
1828 */
1929public class PhpArrayGotoCompletionRegistrar implements GotoCompletionRegistrar {
2030 @ Override
@@ -34,6 +44,27 @@ public void register(@NotNull GotoCompletionRegistrarParameter registrar) {
3444 return new DecoratesParentContributor (stringLiteralExpression );
3545 }
3646 );
47+
48+ registrar .register (
49+ PlatformPatterns .psiElement ().withParent (StringLiteralExpression .class ).withLanguage (PhpLanguage .INSTANCE ),
50+ psiElement -> {
51+ if (!(psiElement .getParent () instanceof StringLiteralExpression stringLiteralExpression )) {
52+ return null ;
53+ }
54+
55+ PhpArrayServiceUtil .ServiceConfigPath keyPath = PhpArrayServiceUtil .getKeyPath (stringLiteralExpression );
56+ if (keyPath == null || !keyPath .isArgument ()) {
57+ return null ;
58+ }
59+
60+ String contents = stringLiteralExpression .getContents ();
61+ if (!contents .isBlank () && !contents .startsWith ("%" )) {
62+ return null ;
63+ }
64+
65+ return new ParameterArgumentContributor (stringLiteralExpression );
66+ }
67+ );
3768 }
3869
3970 private static class DecoratesParentContributor extends DecoratedServiceCompletionProvider {
@@ -53,4 +84,33 @@ public String findIdForElement(@NotNull PsiElement psiElement) {
5384 return PhpArrayServiceUtil .getServiceId (psiElement );
5485 }
5586 }
87+
88+ private static class ParameterArgumentContributor extends GotoCompletionProvider {
89+ private ParameterArgumentContributor (@ NotNull StringLiteralExpression element ) {
90+ super (element );
91+ }
92+
93+ @ NotNull
94+ @ Override
95+ public Collection <LookupElement > getLookupElements () {
96+ List <LookupElement > results = new ArrayList <>();
97+
98+ for (ContainerParameter containerParameter : ContainerCollectionResolver .getParameters (getProject ()).values ()) {
99+ results .add (new ParameterLookupPercentElement (containerParameter ));
100+ }
101+
102+ return results ;
103+ }
104+
105+ @ NotNull
106+ @ Override
107+ public Collection <PsiElement > getPsiTargets (PsiElement element ) {
108+ String parameterName = GotoCompletionUtil .getStringLiteralValue (element );
109+ if (parameterName == null || !YamlHelper .isValidParameterName (parameterName )) {
110+ return Collections .emptyList ();
111+ }
112+
113+ return ServiceUtil .getServiceClassTargets (element .getProject (), parameterName );
114+ }
115+ }
56116}
0 commit comments