NamedScope (NamedScope scope, Map<String, String> nameMapping) { PackageSet oldSet = scope.getValue(); if (oldSet == null) return scope; PackageSet newSet = oldSet.map(packageSet -> { if (packageSet instanceof PatternBasedPackageSet) { String modulePattern = ((PatternBasedPackageSet)packageSet).getModulePattern(); String newName = nameMapping.get(modulePattern); if (newName != null) { return ((PatternBasedPackageSet)packageSet).updateModulePattern(modulePattern, newName); } } return packageSet; }); if (newSet != oldSet) { String presentableName = scope.getPresentableName(); return new NamedScope(scope.getScopeId(), () -> presentableName, scope.getIcon(), newSet); } return scope; }