Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug #83 #95

Merged
merged 2 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/main/kotlin/wu/seal/jsontokotlin/ImportClassWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,19 @@ object ImportClassWriter : IImportClassWriter {

if (importClassLineString !in text) {

val packageIndex = text.indexOf("package ")
val importIndex = Math.max(text.lastIndexOf("import"), packageIndex)
val packageIndex = try {
"^[\\s]*package".toRegex(RegexOption.MULTILINE).find(text)!!.range.endInclusive
} catch (e: Exception) {
-1
}
val lastImportKeywordIndex = try {
"^[\\s]*import".toRegex(RegexOption.MULTILINE).findAll(text).last().range.endInclusive
} catch (e: Exception) {
-1
}
val index = Math.max(lastImportKeywordIndex, packageIndex)
val insertIndex =
if (importIndex == -1) 0 else editFile.getLineEndOffset(editFile.getLineNumber(importIndex))

if (index == -1) 0 else editFile.getLineEndOffset(editFile.getLineNumber(index))

executeCouldRollBackAction(project) {
editFile.insertString(insertIndex, "\n" + importClassLineString + "\n")
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/wu/seal/jsontokotlin/MakeKotlinClassAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import wu.seal.jsontokotlin.feedback.sendActionInfo
import wu.seal.jsontokotlin.ui.JsonInputDialog
import wu.seal.jsontokotlin.utils.ClassCodeFilter
import wu.seal.jsontokotlin.utils.executeCouldRollBackAction
import java.awt.SystemColor.text
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import

import java.net.URL
import java.util.*

Expand Down Expand Up @@ -144,6 +145,23 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
if (offset == 0) {
offset = document.textLength
}
val lastPackageKeywordLineEndIndex = try {
"^[\\s]*package.+\n$".toRegex(RegexOption.MULTILINE).findAll(document.text).last().range.endInclusive
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a space after package in the regex in case that other statement starts with package? i.e. "^\\s*package\\s+.+\n$"

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right.

} catch (e: Exception) {
-1
}
val lastImportKeywordLineEndIndex = try {
"^[\\s]*import.+\n$".toRegex(RegexOption.MULTILINE).findAll(document.text).last().range.endInclusive
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with import, "^\\s*import\\s+.+\n$"

} catch (e: Exception) {
-1
}
if (offset < lastPackageKeywordLineEndIndex) {
offset = lastPackageKeywordLineEndIndex + 1
}
if (offset < lastImportKeywordLineEndIndex) {
offset = lastImportKeywordLineEndIndex + 1
}

} else {
offset = document.textLength
}
Expand Down