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();
}
}
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);
}
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);
}
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();
}
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());
}
Aggregations