Skip to content

#2102 add completion for "When" attribute #2113

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

Merged
merged 1 commit into from
Mar 28, 2023
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
@@ -1,12 +1,14 @@
package fr.adrienbrault.idea.symfony2plugin.dic.registrar;

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.psi.elements.PhpAttribute;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider;
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrar;
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrarParameter;
Expand All @@ -23,6 +25,8 @@
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
Expand Down Expand Up @@ -120,26 +124,6 @@ public void register(@NotNull GotoCompletionRegistrarParameter registrar) {
}
);

// #[Autoconfigure(tags: ['app.some_tag'])]
// #[Autoconfigure(['app.some_tag'])]
registrar.register(
PlatformPatterns.or(

), 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;
}
);

// #[Autowire(service: 'some_service')]
// #[AsDecorator(decorates: 'some_service')]
registrar.register(
Expand All @@ -162,6 +146,20 @@ public void register(@NotNull GotoCompletionRegistrarParameter registrar) {
return null;
}
);

// #[When('dev')]
registrar.register(
PlatformPatterns.or(
PhpElementsUtil.getFirstAttributeStringPattern("\\Symfony\\Component\\DependencyInjection\\Attribute\\When")
), psiElement -> new GotoCompletionProvider(psiElement) {
@Override
public @NotNull Collection<LookupElement> getLookupElements() {
return Arrays.stream((new String[]{"prod", "dev", "test"}))
.map((Function<String, LookupElement>) s -> LookupElementBuilder.create(s).withIcon(Symfony2Icons.SYMFONY))
.collect(Collectors.toList());
}
}
);
}

private static class ParameterContributor extends GotoCompletionProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,13 @@ public void testTagContributorForAutoconfigureTagsAttribute() {
PlatformPatterns.psiElement()
);
}

public void testTagContributorForWhenAttribute() {
assertCompletionContains(PhpFileType.INSTANCE, "<?php\n" +
"use Symfony\\Component\\DependencyInjection\\Attribute\\When;\n" +
"#[When('<caret>')]\n" +
"class HandlerCollection {}",
"dev", "test", "prod"
);
}
}