void (@NotNull Module module, @NotNull Function<Pair<Module, Collection<VirtualFile>>, ? extends STATE> detector) { final String extension = PythonFileType.INSTANCE.getDefaultExtension(); // Module#getModuleScope() and GlobalSearchScope#getModuleScope() search only in source roots Project project = module.getProject(); final GlobalSearchScope searchScope = new ModulesScope(Collections.singleton(module), project); Collection<VirtualFile> pyFiles = ReadAction.nonBlocking(() -> FilenameIndex .getAllFilesByExt(project, extension, searchScope)) .inSmartMode(project) .executeSynchronously(); STATE state = detector.fun(Pair.create(module, pyFiles)); if (state != null) { myClasses.getModuleService(module).loadState(state); } else { ApplicationManager.getApplication().invokeAndWait(() -> myClasses.copyFromAppToModule(module)); } }