Search in sources :

Example 6 with QuickFix

use of com.intellij.codeInspection.QuickFix in project intellij-community by JetBrains.

the class XmlImportOptimizer method processFile.

@NotNull
@Override
public CollectingInfoRunnable processFile(final PsiFile file) {
    return new CollectingInfoRunnable() {

        int myRemovedNameSpaces = 0;

        @Override
        public void run() {
            XmlFile xmlFile = (XmlFile) file;
            Project project = xmlFile.getProject();
            HighlightDisplayKey key = HighlightDisplayKey.find(myInspection.getShortName());
            if (!InspectionProjectProfileManager.getInstance(project).getCurrentProfile().isToolEnabled(key, xmlFile))
                return;
            ProblemsHolder holder = new ProblemsHolder(InspectionManager.getInstance(project), xmlFile, false);
            final XmlElementVisitor visitor = (XmlElementVisitor) myInspection.buildVisitor(holder, false);
            new PsiRecursiveElementVisitor() {

                @Override
                public void visitElement(PsiElement element) {
                    if (element instanceof XmlAttribute) {
                        visitor.visitXmlAttribute((XmlAttribute) element);
                    } else {
                        super.visitElement(element);
                    }
                }
            }.visitFile(xmlFile);
            ProblemDescriptor[] results = holder.getResultsArray();
            List<ProblemDescriptor> list = ContainerUtil.filter(results, myCondition);
            Map<XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix, ProblemDescriptor> fixes = new LinkedHashMap<>();
            for (ProblemDescriptor result : list) {
                for (QuickFix fix : result.getFixes()) {
                    if (fix instanceof XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix) {
                        fixes.put((XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix) fix, result);
                    }
                }
            }
            SmartPsiElementPointer<XmlTag> pointer = null;
            for (Map.Entry<XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix, ProblemDescriptor> fix : fixes.entrySet()) {
                pointer = fix.getKey().doFix(project, fix.getValue(), false);
                myRemovedNameSpaces++;
            }
            if (pointer != null) {
                XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix.reformatStartTag(project, pointer);
            }
        }

        @Nullable
        @Override
        public String getUserNotificationInfo() {
            return myRemovedNameSpaces > 0 ? "Removed " + myRemovedNameSpaces + " namespace" + (myRemovedNameSpaces > 1 ? "s" : "") : null;
        }
    };
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) XmlUnusedNamespaceInspection(com.intellij.codeInsight.daemon.impl.analysis.XmlUnusedNamespaceInspection) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) LinkedHashMap(java.util.LinkedHashMap) Project(com.intellij.openapi.project.Project) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

QuickFix (com.intellij.codeInspection.QuickFix)6 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)3 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)2 CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)2 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 NotNull (org.jetbrains.annotations.NotNull)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 ShowIntentionsPass (com.intellij.codeInsight.daemon.impl.ShowIntentionsPass)1 XmlUnusedNamespaceInspection (com.intellij.codeInsight.daemon.impl.analysis.XmlUnusedNamespaceInspection)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 IntentionListStep (com.intellij.codeInsight.intention.impl.IntentionListStep)1 BatchQuickFix (com.intellij.codeInspection.BatchQuickFix)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 RefManager (com.intellij.codeInspection.reference.RefManager)1 ResourceBundleEditorProblemDescriptor (com.intellij.lang.properties.editor.inspections.ResourceBundleEditorProblemDescriptor)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Pair (com.intellij.openapi.util.Pair)1 PsiFile (com.intellij.psi.PsiFile)1