|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.inspections.xml; |
| 7 | + |
| 8 | +import com.intellij.codeInspection.ProblemHighlightType; |
| 9 | +import com.intellij.codeInspection.ProblemsHolder; |
| 10 | +import com.intellij.codeInspection.XmlSuppressableInspectionTool; |
| 11 | +import com.intellij.psi.PsiDirectory; |
| 12 | +import com.intellij.psi.PsiElementVisitor; |
| 13 | +import com.intellij.psi.PsiFile; |
| 14 | +import com.intellij.psi.XmlElementVisitor; |
| 15 | +import com.intellij.psi.util.PsiTreeUtil; |
| 16 | +import com.intellij.psi.xml.XmlAttribute; |
| 17 | +import com.intellij.psi.xml.XmlAttributeValue; |
| 18 | +import com.intellij.psi.xml.XmlDocument; |
| 19 | +import com.intellij.psi.xml.XmlFile; |
| 20 | +import com.intellij.psi.xml.XmlTag; |
| 21 | +import com.magento.idea.magento2plugin.bundles.InspectionBundle; |
| 22 | +import com.magento.idea.magento2plugin.inspections.util.GetEditableModuleNameByRootFileUtil; |
| 23 | +import com.magento.idea.magento2plugin.inspections.xml.fix.XmlModuleNameQuickFix; |
| 24 | +import com.magento.idea.magento2plugin.magento.files.ModuleXml; |
| 25 | +import com.magento.idea.magento2plugin.util.magento.IsFileInEditableModuleUtil; |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | + |
| 28 | +public class ModuleDeclarationInModuleXmlInspection extends XmlSuppressableInspectionTool { |
| 29 | + |
| 30 | + @NotNull |
| 31 | + @Override |
| 32 | + public PsiElementVisitor buildVisitor( |
| 33 | + final @NotNull ProblemsHolder problemsHolder, |
| 34 | + final boolean isOnTheFly |
| 35 | + ) { |
| 36 | + return new XmlElementVisitor() { |
| 37 | + @Override |
| 38 | + public void visitXmlAttributeValue(final XmlAttributeValue value) { |
| 39 | + final PsiFile file = value.getContainingFile(); |
| 40 | + final String filename = file.getName(); |
| 41 | + if (!filename.equals(ModuleXml.FILE_NAME)) { |
| 42 | + return; |
| 43 | + } |
| 44 | + if (!IsFileInEditableModuleUtil.execute(file)) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (isSubTag(value, (XmlFile) file)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + final PsiDirectory etcDirectory = file.getParent(); |
| 53 | + if (etcDirectory == null) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + final String expectedName |
| 58 | + = GetEditableModuleNameByRootFileUtil.execute(etcDirectory); |
| 59 | + final String actualName = value.getValue(); |
| 60 | + if (actualName.equals(expectedName)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + final InspectionBundle inspectionBundle = new InspectionBundle(); |
| 65 | + problemsHolder.registerProblem( |
| 66 | + value, |
| 67 | + inspectionBundle.message( |
| 68 | + "inspection.moduleDeclaration.warning.wrongModuleName", |
| 69 | + actualName, |
| 70 | + expectedName |
| 71 | + ), |
| 72 | + ProblemHighlightType.ERROR, |
| 73 | + new XmlModuleNameQuickFix(expectedName) |
| 74 | + ); |
| 75 | + } |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + protected boolean isSubTag(final XmlAttributeValue value, final XmlFile file) { |
| 80 | + final XmlAttribute xmlAttribute = PsiTreeUtil.getParentOfType(value, XmlAttribute.class); |
| 81 | + if (xmlAttribute == null) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + final XmlTag xmlTag = PsiTreeUtil.getParentOfType(xmlAttribute, XmlTag.class); |
| 86 | + if (xmlTag == null) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + final XmlDocument xmlDocument = file.getDocument(); |
| 91 | + if (xmlDocument == null) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + final XmlTag xmlRootTag = xmlDocument.getRootTag(); |
| 96 | + if (xmlRootTag == null) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + final XmlTag rootTag = PsiTreeUtil.getParentOfType(xmlTag, XmlTag.class); |
| 101 | + return rootTag == null || !(rootTag.getName().equals(xmlRootTag.getName())); |
| 102 | + } |
| 103 | +} |
0 commit comments