diff --git a/typo3-cms/src/main/java/com/cedricziel/idea/typo3/codeInspection/daemon/XmlNamespaceImplicitUsageProvider.java b/typo3-cms/src/main/java/com/cedricziel/idea/typo3/codeInspection/daemon/XmlNamespaceImplicitUsageProvider.java new file mode 100644 index 00000000..6b1fe26e --- /dev/null +++ b/typo3-cms/src/main/java/com/cedricziel/idea/typo3/codeInspection/daemon/XmlNamespaceImplicitUsageProvider.java @@ -0,0 +1,50 @@ +package com.cedricziel.idea.typo3.codeInspection.daemon; + +import com.intellij.codeInsight.daemon.ImplicitUsageProvider; +import com.intellij.lang.xml.XMLLanguage; +import com.intellij.psi.PsiElement; +import com.intellij.psi.xml.XmlAttribute; +import org.jetbrains.annotations.NotNull; + +public class XmlNamespaceImplicitUsageProvider implements ImplicitUsageProvider { + /** + * @param element The element + * @return true if element should not be reported as unused + */ + @Override + public boolean isImplicitUsage(@NotNull PsiElement element) { + if (!(element.getLanguage() instanceof XMLLanguage)) { + return false; + } + + if (!(element instanceof XmlAttribute)) { + return false; + } + + if (!((XmlAttribute) element).isNamespaceDeclaration()) { + return false; + } + + String namespace = ((XmlAttribute) element).getValue(); + + return namespace != null && namespace.contains("typo3.org/ns"); + } + + /** + * @param element The element + * @return true if element should not be reported as "assigned but not used" + */ + @Override + public boolean isImplicitRead(@NotNull PsiElement element) { + return false; + } + + /** + * @param element The element + * @return true if element should not be reported as "referenced but never assigned" + */ + @Override + public boolean isImplicitWrite(@NotNull PsiElement element) { + return false; + } +} diff --git a/typo3-cms/src/main/resources/META-INF/plugin.xml b/typo3-cms/src/main/resources/META-INF/plugin.xml index 7372b454..095b175b 100644 --- a/typo3-cms/src/main/resources/META-INF/plugin.xml +++ b/typo3-cms/src/main/resources/META-INF/plugin.xml @@ -194,6 +194,7 @@ implementationClass="com.cedricziel.idea.typo3.codeInspection.XmlUnusedNamespaceDeclarationSuppressor"/> + diff --git a/typo3-cms/src/test/java/com/cedricziel/idea/typo3/codeInspection/daemon/XmlNamespaceImplicitUsageProviderTest.java b/typo3-cms/src/test/java/com/cedricziel/idea/typo3/codeInspection/daemon/XmlNamespaceImplicitUsageProviderTest.java new file mode 100644 index 00000000..11e1d518 --- /dev/null +++ b/typo3-cms/src/test/java/com/cedricziel/idea/typo3/codeInspection/daemon/XmlNamespaceImplicitUsageProviderTest.java @@ -0,0 +1,19 @@ +package com.cedricziel.idea.typo3.codeInspection.daemon; + +import com.cedricziel.idea.typo3.AbstractTestCase; +import com.intellij.codeInsight.daemon.impl.analysis.XmlUnusedNamespaceInspection; + +public class XmlNamespaceImplicitUsageProviderTest extends AbstractTestCase { + @Override + protected String getTestDataPath() { + return super.getTestDataPath() + "/codeInspection/daemon"; + } + + public void testOptimizeImportsDoesNotRemoveImports() { + myFixture.enableInspections(new XmlUnusedNamespaceInspection()); + + myFixture.configureByFile("unused_namespace.html"); + myFixture.performEditorAction("OptimizeImports"); + myFixture.checkResultByFile("unused_namespace.html"); + } +} diff --git a/typo3-cms/testData/com/cedricziel/idea/typo3/codeInspection/daemon/unused_namespace.html b/typo3-cms/testData/com/cedricziel/idea/typo3/codeInspection/daemon/unused_namespace.html new file mode 100644 index 00000000..cc823943 --- /dev/null +++ b/typo3-cms/testData/com/cedricziel/idea/typo3/codeInspection/daemon/unused_namespace.html @@ -0,0 +1,10 @@ +
+ +