public SequentialCleanupTask(Project project, LinkedHashMap<PsiFile, List<HighlightInfo>> results, SequentialModalProgressTask task) { myProject = project; for (Map.Entry<PsiFile, List<HighlightInfo>> entry : results.entrySet()) { PsiFile file = entry.getKey(); List<HighlightInfo> infos = entry.getValue(); // sort from bottom to top Collections.sort(infos, new Comparator<HighlightInfo>() { @Override public int compare(HighlightInfo info1, HighlightInfo info2) { return info2.getStartOffset() - info1.getStartOffset(); } }); for (HighlightInfo info : infos) { myResults.add(Pair.create(file, info)); } } myProgressTask = task; }
public static AbstractPerformFixesTask applyFixesNoSort(@Nonnull Project project, @Nonnull String presentationText, @Nonnull List<ProblemDescriptor> descriptions, @Nullable Class quickfixClass) { final boolean isBatch = quickfixClass != null && BatchQuickFix.class.isAssignableFrom(quickfixClass); final AbstractPerformFixesTask fixesTask = isBatch ? new PerformBatchFixesTask(project, descriptions.toArray(ProblemDescriptor.EMPTY_ARRAY), quickfixClass) : new PerformFixesTask(project, descriptions.toArray(ProblemDescriptor.EMPTY_ARRAY), quickfixClass); final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, presentationText, true); CommandProcessor.getInstance().executeCommand(project, () -> { CommandProcessor.getInstance().markCurrentCommandAsGlobal(project); progressTask.setMinIterationTime(200); progressTask.setTask(fixesTask); ProgressManager.getInstance().run(progressTask); }, presentationText, null); return fixesTask; }
private static Runnable applyRunnable(final Project project, final Computable<UsageInfo[]> computable) { return new Runnable() { @Override public void run() { final LocalHistoryAction action = LocalHistory.getInstance().startAction(INFER_NULLITY_ANNOTATIONS); try { new WriteCommandAction(project, INFER_NULLITY_ANNOTATIONS) { @Override protected void run(@NotNull Result result) throws Throwable { final UsageInfo[] infos = computable.compute(); if (infos.length > 0) { final Set<PsiElement> elements = new LinkedHashSet<PsiElement>(); for (UsageInfo info : infos) { final PsiElement element = info.getElement(); if (element != null) { ContainerUtil.addIfNotNull(elements, element.getContainingFile()); } } if (!FileModificationService.getInstance().preparePsiElementsForWrite(elements)) return; final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, INFER_NULLITY_ANNOTATIONS, false); progressTask.setMinIterationTime(200); progressTask.setTask(new AnnotateTask(project, progressTask, infos)); ProgressManager.getInstance().run(progressTask); } else { NullityInferrer.nothingFoundMessage(project); } } }.execute(); } finally { action.finish(); } } }; }
public AnnotateTask(Project project, SequentialModalProgressTask progressTask, UsageInfo[] infos) { myProject = project; myInfos = infos; myNotNullManager = NullableNotNullManager.getInstance(myProject); myTask = progressTask; myTotal = infos.length; }
public PerformFixesTask(@NotNull Project project, @NotNull CommonProblemDescriptor[] descriptors, @NotNull Set<PsiElement> ignoredElements, @NotNull SequentialModalProgressTask task, @NotNull GlobalInspectionContextImpl context) { super(project, descriptors, task); myContext = context; myIgnoredElements = ignoredElements; }
public PerformFixesModalTask(@NotNull Project project, @NotNull CommonProblemDescriptor[] descriptors, @NotNull SequentialModalProgressTask task) { myProject = project; myDescriptors = descriptors; myTask = task; myDocumentManager = PsiDocumentManager.getInstance(myProject); }
@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(); } }); final String templatePresentationText = "Apply Fixes"; final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, templatePresentationText, true); final PerformFixesTask fixesTask = new PerformFixesTask(project, descriptions.toArray(new CommonProblemDescriptor[descriptions.size()]), progressTask); CommandProcessor.getInstance().executeCommand(project, new Runnable() { @Override public void run() { CommandProcessor.getInstance().markCurrentCommandAsGlobal(project); progressTask.setMinIterationTime(200); progressTask.setTask(fixesTask); ProgressManager.getInstance().run(progressTask); } }, templatePresentationText, null); if (!fixesTask.isApplicableFixFound()) { HintManager.getInstance().showErrorHint(editor, "Unfortunately '" + myText + "' is currently not available for batch mode"); } }
@Nullable private Runnable createIterativeFileProcessor(@NotNull final FileTreeIterator fileIterator) { return new Runnable() { @Override public void run() { SequentialModalProgressTask progressTask = new SequentialModalProgressTask(myProject, myCommandName); progressTask.setMinIterationTime(100); ReformatFilesTask reformatFilesTask = new ReformatFilesTask(fileIterator); reformatFilesTask.setCompositeTask(progressTask); progressTask.setTask(reformatFilesTask); ProgressManager.getInstance().run(progressTask); } }; }
public AnnotateTask(Project project, NullityInferrer inferrer, SequentialModalProgressTask progressTask) { myProject = project; myNotNullManager = NullableNotNullManager.getInstance(myProject); myInferrer = inferrer; myTask = progressTask; myTotal = myInferrer.getCount(); }
public PerformFixesTask(@NotNull Project project, @NotNull CommonProblemDescriptor[] descriptors, @NotNull Set<PsiElement> ignoredElements, @NotNull SequentialModalProgressTask task, @NotNull GlobalInspectionContextImpl context) { myProject = project; myDescriptors = descriptors; myIgnoredElements = ignoredElements; myTask = task; myContext = context; }
public PerformFixesTask(@Nonnull Project project, @Nonnull CommonProblemDescriptor[] descriptors, @Nonnull Set<PsiElement> ignoredElements, @Nonnull SequentialModalProgressTask task, @Nonnull GlobalInspectionContextImpl context) { myProject = project; myDescriptors = descriptors; myIgnoredElements = ignoredElements; myTask = task; myContext = context; }
@Override public void performOperation(final Project project, final Set<PsiJavaFile> javaFiles) { CodeStyleManager.getInstance(project).performActionWithFormatterDisabled(new Runnable() { @Override public void run() { PsiDocumentManager.getInstance(project).commitAllDocuments(); } }); final Set<SmartPsiElementPointer<PsiImportStatementBase>> redundants = new HashSet<SmartPsiElementPointer<PsiImportStatementBase>>(); final Runnable findRedundantImports = new Runnable() { @Override public void run() { ApplicationManager.getApplication().runReadAction(new Runnable() { @Override public void run() { final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project); final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator(); final SmartPointerManager pointerManager = SmartPointerManager.getInstance(project); int i = 0; final int fileCount = javaFiles.size(); for (PsiJavaFile file : javaFiles) { if (file.isValid()) { final VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile != null) { if (progressIndicator != null) { progressIndicator.setText2(virtualFile.getPresentableUrl()); progressIndicator.setFraction((double)i++ / fileCount); } final Collection<PsiImportStatementBase> perFile = styleManager.findRedundantImports(file); if (perFile != null) { for (PsiImportStatementBase redundant : perFile) { redundants.add(pointerManager.createSmartPsiElementPointer(redundant)); } } } } } } }); } }; if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(findRedundantImports, REMOVING_REDUNDANT_IMPORTS_TITLE, false, project)) return; ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, REMOVING_REDUNDANT_IMPORTS_TITLE, false); progressTask.setMinIterationTime(200); progressTask.setTask(new OptimizeImportsTask(progressTask, redundants)); ProgressManager.getInstance().run(progressTask); } }); }
public OptimizeImportsTask(SequentialModalProgressTask progressTask, Set<SmartPsiElementPointer<PsiImportStatementBase>> pointers) { myTask = progressTask; myTotal = pointers.size(); myPointers = pointers.iterator(); }
public PerformFixesTask(@NotNull Project project, @NotNull CommonProblemDescriptor[] descriptors, @NotNull SequentialModalProgressTask task) { super(project, descriptors, task); }
public void setCompositeTask(@Nullable SequentialModalProgressTask compositeTask) { myCompositeTask = compositeTask; }
public CompositeSequentialTask(@NotNull SequentialModalProgressTask progressTask) { myProgressTask = progressTask; }
@Override protected void analyze(@NotNull final Project project, @NotNull final AnalysisScope scope) { PropertiesComponent.getInstance().setValue(UPDATE_EXISTING_COPYRIGHTS, String.valueOf(myUpdateExistingCopyrightsCb.isSelected()), "true"); final Map<PsiFile, Runnable> preparations = new LinkedHashMap<PsiFile, Runnable>(); Task.Backgroundable task = new Task.Backgroundable(project, "Prepare Copyright...", true) { @Override public void run(@NotNull final ProgressIndicator indicator) { scope.accept(new PsiElementVisitor() { @Override public void visitFile(final PsiFile file) { if (indicator.isCanceled()) { return; } final Module module = ModuleUtilCore.findModuleForPsiElement(file); final UpdateCopyrightProcessor processor = new UpdateCopyrightProcessor(project, module, file); final Runnable runnable = processor.preprocessFile(file, myUpdateExistingCopyrightsCb.isSelected()); if (runnable != EmptyRunnable.getInstance()) { preparations.put(file, runnable); } } }); } @Override public void onSuccess() { if (!preparations.isEmpty()) { if (!FileModificationService.getInstance().preparePsiElementsForWrite(preparations.keySet())) return; final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, UpdateCopyrightProcessor.TITLE, true); progressTask.setMinIterationTime(200); progressTask.setTask(new UpdateCopyrightSequentialTask(preparations, progressTask)); CommandProcessor.getInstance().executeCommand(project, new Runnable() { @Override public void run() { CommandProcessor.getInstance().markCurrentCommandAsGlobal(project); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { ProgressManager.getInstance().run(progressTask); } }); } }, getTemplatePresentation().getText(), null); } } }; ProgressManager.getInstance().run(task); }
private UpdateCopyrightSequentialTask(Map<PsiFile, Runnable> runnables, SequentialModalProgressTask progressTask) { myRunnables = runnables.values().iterator(); myProgressTask = progressTask; mySize = runnables.size(); }
private void doApplyFix(@NotNull final Project project, @NotNull final CommonProblemDescriptor[] descriptors, @NotNull final GlobalInspectionContextImpl context) { final Set<VirtualFile> readOnlyFiles = new THashSet<VirtualFile>(); for (CommonProblemDescriptor descriptor : descriptors) { final PsiElement psiElement = descriptor instanceof ProblemDescriptor ? ((ProblemDescriptor)descriptor).getPsiElement() : null; if (psiElement != null && !psiElement.isWritable()) { readOnlyFiles.add(psiElement.getContainingFile().getVirtualFile()); } } 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<PsiElement>(); CommandProcessor.getInstance().executeCommand(project, new Runnable() { @Override public void run() { CommandProcessor.getInstance().markCurrentCommandAsGlobal(project); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, getTemplatePresentation().getText(), false); progressTask.setMinIterationTime(200); progressTask.setTask(new PerformFixesTask(project, descriptors, ignoredElements, progressTask, context)); ProgressManager.getInstance().run(progressTask); } }); } }, getTemplatePresentation().getText(), null); refreshViews(project, ignoredElements, myToolWrapper); } finally { //to make offline view lazy if (initial) refManager.inspectionReadActionStarted(); } }
@Nullable private Runnable preprocessFiles(List<PsiFile> files) { ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator(); String oldText = null; double oldFraction = 0; if (progress != null){ oldText = progress.getText(); oldFraction = progress.getFraction(); progress.setText(myProgressText); } final List<FutureTask<Boolean>> tasks = new ArrayList<FutureTask<Boolean>>(files.size()); for(int i = 0; i < files.size(); i++) { PsiFile file = files.get(i); if (progress != null){ if (progress.isCanceled()) return null; progress.setFraction((double)i / files.size()); } if (file.isWritable()){ try{ tasks.add(preprocessFile(file, myProcessChangedTextOnly)); } catch(IncorrectOperationException e){ LOG.error(e); } } files.set(i, null); } if (progress != null){ progress.setText(oldText); progress.setFraction(oldFraction); } return new Runnable() { @Override public void run() { SequentialModalProgressTask progressTask = new SequentialModalProgressTask(myProject, myCommandName); ReformatFilesTask reformatFilesTask = new ReformatFilesTask(tasks); reformatFilesTask.setCompositeTask(progressTask); progressTask.setTask(reformatFilesTask); ProgressManager.getInstance().run(progressTask); } }; }
private void doApplyFix(@Nonnull final Project project, @Nonnull final CommonProblemDescriptor[] descriptors, @Nonnull final GlobalInspectionContextImpl context) { final Set<VirtualFile> readOnlyFiles = new THashSet<VirtualFile>(); for (CommonProblemDescriptor descriptor : descriptors) { final PsiElement psiElement = descriptor instanceof ProblemDescriptor ? ((ProblemDescriptor)descriptor).getPsiElement() : null; if (psiElement != null && !psiElement.isWritable()) { readOnlyFiles.add(psiElement.getContainingFile().getVirtualFile()); } } 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<PsiElement>(); CommandProcessor.getInstance().executeCommand(project, new Runnable() { @Override public void run() { CommandProcessor.getInstance().markCurrentCommandAsGlobal(project); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, getTemplatePresentation().getText(), false); progressTask.setMinIterationTime(200); progressTask.setTask(new PerformFixesTask(project, descriptors, ignoredElements, progressTask, context)); ProgressManager.getInstance().run(progressTask); } }); } }, getTemplatePresentation().getText(), null); refreshViews(project, ignoredElements, myToolWrapper); } finally { //to make offline view lazy if (initial) refManager.inspectionReadActionStarted(); } }
public SequentialCleanupTask(Project project, LinkedHashMap<PsiFile, List<HighlightInfo>> results, SequentialModalProgressTask task) { myProject = project; myResults = results; myProgressTask = task; myFileIterator = myResults.keySet().iterator(); }
private static Runnable applyRunnable(final Project project, final Computable<UsageInfo[]> computable) { return () -> { final LocalHistoryAction action = LocalHistory.getInstance().startAction(INFER_NULLITY_ANNOTATIONS); try { new WriteCommandAction(project, INFER_NULLITY_ANNOTATIONS) { @Override protected void run(@NotNull Result result) throws Throwable { final UsageInfo[] infos = computable.compute(); if(infos.length > 0) { final Set<PsiElement> elements = new LinkedHashSet<>(); for(UsageInfo info : infos) { final PsiElement element = info.getElement(); if(element != null) { ContainerUtil.addIfNotNull(elements, element.getContainingFile()); } } if(!FileModificationService.getInstance().preparePsiElementsForWrite(elements)) { return; } final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, INFER_NULLITY_ANNOTATIONS, false); progressTask.setMinIterationTime(200); progressTask.setTask(new AnnotateTask(project, progressTask, infos)); ProgressManager.getInstance().run(progressTask); } else { NullityInferrer.nothingFoundMessage(project); } } }.execute(); } finally { action.finish(); } }; }