Skip to content

Commit 3e4bb29

Browse files
committed
Release Version 1.6.3
Optimize Reuse kotlin file class name logic
1 parent 7a393ea commit 3e4bb29

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

resources/META-INF/plugin.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<idea-plugin>
22
<id>wu.seal.tool.jsontokotlin</id>
33
<name>JsonToKotlinClass</name>
4-
<version>1.6.2</version>
4+
<version>1.6.3</version>
55
<vendor email="[email protected]" url="https://www.github.com/wuseal">Seal</vendor>
66

77
<description><![CDATA[
@@ -15,8 +15,7 @@
1515
]]></description>
1616

1717
<change-notes><![CDATA[
18-
1.Add support for formatting JSON String </br>
19-
2.Rename tab name `JSON Converter` to `Annotation` </br>
18+
1.Optimize auto input class name logic when only one class name in the kotlin file </br>
2019
]]>
2120
</change-notes>
2221

src/wu/seal/jsontokotlin/JsonToKotlinApplication.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import wu.seal.jsontokotlin.feedback.sendHistoryActionInfo
1010
* Created by Seal.wu on 2017/8/21.
1111
*/
1212

13-
const val PLUGIN_VERSION = "1.6.2"
13+
const val PLUGIN_VERSION = "1.6.3"
1414

1515
class JsonToKotlinApplication : ApplicationComponent {
1616

src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
145145

146146
internal fun getCleanText(editorText: String): String {
147147
val tempCleanText = editorText.substringBeforeLast("class")
148-
val cleanText = if (tempCleanText.trim().endsWith("data")) tempCleanText.removeSuffix("data") else tempCleanText
148+
val cleanText = if (tempCleanText.trim().endsWith("data")) tempCleanText.trim().removeSuffix("data") else tempCleanText
149149
return cleanText
150150
}
151151

@@ -160,11 +160,15 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
160160
var couldGetAndReuseClassNameInCurrentEditFileForInsertCode = false
161161
val removeCommentEditorText = editorText.replace(Regex("/*\\*(.|\n)*\\*/"),"")
162162
.replace(Regex("^(?:\\s*package |\\s*import ).*$",RegexOption.MULTILINE),"")
163-
if (removeCommentEditorText.indexOf("class") == removeCommentEditorText.lastIndexOf("class")
163+
if ((removeCommentEditorText.indexOf("class") == removeCommentEditorText.lastIndexOf("class")
164164
&& removeCommentEditorText.indexOf("class") != -1
165165
&& removeCommentEditorText.substringAfter("class").contains("(").not()
166166
&& removeCommentEditorText.substringAfter("class").contains(":").not()
167-
&& removeCommentEditorText.substringAfter("class").contains("=").not()) {
167+
&& removeCommentEditorText.substringAfter("class").contains("=").not())
168+
||(removeCommentEditorText.indexOf("class") == removeCommentEditorText.lastIndexOf("class")
169+
&& removeCommentEditorText.indexOf("class") != -1
170+
&&removeCommentEditorText.substringAfter("class").substringAfter("(")
171+
.replace(Regex("\\s"),"").let { it.equals(")")||it.equals("){}") })) {
168172
couldGetAndReuseClassNameInCurrentEditFileForInsertCode = true
169173
}
170174
return couldGetAndReuseClassNameInCurrentEditFileForInsertCode

src/wu/seal/jsontokotlin/ui/JsonInputDialog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ val myInputValidator = MyInputValidator()
4949
/**
5050
* Json input Dialog
5151
*/
52-
class JsonInputDialog(private val classsName: String, project: Project) : Messages.InputDialog(project, "Please input the class name and JSON String for generating Kotlin data class", "Make Kotlin Data Class", Messages.getInformationIcon(), "", myInputValidator) {
52+
class JsonInputDialog(private val classsName: String, project: Project) : Messages.InputDialog(project, "Please input the class name and JSON String for generating Kotlin data class", "Make Kotlin Data Class Code", Messages.getInformationIcon(), "", myInputValidator) {
5353

5454
private lateinit var classNameInput: JTextField
5555

test/wu/seal/jsontokotlin/MakeKotlinClassActionTest.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,42 @@ class MakeKotlinClassActionTest {
5252
action.couldGetAndReuseClassNameInCurrentEditFileForInsertCode(emptyString).should.be.`true`
5353
}
5454

55+
@Test
56+
fun couldGetAndReuseClassNameInCurrentEditFileForInsertCodeWithClassBodyOnlyParentheses() {
57+
val action = MakeKotlinClassAction()
58+
val emptyString = """
59+
package wu.seal.jsontokotlin.supporter
60+
class Test()"""
61+
action.couldGetAndReuseClassNameInCurrentEditFileForInsertCode(emptyString).should.be.`true`
62+
}
63+
64+
@Test
65+
fun couldGetAndReuseClassNameInCurrentEditFileForInsertCodeWithClassBodyOnlyParenthesesAndCurlyBraces() {
66+
val action = MakeKotlinClassAction()
67+
val emptyString = """
68+
package wu.seal.jsontokotlin.supporter
69+
class Test(){ }"""
70+
action.couldGetAndReuseClassNameInCurrentEditFileForInsertCode(emptyString).should.be.`true`
71+
}
72+
73+
@Test
74+
fun couldGetAndReuseClassNameInCurrentEditFileForInsertCodeWithClassBodyWithProperty() {
75+
val action = MakeKotlinClassAction()
76+
val emptyString = """
77+
package wu.seal.jsontokotlin.supporter
78+
class Test(val a:String="1")"""
79+
action.couldGetAndReuseClassNameInCurrentEditFileForInsertCode(emptyString).should.be.`false`
80+
}
81+
82+
@Test
83+
fun couldGetAndReuseClassNameInCurrentEditFileForInsertCodeWithClassBodyWithFunction() {
84+
val action = MakeKotlinClassAction()
85+
val emptyString = """
86+
package wu.seal.jsontokotlin.supporter
87+
class Test{fun c(){}}"""
88+
action.couldGetAndReuseClassNameInCurrentEditFileForInsertCode(emptyString).should.be.`false`
89+
}
90+
5591
@Test
5692
fun couldGetAndReuseClassNameInCurrentEditFileForInsertCodeForPackageAndNotOnlyOneClassNameDeclare() {
5793
val action = MakeKotlinClassAction()
@@ -117,6 +153,15 @@ class MakeKotlinClassActionTest {
117153
}
118154

119155

156+
@Test
157+
fun getCurrentEditFileTemClassNameForInsertCodeWithClassBodyOnlyParentheses() {
158+
val action = MakeKotlinClassAction()
159+
val emptyString = """
160+
package wu.seal.jsontokotlin.supporter
161+
class Test()"""
162+
action.getCurrentEditFileTemClassName(emptyString).should.be.equal("Test")
163+
}
164+
120165
@Test
121166
fun getCurrentEditFileTemClassNameForPackageAndOnlyOneClassNameWithCommentContainsClassStringDeclare() {
122167
val action = MakeKotlinClassAction()
@@ -170,6 +215,23 @@ class MakeKotlinClassActionTest {
170215
}
171216

172217

218+
@Test
219+
fun getCleanTextForInsertCodeWithClassBodyOnlyParentheses() {
220+
val action = MakeKotlinClassAction()
221+
val emptyString = """
222+
package wu.seal.jsontokotlin.supporter
223+
class Test()"""
224+
action.getCleanText(emptyString).trim().should.be.equal("package wu.seal.jsontokotlin.supporter")
225+
}
226+
227+
@Test
228+
fun getCleanTextForInsertCodeWithClassBodyOnlyParenthesesAndCurlyBraces() {
229+
val action = MakeKotlinClassAction()
230+
val emptyString = """
231+
package wu.seal.jsontokotlin.supporter
232+
class Test(){ }"""
233+
action.getCleanText(emptyString).trim().should.be.equal("package wu.seal.jsontokotlin.supporter")
234+
}
173235
@Test
174236
fun getCleanTextForPackageAndOnlyOneClassNameWithCommentContainsClassStringDeclare() {
175237
val action = MakeKotlinClassAction()

0 commit comments

Comments
 (0)