summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/codeInspection
diff options
context:
space:
mode:
Diffstat (limited to 'platform/lang-impl/src/com/intellij/codeInspection')
-rw-r--r--platform/lang-impl/src/com/intellij/codeInspection/actions/CleanupInspectionIntention.java128
-rw-r--r--platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java17
2 files changed, 140 insertions, 5 deletions
diff --git a/platform/lang-impl/src/com/intellij/codeInspection/actions/CleanupInspectionIntention.java b/platform/lang-impl/src/com/intellij/codeInspection/actions/CleanupInspectionIntention.java
new file mode 100644
index 000000000000..b6a68ef88fb0
--- /dev/null
+++ b/platform/lang-impl/src/com/intellij/codeInspection/actions/CleanupInspectionIntention.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2000-2013 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.intellij.codeInspection.actions;
+
+import com.intellij.codeInsight.FileModificationService;
+import com.intellij.codeInsight.hint.HintManager;
+import com.intellij.codeInsight.intention.EmptyIntentionAction;
+import com.intellij.codeInsight.intention.HighPriorityAction;
+import com.intellij.codeInsight.intention.IntentionAction;
+import com.intellij.codeInspection.*;
+import com.intellij.codeInspection.ex.InspectionToolWrapper;
+import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.progress.EmptyProgressIndicator;
+import com.intellij.openapi.progress.ProgressManager;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.Computable;
+import com.intellij.psi.PsiDocumentManager;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
+import com.intellij.util.IncorrectOperationException;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * User: anna
+ * Date: 21-Feb-2006
+ */
+public class CleanupInspectionIntention implements IntentionAction, HighPriorityAction {
+ private final InspectionToolWrapper myToolWrapper;
+ private final Class myQuickfixClass;
+ private final String myText;
+
+ public CleanupInspectionIntention(@NotNull InspectionToolWrapper toolWrapper, @NotNull Class quickFixClass, String text) {
+ myToolWrapper = toolWrapper;
+ myQuickfixClass = quickFixClass;
+ myText = text;
+ }
+
+ @Override
+ @NotNull
+ public String getText() {
+ return InspectionsBundle.message("fix.all.inspection.problems.in.file", myToolWrapper.getDisplayName());
+ }
+
+ @Override
+ @NotNull
+ public String getFamilyName() {
+ return getText();
+ }
+
+ @Override
+ public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
+ if (!FileModificationService.getInstance().preparePsiElementForWrite(file)) return;
+ final List<ProblemDescriptor> descriptions =
+ ProgressManager.getInstance().runProcess(new Computable<List<ProblemDescriptor>>() {
+ @Override
+ public List<ProblemDescriptor> compute() {
+ InspectionManager inspectionManager = InspectionManager.getInstance(project);
+ return InspectionEngine.runInspectionOnFile(file, myToolWrapper, inspectionManager.createNewGlobalContext(false));
+ }
+ }, new EmptyProgressIndicator());
+
+ Collections.sort(descriptions, new Comparator<CommonProblemDescriptor>() {
+ @Override
+ public int compare(final CommonProblemDescriptor o1, final CommonProblemDescriptor o2) {
+ final ProblemDescriptorBase d1 = (ProblemDescriptorBase)o1;
+ final ProblemDescriptorBase d2 = (ProblemDescriptorBase)o2;
+ return d2.getTextRange().getStartOffset() - d1.getTextRange().getStartOffset();
+ }
+ });
+ boolean applicableFixFound = false;
+ for (final ProblemDescriptor descriptor : descriptions) {
+ final QuickFix[] fixes = descriptor.getFixes();
+ if (fixes != null && fixes.length > 0) {
+ for (final QuickFix<CommonProblemDescriptor> fix : fixes) {
+ if (fix != null && fix.getClass().isAssignableFrom(myQuickfixClass)) {
+ final PsiElement element = descriptor.getPsiElement();
+ if (element != null && element.isValid()) {
+ applicableFixFound = true;
+ ApplicationManager.getApplication().runWriteAction(new Runnable() {
+ @Override
+ public void run() {
+ fix.applyFix(project, descriptor);
+ }
+ });
+ PsiDocumentManager.getInstance(project).commitAllDocuments();
+ }
+ break;
+ }
+ }
+ }
+ }
+
+ if (!applicableFixFound) {
+ HintManager.getInstance().showErrorHint(editor, "Unfortunately '" + myText + "' is currently not available for batch mode");
+ }
+ }
+
+ @Override
+ public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
+ return myQuickfixClass != EmptyIntentionAction.class &&
+ !(myToolWrapper instanceof LocalInspectionToolWrapper && ((LocalInspectionToolWrapper)myToolWrapper).isUnfair());
+ }
+
+ @Override
+ public boolean startInWriteAction() {
+ return false;
+ }
+}
diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java
index 090d83389b6c..695c2a72d200 100644
--- a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java
+++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2011 JetBrains s.r.o.
+ * Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -58,7 +58,6 @@ import com.intellij.psi.*;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.util.PsiTreeUtil;
-import com.intellij.psi.util.PsiUtilCore;
import com.intellij.ui.content.*;
import com.intellij.util.Processor;
import com.intellij.util.SequentialModalProgressTask;
@@ -81,7 +80,7 @@ import java.util.*;
public class GlobalInspectionContextImpl extends GlobalInspectionContextBase implements GlobalInspectionContext {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.ex.GlobalInspectionContextImpl");
- private static final NotificationGroup NOTIFICATION_GROUP = NotificationGroup.toolWindowGroup("Inspection Results", ToolWindowId.INSPECTION, true);
+ private static final NotificationGroup NOTIFICATION_GROUP = NotificationGroup.toolWindowGroup("Inspection Results", ToolWindowId.INSPECTION);
private final NotNullLazyValue<ContentManager> myContentManager;
private InspectionResultsView myView = null;
private Content myContent = null;
@@ -641,6 +640,8 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp
final Project project,
final Runnable postRunnable,
final String commandName) {
+ final int fileCount = scope.getFileCount();
+ final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
final List<LocalInspectionToolWrapper> lTools = new ArrayList<LocalInspectionToolWrapper>();
final LinkedHashMap<PsiFile, List<HighlightInfo>> results = new LinkedHashMap<PsiFile, List<HighlightInfo>>();
@@ -655,8 +656,12 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp
range = null;
}
scope.accept(new PsiElementVisitor() {
+ private int myCount = 0;
@Override
public void visitFile(PsiFile file) {
+ if (progressIndicator != null) {
+ progressIndicator.setFraction(((double)++ myCount)/fileCount);
+ }
if (isBinary(file)) return;
for (final Tools tools : profile.getAllEnabledInspectionTools(project)) {
if (tools.getTool().getTool() instanceof CleanupLocalInspectionTool) {
@@ -704,7 +709,9 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
- CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
+ if (commandName != null) {
+ CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
+ }
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
@@ -718,7 +725,7 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp
}, commandName, null);
}
};
- if (ApplicationManager.getApplication().isUnitTestMode()) {
+ if (ApplicationManager.getApplication().isDispatchThread()) {
runnable.run();
} else {
ApplicationManager.getApplication().invokeLater(runnable);