void (@NotNull Collection<ModuleCandidate> modules) { Object2IntMap<File> dirToChildRootCount = new Object2IntOpenHashMap<>(); for (ModuleCandidate module : modules) { walkParents(module.myFolder, this::isEntryPointRoot, file -> { dirToChildRootCount.mergeInt(file, 1, Math::addExact); }); } for (ModuleCandidate module : modules) { File moduleRoot = module.myFolder; Ref<File> adjustedRootRef = new Ref<>(module.myFolder); File current = moduleRoot; while (dirToChildRootCount.getInt(current) == 1) { adjustedRootRef.set(current); if (isEntryPointRoot(current)) break; current = current.getParentFile(); } module.myFolder = adjustedRootRef.get(); } }