Skip to content

Commit bff3a84

Browse files
committed
#2102 add completion for "When" attribute
1 parent 197a46e commit bff3a84

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

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

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package fr.adrienbrault.idea.symfony2plugin.dic.registrar;
22

33
import com.intellij.codeInsight.lookup.LookupElement;
4+
import com.intellij.codeInsight.lookup.LookupElementBuilder;
45
import com.intellij.patterns.PlatformPatterns;
56
import com.intellij.psi.PsiElement;
67
import com.intellij.psi.util.PsiTreeUtil;
78
import com.jetbrains.php.lang.psi.elements.PhpAttribute;
89
import com.jetbrains.php.lang.psi.elements.PhpClass;
910
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
11+
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
1012
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider;
1113
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrar;
1214
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrarParameter;
@@ -23,6 +25,8 @@
2325
import org.jetbrains.annotations.NotNull;
2426

2527
import java.util.*;
28+
import java.util.function.Function;
29+
import java.util.stream.Collectors;
2630

2731
/**
2832
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -120,26 +124,6 @@ public void register(@NotNull GotoCompletionRegistrarParameter registrar) {
120124
}
121125
);
122126

123-
// #[Autoconfigure(tags: ['app.some_tag'])]
124-
// #[Autoconfigure(['app.some_tag'])]
125-
registrar.register(
126-
PlatformPatterns.or(
127-
128-
), psiElement -> {
129-
PsiElement context = psiElement.getContext();
130-
if (!(context instanceof StringLiteralExpression)) {
131-
return null;
132-
}
133-
134-
PhpAttribute phpAttribute = PsiTreeUtil.getParentOfType(context, PhpAttribute.class);
135-
if (phpAttribute != null) {
136-
return new TaggedIteratorContributor((StringLiteralExpression) context);
137-
}
138-
139-
return null;
140-
}
141-
);
142-
143127
// #[Autowire(service: 'some_service')]
144128
// #[AsDecorator(decorates: 'some_service')]
145129
registrar.register(
@@ -162,6 +146,20 @@ public void register(@NotNull GotoCompletionRegistrarParameter registrar) {
162146
return null;
163147
}
164148
);
149+
150+
// #[When('dev')]
151+
registrar.register(
152+
PlatformPatterns.or(
153+
PhpElementsUtil.getFirstAttributeStringPattern("\\Symfony\\Component\\DependencyInjection\\Attribute\\When")
154+
), psiElement -> new GotoCompletionProvider(psiElement) {
155+
@Override
156+
public @NotNull Collection<LookupElement> getLookupElements() {
157+
return Arrays.stream((new String[]{"prod", "dev", "test"}))
158+
.map((Function<String, LookupElement>) s -> LookupElementBuilder.create(s).withIcon(Symfony2Icons.SYMFONY))
159+
.collect(Collectors.toList());
160+
}
161+
}
162+
);
165163
}
166164

167165
private static class ParameterContributor extends GotoCompletionProvider {

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

+9
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,13 @@ public void testTagContributorForAutoconfigureTagsAttribute() {
288288
PlatformPatterns.psiElement()
289289
);
290290
}
291+
292+
public void testTagContributorForWhenAttribute() {
293+
assertCompletionContains(PhpFileType.INSTANCE, "<?php\n" +
294+
"use Symfony\\Component\\DependencyInjection\\Attribute\\When;\n" +
295+
"#[When('<caret>')]\n" +
296+
"class HandlerCollection {}",
297+
"dev", "test", "prod"
298+
);
299+
}
291300
}

0 commit comments

Comments
 (0)