use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.
the class GlobalInspectionContextImpl method cleanup.
private void cleanup(@NotNull final AnalysisScope scope, @NotNull InspectionProfile profile, @Nullable final Runnable postRunnable, @Nullable final String commandName) {
setCurrentScope(scope);
final int fileCount = scope.getFileCount();
final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
final SearchScope searchScope = ReadAction.compute(scope::toSearchScope);
final TextRange range;
if (searchScope instanceof LocalSearchScope) {
final PsiElement[] elements = ((LocalSearchScope) searchScope).getScope();
range = elements.length == 1 ? ReadAction.compute(elements[0]::getTextRange) : null;
} else {
range = null;
}
final Iterable<Tools> inspectionTools = ContainerUtil.filter(profile.getAllEnabledInspectionTools(getProject()), tools -> {
assert tools != null;
return tools.getTool().getTool() instanceof CleanupLocalInspectionTool;
});
boolean includeDoNotShow = includeDoNotShow(profile);
final RefManagerImpl refManager = (RefManagerImpl) getRefManager();
refManager.inspectionReadActionStarted();
List<ProblemDescriptor> descriptors = new ArrayList<>();
Set<PsiFile> files = new HashSet<>();
try {
scope.accept(new PsiElementVisitor() {
private int myCount;
@Override
public void visitFile(PsiFile file) {
if (progressIndicator != null) {
progressIndicator.setFraction((double) ++myCount / fileCount);
}
if (isBinary(file))
return;
final List<LocalInspectionToolWrapper> lTools = new ArrayList<>();
for (final Tools tools : inspectionTools) {
final InspectionToolWrapper tool = tools.getEnabledTool(file, includeDoNotShow);
if (tool instanceof LocalInspectionToolWrapper) {
lTools.add((LocalInspectionToolWrapper) tool);
tool.initialize(GlobalInspectionContextImpl.this);
}
}
if (!lTools.isEmpty()) {
try {
final LocalInspectionsPass pass = new LocalInspectionsPass(file, PsiDocumentManager.getInstance(getProject()).getDocument(file), range != null ? range.getStartOffset() : 0, range != null ? range.getEndOffset() : file.getTextLength(), LocalInspectionsPass.EMPTY_PRIORITY_RANGE, true, HighlightInfoProcessor.getEmpty());
Runnable runnable = () -> pass.doInspectInBatch(GlobalInspectionContextImpl.this, InspectionManager.getInstance(getProject()), lTools);
ApplicationManager.getApplication().runReadAction(runnable);
final Set<ProblemDescriptor> localDescriptors = new TreeSet<>(CommonProblemDescriptor.DESCRIPTOR_COMPARATOR);
for (LocalInspectionToolWrapper tool : lTools) {
InspectionToolPresentation toolPresentation = getPresentation(tool);
for (CommonProblemDescriptor descriptor : toolPresentation.getProblemDescriptors()) {
if (descriptor instanceof ProblemDescriptor) {
localDescriptors.add((ProblemDescriptor) descriptor);
}
}
}
if (searchScope instanceof LocalSearchScope) {
for (Iterator<ProblemDescriptor> iterator = localDescriptors.iterator(); iterator.hasNext(); ) {
final ProblemDescriptor descriptor = iterator.next();
final TextRange infoRange = descriptor instanceof ProblemDescriptorBase ? ((ProblemDescriptorBase) descriptor).getTextRange() : null;
if (infoRange != null && !((LocalSearchScope) searchScope).containsRange(file, infoRange)) {
iterator.remove();
}
}
}
if (!localDescriptors.isEmpty()) {
descriptors.addAll(localDescriptors);
files.add(file);
}
} finally {
myPresentationMap.clear();
}
}
}
});
} finally {
refManager.inspectionReadActionFinished();
}
if (files.isEmpty()) {
GuiUtils.invokeLaterIfNeeded(() -> {
if (commandName != null) {
NOTIFICATION_GROUP.createNotification(InspectionsBundle.message("inspection.no.problems.message", scope.getFileCount(), scope.getDisplayName()), MessageType.INFO).notify(getProject());
}
if (postRunnable != null) {
postRunnable.run();
}
}, ModalityState.defaultModalityState());
return;
}
Runnable runnable = () -> {
if (!FileModificationService.getInstance().preparePsiElementsForWrite(files))
return;
CleanupInspectionIntention.applyFixesNoSort(getProject(), "Code Cleanup", descriptors, null);
if (postRunnable != null) {
postRunnable.run();
}
};
TransactionGuard.submitTransaction(getProject(), runnable);
}
use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.
the class ViewOfflineResultsAction method showOfflineView.
@NotNull
public static InspectionResultsView showOfflineView(@NotNull Project project, @NotNull Map<String, Map<String, Set<OfflineProblemDescriptor>>> resMap, @NotNull InspectionProfileImpl inspectionProfile, @NotNull String title) {
final AnalysisScope scope = new AnalysisScope(project);
final InspectionManagerEx managerEx = (InspectionManagerEx) InspectionManager.getInstance(project);
final GlobalInspectionContextImpl context = managerEx.createNewGlobalContext(false);
context.setExternalProfile(inspectionProfile);
context.setCurrentScope(scope);
context.initializeTools(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
final InspectionResultsView view = new InspectionResultsView(context, new OfflineInspectionRVContentProvider(resMap, project));
((RefManagerImpl) context.getRefManager()).startOfflineView();
context.addView(view, title, true);
view.update();
return view;
}
use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.
the class GlobalInspectionContextImpl method runGlobalTools.
private void runGlobalTools(@NotNull final AnalysisScope scope, @NotNull final InspectionManager inspectionManager, @NotNull List<Tools> globalTools, boolean isOfflineInspections) {
LOG.assertTrue(!ApplicationManager.getApplication().isReadAccessAllowed() || isOfflineInspections, "Must not run under read action, too unresponsive");
final List<InspectionToolWrapper> needRepeatSearchRequest = new ArrayList<>();
final boolean canBeExternalUsages = !(scope.getScopeType() == AnalysisScope.PROJECT && scope.isIncludeTestSource());
for (Tools tools : globalTools) {
for (ScopeToolState state : tools.getTools()) {
final InspectionToolWrapper toolWrapper = state.getTool();
final GlobalInspectionTool tool = (GlobalInspectionTool) toolWrapper.getTool();
final InspectionToolPresentation toolPresentation = getPresentation(toolWrapper);
try {
if (tool.isGraphNeeded()) {
try {
((RefManagerImpl) getRefManager()).findAllDeclarations();
} catch (Throwable e) {
getStdJobDescriptors().BUILD_GRAPH.setDoneAmount(0);
throw e;
}
}
ApplicationManager.getApplication().runReadAction(() -> {
tool.runInspection(scope, inspectionManager, this, toolPresentation);
//skip phase when we are sure that scope already contains everything, unused declaration though needs to proceed with its suspicious code
if ((canBeExternalUsages || tool.getAdditionalJobs(this) != null) && tool.queryExternalUsagesRequests(inspectionManager, this, toolPresentation)) {
needRepeatSearchRequest.add(toolWrapper);
}
});
} catch (ProcessCanceledException | IndexNotReadyException e) {
throw e;
} catch (Throwable e) {
LOG.error(e);
}
}
}
for (GlobalInspectionContextExtension extension : myExtensions.values()) {
try {
extension.performPostRunActivities(needRepeatSearchRequest, this);
} catch (ProcessCanceledException | IndexNotReadyException e) {
throw e;
} catch (Throwable e) {
LOG.error(e);
}
}
addProblemsToView(globalTools);
}
use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.
the class QuickFixAction method doApplyFix.
private void doApplyFix(@NotNull final Project project, @NotNull final CommonProblemDescriptor[] descriptors, @NotNull Set<VirtualFile> readOnlyFiles, @NotNull final GlobalInspectionContextImpl context) {
if (!FileModificationService.getInstance().prepareVirtualFilesForWrite(project, readOnlyFiles))
return;
final RefManagerImpl refManager = (RefManagerImpl) context.getRefManager();
final boolean initial = refManager.isInProcess();
refManager.inspectionReadActionFinished();
try {
final Set<PsiElement> ignoredElements = new HashSet<>();
performFixesInBatch(project, descriptors, context, ignoredElements);
refreshViews(project, ignoredElements, myToolWrapper);
} finally {
//to make offline view lazy
if (initial)
refManager.inspectionReadActionStarted();
}
}
use of com.intellij.codeInspection.reference.RefManagerImpl in project intellij-community by JetBrains.
the class QuickFixAction method doApplyFix.
private void doApplyFix(@NotNull final RefEntity[] refElements, @NotNull InspectionResultsView view) {
final RefManagerImpl refManager = (RefManagerImpl) view.getGlobalInspectionContext().getRefManager();
final boolean initial = refManager.isInProcess();
refManager.inspectionReadActionFinished();
try {
final boolean[] refreshNeeded = { false };
if (refElements.length > 0) {
final Project project = refElements[0].getRefManager().getProject();
CommandProcessor.getInstance().executeCommand(project, () -> {
CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
ApplicationManager.getApplication().runWriteAction(() -> {
refreshNeeded[0] = applyFix(refElements);
});
}, getTemplatePresentation().getText(), null);
}
if (refreshNeeded[0]) {
refreshViews(view.getProject(), refElements, myToolWrapper);
}
} finally {
//to make offline view lazy
if (initial)
refManager.inspectionReadActionStarted();
}
}
Aggregations