|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.navigation; |
| 2 | + |
| 3 | +import com.intellij.lang.ASTNode; |
| 4 | +import com.intellij.lang.folding.FoldingBuilderEx; |
| 5 | +import com.intellij.lang.folding.FoldingDescriptor; |
| 6 | +import com.intellij.openapi.editor.Document; |
| 7 | +import com.intellij.openapi.editor.FoldingGroup; |
| 8 | +import com.intellij.openapi.util.TextRange; |
| 9 | +import com.intellij.psi.PsiElement; |
| 10 | +import com.intellij.psi.util.PsiElementFilter; |
| 11 | +import com.intellij.psi.util.PsiTreeUtil; |
| 12 | +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
| 14 | +import fr.adrienbrault.idea.symfony2plugin.TwigHelper; |
| 15 | +import fr.adrienbrault.idea.symfony2plugin.config.SymfonyPhpReferenceContributor; |
| 16 | +import fr.adrienbrault.idea.symfony2plugin.routing.PhpRouteReferenceContributor; |
| 17 | +import fr.adrienbrault.idea.symfony2plugin.routing.Route; |
| 18 | +import fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper; |
| 19 | +import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher; |
| 20 | +import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | +import org.jetbrains.annotations.Nullable; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | + |
| 30 | +public class TwigFoldingBuilder extends FoldingBuilderEx { |
| 31 | + |
| 32 | + @NotNull |
| 33 | + @Override |
| 34 | + public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement psiElement, @NotNull Document document, boolean b) { |
| 35 | + |
| 36 | + if (!Symfony2ProjectComponent.isEnabled(psiElement)) { |
| 37 | + return new FoldingDescriptor[0]; |
| 38 | + } |
| 39 | + |
| 40 | + List<FoldingDescriptor> descriptors = new ArrayList<FoldingDescriptor>(); |
| 41 | + |
| 42 | + attachPathFoldingDescriptors(psiElement, descriptors); |
| 43 | + |
| 44 | + return descriptors.toArray(new FoldingDescriptor[descriptors.size()]); |
| 45 | + } |
| 46 | + |
| 47 | + private void attachPathFoldingDescriptors(PsiElement psiElement, List<FoldingDescriptor> descriptors) { |
| 48 | + |
| 49 | + // find path calls in file |
| 50 | + PsiElement[] psiElements = PsiTreeUtil.collectElements(psiElement, new PsiElementFilter() { |
| 51 | + @Override |
| 52 | + public boolean isAccepted(PsiElement psiElement) { |
| 53 | + return TwigHelper.getAutocompletableRoutePattern().accepts(psiElement); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + if(psiElements.length == 0) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + FoldingGroup group = FoldingGroup.newGroup("route"); |
| 62 | + Map<String,Route> routes = null; |
| 63 | + for(PsiElement psiElement1: psiElements) { |
| 64 | + |
| 65 | + // cache routes if we need them |
| 66 | + if(routes == null) { |
| 67 | + Symfony2ProjectComponent symfony2ProjectComponent = psiElement.getProject().getComponent(Symfony2ProjectComponent.class); |
| 68 | + routes = symfony2ProjectComponent.getRoutes(); |
| 69 | + } |
| 70 | + |
| 71 | + String contents = PsiElementUtils.trimQuote(psiElement1.getText()); |
| 72 | + if(contents.length() > 0 && routes.containsKey(contents)) { |
| 73 | + final Route route = routes.get(contents); |
| 74 | + |
| 75 | + final String url = RouteHelper.getRouteUrl(route.getTokens()); |
| 76 | + if(url != null) { |
| 77 | + descriptors.add(new FoldingDescriptor(psiElement1.getNode(), |
| 78 | + new TextRange(psiElement1.getTextRange().getStartOffset(), psiElement1.getTextRange().getEndOffset()), group) { |
| 79 | + @Nullable |
| 80 | + @Override |
| 81 | + public String getPlaceholderText() { |
| 82 | + return url; |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Nullable |
| 93 | + @Override |
| 94 | + public String getPlaceholderText(@NotNull ASTNode astNode) { |
| 95 | + return "..."; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean isCollapsedByDefault(@NotNull ASTNode astNode) { |
| 100 | + return true; |
| 101 | + } |
| 102 | +} |
0 commit comments