void (@NotNull final Project project, final Set<GroovyFile> files) { final ProgressManager progressManager = ProgressManager.getInstance(); final Map<GroovyFile, Pair<List<GrImportStatement>, Set<GrImportStatement>>> redundants = new HashMap<>(); final Runnable findUnusedImports = () -> { final ProgressIndicator progressIndicator = progressManager.getProgressIndicator(); if (progressIndicator != null) { progressIndicator.setIndeterminate(false); } final int total = files.size(); int i = 0; for (final GroovyFile file : files) { double fraction = (double)i++ / total; ApplicationManager.getApplication().runReadAction(() -> { if (!file.isValid()) return; final VirtualFile virtualFile = file.getVirtualFile(); if (progressIndicator != null) { progressIndicator.setText2(virtualFile.getPresentableUrl()); progressIndicator.setFraction(fraction); } if (ProjectRootManager.getInstance(project).getFileIndex().isInSource(virtualFile)) { final Set<GrImportStatement> usedImports = usedImports(file); final List<GrImportStatement> validImports = PsiUtil.getValidImportStatements(file); redundants.put(file, Pair.create(validImports, usedImports)); } }); } }; if (!progressManager.runProcessWithProgressSynchronously(findUnusedImports, GroovyBundle.message("optimize.imports.progress.title"), false, project)) { return; } WriteAction.run(() -> { for (GroovyFile groovyFile : redundants.keySet()) { if (!groovyFile.isValid()) continue; final Pair<List<GrImportStatement>, Set<GrImportStatement>> pair = redundants.get(groovyFile); final List<GrImportStatement> validImports = pair.getFirst(); final Set<GrImportStatement> usedImports = pair.getSecond(); for (GrImportStatement importStatement : validImports) { if (!usedImports.contains(importStatement)) { groovyFile.removeImport(importStatement); } } } }); }