instantiate tools outside read action

This commit is contained in:
Alexey Kudravtsev
2012-05-18 16:09:25 +04:00
parent ab7740ea45
commit 8d9c1b175d
2 changed files with 43 additions and 5 deletions
@@ -26,6 +26,7 @@ import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.ReferenceImporter;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.intention.impl.IntentionHintComponent;
import com.intellij.codeInspection.ex.InspectionProfileWrapper;
import com.intellij.concurrency.Job;
import com.intellij.ide.PowerSaveMode;
import com.intellij.lang.annotation.HighlightSeverity;
@@ -53,6 +54,7 @@ import com.intellij.openapi.util.*;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.packageDependencies.DependencyValidationManager;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.psi.PsiCompiledElement;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
@@ -749,10 +751,31 @@ public class DaemonCodeAnalyzerImpl extends DaemonCodeAnalyzer implements JDOMEx
myPassExecutorService.submitPasses(passes, progress, Job.DEFAULT_PRIORITY);
}
};
if (activeEditor == null) {
runnable.run();
}
else {
final InspectionProfileWrapper profile = InspectionProjectProfileManager.getInstance(myProject).getProfileWrapper();
final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(activeEditor.getDocument());
if (psiFile != null && profile != null && !profile.areToolsInstantiated()) {
// optimization: do expensive classloading outside readaction
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
@Override
public void run() {
try {
if (!psiFile.getManager().isDisposed() && !profile.areToolsInstantiated()) {
profile.preInstantiateTools(psiFile);
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(myProject)).cancelAndRunWhenAllCommitted(
"start daemon when all committed", runnable);
}
@@ -22,8 +22,10 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.Function;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -66,9 +68,9 @@ public class InspectionProfileWrapper {
return enabled;
}
// check whether some inspection got registered twice by accident. Bit only once.
// check whether some inspection got registered twice by accident. 've bit once.
private static boolean alreadyChecked;
private static void checkInspectionsDuplicates(InspectionTool[] tools) {
private static void checkInspectionsDuplicates(@NotNull InspectionTool[] tools) {
if (alreadyChecked) return;
alreadyChecked = true;
Set<InspectionTool> uniqTools = new THashSet<InspectionTool>(tools.length);
@@ -79,6 +81,22 @@ public class InspectionProfileWrapper {
}
}
private volatile boolean toolsInstantiated;
public void preInstantiateTools(PsiFile psiFile) {
if (toolsInstantiated) return;
toolsInstantiated = true;
InspectionTool[] tools = getInspectionTools(psiFile);
for (InspectionTool tool : tools) {
if (tool instanceof InspectionToolWrapper) {
((InspectionToolWrapper)tool).getTool();
}
}
}
public boolean areToolsInstantiated() {
return toolsInstantiated;
}
public String getName() {
return myProfile.getName();
}
@@ -105,13 +123,10 @@ public class InspectionProfileWrapper {
}
public void cleanup(final Project project){
myProfile.cleanup(project);
}
public InspectionProfile getInspectionProfile() {
return myProfile;
}
}