Skip to content

Commit

Permalink
provide custom Twig file overlay to indicate "extends" and attached c…
Browse files Browse the repository at this point in the history
…ontroller template types #1485
  • Loading branch information
Haehnchen committed Jun 14, 2020
1 parent 4c83f16 commit 0f08676
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class Symfony2Icons {
public static final Icon TWIG_LINE_MARKER = IconLoader.getIcon("/icons/twig_line_marker.png");
public static final Icon TWIG_LINE_OVERWRITE = IconLoader.getIcon("/icons/overwrite.png");

public static final Icon TWIG_CONTROLLER_FILE = IconLoader.getIcon("/icons/twig_controller_file.png");
public static final Icon TWIG_EXTENDS_FILE = IconLoader.getIcon("/icons/twig_extends_file.png");
public static final Icon TWIG_IMPLEMENTS_FILE = IconLoader.getIcon("/icons/twig_implements_file.png");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package fr.adrienbrault.idea.symfony2plugin.twig.icon;

import com.intellij.ide.IconProvider;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.util.Iconable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.ui.LayeredIcon;
import com.jetbrains.twig.TwigFile;
import com.jetbrains.twig.elements.TwigExtendsTag;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
import icons.TwigIcons;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

/**
* Based on Twig content add overlay to the default Twig file icon, indicating the possible template type
*
* @author Daniel Espendiller <[email protected]>
*/
public class TwigIconProvider extends IconProvider {
public Icon getIcon(@NotNull PsiElement element, @Iconable.IconFlags int flags) {
if (!(element instanceof TwigFile) || !Symfony2ProjectComponent.isEnabled(element.getProject()) || DumbService.getInstance(element.getProject()).isDumb()) {
return null;
}

// attach controller icon overlay
LayeredIcon icon = null;
if (hasController((TwigFile) element)) {
icon = new LayeredIcon(TwigIcons.TwigFileIcon, Symfony2Icons.TWIG_CONTROLLER_FILE);
icon.setIcon(Symfony2Icons.TWIG_CONTROLLER_FILE, 1, SwingConstants.NORTH_WEST);
}

// file provides extends tag, add another layer on top; but put the layer below the previous layer if provided
TwigExtendsTag childOfType = PsiTreeUtil.findChildOfType(element, TwigExtendsTag.class);
if (childOfType != null) {
if (icon == null) {
// we are alone so just place the icon
icon = new LayeredIcon(TwigIcons.TwigFileIcon, Symfony2Icons.TWIG_EXTENDS_FILE);
icon.setIcon(Symfony2Icons.TWIG_EXTENDS_FILE, 1, SwingConstants.NORTH_WEST);
} else {
// icon should be below first one
icon = new LayeredIcon(icon, Symfony2Icons.TWIG_IMPLEMENTS_FILE);
icon.setIcon(Symfony2Icons.TWIG_EXTENDS_FILE, 1, 0, Symfony2Icons.TWIG_CONTROLLER_FILE.getIconHeight() + 1);
}
}

return icon;
}

private boolean hasController(@NotNull TwigFile twigFile) {
return TwigUtil.findTwigFileController(twigFile).size() > 0
|| TwigUtil.getTwigFileMethodUsageOnIndex(twigFile).size() > 0;
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
<codeInsight.parameterNameHints language="yaml" implementationClass="fr.adrienbrault.idea.symfony2plugin.dic.ServiceArgumentParameterHintsProvider"/>

<multiHostInjector implementation="fr.adrienbrault.idea.symfony2plugin.lang.ParameterLanguageInjector"/>
<iconProvider implementation="fr.adrienbrault.idea.symfony2plugin.twig.icon.TwigIconProvider"/>

<localInspection groupPath="Symfony" shortName="PhpRouteMissingInspection" displayName="Route Missing"
groupName="Route"
Expand Down
Binary file added src/main/resources/icons/twig_controller_file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/main/resources/icons/twig_extends_file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/main/resources/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0f08676

Please sign in to comment.