Skip to content

Commit f61270a

Browse files
authored
Merge pull request #212 from wuseal/3.4.0
3.4.0
2 parents b4d56f6 + 1d41213 commit f61270a

File tree

23 files changed

+464
-92
lines changed

23 files changed

+464
-92
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ buildscript {
1212
}
1313

1414
plugins {
15-
id "org.jetbrains.intellij" version "0.3.2"
15+
id "org.jetbrains.intellij" version "0.4.9"
1616
}
1717

1818
apply plugin: 'idea'
1919
apply plugin: 'kotlin'
2020
apply plugin: 'org.jetbrains.intellij'
2121

2222
group 'wu.seal'
23-
version '3.3.0'
23+
version '3.4.0-EAP'
2424

2525
intellij {
2626
version '2017.1'
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Mon May 14 11:49:38 CST 2018
1+
#Sun Jul 21 15:52:28 CST 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip

parceable_support_tip.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#### Use Annotation
2+
3+
##### 1.add in gradle file
4+
5+
```
6+
android {
7+
...
8+
9+
//Using Kotlin experimental characteristics
10+
//You only need this for Kotlin < 1.3.40
11+
androidExtensions {
12+
experimental = true
13+
}
14+
}
15+
```
16+
17+
##### 2. Marking Data Classes with Annotations and Implementing Parcelable Interface
18+
19+
```
20+
@SuppressLint("ParcelCreator")
21+
@Parcelize
22+
data class User(val name: String, val age: Int) : Parcelable
23+
```

src/main/kotlin/extensions/ExtensionsCollector.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import extensions.ted.zeng.PropertyAnnotationLineSupport
55
import extensions.wu.seal.ClassNameSuffixSupport
66
import extensions.wu.seal.PropertyPrefixSupport
77
import extensions.wu.seal.PropertySuffixSupport
8+
import extensions.jose.han.ParcelableAnnotationSupport
89

910
/**
1011
* extension collect, all extensions will be hold by this class's extensions property
@@ -18,6 +19,7 @@ object ExtensionsCollector {
1819
PropertySuffixSupport,
1920
KeepAnnotationSupport,
2021
PropertyAnnotationLineSupport,
21-
ClassNameSuffixSupport
22+
ClassNameSuffixSupport,
23+
ParcelableAnnotationSupport
2224
)
2325
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package extensions.jose.han;
2+
3+
import com.intellij.ide.BrowserUtil
4+
import com.intellij.ui.layout.panel
5+
import extensions.Extension
6+
import wu.seal.jsontokotlin.classscodestruct.Annotation
7+
import wu.seal.jsontokotlin.classscodestruct.KotlinDataClass
8+
import javax.swing.JCheckBox
9+
import javax.swing.JPanel
10+
11+
/**
12+
* @author jose.han
13+
* @Date 2019/7/27Ø
14+
*/
15+
object ParcelableAnnotationSupport : Extension() {
16+
17+
val configKey = "jose.han.add_parcelable_annotatioin_enable"
18+
19+
override fun createUI(): JPanel {
20+
21+
22+
val checkBox = JCheckBox("Enable Parcelable Support ").apply {
23+
isSelected = ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()
24+
addActionListener {
25+
ParcelableAnnotationSupport.setConfig(ParcelableAnnotationSupport.configKey, isSelected.toString())
26+
}
27+
}
28+
29+
return panel {
30+
row {
31+
32+
checkBox()
33+
right {
34+
link("Need Some Config") {
35+
BrowserUtil.browse("https://github.com/wuseal/JsonToKotlinClass/blob/master/parceable_support_tip.md.md")
36+
}
37+
}
38+
}
39+
}
40+
}
41+
42+
override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass {
43+
return if (ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()) {
44+
45+
val classAnnotationString1 = "@SuppressLint(\"ParcelCreator\")"
46+
val classAnnotationString2 = "@Parcelize"
47+
48+
val classAnnotation1 = Annotation.fromAnnotationString(classAnnotationString1)
49+
val classAnnotation2 = Annotation.fromAnnotationString(classAnnotationString2)
50+
51+
return kotlinDataClass.copy(annotations = listOf(classAnnotation1,classAnnotation2),parentClassTemplate = "Parcelable")
52+
} else {
53+
kotlinDataClass
54+
}
55+
}
56+
57+
override fun intercept(originClassImportDeclaration: String): String {
58+
59+
val classAnnotationImportClassString = "import kotlinx.android.parcel.Parcelize".append("import android.os.Parcelable")
60+
61+
return if (ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()) {
62+
originClassImportDeclaration.append(classAnnotationImportClassString)
63+
} else {
64+
originClassImportDeclaration
65+
}
66+
}
67+
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,18 @@ class KotlinCodeMaker {
163163
jsonElementValue.isJsonArray -> {
164164
jsonElementValue.asJsonArray.run {
165165
var type = getArrayType(property, this)
166-
if (isExpectedJsonObjArrayType(this) || onlyHasOneObjectElementRecursive()
167-
|| onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()
168-
) {
169-
val subCode = try {
170-
KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData()
171-
} catch (e: UnSupportJsonException) {
172-
type = e.adviceType
173-
""
166+
if(!allChildrenAreEmptyArray()) {
167+
if (isExpectedJsonObjArrayType(this) || onlyHasOneObjectElementRecursive()
168+
|| onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()
169+
) {
170+
val subCode = try {
171+
KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData()
172+
} catch (e: UnSupportJsonException) {
173+
type = e.adviceType
174+
""
175+
}
176+
toBeAppend.add(subCode)
174177
}
175-
toBeAppend.add(subCode)
176178
}
177179
addProperty(stringBuilder, property, type, "", isLast)
178180
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ data class Property(
3030
append(annotationsCode).append(separatorBetweenAnnotationAndProperty)
3131
}
3232
}
33-
append(keyword).append(" ").append(name).append(": ").append(type)
33+
append(keyword).append(" ")
34+
append(if (name.first().isDigit() || name.contains('$')) "`$name`" else name)
35+
append(": ").append(type)
3436
if (value.isNotBlank()) {
3537
append(" = ").append(value)
3638
}

src/main/kotlin/wu/seal/jsontokotlin/codeelements/KProperty.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ interface IProperty {
1717

1818
}
1919

20-
class KProperty(private val rawPropertyName: String, private val propertyType: String, private val propertyValue: String) : IProperty {
20+
class KProperty(rawPropertyName: String, private val propertyType: String, private val propertyValue: String) : IProperty {
21+
22+
private val noLinebreakPropertyName = rawPropertyName.replace("\n","\\n").take(255)
2123

2224
override fun getPropertyStringBlock(): String {
2325

2426
val blockBuilder = StringBuilder()
2527

26-
blockBuilder.append(NoneSupporter.getNoneLibSupporterProperty(rawPropertyName, propertyType))
28+
blockBuilder.append(NoneSupporter.getNoneLibSupporterProperty(noLinebreakPropertyName, propertyType))
2729

2830
return blockBuilder.toString()
2931
}

0 commit comments

Comments
 (0)