Skip to content

Commit 8dbd663

Browse files
authored
Merge pull request #105 from wuseal/3.0-doc-extensions
3.0 doc extensions
2 parents 20fd516 + 0d61d61 commit 8dbd663

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+305
-365
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ deploy:
1515
on:
1616
tags: true
1717
repo: wuseal/JsonToKotlinClass
18-
condition: "$TRAVIS_TAG =~ (eap|EAP)$"
18+
condition: "$TRAVIS_TAG =~ (eap|EAP)"
1919
- provider: script
2020
script: "./gradlew publishPlugin -Dchannels=alpha"
2121
skip_cleanup: true
2222
on:
2323
tags: true
2424
repo: wuseal/JsonToKotlinClass
25-
condition: "$TRAVIS_TAG =~ (alpha|Alpha)$"
25+
condition: "$TRAVIS_TAG =~ (alpha|Alpha)"
2626
- provider: script
2727
script: "./gradlew publishPlugin"
2828
skip_cleanup: true

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Have a try with the settings dialog :stuck_out_tongue_winking_eye:
5959
* [Android Parcelable support](https://github.com/wuseal/JsonToKotlinClass/issues/44)
6060
* [Customize parent class declaration](https://github.com/wuseal/JsonToKotlinClass/issues/44)
6161
* Generating kotlin data class property order by alphabetical
62+
* [Support customizing this plugin by `Extensions` module](https://github.com/wuseal/JsonToKotlinClass/blob/3.0-doc-extensions/doc_for_extensions.md)
6263

6364
### Generate Example
6465
This is the example JSON from json.org

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ apply plugin: 'kotlin'
2020
apply plugin: 'org.jetbrains.intellij'
2121

2222
group 'wu.seal'
23-
version '3.0-EAP'
23+
version '3.0-EAP-2'
2424

2525
intellij {
2626
version '2017.1'

doc_for_extensions.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
If you want to customize you own `JsonToKotlinClass` Plugin or want to contribute some new featrues for this project, This guide will help you.
2+
3+
#### Customize Your Own Plugin
4+
5+
Customize your own plugin by these steps, and you will have your own plugin, in these guide we will create a extension that will make all the property name to be all upper case
6+
7+
1. Fork `JsonToKotlinClass` from Github
8+
9+
2. Clone your forked project to local
10+
11+
3. Open Project with Intellij Idea
12+
13+
4. Create package under ..src/main/kotlin/extensions
14+
15+
![image](https://user-images.githubusercontent.com/9211902/51250876-ccb8cb00-19d2-11e9-9620-9c45c8975bfa.png)
16+
17+
Package name could be your own domain name. in this case we will you `wu.seal`
18+
19+
5. Create a Extension object class file under your created package
20+
21+
![image](https://user-images.githubusercontent.com/9211902/51249232-a47a9d80-19cd-11e9-866f-b7e283da4853.png)
22+
23+
6. Now the created file would like this:
24+
25+
![image](https://user-images.githubusercontent.com/9211902/51249378-0fc46f80-19ce-11e9-8abd-0c926e8a726f.png)
26+
27+
7. Add interfaces to be implemente
28+
29+
![image](https://user-images.githubusercontent.com/9211902/51249511-75b0f700-19ce-11e9-9e74-ab014d3d77e3.png)
30+
31+
8. Implement all the needed interface
32+
33+
```kotlin
34+
package extensions.wu.seal
35+
36+
import extensions.Extension
37+
import wu.seal.jsontokotlin.classscodestruct.KotlinDataClass
38+
import javax.swing.JPanel
39+
40+
object AllUpperCase :Extension(){
41+
42+
override fun createUI(): JPanel {
43+
44+
}
45+
46+
override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass {
47+
48+
}
49+
}
50+
```
51+
52+
9. Then we create a new UI for switching to enable all property name to be all upper case
53+
54+
```kotlin
55+
override fun createUI(): JPanel {
56+
57+
val configKey = "wu.seal.all_to_be_upper_case"
58+
59+
val checkBox = JCheckBox("Make all properties name to be all upper case").apply {
60+
isSelected = getConfig(configKey).toBoolean()
61+
addActionListener {
62+
setConfig(configKey, isSelected.toString())
63+
}
64+
}
65+
66+
return panel {
67+
row {
68+
checkBox()
69+
}
70+
}
71+
72+
}
73+
```
74+
75+
10. And make the properties names to be all upper case
76+
77+
```kotlin
78+
override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass {
79+
80+
//make all properties name to be all upper case
81+
val newProperties = kotlinDataClass.properties.map { it.copy(name = it.name.toUpperCase()) }
82+
83+
return kotlinDataClass.copy(properties = newProperties)
84+
}
85+
```
86+
87+
We use `map`method to create a new properties which all properties names are translate to be all upper case, And copy the incoming parameter `kotlinDataClass` with replacing the new created properties, and return it to the system.
88+
89+
11. Add the new object class into `ExtensionsCollector` object class which inside extensions package
90+
91+
```kotlin
92+
/**
93+
* extension collect, all extensions will be hold by this class's extensions property
94+
*/
95+
object ExtensionsCollector {
96+
/**
97+
* all extensions
98+
*/
99+
val extensions = listOf(
100+
PropertyPrefixSupport,
101+
PropertySuffixSupport,
102+
AllUpperCase
103+
)
104+
}
105+
```
106+
107+
12. Run the plugin by clicking`runIde`task
108+
109+
![image](https://user-images.githubusercontent.com/9211902/51250409-40f26f00-19d1-11e9-962c-7f44fb5549e4.png)
110+
111+
Then we will see the new IDE opened
112+
113+
13. Create a new project in new opened IDE and create a new Kotlin File for inserting new Kotlin Data Class code, and open `JsonToKotlinClass` Plugin, Then we will see that new Extension was added in the extensions tab,Let's selected the new added checkbox and have a test if it works ok.
114+
115+
![image](https://user-images.githubusercontent.com/9211902/51250670-15bc4f80-19d2-11e9-9cee-798489108adf.png)
116+
117+
14. Then Let's see the tested result:
118+
119+
![image](https://user-images.githubusercontent.com/9211902/51250749-5a47eb00-19d2-11e9-9824-c8f9ec0cc664.png)
120+
121+
```kotlin
122+
import com.google.gson.annotations.SerializedName
123+
data class TestCode(
124+
@SerializedName("hello")
125+
val HELLO: String = ""
126+
)
127+
```
128+
129+
Yeah! That what we want. Greate we have created the first our style plugin.
130+
131+
Any Question? Welcome to raise an issue [here](https://github.com/wuseal/JsonToKotlinClass/issues)
132+
133+
Demo could be found [here](https://github.com/wuseal/JsonToKotlinClass/tree/3.0-extension-demo)
134+
135+

src/main/kotlin/extensions/ExtensionsCollector.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ object ExtensionsCollector {
1010
/**
1111
* all extensions
1212
*/
13-
val extensions = listOf<Extension>(
13+
val extensions = listOf(
1414
PropertyPrefixSupport,
1515
PropertySuffixSupport
1616
)
17-
}
17+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class JsonToKotlinApplication : ApplicationComponent {
1818

1919
LogUtil.i("init JSON To Kotlin Class version ==$PLUGIN_VERSION")
2020

21-
Thread() {
21+
Thread {
2222
try {
2323
sendConfigInfo()
2424
sendHistoryExceptionInfo()

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ class KotlinDataClassCodeMaker( private val rootClassName: String, private val j
2525
val kotlinDataClasses = KotlinDataClassMaker(rootClassName = rootClassName, json = json).makeKotlinDataClasses()
2626

2727
val interceptedDataClasses = kotlinDataClasses.map {it.applyInterceptors(interceptors)}
28-
val code = interceptedDataClasses.joinToString("\n\n") {
28+
return interceptedDataClasses.joinToString("\n\n") {
2929
it.getCode()
3030
}
31-
return code
3231
}
33-
}
32+
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
5656
inputDialog.show()
5757
val className = inputDialog.getClassName()
5858
val inputString = inputDialog.inputString
59-
val json = if (inputString?.startsWith("http") == true) {
59+
val json = if (inputString.startsWith("http")) {
6060
URL(inputString).readText()
6161
} else inputString
62-
if (json == null || json.isEmpty()) {
62+
if (json.isEmpty()) {
6363
return
6464
}
6565
jsonString = json
@@ -179,9 +179,7 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
179179

180180
fun getCleanText(editorText: String): String {
181181
val tempCleanText = editorText.substringBeforeLast("class")
182-
val cleanText =
183-
if (tempCleanText.trim().endsWith("data")) tempCleanText.trim().removeSuffix("data") else tempCleanText
184-
return cleanText
182+
return if (tempCleanText.trim().endsWith("data")) tempCleanText.trim().removeSuffix("data") else tempCleanText
185183
}
186184

187185
fun getCurrentEditFileTemClassName(editorText: String) = editorText.substringAfterLast("class")
@@ -209,7 +207,7 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
209207
)
210208
&& removeDocCommentAndPackageDeclareText.indexOf("class") != -1
211209
&& removeDocCommentAndPackageDeclareText.substringAfter("class").substringAfter("(")
212-
.replace(Regex("\\s"), "").let { it.equals(")") || it.equals("){}") })
210+
.replace(Regex("\\s"), "").let { it == ")" || it == "){}" })
213211
) {
214212
couldGetAndReuseClassNameInCurrentEditFileForInsertCode = true
215213
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ class TargetJsonElement : ITargetJsonElement {
8585
}
8686

8787
private fun getArrayChildElement(jsonArray: JsonArray): JsonElement {
88-
if (jsonArray.size() >= 1) {
89-
return getFullFieldElementFromArrayElement(jsonArray)
88+
return if (jsonArray.size() >= 1) {
89+
getFullFieldElementFromArrayElement(jsonArray)
9090
} else {
91-
return Gson().toJsonTree(Any())
91+
Gson().toJsonTree(Any())
9292
}
9393
}
9494

src/main/kotlin/wu/seal/jsontokotlin/classscodestruct/Annotation.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import wu.seal.jsontokotlin.utils.numberOf
66
data class Annotation(val annotationTemplate: String, val rawName: String) {
77

88
fun getAnnotationString(): String {
9-
if (annotationTemplate.contains("%s")) {
9+
return if (annotationTemplate.contains("%s")) {
1010
val countS = annotationTemplate.numberOf("%s")
11-
val args = Array(countS, { rawName })
12-
return annotationTemplate.format(*args)
11+
val args = Array(countS) { rawName }
12+
annotationTemplate.format(*args)
1313
} else {
14-
return annotationTemplate
14+
annotationTemplate
1515
}
1616
}
1717

@@ -22,16 +22,16 @@ data class Annotation(val annotationTemplate: String, val rawName: String) {
2222
throw IllegalArgumentException("Only support one line annotation!! current is $annotationString")
2323
}
2424

25-
if (annotationString.contains("\"")) {
25+
return if (annotationString.contains("\"")) {
2626
if (annotationString.numberOf("\"") != 2) {
2727
throw IllegalArgumentException("Only support one line annotation with one couple Double quotes!! current is $annotationString")
2828
}
2929
val rawName = annotationString.substringAfter("\"").substringBefore("\"")
3030
val annotationTemplate =
31-
annotationString.substringBefore("\"") + "\"%s\"" + annotationString.substringAfterLast("\"")
32-
return Annotation(annotationTemplate, rawName)
31+
annotationString.substringBefore("\"") + "\"%s\"" + annotationString.substringAfterLast("\"")
32+
Annotation(annotationTemplate, rawName)
3333
} else {
34-
return Annotation(annotationString, "")
34+
Annotation(annotationString, "")
3535
}
3636

3737
}
@@ -64,4 +64,4 @@ data class Annotation(val annotationTemplate: String, val rawName: String) {
6464

6565
}
6666
}
67-
}
67+
}

0 commit comments

Comments
 (0)