fun contract()

in src/main/kotlin/org/arend/codeInsight/ArendImportOptimizer.kt [558:611]


    fun contract(useTypecheckedInstances: Boolean, fileImports: Set<ImportedName>, settings: ArendCustomCodeStyleSettings): Map<ModulePath, Set<ImportedName>> {
        val submaps = subgroups.map { it.contract(useTypecheckedInstances, fileImports, settings) }
        val additionalFiles = mutableMapOf<FilePath, MutableSet<ImportedName>>()
        submaps.forEach {
            it.forEach { (filePath, ids) ->
                additionalFiles.computeIfAbsent(filePath) { HashSet() }.addAll(ids)
            }
        }
        val allInnerIdentifiers = subgroups.flatMapTo(HashSet()) { it.usages.keys }

        for (identifier in allInnerIdentifiers) {
            if (usages.containsKey(identifier)) {
                // the subgroups that open something to bring this identifier will shadow it
                subgroups.forEach {
                    // the 'open' of parent group will be inherited
                    if (it.usages.containsKey(identifier)) {
                        if (settings.OPTIMIZE_IMPORTS_POLICY != OptimizeImportsPolicy.SOFT) it.usages.remove(identifier)
                    }
                }
            }
            if (definitions.contains(identifier.visibleName) || (name == "" && fileImports.contains(identifier))) {
                subgroups.forEach {
                    if (it.usages[identifier] == usages[identifier]) {
                        if (settings.OPTIMIZE_IMPORTS_POLICY != OptimizeImportsPolicy.SOFT) it.usages.remove(identifier)
                    }
                }
                continue
            }
            val paths = subgroups.mapNotNullTo(SmartSet.create()) { it.usages[identifier] }
            usages[identifier]?.let(paths::add)
            if (paths.size > 1) {
                // identifiers with the same name occur with different qualifiers in submodules,
                // therefore they cannot be lifted
                continue
            } else {
                // identifier is unique for each of the submodules, import for it can be lifted
                usages[identifier] = paths.first()
                if (settings.OPTIMIZE_IMPORTS_POLICY != OptimizeImportsPolicy.SOFT) subgroups.forEach { it.usages.remove(identifier) }
            }
        }
        // the order is important. Implicitly used instances are not inherited, so they should be adder after erasing unnecessary usages
        instancesFromCore.addAll(subgroups.flatMap { it.instancesFromCore })
        val instanceSource =
            if (useTypecheckedInstances) instancesFromCore intersect instancesFromScope.toSet() else instancesFromScope
        for (instance in instanceSource) {
            val (importedFile, qualifier) = collectQualifier(instance) ?: continue
            additionalFiles.computeIfAbsent(importedFile) { HashSet() }
                .add(ImportedName(qualifier.firstName ?: instance.refName, null, instance))
            usages[ImportedName(instance.refName, null, instance)] =
                qualifier.takeIf { this@MutableFrame.name == "" || it.toList().isNotEmpty() } ?: importedFile
        }
        subgroups.removeAll { it.usages.isEmpty() && it.subgroups.isEmpty() }
        return additionalFiles
    }