From c4c30fba3e69ffbe61c132f3fa3091388dff2b4a Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Sun, 29 Mar 2020 11:46:07 +0530 Subject: [PATCH 1/4] Inspection added for cacheable false in default.xml --- resources/META-INF/plugin.xml | 7 ++++ .../CacheDisableInspection.html | 10 +++++ .../xml/CacheDisableInspection.java | 42 +++++++++++++++++++ .../XmlRemoveCacheableAttributeQuickFix.java | 19 +++++++++ 4 files changed, 78 insertions(+) create mode 100644 resources/inspectionDescriptions/CacheDisableInspection.html create mode 100644 src/com/magento/idea/magento2plugin/inspections/xml/CacheDisableInspection.java create mode 100644 src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 10b1a6b8f..2f6e62a71 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -115,6 +115,13 @@ level="WARNING" implementationClass="com.magento.idea.magento2plugin.inspections.xml.ObserverDeclarationInspection"/> + + diff --git a/resources/inspectionDescriptions/CacheDisableInspection.html b/resources/inspectionDescriptions/CacheDisableInspection.html new file mode 100644 index 000000000..938836ffe --- /dev/null +++ b/resources/inspectionDescriptions/CacheDisableInspection.html @@ -0,0 +1,10 @@ + + +

+ This inspection detects cacheable attribute which are set to be false in default.xml and it result to be whole site uncacheable. +

+

+ Read more about caching +

+ + \ No newline at end of file diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/CacheDisableInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/CacheDisableInspection.java new file mode 100644 index 000000000..1f9547382 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/inspections/xml/CacheDisableInspection.java @@ -0,0 +1,42 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.inspections.xml; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.codeInspection.XmlSuppressableInspectionTool; +import com.intellij.psi.PsiElementVisitor; +import com.intellij.psi.XmlElementVisitor; +import com.intellij.psi.xml.XmlAttribute; +import com.magento.idea.magento2plugin.inspections.xml.fix.XmlRemoveCacheableAttributeQuickFix; +import org.jetbrains.annotations.NotNull; + + +public class CacheDisableInspection extends XmlSuppressableInspectionTool { + @NotNull + @Override + public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, final boolean isOnTheFly) { + return new XmlElementVisitor() { + private static final String CacheDisableProblemDescription = "Cacheable false will make whole site uncacheable"; + private static final String DefaultFileName = "default.xml"; + private static final String CacheableAttributeName = "cacheable"; + private static final String CacheableAttributeValue = "false"; + @Override + public void visitXmlAttribute(XmlAttribute attribute) { + String fileName = holder.getFile().getName(); + if (!fileName.equals(DefaultFileName)) return; + final String text = attribute.getValue(); + final String attributeName = attribute.getName(); + if (!attributeName.equals(CacheableAttributeName)) return; + if (text == null) return; + if (text.equals(CacheableAttributeValue)) { + holder.registerProblem(attribute, CacheDisableProblemDescription, + ProblemHighlightType.WARNING, + new XmlRemoveCacheableAttributeQuickFix()); + } + } + }; + } +} diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java b/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java new file mode 100644 index 000000000..46f516773 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java @@ -0,0 +1,19 @@ +package com.magento.idea.magento2plugin.inspections.xml.fix; + +import com.intellij.codeInspection.LocalQuickFix; +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; + +public class XmlRemoveCacheableAttributeQuickFix implements LocalQuickFix { + @NotNull + @Override + public String getFamilyName() { + return "Remove cacheable attribute"; + } + + @Override + public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { + descriptor.getPsiElement().delete(); + } +} From 56f99c8eadf73348381af8f998c2059f1640d8f1 Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Sun, 29 Mar 2020 11:58:34 +0530 Subject: [PATCH 2/4] Copyright text added --- .../inspectionDescriptions/CacheDisableInspection.html | 8 +++++++- .../xml/fix/XmlRemoveCacheableAttributeQuickFix.java | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/resources/inspectionDescriptions/CacheDisableInspection.html b/resources/inspectionDescriptions/CacheDisableInspection.html index 938836ffe..53e2952c5 100644 --- a/resources/inspectionDescriptions/CacheDisableInspection.html +++ b/resources/inspectionDescriptions/CacheDisableInspection.html @@ -1,3 +1,9 @@ +

@@ -7,4 +13,4 @@ Read more about caching

- \ No newline at end of file + diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java b/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java index 46f516773..6b74347f9 100644 --- a/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java +++ b/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java @@ -1,3 +1,7 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ package com.magento.idea.magento2plugin.inspections.xml.fix; import com.intellij.codeInspection.LocalQuickFix; From ed38006a534847fce99e8238c11fc3bba0726d28 Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Sun, 29 Mar 2020 12:03:07 +0530 Subject: [PATCH 3/4] Change log updated --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 258ba6980..962a1e93d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Fixed support of 2020.* versions of IDE's * Create a New Magento 2 Module action * Code Inspection: Duplicated Observer Usage in events XML + * Code Inspection: Warning regarding Cacheable false attribute in default XML 0.3.0 ============= From 49559bd512878ee958168448cf52ff312d2b4926 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Sun, 29 Mar 2020 19:06:50 +0300 Subject: [PATCH 4/4] Adjusting a code --- resources/META-INF/plugin.xml | 6 +++--- ...CacheableFalseInDefaultLayoutInspection.html} | 2 +- ...CacheableFalseInDefaultLayoutInspection.java} | 16 +++++++--------- .../magento2plugin/magento/files/LayoutXml.java | 12 ++++++++++++ 4 files changed, 23 insertions(+), 13 deletions(-) rename resources/inspectionDescriptions/{CacheDisableInspection.html => CacheableFalseInDefaultLayoutInspection.html} (69%) rename src/com/magento/idea/magento2plugin/inspections/xml/{CacheDisableInspection.java => CacheableFalseInDefaultLayoutInspection.java} (70%) create mode 100644 src/com/magento/idea/magento2plugin/magento/files/LayoutXml.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index d3cf0e08b..480e048b8 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -118,11 +118,11 @@ implementationClass="com.magento.idea.magento2plugin.inspections.xml.ObserverDeclarationInspection"/> + implementationClass="com.magento.idea.magento2plugin.inspections.xml.CacheableFalseInDefaultLayoutInspection"/> diff --git a/resources/inspectionDescriptions/CacheDisableInspection.html b/resources/inspectionDescriptions/CacheableFalseInDefaultLayoutInspection.html similarity index 69% rename from resources/inspectionDescriptions/CacheDisableInspection.html rename to resources/inspectionDescriptions/CacheableFalseInDefaultLayoutInspection.html index 53e2952c5..aa3b75342 100644 --- a/resources/inspectionDescriptions/CacheDisableInspection.html +++ b/resources/inspectionDescriptions/CacheableFalseInDefaultLayoutInspection.html @@ -7,7 +7,7 @@

- This inspection detects cacheable attribute which are set to be false in default.xml and it result to be whole site uncacheable. + This inspection detects `cacheable` attribute which is set to be false in default.xml and its result to be whole site uncacheable.

Read more about caching diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/CacheDisableInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/CacheableFalseInDefaultLayoutInspection.java similarity index 70% rename from src/com/magento/idea/magento2plugin/inspections/xml/CacheDisableInspection.java rename to src/com/magento/idea/magento2plugin/inspections/xml/CacheableFalseInDefaultLayoutInspection.java index 1f9547382..f99f43158 100644 --- a/src/com/magento/idea/magento2plugin/inspections/xml/CacheDisableInspection.java +++ b/src/com/magento/idea/magento2plugin/inspections/xml/CacheableFalseInDefaultLayoutInspection.java @@ -11,27 +11,25 @@ import com.intellij.psi.XmlElementVisitor; import com.intellij.psi.xml.XmlAttribute; import com.magento.idea.magento2plugin.inspections.xml.fix.XmlRemoveCacheableAttributeQuickFix; +import com.magento.idea.magento2plugin.magento.files.LayoutXml; import org.jetbrains.annotations.NotNull; - -public class CacheDisableInspection extends XmlSuppressableInspectionTool { +public class CacheableFalseInDefaultLayoutInspection extends XmlSuppressableInspectionTool { @NotNull @Override public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, final boolean isOnTheFly) { return new XmlElementVisitor() { - private static final String CacheDisableProblemDescription = "Cacheable false will make whole site uncacheable"; - private static final String DefaultFileName = "default.xml"; - private static final String CacheableAttributeName = "cacheable"; - private static final String CacheableAttributeValue = "false"; + private static final String CacheDisableProblemDescription = "Cacheable false attribute on the default layout will disable cache site-wide"; @Override public void visitXmlAttribute(XmlAttribute attribute) { String fileName = holder.getFile().getName(); - if (!fileName.equals(DefaultFileName)) return; + if (!fileName.equals(LayoutXml.DefaultFileName)) return; final String text = attribute.getValue(); final String attributeName = attribute.getName(); - if (!attributeName.equals(CacheableAttributeName)) return; + if (!attributeName.equals(LayoutXml.CacheableAttributeName)) return; + if (!attribute.getParent().getName().equals(LayoutXml.BlockAttributeTagName)) return; if (text == null) return; - if (text.equals(CacheableAttributeValue)) { + if (text.equals(LayoutXml.CacheableAttributeFalseValue)) { holder.registerProblem(attribute, CacheDisableProblemDescription, ProblemHighlightType.WARNING, new XmlRemoveCacheableAttributeQuickFix()); diff --git a/src/com/magento/idea/magento2plugin/magento/files/LayoutXml.java b/src/com/magento/idea/magento2plugin/magento/files/LayoutXml.java new file mode 100644 index 000000000..647e2cec0 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/LayoutXml.java @@ -0,0 +1,12 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.magento.files; + +public class LayoutXml { + public static String DefaultFileName = "default.xml"; + public static String CacheableAttributeName = "cacheable"; + public static String CacheableAttributeFalseValue = "false"; + public static String BlockAttributeTagName = "block"; +}