|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.twig.icon; |
| 2 | + |
| 3 | +import com.intellij.ide.IconProvider; |
| 4 | +import com.intellij.openapi.project.DumbService; |
| 5 | +import com.intellij.openapi.util.Iconable; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.intellij.psi.util.PsiTreeUtil; |
| 8 | +import com.intellij.ui.LayeredIcon; |
| 9 | +import com.jetbrains.twig.TwigFile; |
| 10 | +import com.jetbrains.twig.elements.TwigExtendsTag; |
| 11 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons; |
| 12 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil; |
| 14 | +import icons.TwigIcons; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +import javax.swing.*; |
| 18 | + |
| 19 | +/** |
| 20 | + * Based on Twig content add overlay to the default Twig file icon, indicating the possible template type |
| 21 | + * |
| 22 | + * @author Daniel Espendiller <[email protected]> |
| 23 | + */ |
| 24 | +public class TwigIconProvider extends IconProvider { |
| 25 | + public Icon getIcon(@NotNull PsiElement element, @Iconable.IconFlags int flags) { |
| 26 | + if (!(element instanceof TwigFile) || !Symfony2ProjectComponent.isEnabled(element.getProject()) || DumbService.getInstance(element.getProject()).isDumb()) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + // attach controller icon overlay |
| 31 | + LayeredIcon icon = null; |
| 32 | + if (hasController((TwigFile) element)) { |
| 33 | + icon = new LayeredIcon(TwigIcons.TwigFileIcon, Symfony2Icons.TWIG_CONTROLLER_FILE); |
| 34 | + icon.setIcon(Symfony2Icons.TWIG_CONTROLLER_FILE, 1, SwingConstants.NORTH_WEST); |
| 35 | + } |
| 36 | + |
| 37 | + // file provides extends tag, add another layer on top; but put the layer below the previous layer if provided |
| 38 | + TwigExtendsTag childOfType = PsiTreeUtil.findChildOfType(element, TwigExtendsTag.class); |
| 39 | + if (childOfType != null) { |
| 40 | + if (icon == null) { |
| 41 | + // we are alone so just place the icon |
| 42 | + icon = new LayeredIcon(TwigIcons.TwigFileIcon, Symfony2Icons.TWIG_EXTENDS_FILE); |
| 43 | + icon.setIcon(Symfony2Icons.TWIG_EXTENDS_FILE, 1, SwingConstants.NORTH_WEST); |
| 44 | + } else { |
| 45 | + // icon should be below first one |
| 46 | + icon = new LayeredIcon(icon, Symfony2Icons.TWIG_IMPLEMENTS_FILE); |
| 47 | + icon.setIcon(Symfony2Icons.TWIG_EXTENDS_FILE, 1, 0, Symfony2Icons.TWIG_CONTROLLER_FILE.getIconHeight() + 1); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return icon; |
| 52 | + } |
| 53 | + |
| 54 | + private boolean hasController(@NotNull TwigFile twigFile) { |
| 55 | + return TwigUtil.findTwigFileController(twigFile).size() > 0 |
| 56 | + || TwigUtil.getTwigFileMethodUsageOnIndex(twigFile).size() > 0; |
| 57 | + } |
| 58 | +} |
0 commit comments