Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,69 @@ class Foo {
}
```

### MarkName

> since v0.13.0, [#96](https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin/pull/96)

You can use `markName` to add a name mark annotation (e.g. `@JvmName`, `@JsName`) to the generated synthetic function.

For example the JVM:

```kotlin
class Foo {
@JvmBlocking(markName = "namedWaitAndGet")
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
```

compiled 👇

```kotlin
class Foo {
// Hide from Java
@JvmSynthetic
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
@Api4J // RequiresOptIn annotation, provide warnings to Kotlin
@JvmName("namedWaitAndGet") // From the `markName`'s value
fun waitAndGetBlocking(): String = runInBlocking { waitAndGet() } // 'runInBlocking' from the runtime provided by the plugin
}
```

Note: `@JvmName` has limitations on non-final functions, and even the compiler may prevent compilation.

For example the JS:

```kotlin
class Foo {
@JsPromise(markName = "namedWaitAndGet")
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
```

compiled 👇

```kotlin
class Foo {
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}

@Api4Js // RequiresOptIn annotation, provide warnings to Kotlin
@JsName("namedWaitAndGet") // From the `markName`'s value
fun waitAndGetAsync(): Promise<String> = runInAsync { waitAndGet() } // 'runInAsync' from the runtime provided by the plugin
}
```

## Usage

### The version
Expand Down Expand Up @@ -246,7 +309,7 @@ You can also disable them and add dependencies manually.
```Kotlin
plugin {
kotlin("jvm") version "..." // Take the Kotlin/JVM as an example
id("love.forte.plugin.suspend-transform") version "2.1.20-0.12.0"
id("love.forte.plugin.suspend-transform") version "<VERSION>"
}

dependencies {
Expand Down
77 changes: 70 additions & 7 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,69 @@ class Foo {
}
```

### MarkName

> 自 v0.13.0 起, [#96](https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin/pull/96)

你可以使用 `markName` 为生成的合成函数添加名称标记注解(例如 `@JvmName`、`@JsName`)。

例如 JVM:

```kotlin
class Foo {
@JvmBlocking(markName = "namedWaitAndGet")
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
```

编译后 👇

```kotlin
class Foo {
// 对 Java 隐藏
@JvmSynthetic
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
@Api4J // 需要显式启用的注解,向 Kotlin 提供警告
@JvmName("namedWaitAndGet") // 来自 `markName` 的值
fun waitAndGetBlocking(): String = runInBlocking { waitAndGet() } // 'runInBlocking' 来自插件提供的运行时
}
```

注意:`@JvmName` 在非 final 函数上有限制,甚至编译器可能会阻止编译。

例如 JS:

```kotlin
class Foo {
@JsPromise(markName = "namedWaitAndGet")
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}
}
```

编译后 👇

```kotlin
class Foo {
suspend fun waitAndGet(): String {
delay(5)
return "Hello"
}

@Api4Js // 需要显式启用的注解,向 Kotlin 提供警告
@JsName("namedWaitAndGet") // 来自 `markName` 的值
fun waitAndGetAsync(): Promise<String> = runInAsync { waitAndGet() } // 'runInAsync' 来自插件提供的运行时
}
```

## 使用方式

### 版本说明
Expand Down Expand Up @@ -222,7 +285,7 @@ suspendTransformPlugin {
// 默认与插件版本相同
version = "<ANNOTATION_VERSION>"
}

// 包含运行时
// 默认为 `true`
includeRuntime = true
Expand Down Expand Up @@ -315,7 +378,7 @@ class Cat {
suspend fun meow() {
// ...
}

// 生成:
fun meowBlocking() {
`$runInBlocking$` { meow() }
Expand Down Expand Up @@ -345,7 +408,7 @@ suspendTransformPlugin {
class Cat {
@JvmBlocking
suspend fun meow(): String = "Meow!"

// 生成:
fun meowAsync(): CompletableFuture<out String> {
`$runInAsync$`(block = { meow() }, scope = this as? CoroutineScope)
Expand Down Expand Up @@ -382,7 +445,7 @@ suspendTransformPlugin {
class Cat {
@JsPromise
suspend fun meow(): String = "Meow!"

// 生成:
fun meowAsync(): Promise<String> {
`$runInAsync$`(block = { meow() }, scope = this as? CoroutineScope)
Expand Down Expand Up @@ -437,7 +500,7 @@ suspendTransformPlugin {
// 自定义时可能无需默认注解和运行时
includeAnnotation = false
includeRuntime = false

transformer {
// 具体配置见下文
}
Expand Down Expand Up @@ -596,12 +659,12 @@ suspendTransformPlugin {
defaultSuffix = "Blocking"
defaultAsProperty = false
}

transformFunctionInfo {
packageName = "com.example"
functionName = "inBlock"
}

copyAnnotationsToSyntheticFunction = true
copyAnnotationsToSyntheticProperty = true

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/IProject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object IProject : ProjectDetail() {

// Remember the libs.versions.toml!
val ktVersion = "2.2.0-RC2"
val pluginVersion = "0.12.0"
val pluginVersion = "0.13.0"

override val version: String = "$ktVersion-$pluginVersion"

Expand Down
Loading