File tree Expand file tree Collapse file tree 2 files changed +29
-4
lines changed
src/main/kotlin/wu/seal/jsontokotlin Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Original file line number Diff line number Diff 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 " )
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments