Skip to content

Commit

Permalink
Merge pull request #11 from ltttttttttttt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ltttttttttttt authored Jul 21, 2023
2 parents 21dd0fe + a9783fa commit caead7a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ internal class FunctionFieldsInfo(
val isInTheConstructor: Boolean,
val isBuffBean: Boolean,
val nullable: String,
val isList: Boolean = false,
)
val isList: Boolean,
val typeString: String,
) {
/**
* 获取addBuff或removeBuff的后缀,用于区分type是否可空
*/
fun getBuffSuffix(): String = if (isList && typeString.endsWith("?>")) "WithNull" else ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.lt.buff.provider
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.validate
Expand All @@ -18,14 +19,13 @@ internal class BuffSymbolProcessor(private val environment: SymbolProcessorEnvir
override fun process(resolver: Resolver): List<KSAnnotated> {
val ret = mutableListOf<KSAnnotated>()
resolver.getSymbolsWithAnnotation(Buff::class.qualifiedName!!)
.toList()
.forEach {
if (it is KSClassDeclaration && it.classKind.type == "class") {
if (it is KSClassDeclaration && it.classKind == ClassKind.CLASS) {
if (!it.validate()) ret.add(it)
else it.accept(BuffVisitor(environment), Unit)//处理符号
}
}
//返回无法处理的符号
return ret
}
}
}
69 changes: 47 additions & 22 deletions Buff/src/jvmMain/kotlin/com/lt/buff/provider/BuffVisitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
//遍历构造内的字段
classDeclaration.primaryConstructor?.parameters?.forEach {
val name = it.name?.getShortName() ?: ""
val (ksType, isBuffBean, typeName, nullable, finallyTypeName) = getKSTypeInfo(it.type, options)
val ksTypeInfo = getKSTypeInfo(it.type, options)
//写入构造内的普通字段
file.appendText(" ${if (it.isVal) "val" else "var"} $name: $finallyTypeName,\n")
functionFields.add(FunctionFieldsInfo(name, true, isBuffBean, nullable))
file.appendText(" ${if (it.isVal) "val" else "var"} $name: ${ksTypeInfo.finallyTypeName},\n")
functionFields.add(
FunctionFieldsInfo(
name,
true,
ksTypeInfo.isBuffBean,
ksTypeInfo.nullable,
ksTypeInfo.isList,
ksTypeInfo.typeString,
)
)
}
//遍历所有字段
classDeclaration.getAllProperties().forEach {
Expand Down Expand Up @@ -89,7 +98,16 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
" val $fieldName: $typeName${info.typeString} = $stateFieldName\n"
)
}
functionFields.add(FunctionFieldsInfo(fieldName, false, isBuffBean, nullable, info.isList))
functionFields.add(
FunctionFieldsInfo(
fieldName,
false,
isBuffBean,
nullable,
info.isList,
info.typeString,
)
)
}
}
file.appendText(") {\n")
Expand All @@ -103,24 +121,23 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
file.appendText(
"\n fun removeBuff(): $fullName =\n" +
" $fullName(${
functionFields.filter { it.isInTheConstructor }
.map {
if (it.isBuffBean)
"${it.fieldName}${it.nullable}.removeBuff()"
else
it.fieldName
}
.joinToString()
functionFields.filter { it.isInTheConstructor }.joinToString {
if (it.isBuffBean)
"${it.fieldName}${it.nullable}.removeBuff${it.getBuffSuffix()}()"
else
it.fieldName
}
}).also {\n"
)
functionFields.filter { !it.isInTheConstructor }.forEach {
val isList = if (it.isList) it.nullable + ".toList()" else ""
file.appendText(
" it.${it.fieldName} = ${
if (it.isBuffBean)
"${it.fieldName}$isList${it.nullable}.removeBuff()"
"${it.fieldName}${
if (it.isList) "" else it.nullable
}.removeBuff${it.getBuffSuffix()}()"
else
it.fieldName + isList
it.fieldName
}\n"
)
}
Expand All @@ -137,7 +154,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
file.appendText(
" ${
if (it.isBuffBean)
"${it.fieldName}${it.nullable}.addBuff()"
"${it.fieldName}${it.nullable}.addBuff${it.getBuffSuffix()}()"
else
it.fieldName
},\n"
Expand All @@ -147,7 +164,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
file.appendText(
" mutableStateOf(${
if (it.isBuffBean)
"${it.fieldName}${it.nullable}.addBuff()"
"${it.fieldName}${it.nullable}.addBuff${it.getBuffSuffix()}()"
else
it.fieldName
}),\n"
Expand All @@ -156,7 +173,7 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
file.appendText(
" ${
if (it.isBuffBean)
"${it.fieldName}${it.nullable}.addBuff()"
"${it.fieldName}${it.nullable}.addBuff${it.getBuffSuffix()}()"
else
it.fieldName
}${it.nullable}.toMutableStateList() ?: mutableStateListOf(),\n"
Expand All @@ -167,13 +184,21 @@ internal class BuffVisitor(private val environment: SymbolProcessorEnvironment)
file.appendText(" )\n\n${options.getCustomInFile(::getInfo)}")
//写入Collection<addBuff>
file.appendText(
"\n\nfun Collection<$fullName?>.addBuff() =\n" +
" map { it?.addBuff() } as List<$className>"
"\n\nfun Collection<$fullName?>.addBuffWithNull() =\n" +
" map { it?.addBuff() }"
)
file.appendText(
"\nfun Collection<$fullName>.addBuff() =\n" +
" map { it.addBuff() }"
)
//写入Collection<removeBuff>
file.appendText(
"\n\nfun Collection<$className?>.removeBuff() =\n" +
" map { it?.removeBuff() } as List<$fullName>"
"\n\nfun Collection<$className?>.removeBuffWithNull() =\n" +
" map { it?.removeBuff() }"
)
file.appendText(
"\nfun Collection<$className>.removeBuff() =\n" +
" map { it.removeBuff() }"
)
file.close()
}
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

1. Convert some fields in beans to MutableState&lt;T&gt; that can be used directly

## Capability to be achieved

<a href="https://github.com/ltttttttttttt/Buff/blob/main/README_CN.md">see this</a>

## How to use

Step 1 and 2.add dependencies:
Expand Down
6 changes: 1 addition & 5 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

1. 将beans中的某些字段转换为可以直接使用的MutableState&lt;T&gt;

## 待实现功能

1. 支持MutableList

## 使用方式

Step 1和2.添加依赖:
Expand Down Expand Up @@ -47,7 +43,7 @@ dependencies {
```kotlin
plugins {
...
id("com.google.devtools.ksp") version "1.8.20-1.0.10"//this,前面的1.7.10对应你的kotlin版本,更多版本参考: https://github.com/google/ksp/releases
id("com.google.devtools.ksp") version "1.8.20-1.0.10"//this,前面的1.8.20对应你的kotlin版本,更多版本参考: https://github.com/google/ksp/releases
}

...
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/lt/buffapp/UseBuff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ class BuffBean(
val id: Int? = null,
var info2: InfoBean? = null,
var infoList2: List<InfoBean>? = null,
var infoList4: List<InfoBean?>? = null,
) {
var name: String? = null
var info: InfoBean? = null
var infoBean: InfoBean = InfoBean()
var type: Type? = null
var infoList: List<InfoBean?>? = null
var infoList3: List<InfoBean>? = null
var list: List<String>? = null
var infoListList: List<List<InfoBean>>? = null
var map: Map<String, InfoBean>? = null
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ const val composeVersion = "1.4.0"//compose版本
const val composeCompilerVersion = "1.4.5"//compose编译版本
const val kspVersion = "$kotlinVersion-1.0.10"//ksp版本

const val mVersion = "0.1.2"//此库的版本
const val mVersion = "1.0.0"//此库的版本

0 comments on commit caead7a

Please sign in to comment.