Skip to content

Commit

Permalink
Merge pull request #212 from wuseal/3.4.0
Browse files Browse the repository at this point in the history
3.4.0
  • Loading branch information
wuseal committed Jul 29, 2019
2 parents b4d56f6 + 1d41213 commit f61270a
Show file tree
Hide file tree
Showing 23 changed files with 464 additions and 92 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -12,15 +12,15 @@ buildscript {
}

plugins {
id "org.jetbrains.intellij" version "0.3.2"
id "org.jetbrains.intellij" version "0.4.9"
}

apply plugin: 'idea'
apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.intellij'

group 'wu.seal'
version '3.3.0'
version '3.4.0-EAP'

intellij {
version '2017.1'
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Mon May 14 11:49:38 CST 2018
#Sun Jul 21 15:52:28 CST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip
23 changes: 23 additions & 0 deletions parceable_support_tip.md
@@ -0,0 +1,23 @@
#### Use Annotation

##### 1.add in gradle file

```
android {
...
//Using Kotlin experimental characteristics
//You only need this for Kotlin < 1.3.40
androidExtensions {
experimental = true
}
}
```

##### 2. Marking Data Classes with Annotations and Implementing Parcelable Interface

```
@SuppressLint("ParcelCreator")
@Parcelize
data class User(val name: String, val age: Int) : Parcelable
```
4 changes: 3 additions & 1 deletion src/main/kotlin/extensions/ExtensionsCollector.kt
Expand Up @@ -5,6 +5,7 @@ import extensions.ted.zeng.PropertyAnnotationLineSupport
import extensions.wu.seal.ClassNameSuffixSupport
import extensions.wu.seal.PropertyPrefixSupport
import extensions.wu.seal.PropertySuffixSupport
import extensions.jose.han.ParcelableAnnotationSupport

/**
* extension collect, all extensions will be hold by this class's extensions property
Expand All @@ -18,6 +19,7 @@ object ExtensionsCollector {
PropertySuffixSupport,
KeepAnnotationSupport,
PropertyAnnotationLineSupport,
ClassNameSuffixSupport
ClassNameSuffixSupport,
ParcelableAnnotationSupport
)
}
67 changes: 67 additions & 0 deletions src/main/kotlin/extensions/jose/han/ParcelableAnnotationSupport.kt
@@ -0,0 +1,67 @@
package extensions.jose.han;

import com.intellij.ide.BrowserUtil
import com.intellij.ui.layout.panel
import extensions.Extension
import wu.seal.jsontokotlin.classscodestruct.Annotation
import wu.seal.jsontokotlin.classscodestruct.KotlinDataClass
import javax.swing.JCheckBox
import javax.swing.JPanel

/**
* @author jose.han
* @Date 2019/7/27脴
*/
object ParcelableAnnotationSupport : Extension() {

val configKey = "jose.han.add_parcelable_annotatioin_enable"

override fun createUI(): JPanel {


val checkBox = JCheckBox("Enable Parcelable Support ").apply {
isSelected = ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()
addActionListener {
ParcelableAnnotationSupport.setConfig(ParcelableAnnotationSupport.configKey, isSelected.toString())
}
}

return panel {
row {

checkBox()
right {
link("Need Some Config") {
BrowserUtil.browse("https://github.com/wuseal/JsonToKotlinClass/blob/master/parceable_support_tip.md.md")
}
}
}
}
}

override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass {
return if (ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()) {

val classAnnotationString1 = "@SuppressLint(\"ParcelCreator\")"
val classAnnotationString2 = "@Parcelize"

val classAnnotation1 = Annotation.fromAnnotationString(classAnnotationString1)
val classAnnotation2 = Annotation.fromAnnotationString(classAnnotationString2)

return kotlinDataClass.copy(annotations = listOf(classAnnotation1,classAnnotation2),parentClassTemplate = "Parcelable")
} else {
kotlinDataClass
}
}

override fun intercept(originClassImportDeclaration: String): String {

val classAnnotationImportClassString = "import kotlinx.android.parcel.Parcelize".append("import android.os.Parcelable")

return if (ParcelableAnnotationSupport.getConfig(ParcelableAnnotationSupport.configKey).toBoolean()) {
originClassImportDeclaration.append(classAnnotationImportClassString)
} else {
originClassImportDeclaration
}
}
}
20 changes: 11 additions & 9 deletions src/main/kotlin/wu/seal/jsontokotlin/KotlinCodeMaker.kt
Expand Up @@ -163,16 +163,18 @@ class KotlinCodeMaker {
jsonElementValue.isJsonArray -> {
jsonElementValue.asJsonArray.run {
var type = getArrayType(property, this)
if (isExpectedJsonObjArrayType(this) || onlyHasOneObjectElementRecursive()
|| onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()
) {
val subCode = try {
KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData()
} catch (e: UnSupportJsonException) {
type = e.adviceType
""
if(!allChildrenAreEmptyArray()) {
if (isExpectedJsonObjArrayType(this) || onlyHasOneObjectElementRecursive()
|| onlyHasOneSubArrayAndAllItemsAreObjectElementRecursive()
) {
val subCode = try {
KotlinCodeMaker(getChildType(getRawType(type)), jsonElementValue).makeKotlinData()
} catch (e: UnSupportJsonException) {
type = e.adviceType
""
}
toBeAppend.add(subCode)
}
toBeAppend.add(subCode)
}
addProperty(stringBuilder, property, type, "", isLast)
}
Expand Down
Expand Up @@ -30,7 +30,9 @@ data class Property(
append(annotationsCode).append(separatorBetweenAnnotationAndProperty)
}
}
append(keyword).append(" ").append(name).append(": ").append(type)
append(keyword).append(" ")
append(if (name.first().isDigit() || name.contains('$')) "`$name`" else name)
append(": ").append(type)
if (value.isNotBlank()) {
append(" = ").append(value)
}
Expand Down
Expand Up @@ -17,13 +17,15 @@ interface IProperty {

}

class KProperty(private val rawPropertyName: String, private val propertyType: String, private val propertyValue: String) : IProperty {
class KProperty(rawPropertyName: String, private val propertyType: String, private val propertyValue: String) : IProperty {

private val noLinebreakPropertyName = rawPropertyName.replace("\n","\\n").take(255)

override fun getPropertyStringBlock(): String {

val blockBuilder = StringBuilder()

blockBuilder.append(NoneSupporter.getNoneLibSupporterProperty(rawPropertyName, propertyType))
blockBuilder.append(NoneSupporter.getNoneLibSupporterProperty(noLinebreakPropertyName, propertyType))

return blockBuilder.toString()
}
Expand Down

0 comments on commit f61270a

Please sign in to comment.