Skip to content

Commit f310716

Browse files
author
seal
committed
1.support for LoganSquare and Moshi.
2.support for cutomize annotation. 3.simply usage steps.
1 parent 1571092 commit f310716

33 files changed

+975
-413
lines changed

resources/META-INF/plugin.xml

Lines changed: 4 additions & 19 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.4.1</version>
4+
<version>1.5</version>
55
<vendor email="[email protected]" url="https://www.github.com/wuseal">Seal</vendor>
66

77
<description><![CDATA[
@@ -11,24 +11,9 @@
1111
]]></description>
1212

1313
<change-notes><![CDATA[
14-
1.4.1:<br>
15-
* Fix an issue about Fastjson property name [#ISSUE9](https://github.com/wuseal/JsonToKotlinClass/issues/9)<br>
16-
* Fix an issue about using in none network condition it will be stuck [#ISSUE10](https://github.com/wuseal/JsonToKotlinClass/issues/10)<br>
17-
1.4:<br>
18-
* Add supporter for Jackson annotation generate,supporter json lib --Jackson.<br>
19-
* Add supporter for Fastjson annotation generate,supporter json lib --Fastjson.<br>
20-
* Beautify the config settings dialog.<br>
21-
1.3:<br>
22-
* add property init with default value option in property tab.<br>
23-
* add property could be nullable option in property tab.<br>
24-
* fix a bug when the property name is 'list' and it's type is array then the plugin will broken.<br>
25-
* beautify dialog layout.<br>
26-
1.2.1:<br>
27-
* Fix insert improt class code upon package declare<br>
28-
* Format property name and class name to camelcase name<br>
29-
1.2:<br>
30-
* Add support for generate anotations for target json lib --gson<br>
31-
* Add Comment option to switch the comment content to append<br>
14+
1.support for LoganSquare and Moshi.<br>
15+
2.support for cutomize annotation.<br>
16+
3.simply usage steps.
3217
]]>
3318
</change-notes>
3419

src/wu/seal/jsontokotlin/ConfigManager.kt

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package wu.seal.jsontokotlin
22

33
import com.intellij.ide.util.PropertiesComponent
44
import wu.seal.jsontokotlin.statistics.sendConfigInfo
5+
import wu.seal.jsontokotlin.supporter.GsonSupporter
56

67
/**
78
* ConfigManager
@@ -31,12 +32,18 @@ interface IConfigManager {
3132
get() = "jsonToKotlin_user_uuid_value_key"
3233

3334

35+
private val USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS: String
36+
get() = "jsonToKotlin_user_custom_json_lib_annotation_import_class"
37+
38+
private val USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING: String
39+
get() = "jsontokotlin_user_custom_json_lib_annotation_format_string"
40+
3441
var isPropertiesVar: Boolean
3542
get() = if (isTestModel) TestConfig.isPropertiesVar else PropertiesComponent.getInstance().isTrueValue(IS_PROPERTIES_VAR_KEY)
3643
set(value) = if (isTestModel) {
3744
} else {
3845
PropertiesComponent.getInstance().setValue(IS_PROPERTIES_VAR_KEY, value)
39-
Thread(){
46+
Thread() {
4047
sendConfigInfo()
4148
}.start()
4249
}
@@ -47,7 +54,7 @@ interface IConfigManager {
4754
set(value) = if (isTestModel) {
4855
} else {
4956
PropertiesComponent.getInstance().setValue(IS_COMMENT_OFF, value)
50-
Thread(){
57+
Thread() {
5158
sendConfigInfo()
5259
}.start()
5360
}
@@ -56,9 +63,10 @@ interface IConfigManager {
5663
var targetJsonConverterLib: TargetJsonConverter
5764
get() = if (isTestModel) TestConfig.targetJsonConvertLib else TargetJsonConverter.valueOf(PropertiesComponent.getInstance().getValue(TARGET_JSON_CONVERTER_LIB_KEY) ?: TargetJsonConverter.None.name)
5865
set(value) = if (isTestModel) {
66+
TestConfig.targetJsonConvertLib = value
5967
} else {
6068
PropertiesComponent.getInstance().setValue(TARGET_JSON_CONVERTER_LIB_KEY, value.name)
61-
Thread(){
69+
Thread() {
6270
sendConfigInfo()
6371
}.start()
6472
}
@@ -68,7 +76,7 @@ interface IConfigManager {
6876
set(value) = if (isTestModel) {
6977
} else {
7078
PropertiesComponent.getInstance().setValue(IS_PROPERTY_NULLABLE_KEY, value)
71-
Thread(){
79+
Thread() {
7280
sendConfigInfo()
7381
}.start()
7482
}
@@ -77,9 +85,9 @@ interface IConfigManager {
7785
var initWithDefaultValue: Boolean
7886
get() = if (isTestModel) TestConfig.initWithDefaultValue else PropertiesComponent.getInstance().getBoolean(INIT_WITH_DEFAULT_VALUE_KEY)
7987
set(value) = if (isTestModel) {
80-
} else{
88+
} else {
8189
PropertiesComponent.getInstance().setValue(INIT_WITH_DEFAULT_VALUE_KEY, value)
82-
Thread(){
90+
Thread() {
8391
sendConfigInfo()
8492
}.start()
8593
}
@@ -89,7 +97,27 @@ interface IConfigManager {
8997
set(value) = if (isTestModel) {
9098
} else {
9199
PropertiesComponent.getInstance().setValue(USER_UUID_KEY, value)
92-
Thread(){
100+
Thread() {
101+
sendConfigInfo()
102+
}.start()
103+
}
104+
105+
var customAnnotaionImportClassString:String
106+
get() = if (isTestModel) GsonSupporter.annotationImportClassString else PropertiesComponent.getInstance().getValue(USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS, GsonSupporter.annotationImportClassString)
107+
set(value) = if (isTestModel) {
108+
} else {
109+
PropertiesComponent.getInstance().setValue(USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS, value)
110+
Thread() {
111+
sendConfigInfo()
112+
}.start()
113+
}
114+
115+
var customAnnotaionFormatString:String
116+
get() = if (isTestModel) GsonSupporter.anotaionOnProperty else PropertiesComponent.getInstance().getValue(USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING, GsonSupporter.anotaionOnProperty)
117+
set(value) = if (isTestModel) {
118+
} else {
119+
PropertiesComponent.getInstance().setValue(USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING, value)
120+
Thread() {
93121
sendConfigInfo()
94122
}.start()
95123
}
@@ -99,7 +127,7 @@ interface IConfigManager {
99127
* This means which Json convert library you are using in you project
100128
*/
101129
enum class TargetJsonConverter {
102-
None, Gson, FastJson, Jackson
130+
None, Gson, FastJson, Jackson, MoShi, LoganSquare, Custom
103131
}
104132

105133

@@ -116,7 +144,7 @@ var isTestModel = false
116144
object TestConfig {
117145
var isCommentOff = false
118146
var isPropertiesVar = false
119-
var targetJsonConvertLib = TargetJsonConverter.None
147+
var targetJsonConvertLib = TargetJsonConverter.Gson
120148
var isPropertyNullable = true
121149
var initWithDefaultValue = true
122150
}

src/wu/seal/jsontokotlin/ImportClassWriter.kt

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package wu.seal.jsontokotlin
22

33
import com.intellij.openapi.editor.Document
44
import com.intellij.openapi.project.Project
5+
import wu.seal.jsontokotlin.supporter.*
56

67
/**
78
* to be a helper to insert Import class declare code
@@ -22,18 +23,42 @@ interface IImportClassWriter {
2223

2324
fun insertImportClassCode(project: Project?, editFile: Document)
2425

26+
fun insertMoShiImportClass(project: Project?, editFile: Document)
2527

28+
fun insertLoganSquareImportClass(project: Project?, editFile: Document)
29+
30+
fun insertCustomImportClass(project: Project?, editFile: Document)
2631
}
2732

2833

2934
object ImportClassWriter : IImportClassWriter {
35+
36+
37+
override fun insertCustomImportClass(project: Project?, editFile: Document) {
38+
val importClassString = CustomJsonLibSupporter.annotationImportClassString
39+
insertImportClassString(editFile, importClassString, project)
40+
}
41+
42+
override fun insertMoShiImportClass(project: Project?, editFile: Document) {
43+
val importClassString = MoShiSupporter.annotationImportClassString
44+
insertImportClassString(editFile, importClassString, project)
45+
}
46+
47+
override fun insertLoganSquareImportClass(project: Project?, editFile: Document) {
48+
val importClassString = LoganSquareSupporter.annotationImportClassString
49+
insertImportClassString(editFile, importClassString, project)
50+
}
51+
3052
override fun insertImportClassCode(project: Project?, editFile: Document) {
3153

3254
when (ConfigManager.targetJsonConverterLib) {
3355

3456
TargetJsonConverter.Gson -> insertGsonImportClass(project, editFile)
3557
TargetJsonConverter.FastJson -> insertFastJsonImportClass(project, editFile)
3658
TargetJsonConverter.Jackson -> insertJackSonImportClass(project, editFile)
59+
TargetJsonConverter.MoShi -> insertMoShiImportClass(project, editFile)
60+
TargetJsonConverter.LoganSquare -> insertLoganSquareImportClass(project, editFile)
61+
TargetJsonConverter.Custom -> insertCustomImportClass(project, editFile)
3762

3863
else -> {
3964
println("No need to import any Class code")
@@ -59,18 +84,22 @@ object ImportClassWriter : IImportClassWriter {
5984
}
6085

6186
private fun insertImportClassString(editFile: Document, importClassString: String, project: Project?) {
87+
6288
val text = editFile.text
63-
if (importClassString !in text) {
89+
importClassString.split("\n").forEach { importClassLineString ->
6490

65-
val packageIndex = text.indexOf("package ")
66-
val importIndex = Math.max(text.lastIndexOf("import"), packageIndex)
67-
val insertIndex = if (importIndex == -1) 0 else editFile.getLineEndOffset(editFile.getLineNumber(importIndex))
91+
if (importClassLineString !in text) {
6892

93+
val packageIndex = text.indexOf("package ")
94+
val importIndex = Math.max(text.lastIndexOf("import"), packageIndex)
95+
val insertIndex = if (importIndex == -1) 0 else editFile.getLineEndOffset(editFile.getLineNumber(importIndex))
6996

70-
executeCouldRollBackAction(project) {
71-
editFile.insertString(insertIndex, "\n" + importClassString + "\n")
72-
}
7397

98+
executeCouldRollBackAction(project) {
99+
editFile.insertString(insertIndex, "\n" + importClassLineString + "\n")
100+
}
101+
102+
}
74103
}
75104
}
76105

src/wu/seal/jsontokotlin/JsonInputDialog.kt

Lines changed: 0 additions & 82 deletions
This file was deleted.

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.statistics.sendHistoryActionInfo
1010
* Created by Seal.wu on 2017/8/21.
1111
*/
1212

13-
const val PLUGIN_VERSION = "1.4.1"
13+
const val PLUGIN_VERSION = "1.5"
1414

1515
class JsonToKotlinApplication : ApplicationComponent {
1616

src/wu/seal/jsontokotlin/KPropertyKeyword.kt

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/wu/seal/jsontokotlin/KotlinMaker.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package wu.seal.jsontokotlin
22

33
import com.google.gson.*
4+
import wu.seal.jsontokotlin.codeelements.KClassAnnotation
5+
import wu.seal.jsontokotlin.codeelements.KProperty
46

57
import java.util.HashSet
68

@@ -50,6 +52,9 @@ class KotlinMaker {
5052
}
5153

5254
private fun appClassName(stringBuilder: StringBuilder) {
55+
val classAnnotation = KClassAnnotation.getClassAnnotation()
56+
stringBuilder.append(classAnnotation)
57+
if (classAnnotation.isNotBlank()) stringBuilder.append("\n")
5358
stringBuilder.append("data class ").append(className).append("(\n")
5459
}
5560

0 commit comments

Comments
 (0)