Skip to content

Commit 1650628

Browse files
authored
Merge pull request #95 from wuseal/fix/issue/83
Fix bug #83
2 parents 52e765e + 649d98b commit 1650628

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/main/kotlin/wu/seal/jsontokotlin/ImportClassWriter.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,19 @@ object ImportClassWriter : IImportClassWriter {
7878

7979
if (importClassLineString !in text) {
8080

81-
val packageIndex = text.indexOf("package ")
82-
val importIndex = Math.max(text.lastIndexOf("import"), packageIndex)
81+
val packageIndex = try {
82+
"^[\\s]*package\\s.+\n$".toRegex(RegexOption.MULTILINE).find(text)!!.range.endInclusive
83+
} catch (e: Exception) {
84+
-1
85+
}
86+
val lastImportKeywordIndex = try {
87+
"^[\\s]*import\\s.+\n$".toRegex(RegexOption.MULTILINE).findAll(text).last().range.endInclusive
88+
} catch (e: Exception) {
89+
-1
90+
}
91+
val index = Math.max(lastImportKeywordIndex, packageIndex)
8392
val insertIndex =
84-
if (importIndex == -1) 0 else editFile.getLineEndOffset(editFile.getLineNumber(importIndex))
85-
93+
if (index == -1) 0 else editFile.getLineEndOffset(editFile.getLineNumber(index))
8694

8795
executeCouldRollBackAction(project) {
8896
editFile.insertString(insertIndex, "\n" + importClassLineString + "\n")

src/main/kotlin/wu/seal/jsontokotlin/MakeKotlinClassAction.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
144144
if (offset == 0) {
145145
offset = document.textLength
146146
}
147+
val lastPackageKeywordLineEndIndex = try {
148+
"^[\\s]*package\\s.+\n$".toRegex(RegexOption.MULTILINE).findAll(document.text).last().range.endInclusive
149+
} catch (e: Exception) {
150+
-1
151+
}
152+
val lastImportKeywordLineEndIndex = try {
153+
"^[\\s]*import\\s.+\n$".toRegex(RegexOption.MULTILINE).findAll(document.text).last().range.endInclusive
154+
} catch (e: Exception) {
155+
-1
156+
}
157+
if (offset < lastPackageKeywordLineEndIndex) {
158+
offset = lastPackageKeywordLineEndIndex + 1
159+
}
160+
if (offset < lastImportKeywordLineEndIndex) {
161+
offset = lastImportKeywordLineEndIndex + 1
162+
}
163+
147164
} else {
148165
offset = document.textLength
149166
}

0 commit comments

Comments
 (0)