optimize imports for template languages (JSP, JSPX, Facelets)

This commit is contained in:
Dmitry Avdeev
2011-12-19 16:26:12 +04:00
parent 10f3d1b980
commit 5dafa4d52e
3 changed files with 26 additions and 12 deletions
@@ -20,7 +20,11 @@
package com.intellij.lang;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class LanguageImportStatements extends LanguageExtension<ImportOptimizer> {
public static final LanguageImportStatements INSTANCE = new LanguageImportStatements();
@@ -29,9 +33,15 @@ public class LanguageImportStatements extends LanguageExtension<ImportOptimizer>
super("com.intellij.lang.importOptimizer");
}
@Nullable
public ImportOptimizer forFile(PsiFile file) {
ImportOptimizer optimizer = forLanguage(file.getLanguage());
return optimizer != null && optimizer.supports(file) ? optimizer : null;
public List<Runnable> forFile(PsiFile file) {
List<Runnable> runnables = new ArrayList<Runnable>();
Set<ImportOptimizer> optimizers = new HashSet<ImportOptimizer>();
for (PsiFile psiFile : file.getViewProvider().getAllFiles()) {
ImportOptimizer optimizer = forLanguage(psiFile.getLanguage());
if (optimizer != null && optimizer.supports(psiFile) && optimizers.add(optimizer)) {
runnables.add(optimizer.processFile(psiFile));
}
}
return runnables;
}
}
@@ -193,6 +193,6 @@ public class OptimizeImportsAction extends AnAction {
}
private static boolean isOptimizeImportsAvailable(final PsiFile file) {
return LanguageImportStatements.INSTANCE.forFile(file) != null;
return !LanguageImportStatements.INSTANCE.forFile(file).isEmpty();
}
}
@@ -73,21 +73,21 @@ public class XmlUnusedNamespaceInspection extends XmlSuppressableInspectionTool
XmlAttributeValue value = attribute.getValueElement();
assert value != null;
holder.registerProblem(attribute, "Namespace declaration is never used", ProblemHighlightType.LIKE_UNUSED_SYMBOL,
new RemoveNamespaceDeclarationFix(declaredPrefix));
new RemoveNamespaceDeclarationFix(declaredPrefix, false));
XmlTag parent = attribute.getParent();
if (declaredPrefix.length() == 0) {
XmlAttribute location = getDefaultLocation(parent);
if (location != null) {
holder.registerProblem(location, NAMESPACE_LOCATION_IS_NEVER_USED, ProblemHighlightType.LIKE_UNUSED_SYMBOL,
new RemoveNamespaceDeclarationFix(declaredPrefix));
new RemoveNamespaceDeclarationFix(declaredPrefix, true));
}
}
else {
for (PsiReference reference : getLocationReferences(namespace, parent)) {
if (!XmlHighlightVisitor.hasBadResolve(reference, false))
holder.registerProblemForReference(reference, ProblemHighlightType.LIKE_UNUSED_SYMBOL, NAMESPACE_LOCATION_IS_NEVER_USED,
new RemoveNamespaceDeclarationFix(declaredPrefix));
new RemoveNamespaceDeclarationFix(declaredPrefix, true));
}
}
}
@@ -234,9 +234,11 @@ public class XmlUnusedNamespaceInspection extends XmlSuppressableInspectionTool
public static final String NAME = "Remove unused namespace declaration";
protected final String myPrefix;
private final boolean myLocationFix;
private RemoveNamespaceDeclarationFix(@Nullable String prefix) {
private RemoveNamespaceDeclarationFix(@Nullable String prefix, boolean locationFix) {
myPrefix = prefix;
myLocationFix = locationFix;
}
@NotNull
@@ -328,7 +330,9 @@ public class XmlUnusedNamespaceInspection extends XmlSuppressableInspectionTool
@Override
public boolean equals(Object obj) {
return obj instanceof RemoveNamespaceDeclarationFix && Comparing.equal(myPrefix, ((RemoveNamespaceDeclarationFix)obj).myPrefix);
return obj instanceof RemoveNamespaceDeclarationFix &&
Comparing.equal(myPrefix, ((RemoveNamespaceDeclarationFix)obj).myPrefix) &&
(myLocationFix || ((RemoveNamespaceDeclarationFix)obj).myLocationFix);
}
@Override
@@ -342,7 +346,7 @@ public class XmlUnusedNamespaceInspection extends XmlSuppressableInspectionTool
public static final String NAME = "Remove unused namespace location";
private RemoveNamespaceLocationFix(String namespace) {
super(namespace);
super(namespace, true);
}
@NotNull