Search in sources :

Example 1 with QuickFix

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

the class LocalQuickFixWrapper method applyFix.

@Override
protected void applyFix(@NotNull final Project project, @NotNull final GlobalInspectionContextImpl context, @NotNull final CommonProblemDescriptor[] descriptors, @NotNull final Set<PsiElement> ignoredElements) {
    if (myFix instanceof BatchQuickFix) {
        final List<PsiElement> collectedElementsToIgnore = new ArrayList<>();
        final Runnable refreshViews = () -> {
            DaemonCodeAnalyzer.getInstance(project).restart();
            for (CommonProblemDescriptor descriptor : descriptors) {
                ignore(ignoredElements, descriptor, getWorkingQuickFix(descriptor.getFixes()), context);
            }
            final RefManager refManager = context.getRefManager();
            final RefElement[] refElements = new RefElement[collectedElementsToIgnore.size()];
            for (int i = 0, collectedElementsToIgnoreSize = collectedElementsToIgnore.size(); i < collectedElementsToIgnoreSize; i++) {
                refElements[i] = refManager.getReference(collectedElementsToIgnore.get(i));
            }
            removeElements(refElements, project, myToolWrapper);
        };
        ((BatchQuickFix) myFix).applyFix(project, descriptors, collectedElementsToIgnore, refreshViews);
        return;
    }
    boolean restart = false;
    for (CommonProblemDescriptor descriptor : descriptors) {
        if (descriptor == null)
            continue;
        final QuickFix[] fixes = descriptor.getFixes();
        if (fixes != null) {
            final QuickFix fix = getWorkingQuickFix(fixes);
            if (fix != null) {
                //CCE here means QuickFix was incorrectly inherited, is there a way to signal (plugin) it is wrong?
                fix.applyFix(project, descriptor);
                restart = true;
                ignore(ignoredElements, descriptor, fix, context);
            }
        }
    }
    if (restart) {
        DaemonCodeAnalyzer.getInstance(project).restart();
    }
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) BatchQuickFix(com.intellij.codeInspection.BatchQuickFix) BatchQuickFix(com.intellij.codeInspection.BatchQuickFix) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefManager(com.intellij.codeInspection.reference.RefManager) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 2 with QuickFix

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

the class QuickFixWrapper method wrap.

@NotNull
public static IntentionAction wrap(@NotNull ProblemDescriptor descriptor, int fixNumber) {
    LOG.assertTrue(fixNumber >= 0, fixNumber);
    QuickFix[] fixes = descriptor.getFixes();
    LOG.assertTrue(fixes != null && fixes.length > fixNumber);
    final QuickFix fix = fixes[fixNumber];
    return fix instanceof IntentionAction ? (IntentionAction) fix : new QuickFixWrapper(descriptor, fixNumber);
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with QuickFix

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

the class JavaDocCommentFixer method fixCommonProblems.

/**
   * This fixer is based on existing javadoc inspections - there are two of them. One detects invalid references (to nonexistent
   * method parameter or non-declared checked exception). Another one handles all other cases (parameter documentation is missing;
   * parameter doesn't have a description etc). This method handles result of the second exception
   * 
   * @param problems  detected problems
   * @param comment   target comment to fix
   * @param document  target document which contains text of the comment being fixed
   * @param project   current project
   */
@SuppressWarnings("unchecked")
private static void fixCommonProblems(@NotNull ProblemDescriptor[] problems, @NotNull PsiComment comment, @NotNull final Document document, @NotNull Project project) {
    List<PsiElement> toRemove = new ArrayList<>();
    for (ProblemDescriptor problem : problems) {
        PsiElement element = problem.getPsiElement();
        if (element == null) {
            continue;
        }
        if ((!(element instanceof PsiDocToken) || !JavaDocTokenType.DOC_COMMENT_START.equals(((PsiDocToken) element).getTokenType())) && comment.getTextRange().contains(element.getTextRange())) {
            // Unnecessary element like '@return' at the void method's javadoc.
            for (PsiElement e = element; e != null; e = e.getParent()) {
                if (e instanceof PsiDocTag) {
                    toRemove.add(e);
                    break;
                }
            }
        } else {
            // Problems like 'missing @param'.
            QuickFix[] fixes = problem.getFixes();
            if (fixes != null && fixes.length > 0) {
                fixes[0].applyFix(project, problem);
            }
        }
    }
    if (toRemove.isEmpty()) {
        return;
    }
    if (toRemove.size() > 1) {
        Collections.sort(toRemove, COMPARATOR);
    }
    PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    psiDocumentManager.doPostponedOperationsAndUnblockDocument(document);
    CharSequence text = document.getCharsSequence();
    for (PsiElement element : toRemove) {
        int startOffset = element.getTextRange().getStartOffset();
        int startLine = document.getLineNumber(startOffset);
        int i = CharArrayUtil.shiftBackward(text, startOffset - 1, " \t");
        if (i >= 0) {
            char c = text.charAt(i);
            if (c == '*') {
                i = CharArrayUtil.shiftBackward(text, i - 1, " \t");
            }
        }
        if (i >= 0 && text.charAt(i) == '\n') {
            startOffset = Math.max(i, document.getLineStartOffset(startLine) - 1);
        }
        int endOffset = element.getTextRange().getEndOffset();
        // Javadoc PSI is awkward, it includes next line text before the next tag. That's why we need to strip it.
        i = CharArrayUtil.shiftBackward(text, endOffset - 1, " \t*");
        if (i > 0 && text.charAt(i) == '\n') {
            endOffset = i;
        }
        document.deleteString(startOffset, endOffset);
    }
    psiDocumentManager.commitDocument(document);
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken)

Example 4 with QuickFix

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

the class PerformFixesModalTask method iteration.

@Override
public boolean iteration() {
    final CommonProblemDescriptor descriptor = myDescriptors[myCount++];
    ProgressIndicator indicator = myTask.getIndicator();
    if (indicator != null) {
        indicator.setFraction((double) myCount / myDescriptors.length);
        String presentableText = "usages";
        if (descriptor instanceof ProblemDescriptor) {
            final PsiElement psiElement = ((ProblemDescriptor) descriptor).getPsiElement();
            if (psiElement != null) {
                presentableText = SymbolPresentationUtil.getSymbolPresentableText(psiElement);
            }
        }
        indicator.setText("Processing " + presentableText);
    }
    final boolean[] runInReadAction = { false };
    final QuickFix[] fixes = descriptor.getFixes();
    if (fixes != null) {
        for (QuickFix fix : fixes) {
            if (!fix.startInWriteAction()) {
                runInReadAction[0] = true;
            } else {
                runInReadAction[0] = false;
                break;
            }
        }
    }
    ApplicationManager.getApplication().runWriteAction(() -> {
        myDocumentManager.commitAllDocuments();
        if (!runInReadAction[0]) {
            applyFix(myProject, descriptor);
        }
    });
    if (runInReadAction[0]) {
        applyFix(myProject, descriptor);
    }
    return isDone();
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiElement(com.intellij.psi.PsiElement)

Example 5 with QuickFix

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

the class ResourceBundleEditorShowQuickFixesAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final ResourceBundleEditor editor = getEditor(e);
    LOG.assertTrue(editor != null);
    final ResourceBundlePropertyStructureViewElement element = (ResourceBundlePropertyStructureViewElement) editor.getSelectedElementIfOnlyOne();
    LOG.assertTrue(element != null);
    final PsiFile file = editor.getResourceBundle().getDefaultPropertiesFile().getContainingFile();
    final ShowIntentionsPass.IntentionsInfo intentions = new ShowIntentionsPass.IntentionsInfo();
    boolean isQuickFixListEmpty = true;
    Pair<ResourceBundleEditorProblemDescriptor, HighlightDisplayKey>[] descriptorsAndSources = element.getProblemDescriptors();
    for (Pair<ResourceBundleEditorProblemDescriptor, HighlightDisplayKey> p : descriptorsAndSources) {
        final ResourceBundleEditorProblemDescriptor d = p.getFirst();
        final HighlightDisplayKey sourceKey = p.getSecond();
        QuickFix[] fixes = d.getFixes();
        if (fixes != null) {
            for (int i = 0; i < fixes.length; i++) {
                intentions.inspectionFixesToShow.add(new HighlightInfo.IntentionActionDescriptor(new RBEQuickFixWrapper(d, i), null, null, AllIcons.Actions.IntentionBulb, sourceKey, null, null));
                isQuickFixListEmpty = false;
            }
        }
    }
    if (isQuickFixListEmpty) {
        return;
    }
    final Project project = e.getProject();
    LOG.assertTrue(project != null);
    JBPopupFactory.getInstance().createListPopup(new IntentionListStep(null, intentions, null, file, project)).showInBestPositionFor(e.getDataContext());
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) IntentionListStep(com.intellij.codeInsight.intention.impl.IntentionListStep) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) ResourceBundleEditorProblemDescriptor(com.intellij.lang.properties.editor.inspections.ResourceBundleEditorProblemDescriptor) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Project(com.intellij.openapi.project.Project) ShowIntentionsPass(com.intellij.codeInsight.daemon.impl.ShowIntentionsPass) PsiFile(com.intellij.psi.PsiFile) Pair(com.intellij.openapi.util.Pair)

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