Skip to content

Commit

Permalink
#1984 support parameter inside "TaggedIterator" attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Jul 22, 2022
1 parent 8ae5672 commit 7bd5990
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
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 Down Expand Up @@ -122,4 +142,27 @@ public Collection<PsiElement> getPsiTargets(PsiElement element) {
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,29 @@ 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"
);

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 }

0 comments on commit 7bd5990

Please sign in to comment.