Skip to content

Commit cd5d6cd

Browse files
committed
Fix bug in generateSingleKotlinClassFile where duplicate filename check was using wrong variable
1 parent 0b0ed5b commit cd5d6cd

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/main/kotlin/wu/seal/jsontokotlin/utils/KotlinClassFileGenerator.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class KotlinClassFileGenerator {
1919
) {
2020
val fileNamesWithoutSuffix = currentDirExistsFileNamesWithoutKTSuffix(directory)
2121
var kotlinClassForGenerateFile = kotlinClass
22-
while (fileNamesWithoutSuffix.contains(kotlinClass.name)) {
22+
while (fileNamesWithoutSuffix.contains(kotlinClassForGenerateFile.name)) {
2323
kotlinClassForGenerateFile =
2424
kotlinClassForGenerateFile.rename(newName = kotlinClassForGenerateFile.name + "X")
2525
}
@@ -110,11 +110,21 @@ class KotlinClassFileGenerator {
110110
}
111111
append(classCodeContent)
112112
}
113+
114+
var finalFileName = fileName.trim('`')
115+
var ktFileName = "$finalFileName.kt"
116+
117+
// Check if file already exists and rename it by adding 'X' suffix if needed
118+
val fileNamesWithoutSuffix = currentDirExistsFileNamesWithoutKTSuffix(directory)
119+
while (fileNamesWithoutSuffix.contains(finalFileName)) {
120+
finalFileName += "X"
121+
ktFileName = "$finalFileName.kt"
122+
}
123+
124+
// Create the file with the potentially modified name
113125
executeCouldRollBackAction(project) {
114-
val file =
115-
psiFileFactory.createFileFromText("${fileName.trim('`')}.kt", KotlinFileType(), kotlinFileContent)
126+
val file = psiFileFactory.createFileFromText(ktFileName, KotlinFileType(), kotlinFileContent)
116127
directory.add(file)
117128
}
118129
}
119-
120130
}

0 commit comments

Comments
 (0)