Repository: square/kotlinpoet
Issue: Import aliases for conflicting imports can mess up type names for members

**Describe the bug**

When import aliases are generated for conflicting types, they seem to overwrite usages of members of the original types that are aliased. This means that if there are two conflicting usages of `Message` classes (`packageA.first.Message` and `packageB.second.Message`), we get two `FirstMessage` and `SecondMessage` import aliases. However, if the uses of `Message` look like `packageA.first.Message.Builder` and `packageB.second.Message.Builder`, these uses get rewritten as `FirstMessage` and `SecondMessage` and the reference to `.Builder` is lost.

**To Reproduce**

```
  @Test fun conflictingImportsWithMember() {
    val source = FileSpec.builder("com.squareup.tacos", "Taco")
      .addType(
        TypeSpec.classBuilder("Taco")
          .addProperty("madeFreshDate", ClassName("java.util", "Date", "Builder"))
          .addProperty("madeFreshDatabaseDate", ClassName("java.sql", "Date", "Builder"))
          .build(),
      )
      .build()
    assertThat(source.toString()).isEqualTo(
      """
        |package com.squareup.tacos
        |
        |import java.sql.Date as SqlDate
        |import java.util.Date as UtilDate
        |
        |public class Taco {
        |  public val madeFreshDate: UtilDate.Builder
        |
        |  public val madeFreshDatabaseDate: SqlDate.Builder
        |}
        |
      """.trimMargin(),
    )
  }
```

Resuts:
```
expected:
    …eFreshDate: UtilDate.Builder
    
      public val madeFreshDatabaseDate: SqlDate.Builder
    }
    
but was:
    …eFreshDate: UtilDate
    
      public val madeFreshDatabaseDate: SqlDate
    }
```

**Expected behavior**

The alias should rewrite the conflicting use but not any subsequent context.

**Additional context**
This was exposed by code in the Wire project.

Changed files:
kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt, kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt

diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt
index 2d9e5239..f04c7488 100644
--- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt
+++ b/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt
@@ -428,13 +428,14 @@ internal class CodeWriter constructor(
 
       // We don't care about nullability and type annotations here, as it's irrelevant for imports.
       if (resolved == c.copy(nullable = false, annotations = emptyList())) {
-        if (alias != null) return alias
-        val suffixOffset = c.simpleNames.size - 1
-        referencedNames.add(className.topLevelClassName().simpleName)
-        return className.simpleNames.subList(
-          suffixOffset,
+        if (alias == null) {
+          referencedNames.add(className.topLevelClassName().simpleName)
+        }
+        val nestedClassNames = className.simpleNames.subList(
+          c.simpleNames.size,
           className.simpleNames.size,
         ).joinToString(".")
+        return "$simpleName.$nestedClassNames"
       }
       c = c.enclosingClassName()
     }
diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt b/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt
index 2ba9052a..642b65c5 100644
--- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt
+++ b/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt
@@ -1382,4 +1382,60 @@ class KotlinPoetTest {
       """.trimMargin(),
     )
   }
+
+  // https://github.com/square/kotlinpoet/issues/1563
+  @Test fun nestedClassesWithConflictingAutoGeneratedImports() {
+    val source = FileSpec.builder("com.squareup.tacos", "Taco")
+      .addType(
+        TypeSpec.classBuilder("Taco")
+          .addProperty("madeFreshDate", ClassName("java.util", "Date", "Builder"))
+          .addProperty("madeFreshDatabaseDate", ClassName("java.sql", "Date", "Builder"))
+          .build(),
+      )
+      .build()
+    assertThat(source.toString()).isEqualTo(
+      """
+        |package com.squareup.tacos
+        |
+        |import java.sql.Date as SqlDate
+        |import java.util.Date as UtilDate
+        |
+        |public class Taco {
+        |  public val madeFreshDate: UtilDate.Builder
+        |
+        |  public val madeFreshDatabaseDate: SqlDate.Builder
+        |}
+        |
+      """.trimMargin(),
+    )
+  }
+
+  // https://github.com/square/kotlinpoet/issues/1563
+  @Test fun nestedClassesWithConflictingManuallySuppliedImports() {
+    val source = FileSpec.builder("com.squareup.tacos", "Taco")
+      .addAliasedImport(ClassName("java.util", "Date"), "UtilDate")
+      .addAliasedImport(ClassName("java.sql", "Date"), "SqlDate")
+      .addType(
+        TypeSpec.classBuilder("Taco")
+          .addProperty("madeFreshDate", ClassName("java.util", "Date", "Builder"))
+          .addProperty("madeFreshDatabaseDate", ClassName("java.sql", "Date", "Builder"))
+          .build(),
+      )
+      .build()
+    assertThat(source.toString()).isEqualTo(
+      """
+        |package com.squareup.tacos
+        |
+        |import java.sql.Date as SqlDate
+        |import java.util.Date as UtilDate
+        |
+        |public class Taco {
+        |  public val madeFreshDate: UtilDate.Builder
+        |
+        |  public val madeFreshDatabaseDate: SqlDate.Builder
+        |}
+        |
+      """.trimMargin(),
+    )
+  }
 }