|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.templating; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.lookup.LookupElement; |
| 4 | +import com.intellij.codeInsight.lookup.LookupElementBuilder; |
| 5 | +import com.intellij.openapi.project.Project; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.intellij.util.containers.ContainerUtil; |
| 8 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons; |
| 9 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
| 10 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider; |
| 11 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrar; |
| 12 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrarParameter; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.templating.dict.TwigExtension; |
| 14 | +import fr.adrienbrault.idea.symfony2plugin.templating.dict.TwigExtensionLookupElement; |
| 15 | +import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigExtensionParser; |
| 16 | +import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils; |
| 17 | +import org.apache.commons.lang3.StringUtils; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | +import org.jetbrains.annotations.Nullable; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +/** |
| 27 | + * Provides completion and navigation for Twig guard tag |
| 28 | + * {% guard function importmap %} |
| 29 | + * {% guard filter upper %} |
| 30 | + * {% guard test even %} |
| 31 | + * |
| 32 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 33 | + */ |
| 34 | +public class GuardGotoCompletionRegistrar implements GotoCompletionRegistrar { |
| 35 | + @Override |
| 36 | + public void register(@NotNull GotoCompletionRegistrarParameter registrar) { |
| 37 | + // {% guard <caret> %} |
| 38 | + // Completion for "function", "filter", "test" keywords |
| 39 | + registrar.register(TwigPattern.getGuardTypePattern(), psiElement -> { |
| 40 | + if (!Symfony2ProjectComponent.isEnabled(psiElement)) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + return new GuardTypeGotoCompletionProvider(psiElement); |
| 45 | + }); |
| 46 | + |
| 47 | + // {% guard function <caret> %} |
| 48 | + // Completion for callable name based on type |
| 49 | + registrar.register(TwigPattern.getGuardCallablePattern(), psiElement -> { |
| 50 | + if (!Symfony2ProjectComponent.isEnabled(psiElement)) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + return new GuardCallableGotoCompletionProvider(psiElement); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {% guard <caret> %} |
| 60 | + * Provides completion for "function", "filter", "test" keywords |
| 61 | + */ |
| 62 | + private static class GuardTypeGotoCompletionProvider extends GotoCompletionProvider { |
| 63 | + GuardTypeGotoCompletionProvider(PsiElement psiElement) { |
| 64 | + super(psiElement); |
| 65 | + } |
| 66 | + |
| 67 | + @NotNull |
| 68 | + @Override |
| 69 | + public Collection<LookupElement> getLookupElements() { |
| 70 | + Collection<LookupElement> lookupElements = new ArrayList<>(); |
| 71 | + |
| 72 | + lookupElements.add(LookupElementBuilder.create("function").withTypeText("Twig function", true).withIcon(Symfony2Icons.SYMFONY)); |
| 73 | + lookupElements.add(LookupElementBuilder.create("filter").withTypeText("Twig filter", true).withIcon(Symfony2Icons.SYMFONY)); |
| 74 | + lookupElements.add(LookupElementBuilder.create("test").withTypeText("Twig test", true).withIcon(Symfony2Icons.SYMFONY)); |
| 75 | + |
| 76 | + return lookupElements; |
| 77 | + } |
| 78 | + |
| 79 | + @NotNull |
| 80 | + @Override |
| 81 | + public Collection<PsiElement> getPsiTargets(PsiElement element) { |
| 82 | + return Collections.emptyList(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {% guard function <caret> %} |
| 88 | + * {% guard filter <caret> %} |
| 89 | + * {% guard test <caret> %} |
| 90 | + * |
| 91 | + * Provides completion and navigation for callable names based on the guard type |
| 92 | + */ |
| 93 | + private static class GuardCallableGotoCompletionProvider extends GotoCompletionProvider { |
| 94 | + GuardCallableGotoCompletionProvider(PsiElement psiElement) { |
| 95 | + super(psiElement); |
| 96 | + } |
| 97 | + |
| 98 | + @NotNull |
| 99 | + @Override |
| 100 | + public Collection<LookupElement> getLookupElements() { |
| 101 | + Collection<LookupElement> lookupElements = new ArrayList<>(); |
| 102 | + |
| 103 | + Project project = getProject(); |
| 104 | + String guardType = getGuardType(getElement()); |
| 105 | + |
| 106 | + if ("function".equals(guardType)) { |
| 107 | + // Provide Twig functions |
| 108 | + for (Map.Entry<String, TwigExtension> entry : TwigExtensionParser.getFunctions(project).entrySet()) { |
| 109 | + lookupElements.add(new TwigExtensionLookupElement(project, entry.getKey(), entry.getValue())); |
| 110 | + } |
| 111 | + } else if ("filter".equals(guardType)) { |
| 112 | + // Provide Twig filters |
| 113 | + for (Map.Entry<String, TwigExtension> entry : TwigExtensionParser.getFilters(project).entrySet()) { |
| 114 | + lookupElements.add(new TwigExtensionLookupElement(project, entry.getKey(), entry.getValue())); |
| 115 | + } |
| 116 | + } else if ("test".equals(guardType)) { |
| 117 | + // Provide Twig tests |
| 118 | + for (Map.Entry<String, TwigExtension> entry : TwigExtensionParser.getSimpleTest(project).entrySet()) { |
| 119 | + lookupElements.add(new TwigExtensionLookupElement(project, entry.getKey(), entry.getValue())); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return lookupElements; |
| 124 | + } |
| 125 | + |
| 126 | + @NotNull |
| 127 | + @Override |
| 128 | + public Collection<PsiElement> getPsiTargets(PsiElement element) { |
| 129 | + String text = element.getText(); |
| 130 | + if (StringUtils.isBlank(text)) { |
| 131 | + return Collections.emptyList(); |
| 132 | + } |
| 133 | + |
| 134 | + Collection<PsiElement> targets = new ArrayList<>(); |
| 135 | + Project project = getProject(); |
| 136 | + String guardType = getGuardType(element); |
| 137 | + |
| 138 | + Map<String, TwigExtension> extensions = null; |
| 139 | + if ("function".equals(guardType)) { |
| 140 | + extensions = TwigExtensionParser.getFunctions(project); |
| 141 | + } else if ("filter".equals(guardType)) { |
| 142 | + extensions = TwigExtensionParser.getFilters(project); |
| 143 | + } else if ("test".equals(guardType)) { |
| 144 | + extensions = TwigExtensionParser.getSimpleTest(project); |
| 145 | + } |
| 146 | + |
| 147 | + if (extensions != null) { |
| 148 | + for (Map.Entry<String, TwigExtension> entry : extensions.entrySet()) { |
| 149 | + if (text.equals(entry.getKey())) { |
| 150 | + ContainerUtil.addIfNotNull( |
| 151 | + targets, |
| 152 | + TwigExtensionParser.getExtensionTarget(project, entry.getValue()) |
| 153 | + ); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return targets; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Get the guard type (function, filter, test) by looking at the previous IDENTIFIER |
| 163 | + * {% guard function importmap %} |
| 164 | + * ^-------^ |
| 165 | + */ |
| 166 | + @Nullable |
| 167 | + private String getGuardType(@NotNull PsiElement element) { |
| 168 | + PsiElement prevIdentifier = PsiElementUtils.getPrevSiblingOfType( |
| 169 | + element, |
| 170 | + com.intellij.patterns.PlatformPatterns.psiElement(com.jetbrains.twig.TwigTokenTypes.IDENTIFIER) |
| 171 | + ); |
| 172 | + |
| 173 | + if (prevIdentifier != null) { |
| 174 | + String text = prevIdentifier.getText(); |
| 175 | + if ("function".equals(text) || "filter".equals(text) || "test".equals(text)) { |
| 176 | + return text; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + return null; |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments