Skip to content

Commit 0cd3ee1

Browse files
committed
- add name for custom actions
1 parent a538d51 commit 0cd3ee1

File tree

9 files changed

+46
-17
lines changed

9 files changed

+46
-17
lines changed

app/src/main/kotlin/com/stslex/compiler_app/MainActivity.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ class MainActivity : ComponentActivity() {
6161
findViewById<TextView>(R.id.usernameFieldTextView).text = name
6262
}
6363

64-
@DistinctUntilChangeFun(true, action = TestLogger::class)
64+
@DistinctUntilChangeFun(
65+
name = "set_user_second_name",
66+
logging = true,
67+
action = TestLogger::class
68+
)
6569
private fun setSecondName(name: String) {
6670
logger.log(Level.INFO, "setSecondName: $name")
6771
findViewById<TextView>(R.id.secondNameFieldTextView).text = name

app/src/main/kotlin/com/stslex/compiler_app/TestLogger.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import io.github.stslex.compiler_plugin.utils.Action
44

55
class TestLogger : Action {
66

7-
override fun invoke(isProcess: Boolean) {
8-
println("test logger procession: $isProcess")
7+
override fun invoke(
8+
name: String,
9+
isProcess: Boolean
10+
) {
11+
println("test action $name procession: $isProcess")
912
}
1013
}

compiler-plugin/src/main/kotlin/io/github/stslex/compiler_plugin/DistinctChangeCache.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ internal class DistinctChangeCache(
1818
): R {
1919
val entry = cache[key]
2020

21+
// log enter to invoke processing
2122
if (config.logging) {
22-
logger.i("key: $key, config:$config, entry: $entry, args: $args")
23+
logger.i("name: ${config.name} key: $key, config:$config, entry: $entry, args: $args")
2324
}
2425

26+
config.action(
27+
name = config.name,
28+
isProcess = entry != null && entry.first == args
29+
)
30+
2531
if (entry != null && entry.first == args) {
26-
if (config.logging) {
27-
logger.i("$key not change")
28-
}
29-
config.action(isProcess = false)
32+
if (config.logging) logger.i("${config.name} with key $key not change")
3033
return entry.second as R
3134
}
3235

33-
config.action(isProcess = true)
34-
3536
val result = body()
3637
cache[key] = args to result
38+
3739
return result
3840
}
3941
}

compiler-plugin/src/main/kotlin/io/github/stslex/compiler_plugin/DistinctUntilChangeFun.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import kotlin.reflect.KClass
77
/**
88
* @param logging enable logs for Kotlin Compiler Runtime work (useful for debug - don't use in production)
99
* @param singletonAllow if enable - generates distinction for function without classes (so it's singleton)
10+
* @param name set name for function in compiler logs and put in into action, else take function name
11+
* @param action any action to process when function enter - it invokes with state of processing function body info and [name].
12+
* @suppress [action] shouldn't have properties in it's constructor
1013
* */
1114
@Target(AnnotationTarget.FUNCTION)
1215
@Retention(AnnotationRetention.BINARY)
1316
public annotation class DistinctUntilChangeFun(
1417
val logging: Boolean = LOGGING_DEFAULT,
1518
val singletonAllow: Boolean = SINGLETON_ALLOW,
19+
val name: String = "",
1620
val action: KClass<out Action> = DefaultAction::class
1721
) {
1822

compiler-plugin/src/main/kotlin/io/github/stslex/compiler_plugin/model/DistinctChangeConfig.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import io.github.stslex.compiler_plugin.utils.Action
44

55
internal data class DistinctChangeConfig(
66
val logging: Boolean,
7-
val action: Action
7+
val action: Action,
8+
val name: String
89
)
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package io.github.stslex.compiler_plugin.utils
22

3-
/**Use for any action in runtime plugin*/
3+
/**
4+
* Use for any action in runtime plugin
5+
* @suppress shouldn't have properties in it's constructor - cause compiling crush
6+
**/
47
public fun interface Action {
58

69
/**
10+
* @param name setted at [io.github.stslex.compiler_plugin.DistinctUntilChangeFun] property name
711
* @param isProcess show that function block will process
812
**/
9-
public operator fun invoke(isProcess: Boolean)
13+
public operator fun invoke(
14+
name: String,
15+
isProcess: Boolean
16+
)
1017

1118
}

compiler-plugin/src/main/kotlin/io/github/stslex/compiler_plugin/utils/DefaultAction.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ package io.github.stslex.compiler_plugin.utils
22

33
internal class DefaultAction : Action {
44

5-
override fun invoke(isProcess: Boolean) = Unit
5+
override fun invoke(
6+
name: String,
7+
isProcess: Boolean
8+
) = Unit
69
}

compiler-plugin/src/main/kotlin/io/github/stslex/compiler_plugin/utils/ReadQualifierUtil.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
77
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
88
import org.jetbrains.kotlin.ir.builders.irBoolean
99
import org.jetbrains.kotlin.ir.builders.irCallConstructor
10+
import org.jetbrains.kotlin.ir.builders.irString
1011
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
1112
import org.jetbrains.kotlin.ir.expressions.IrClassReference
1213
import org.jetbrains.kotlin.ir.expressions.IrConst
@@ -36,7 +37,10 @@ internal fun IrPluginContext.readQualifier(
3637

3738
val irBuilder = createIrBuilder(function)
3839

39-
val loggingExpr = annotation.getValueArgument(0) ?: irBuilder.irBoolean(LOGGING_DEFAULT)
40+
val loggingExpr = annotation.getValueArgument(0)
41+
?: irBuilder.irBoolean(LOGGING_DEFAULT)
42+
val actionName = annotation.getValueArgument(2)
43+
?: irBuilder.irString(function.name.identifier)
4044
val actionInstanceExpr = getQualifierAction(annotation, irBuilder)
4145

4246
val constructorSymbol = referenceClass(DistinctChangeConfig::class.classId)
@@ -53,6 +57,7 @@ internal fun IrPluginContext.readQualifier(
5357
.apply {
5458
putValueArgument(0, loggingExpr)
5559
putValueArgument(1, actionInstanceExpr)
60+
putValueArgument(2, actionName)
5661
}
5762
}
5863

@@ -65,7 +70,7 @@ private fun IrPluginContext.getQualifierAction(
6570
.findClass(DefaultAction::class.name, DefaultAction::class.fqName)
6671
?: error("readQualifier ${DefaultAction::class.java.simpleName} not found")
6772

68-
val actionReference = annotation.getValueArgument(2) as? IrClassReference
73+
val actionReference = annotation.getValueArgument(3) as? IrClassReference
6974

7075
val actionClassSymbol = actionReference?.symbol as? IrClassSymbol ?: defaultActionClass
7176

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ activity = "1.10.0"
1414
constraintLayout = "2.2.0"
1515
jetbrainsKotlinJvm = "2.0.20"
1616

17-
stslexCompilerPlugin = "0.0.3"
17+
stslexCompilerPlugin = "0.0.4"
1818

1919
[libraries]
2020
android-desugarJdkLibs = { module = "com.android.tools:desugar_jdk_libs", version.ref = "androidDesugarJdkLibs" }

0 commit comments

Comments
 (0)