public static void invokeOnScope( final Project project, final Set<PsiMember> members, final AnalysisScope scope, boolean silent) { final Map<PsiMember, List<Match>> duplicates = new HashMap<>(); final int fileCount = scope.getFileCount(); final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator(); if (progressIndicator != null) { progressIndicator.setIndeterminate(false); } final Map<PsiMember, Set<Module>> memberWithModulesMap = new HashMap<>(); for (final PsiMember member : members) { final Module module = ApplicationManager.getApplication() .runReadAction( new Computable<Module>() { @Override public Module compute() { return ModuleUtilCore.findModuleForPsiElement(member); } }); if (module != null) { final HashSet<Module> dependencies = new HashSet<>(); ApplicationManager.getApplication() .runReadAction(() -> ModuleUtilCore.collectModulesDependsOn(module, dependencies)); memberWithModulesMap.put(member, dependencies); } } scope.accept( new PsiRecursiveElementVisitor() { private int myFileCount; @Override public void visitFile(final PsiFile file) { if (progressIndicator != null) { if (progressIndicator.isCanceled()) return; progressIndicator.setFraction(((double) myFileCount++) / fileCount); final VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile != null) { progressIndicator.setText2( ProjectUtil.calcRelativeToProjectPath(virtualFile, project)); } } final Module targetModule = ModuleUtilCore.findModuleForPsiElement(file); if (targetModule == null) return; for (Map.Entry<PsiMember, Set<Module>> entry : memberWithModulesMap.entrySet()) { final Set<Module> dependencies = entry.getValue(); if (dependencies == null || !dependencies.contains(targetModule)) continue; final PsiMember method = entry.getKey(); final List<Match> matchList = hasDuplicates(file, method); for (Iterator<Match> iterator = matchList.iterator(); iterator.hasNext(); ) { Match match = iterator.next(); final PsiElement matchStart = match.getMatchStart(); final PsiElement matchEnd = match.getMatchEnd(); for (PsiMember psiMember : members) { if (PsiTreeUtil.isAncestor(psiMember, matchStart, false) || PsiTreeUtil.isAncestor(psiMember, matchEnd, false)) { iterator.remove(); break; } } } if (!matchList.isEmpty()) { List<Match> matches = duplicates.get(method); if (matches == null) { matches = new ArrayList<>(); duplicates.put(method, matches); } matches.addAll(matchList); } } } }); if (duplicates.isEmpty()) { if (!silent) { final Runnable nothingFoundRunnable = () -> { final String message = RefactoringBundle.message( "idea.has.not.found.any.code.that.can.be.replaced.with.method.call", ApplicationNamesInfo.getInstance().getProductName()); Messages.showInfoMessage(project, message, REFACTORING_NAME); }; if (ApplicationManager.getApplication().isUnitTestMode()) { nothingFoundRunnable.run(); } else { ApplicationManager.getApplication() .invokeLater(nothingFoundRunnable, ModalityState.NON_MODAL); } } } else { replaceDuplicate(project, duplicates, members); } }