override fun hasDuplicatedImportsHelper()

in plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/importadder/CodeWhispererPythonImportAdder.kt [35:64]


    override fun hasDuplicatedImportsHelper(newImport: PsiElement, existingImports: List<PsiElement>): PsiElement? {
        if (newImport !is PyImportStatementBase) return newImport

        newImport.importElements.map { it.importedQName }.forEachIndexed outer@{ newI, newImportedQName ->
            if (newImportedQName == null) return@outer
            existingImports.forEach { existingImport ->
                if (existingImport !is PyImportStatementBase) return@outer
                if (existingImport::class.java != newImport::class.java) return@outer
                existingImport.importElements.map { it.importedQName }.forEachIndexed inner@{ existingI, existingImportedQName ->
                    if (existingImportedQName == null) return@inner
                    val existingImportAsName = existingImport.importElements[existingI].asName
                    val newImportAsName = newImport.importElements[newI].asName
                    if (existingImportAsName != newImportAsName) return@inner

                    val newFullyQName = QualifiedName.fromDottedString(newImport.fullyQualifiedObjectNames[newI])
                    val existingFullyQName = QualifiedName.fromDottedString(existingImport.fullyQualifiedObjectNames[existingI])
                    if (newImport is PyImportStatement) {
                        if (newImportAsName != null) {
                            if (existingFullyQName == newFullyQName) return existingImport
                        } else {
                            if (existingFullyQName.matchesPrefix(newFullyQName)) return existingImport
                        }
                    } else if (newImport is PyFromImportStatement) {
                        if (newImportedQName == existingImportedQName && newFullyQName == existingFullyQName) return existingImport
                    }
                }
            }
        }
        return null
    }