Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class TYPO3CMSProjectSettings implements PersistentStateComponent<TYPO3CM
public boolean routeAnnotatorEnabled;
public boolean translationEnableTextFolding;
public Object translationFavoriteLocale;
public boolean iconUsageGutterIconsEnabled;
public boolean iconDefinitionGutterIconsEnabled;

public TYPO3CMSProjectSettings() {
this.pluginEnabled = false;
Expand All @@ -36,14 +38,17 @@ public TYPO3CMSProjectSettings() {

this.translationEnableTextFolding = true;
this.translationFavoriteLocale = "en";

this.iconUsageGutterIconsEnabled = true;
this.iconDefinitionGutterIconsEnabled = true;
}

public static TYPO3CMSProjectSettings getInstance(@NotNull Project project) {

return ServiceManager.getService(project, TYPO3CMSProjectSettings.class);
}

public static boolean isEnabled(PsiElement element) {
public static boolean isEnabled(@NotNull PsiElement element) {

return getInstance(element.getProject()).pluginEnabled;
}
Expand All @@ -61,6 +66,10 @@ public static String[] getAvailableLocales() {
return new String[]{"en", "de"};
}

public static TYPO3CMSProjectSettings getInstance(@NotNull PsiElement element) {
return getInstance(element.getProject());
}

@Nullable
@Override
public TYPO3CMSProjectSettings getState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import org.jetbrains.annotations.NotNull;

public class TYPO3Patterns {
/*
Expand All @@ -22,4 +25,32 @@ public static ElementPattern<? extends PsiElement> connectSignalMethodNameString
return PlatformPatterns.psiElement(StringLiteralExpression.class)
.withSuperParent(2, PlatformPatterns.psiElement(MethodReference.class));
}

public static boolean iconAPIIconRetrieval(@NotNull PsiElement element) {
if (!PlatformPatterns
.and(
stringLeafElementPattern(),
PlatformPatterns.psiElement().withSuperParent(
3, PlatformPatterns.psiElement(MethodReference.class)
)
)
.accepts(element)) {

return false;
}


MethodReference methodReference = (MethodReference) PsiTreeUtil.findFirstParent(element, e -> e instanceof MethodReference);

return methodReference.getName().equals("getIcon");
}

@NotNull
private static ElementPattern<PsiElement> stringLeafElementPattern() {

return PlatformPatterns.or(
PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE).withParent(StringLiteralExpression.class),
PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL).withParent(StringLiteralExpression.class)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ private void annotateIconUsage(PsiElement psiElement, AnnotationHolder annotatio
return;
}

if (IconIndex.getDeprecatedIconIdentifiers(psiElement.getProject()).containsKey(value)) {
return;
}

annotationHolder.createWarningAnnotation(new TextRange(psiElement.getTextRange().getStartOffset() + 1, psiElement.getTextRange().getEndOffset() - 1), "Unresolved icon - this may also occur if the icon is defined in your extension, but not in the global icon registry.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.cedricziel.idea.typo3.codeInsight;

import com.cedricziel.idea.typo3.TYPO3CMSIcons;
import com.cedricziel.idea.typo3.TYPO3CMSProjectComponent;
import com.cedricziel.idea.typo3.TYPO3CMSProjectSettings;
import com.cedricziel.idea.typo3.TYPO3Patterns;
import com.cedricziel.idea.typo3.icons.IconStub;
import com.cedricziel.idea.typo3.index.IconIndex;
import com.cedricziel.idea.typo3.util.IconUtil;
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo;
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider;
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.io.IOException;
import java.util.Collection;

import static com.cedricziel.idea.typo3.util.IconUtil.createIconFromFile;

public class IconLineMarkerProvider extends RelatedItemLineMarkerProvider {
@Override
protected void collectNavigationMarkers(@NotNull PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo> result) {

if (!TYPO3CMSProjectSettings.isEnabled(element)) {
return;
}

if (TYPO3CMSProjectSettings.getInstance(element).iconUsageGutterIconsEnabled) {
if (TYPO3Patterns.iconAPIIconRetrieval(element)) {
renderUsageMarker(element, result);
}
}

if (TYPO3CMSProjectSettings.getInstance(element).iconDefinitionGutterIconsEnabled) {
renderDefinitionMarker(element, result);
}
}

private void renderUsageMarker(PsiElement element, Collection<? super RelatedItemLineMarkerInfo> result) {
StringLiteralExpression literalExpression = (StringLiteralExpression) element.getParent();
String value = literalExpression.getContents();

if (value.isEmpty()) {
return;
}

if (!IconIndex.hasIcon(element.getProject(), value)) {
return;
}

IconIndex.getIcon(element.getProject(), value).forEach((s) -> markLineForIcon(element, result, s));
}

private void renderDefinitionMarker(PsiElement element, Collection<? super RelatedItemLineMarkerInfo> result) {
PhpClass parentClass = PsiTreeUtil.getParentOfType(element, PhpClass.class);
if (parentClass == null) {
return;
}

if (!parentClass.getPresentableFQN().equals(IconUtil.ICON_REGISTRY_CLASS)) {
return;
}

for (IconStub icon : IconIndex.getAllIcons(element.getProject())) {
if (icon.getElement().equals(element)) {
if (!(element instanceof LeafPsiElement)) {
LeafPsiElement leafChild = PsiTreeUtil.findChildOfType(element, LeafPsiElement.class);
if (leafChild != null) {
markLineForIcon(leafChild, result, icon);
}
} else {
markLineForIcon(element, result, icon);
}
}
}
}

private void markLineForIcon(PsiElement element, Collection<? super RelatedItemLineMarkerInfo> result, IconStub iconForLine) {
NavigationGutterIconBuilder<PsiElement> builder;
VirtualFile virtualFile = iconForLine.getElement().getContainingFile().getVirtualFile();
if (virtualFile == null) {
builder = NavigationGutterIconBuilder
.create(TYPO3CMSIcons.ICON_NOT_RESOLVED)
.setTarget(iconForLine.getElement())
.setTooltipText("Navigate to icon definition");
} else {
try {
Icon icon = createIconFromFile(virtualFile);
if (icon == null) {
icon = TYPO3CMSIcons.ICON_NOT_RESOLVED;
}

builder = NavigationGutterIconBuilder
.create(icon)
.setTarget(iconForLine.getElement())
.setTooltipTitle("Navigate to icon definition");
} catch (IOException e) {
// icon could not be loaded
TYPO3CMSProjectComponent.getLogger().error("Could not find image.");
return;
}
}

result.add(builder.createLineMarkerInfo(element));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.cedricziel.idea.typo3.codeInspection;

import com.cedricziel.idea.typo3.TYPO3CMSProjectSettings;
import com.cedricziel.idea.typo3.index.IconIndex;
import com.cedricziel.idea.typo3.psi.PhpElementsUtil;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.project.Project;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.patterns.PsiElementPattern;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.inspections.PhpInspection;
import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

public class DeprecatedIconUsageInspection extends PhpInspection {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
if (!TYPO3CMSProjectSettings.getInstance(problemsHolder.getProject()).pluginEnabled) {
return new PsiElementVisitor() {
};
}

return new PsiElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (!stringParameterInMethodReference().accepts(element)) {
return;
}

StringLiteralExpression literalExpression = (StringLiteralExpression) element;
String iconIdentifier = literalExpression.getContents();

if (iconIdentifier.isEmpty()) {
return;
}

PsiElement methodReference = PsiTreeUtil.getParentOfType(element, MethodReference.class);
if (PhpElementsUtil.isMethodWithFirstStringOrFieldReference(methodReference, "getIcon")) {
if (IconIndex.getDeprecatedIconIdentifiers(problemsHolder.getProject()).containsKey(iconIdentifier)) {
String alternative = IconIndex.getDeprecatedIconIdentifiers(problemsHolder.getProject()).get(iconIdentifier);
if (alternative == null || alternative.isEmpty()) {
problemsHolder.registerProblem(element, "Deprecated icon usage", ProblemHighlightType.LIKE_DEPRECATED);
} else {
problemsHolder.registerProblem(element, String.format("Deprecated icon usage - replace with %s", alternative), ProblemHighlightType.LIKE_DEPRECATED, new ReplaceIconIdentifierQuickFix(alternative));
}
}
}

super.visitElement(element);
}
};
}

private PsiElementPattern.Capture<StringLiteralExpression> stringParameterInMethodReference() {
return PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(ParameterList.class);
}

private static class ReplaceIconIdentifierQuickFix implements LocalQuickFix {
private final String alternative;

public ReplaceIconIdentifierQuickFix(String alternative) {
this.alternative = alternative;
}

@Nls(capitalization = Nls.Capitalization.Sentence)
@NotNull
@Override
public String getFamilyName() {
return "Migrate deprecated icon usage";
}

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement psiElement = descriptor.getPsiElement();
if (psiElement instanceof StringLiteralExpression) {
psiElement.replace(PhpPsiElementFactory.createFromText(project, StringLiteralExpression.class, String.format("'%s'", alternative)));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.cedricziel.idea.typo3.configuration.TYPO3CMSSettingsForm">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="10" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="12" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
Expand All @@ -10,7 +10,7 @@
<children>
<vspacer id="ba971">
<constraints>
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="11" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="d8d1e" class="javax.swing.JCheckBox" binding="enablePlugin">
Expand Down Expand Up @@ -47,52 +47,68 @@
</component>
<component id="b564d" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="6" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Route Annotator enabled"/>
</properties>
</component>
<component id="fff21" class="javax.swing.JCheckBox" binding="routeAnnotatorEnabled">
<constraints>
<grid row="5" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="7" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Enable Icon Annotator"/>
</properties>
</component>
<component id="ea6a" class="javax.swing.JLabel">
<constraints>
<grid row="6" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="8" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Translation Handling"/>
</properties>
</component>
<component id="6ae42" class="javax.swing.JCheckBox" binding="translationEnableTextFolding">
<constraints>
<grid row="7" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="9" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Enable Text folding for LLL: strings"/>
</properties>
</component>
<component id="d8914" class="javax.swing.JLabel">
<constraints>
<grid row="8" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="10" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Favorite locale"/>
</properties>
</component>
<component id="98b40" class="javax.swing.JComboBox" binding="translationFavoriteLocale">
<constraints>
<grid row="8" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<grid row="10" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<editable value="true"/>
</properties>
</component>
<component id="e4104" class="javax.swing.JCheckBox" binding="iconUsageGutterIconsEnabled">
<constraints>
<grid row="4" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Enable Icon usage gutter icon"/>
</properties>
</component>
<component id="f61c5" class="javax.swing.JCheckBox" binding="iconDefinitionGutterIconsEnabled">
<constraints>
<grid row="5" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Enable Icon definition gutter icon"/>
</properties>
</component>
</children>
</grid>
</form>
Loading