|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.completion; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.lookup.LookupElement; |
| 4 | +import com.intellij.psi.PsiElement; |
| 5 | +import com.intellij.psi.util.PsiTreeUtil; |
| 6 | +import com.intellij.psi.xml.XmlTag; |
| 7 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 8 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider; |
| 9 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProviderLookupArguments; |
| 10 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.utils.GotoCompletionUtil; |
| 11 | +import fr.adrienbrault.idea.symfony2plugin.dic.ContainerService; |
| 12 | +import fr.adrienbrault.idea.symfony2plugin.dic.ServiceCompletionProvider; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.dic.ServiceStringLookupElement; |
| 14 | +import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver; |
| 15 | +import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | + |
| 19 | +import java.util.*; |
| 20 | +import java.util.function.Function; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 25 | + */ |
| 26 | +abstract public class DecoratedServiceCompletionProvider extends GotoCompletionProvider { |
| 27 | + public DecoratedServiceCompletionProvider(PsiElement psiElement) { |
| 28 | + super(psiElement); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void getLookupElements(@NotNull GotoCompletionProviderLookupArguments arguments) { |
| 33 | + ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(getProject()); |
| 34 | + Map<String, ContainerService> services = lazyServiceCollector.getCollector().getServices(); |
| 35 | + |
| 36 | + Collection<String> servicesMatches = collectDecorateServiceSuggestions(lazyServiceCollector); |
| 37 | + |
| 38 | + Collection<LookupElement> collect = services.values().stream() |
| 39 | + .map((Function<ContainerService, LookupElement>) |
| 40 | + service -> new ServiceStringLookupElement(service, servicesMatches.contains(service.getName()))) |
| 41 | + .collect(Collectors.toList()); |
| 42 | + |
| 43 | + ServiceCompletionProvider.addPrioritizedServiceLookupElements( |
| 44 | + arguments.getParameters(), |
| 45 | + arguments.getResultSet(), |
| 46 | + new ServiceCompletionProvider.PrioritizedLookupResult(collect, servicesMatches) |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @NotNull |
| 51 | + @Override |
| 52 | + public Collection<LookupElement> getLookupElements() { |
| 53 | + return Collections.emptyList(); |
| 54 | + } |
| 55 | + |
| 56 | + @NotNull |
| 57 | + @Override |
| 58 | + public Collection<PsiElement> getPsiTargets(PsiElement element) { |
| 59 | + String decoratesId = GotoCompletionUtil.getTextValueForElement(element); |
| 60 | + if(decoratesId == null) { |
| 61 | + return Collections.emptyList(); |
| 62 | + } |
| 63 | + |
| 64 | + return Collections.singletonList(ServiceUtil.getResolvedClassDefinition(getProject(), decoratesId)); |
| 65 | + } |
| 66 | + |
| 67 | + @Nullable |
| 68 | + abstract public String findClassForElement(@NotNull PsiElement psiElement); |
| 69 | + |
| 70 | + @Nullable |
| 71 | + abstract public String findIdForElement(@NotNull PsiElement psiElement); |
| 72 | + |
| 73 | + @NotNull |
| 74 | + private Collection<String> collectDecorateServiceSuggestions(@NotNull ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector) { |
| 75 | + String classForElement = findClassForElement(getElement()); |
| 76 | + if(classForElement == null) { |
| 77 | + return Collections.emptyList(); |
| 78 | + } |
| 79 | + |
| 80 | + return createSuggestions(lazyServiceCollector, classForElement, findIdForElement(getElement())); |
| 81 | + } |
| 82 | + |
| 83 | + @NotNull |
| 84 | + private Collection<String> createSuggestions(@NotNull ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector, @NotNull String aClass, @Nullable String myId) { |
| 85 | + Set<String> servicesMatches = new HashSet<>(); |
| 86 | + |
| 87 | + PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(getProject(), aClass, lazyServiceCollector); |
| 88 | + if(phpClass != null) { |
| 89 | + for (PhpClass serviceClass : ServiceUtil.getSuperClasses(phpClass)) { |
| 90 | + servicesMatches.addAll(ServiceUtil.getServiceSuggestionForPhpClass(serviceClass, lazyServiceCollector.getCollector().getServices()).stream() |
| 91 | + .map(ContainerService::getName).collect(Collectors.toSet()) |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + servicesMatches.remove(myId); |
| 97 | + |
| 98 | + return servicesMatches; |
| 99 | + } |
| 100 | +} |
0 commit comments