Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1984 support parameter inside "TaggedIterator" attribute #1986

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class ServiceContainerUtil {
};

public static final String AUTOWIRE_ATTRIBUTE_CLASS = "\\Symfony\\Component\\DependencyInjection\\Attribute\\Autowire";
public static final String TAGGET_ITERATOR_ATTRIBUTE_CLASS = "\\Symfony\\Component\\DependencyInjection\\Attribute\\TaggedIterator";

@NotNull
public static Collection<ServiceSerializable> getServicesInFile(@NotNull PsiFile psiFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementResolveResult;
import com.intellij.psi.ResolveResult;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.psi.elements.PhpAttribute;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
Expand All @@ -16,6 +18,7 @@
import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver;
import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.completion.TagNameCompletionProvider;
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -82,10 +85,27 @@ public void register(@NotNull GotoCompletionRegistrarParameter registrar) {

PhpAttribute phpAttribute = PsiTreeUtil.getParentOfType(context, PhpAttribute.class);
if (phpAttribute != null) {
String fqn = phpAttribute.getFQN();
if (fqn != null && PhpElementsUtil.isInstanceOf(psiElement.getProject(), fqn, ServiceContainerUtil.AUTOWIRE_ATTRIBUTE_CLASS)) {
return new ParameterContributor((StringLiteralExpression) context);
}
return new ParameterContributor((StringLiteralExpression) context);
}

return null;
}
);

// #[TaggedIterator('app.handler')] iterable $handlers
registrar.register(
PlatformPatterns.or(
PhpElementsUtil.getFirstAttributeStringPattern(ServiceContainerUtil.TAGGET_ITERATOR_ATTRIBUTE_CLASS),
PhpElementsUtil.getAttributeNamedArgumentStringPattern(ServiceContainerUtil.TAGGET_ITERATOR_ATTRIBUTE_CLASS, "tag")
), psiElement -> {
PsiElement context = psiElement.getContext();
if (!(context instanceof StringLiteralExpression)) {
return null;
}

PhpAttribute phpAttribute = PsiTreeUtil.getParentOfType(context, PhpAttribute.class);
if (phpAttribute != null) {
return new TaggedIteratorContributor((StringLiteralExpression) context);
}

return null;
Expand All @@ -104,7 +124,7 @@ public ParameterContributor(StringLiteralExpression element) {
public Collection<LookupElement> getLookupElements() {
Collection<LookupElement> results = new ArrayList<>();

for(Map.Entry<String, ContainerParameter> entry: ContainerCollectionResolver.getParameters(getElement().getProject()).entrySet()) {
for (Map.Entry<String, ContainerParameter> entry: ContainerCollectionResolver.getParameters(getElement().getProject()).entrySet()) {
results.add(new ParameterLookupElement(entry.getValue()));
}

Expand All @@ -115,11 +135,34 @@ public Collection<LookupElement> getLookupElements() {
@Override
public Collection<PsiElement> getPsiTargets(PsiElement element) {
String contents = GotoCompletionUtil.getStringLiteralValue(element);
if(contents == null) {
if (contents == null) {
return Collections.emptyList();
}

return ServiceUtil.getParameterDefinition(element.getProject(), contents);
}
}

private static class TaggedIteratorContributor extends GotoCompletionProvider {
public TaggedIteratorContributor(StringLiteralExpression element) {
super(element);
}

@NotNull
@Override
public Collection<LookupElement> getLookupElements() {
return TagNameCompletionProvider.getTagLookupElements(getElement().getProject());
}

@NotNull
@Override
public Collection<PsiElement> getPsiTargets(PsiElement element) {
String contents = GotoCompletionUtil.getStringLiteralValue(element);
if(contents == null) {
return Collections.emptyList();
}

return new ArrayList<>(ServiceUtil.getTaggedClasses(getElement().getProject(), contents));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.intellij.patterns.PlatformPatterns;
import com.jetbrains.php.lang.PhpFileType;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
import org.jetbrains.yaml.YAMLFileType;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
Expand All @@ -13,8 +12,7 @@ public class DicGotoCompletionRegistrarTest extends SymfonyLightCodeInsightFixtu
public void setUp() throws Exception {
super.setUp();
myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject("classes.php"));
myFixture.configureByText(YAMLFileType.YML, "parameters:\n foo: foo");

myFixture.copyFileToProject("services.yml");
}

public String getTestDataPath() {
Expand Down Expand Up @@ -132,4 +130,40 @@ public void testParameterContributorForNamedAttribute() {
PlatformPatterns.psiElement()
);
}

public void testTagContributorForTaggedIterator() {
assertCompletionContains(PhpFileType.INSTANCE, "<?php\n" +
"use Symfony\\Component\\DependencyInjection\\Attribute\\TaggedIterator;\n" +
"\n" +
"class HandlerCollection\n" +
" public function __construct(\n" +
" #[TaggedIterator('<caret>')] iterable $handlers\n" +
" ) {}\n" +
"}",
"yaml_type_tag"
);

assertCompletionContains(PhpFileType.INSTANCE, "<?php\n" +
"use Symfony\\Component\\DependencyInjection\\Attribute\\TaggedIterator;\n" +
"\n" +
"class HandlerCollection\n" +
" public function __construct(\n" +
" #[TaggedIterator(tag: '<caret>')] iterable $handlers\n" +
" ) {}\n" +
"}",
"yaml_type_tag"
);

assertNavigationMatch(PhpFileType.INSTANCE, "<?php\n" +
"use Symfony\\Component\\DependencyInjection\\Attribute\\TaggedIterator;\n" +
"\n" +
"class HandlerCollection\n" +
"{\n" +
" public function __construct(\n" +
" #[TaggedIterator('yaml_t<caret>ype_tag')] iterable $handlers\n" +
" ) {}\n" +
"}",
PlatformPatterns.psiElement()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ public function __construct(
string $expression = null,
) {}
}

class TaggedIterator
{
public function __construct(
public string $tag,
public ?string $indexAttribute = null,
public ?string $defaultIndexMethod = null,
public ?string $defaultPriorityMethod = null,
public string|array $exclude = [],
) {}
}
}

namespace
Expand All @@ -46,4 +57,10 @@ public function bar($parameter) {
}
}

namespace Foo
{
class Bar
{
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
foo: foo

services:
Foo\Bar:
tags:
- { name: yaml_type_tag }