Skip to content

Commit

Permalink
1.support for LoganSquare and Moshi.
Browse files Browse the repository at this point in the history
2.support for cutomize annotation.
3.simply usage steps.
  • Loading branch information
seal committed Nov 1, 2017
1 parent 1571092 commit f310716
Show file tree
Hide file tree
Showing 33 changed files with 975 additions and 413 deletions.
23 changes: 4 additions & 19 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>wu.seal.tool.jsontokotlin</id>
<name>JsonToKotlinClass</name>
<version>1.4.1</version>
<version>1.5</version>
<vendor email="sealkingking@163.com" url="https://www.github.com/wuseal">Seal</vendor>

<description><![CDATA[
Expand All @@ -11,24 +11,9 @@
]]></description>

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

Expand Down
46 changes: 37 additions & 9 deletions src/wu/seal/jsontokotlin/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wu.seal.jsontokotlin

import com.intellij.ide.util.PropertiesComponent
import wu.seal.jsontokotlin.statistics.sendConfigInfo
import wu.seal.jsontokotlin.supporter.GsonSupporter

/**
* ConfigManager
Expand Down Expand Up @@ -31,12 +32,18 @@ interface IConfigManager {
get() = "jsonToKotlin_user_uuid_value_key"


private val USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS: String
get() = "jsonToKotlin_user_custom_json_lib_annotation_import_class"

private val USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING: String
get() = "jsontokotlin_user_custom_json_lib_annotation_format_string"

var isPropertiesVar: Boolean
get() = if (isTestModel) TestConfig.isPropertiesVar else PropertiesComponent.getInstance().isTrueValue(IS_PROPERTIES_VAR_KEY)
set(value) = if (isTestModel) {
} else {
PropertiesComponent.getInstance().setValue(IS_PROPERTIES_VAR_KEY, value)
Thread(){
Thread() {
sendConfigInfo()
}.start()
}
Expand All @@ -47,7 +54,7 @@ interface IConfigManager {
set(value) = if (isTestModel) {
} else {
PropertiesComponent.getInstance().setValue(IS_COMMENT_OFF, value)
Thread(){
Thread() {
sendConfigInfo()
}.start()
}
Expand All @@ -56,9 +63,10 @@ interface IConfigManager {
var targetJsonConverterLib: TargetJsonConverter
get() = if (isTestModel) TestConfig.targetJsonConvertLib else TargetJsonConverter.valueOf(PropertiesComponent.getInstance().getValue(TARGET_JSON_CONVERTER_LIB_KEY) ?: TargetJsonConverter.None.name)
set(value) = if (isTestModel) {
TestConfig.targetJsonConvertLib = value
} else {
PropertiesComponent.getInstance().setValue(TARGET_JSON_CONVERTER_LIB_KEY, value.name)
Thread(){
Thread() {
sendConfigInfo()
}.start()
}
Expand All @@ -68,7 +76,7 @@ interface IConfigManager {
set(value) = if (isTestModel) {
} else {
PropertiesComponent.getInstance().setValue(IS_PROPERTY_NULLABLE_KEY, value)
Thread(){
Thread() {
sendConfigInfo()
}.start()
}
Expand All @@ -77,9 +85,9 @@ interface IConfigManager {
var initWithDefaultValue: Boolean
get() = if (isTestModel) TestConfig.initWithDefaultValue else PropertiesComponent.getInstance().getBoolean(INIT_WITH_DEFAULT_VALUE_KEY)
set(value) = if (isTestModel) {
} else{
} else {
PropertiesComponent.getInstance().setValue(INIT_WITH_DEFAULT_VALUE_KEY, value)
Thread(){
Thread() {
sendConfigInfo()
}.start()
}
Expand All @@ -89,7 +97,27 @@ interface IConfigManager {
set(value) = if (isTestModel) {
} else {
PropertiesComponent.getInstance().setValue(USER_UUID_KEY, value)
Thread(){
Thread() {
sendConfigInfo()
}.start()
}

var customAnnotaionImportClassString:String
get() = if (isTestModel) GsonSupporter.annotationImportClassString else PropertiesComponent.getInstance().getValue(USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS, GsonSupporter.annotationImportClassString)
set(value) = if (isTestModel) {
} else {
PropertiesComponent.getInstance().setValue(USER_CUSTOM_JSON_LIB_ANNOTATION_IMPORT_CLASS, value)
Thread() {
sendConfigInfo()
}.start()
}

var customAnnotaionFormatString:String
get() = if (isTestModel) GsonSupporter.anotaionOnProperty else PropertiesComponent.getInstance().getValue(USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING, GsonSupporter.anotaionOnProperty)
set(value) = if (isTestModel) {
} else {
PropertiesComponent.getInstance().setValue(USER_CUSTOM_JSON_LIB_ANNOTATION_FORMAT_STRING, value)
Thread() {
sendConfigInfo()
}.start()
}
Expand All @@ -99,7 +127,7 @@ interface IConfigManager {
* This means which Json convert library you are using in you project
*/
enum class TargetJsonConverter {
None, Gson, FastJson, Jackson
None, Gson, FastJson, Jackson, MoShi, LoganSquare, Custom
}


Expand All @@ -116,7 +144,7 @@ var isTestModel = false
object TestConfig {
var isCommentOff = false
var isPropertiesVar = false
var targetJsonConvertLib = TargetJsonConverter.None
var targetJsonConvertLib = TargetJsonConverter.Gson
var isPropertyNullable = true
var initWithDefaultValue = true
}
43 changes: 36 additions & 7 deletions src/wu/seal/jsontokotlin/ImportClassWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wu.seal.jsontokotlin

import com.intellij.openapi.editor.Document
import com.intellij.openapi.project.Project
import wu.seal.jsontokotlin.supporter.*

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

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

fun insertMoShiImportClass(project: Project?, editFile: Document)

fun insertLoganSquareImportClass(project: Project?, editFile: Document)

fun insertCustomImportClass(project: Project?, editFile: Document)
}


object ImportClassWriter : IImportClassWriter {


override fun insertCustomImportClass(project: Project?, editFile: Document) {
val importClassString = CustomJsonLibSupporter.annotationImportClassString
insertImportClassString(editFile, importClassString, project)
}

override fun insertMoShiImportClass(project: Project?, editFile: Document) {
val importClassString = MoShiSupporter.annotationImportClassString
insertImportClassString(editFile, importClassString, project)
}

override fun insertLoganSquareImportClass(project: Project?, editFile: Document) {
val importClassString = LoganSquareSupporter.annotationImportClassString
insertImportClassString(editFile, importClassString, project)
}

override fun insertImportClassCode(project: Project?, editFile: Document) {

when (ConfigManager.targetJsonConverterLib) {

TargetJsonConverter.Gson -> insertGsonImportClass(project, editFile)
TargetJsonConverter.FastJson -> insertFastJsonImportClass(project, editFile)
TargetJsonConverter.Jackson -> insertJackSonImportClass(project, editFile)
TargetJsonConverter.MoShi -> insertMoShiImportClass(project, editFile)
TargetJsonConverter.LoganSquare -> insertLoganSquareImportClass(project, editFile)
TargetJsonConverter.Custom -> insertCustomImportClass(project, editFile)

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

private fun insertImportClassString(editFile: Document, importClassString: String, project: Project?) {

val text = editFile.text
if (importClassString !in text) {
importClassString.split("\n").forEach { importClassLineString ->

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

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

executeCouldRollBackAction(project) {
editFile.insertString(insertIndex, "\n" + importClassString + "\n")
}

executeCouldRollBackAction(project) {
editFile.insertString(insertIndex, "\n" + importClassLineString + "\n")
}

}
}
}

Expand Down
82 changes: 0 additions & 82 deletions src/wu/seal/jsontokotlin/JsonInputDialog.kt

This file was deleted.

2 changes: 1 addition & 1 deletion src/wu/seal/jsontokotlin/JsonToKotlinApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import wu.seal.jsontokotlin.statistics.sendHistoryActionInfo
* Created by Seal.wu on 2017/8/21.
*/

const val PLUGIN_VERSION = "1.4.1"
const val PLUGIN_VERSION = "1.5"

class JsonToKotlinApplication : ApplicationComponent {

Expand Down
22 changes: 0 additions & 22 deletions src/wu/seal/jsontokotlin/KPropertyKeyword.kt

This file was deleted.

5 changes: 5 additions & 0 deletions src/wu/seal/jsontokotlin/KotlinMaker.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wu.seal.jsontokotlin

import com.google.gson.*
import wu.seal.jsontokotlin.codeelements.KClassAnnotation
import wu.seal.jsontokotlin.codeelements.KProperty

import java.util.HashSet

Expand Down Expand Up @@ -50,6 +52,9 @@ class KotlinMaker {
}

private fun appClassName(stringBuilder: StringBuilder) {
val classAnnotation = KClassAnnotation.getClassAnnotation()
stringBuilder.append(classAnnotation)
if (classAnnotation.isNotBlank()) stringBuilder.append("\n")
stringBuilder.append("data class ").append(className).append("(\n")
}

Expand Down
Loading

0 comments on commit f310716

Please sign in to comment.