|
| 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.patterns.PlatformPatterns; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.intellij.psi.PsiFile; |
| 8 | +import com.jetbrains.php.lang.psi.elements.ArrayCreationExpression; |
| 9 | +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; |
| 10 | +import com.jetbrains.twig.TwigFile; |
| 11 | +import fr.adrienbrault.idea.symfony2plugin.TwigHelper; |
| 12 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrar; |
| 14 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrarParameter; |
| 15 | +import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil; |
| 16 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 17 | +import icons.TwigIcons; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Collections; |
| 23 | + |
| 24 | +/** |
| 25 | + * render('foo.html.twig', ['fo<caret>obar' => 'foobar']) |
| 26 | + * |
| 27 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 28 | + */ |
| 29 | +public class RenderParameterGotoCompletionRegistrar implements GotoCompletionRegistrar { |
| 30 | + @Override |
| 31 | + public void register(GotoCompletionRegistrarParameter registrar) { |
| 32 | + registrar.register( |
| 33 | + PlatformPatterns.psiElement(), |
| 34 | + MyGotoCompletionProvider::new |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + private static class MyGotoCompletionProvider extends GotoCompletionProvider { |
| 39 | + private MyGotoCompletionProvider(PsiElement psiElement) { |
| 40 | + super(psiElement); |
| 41 | + } |
| 42 | + |
| 43 | + @NotNull |
| 44 | + @Override |
| 45 | + public Collection<LookupElement> getLookupElements() { |
| 46 | + PsiElement parent = getElement().getParent(); |
| 47 | + if(!(parent instanceof StringLiteralExpression)) { |
| 48 | + return Collections.emptyList(); |
| 49 | + } |
| 50 | + |
| 51 | + ArrayCreationExpression arrayCreationElement = PhpElementsUtil.getCompletableArrayCreationElement(parent); |
| 52 | + if(arrayCreationElement == null) { |
| 53 | + return Collections.emptyList(); |
| 54 | + } |
| 55 | + |
| 56 | + PsiElement prevSibling = arrayCreationElement.getPrevPsiSibling(); |
| 57 | + if(prevSibling == null) { |
| 58 | + return Collections.emptyList(); |
| 59 | + } |
| 60 | + |
| 61 | + String stringValue = PhpElementsUtil.getStringValue(prevSibling); |
| 62 | + if(stringValue == null || !stringValue.toLowerCase().endsWith(".twig")) { |
| 63 | + return Collections.emptyList(); |
| 64 | + } |
| 65 | + |
| 66 | + Collection<LookupElement> elements = new ArrayList<>(); |
| 67 | + |
| 68 | + for (PsiFile psiFile : TwigHelper.getTemplatePsiElements(getProject(), stringValue)) { |
| 69 | + if(!(psiFile instanceof TwigFile)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + TwigUtil.visitTemplateVariables((TwigFile) psiFile, pair -> |
| 74 | + elements.add(LookupElementBuilder.create(pair.getFirst()) |
| 75 | + .withIcon(TwigIcons.TwigFileIcon) |
| 76 | + .withTypeText(stringValue, true) |
| 77 | + )); |
| 78 | + } |
| 79 | + |
| 80 | + return elements; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments