Skip to content

Commit

Permalink
feat: document the kotlin modules (#16)
Browse files Browse the repository at this point in the history
<!-- readthedocs-preview incendocloud start -->
----
πŸ“š Documentation preview πŸ“š:
https://incendocloud--16.org.readthedocs.build/en/16/

<!-- readthedocs-preview incendocloud end -->
  • Loading branch information
Citymonstret authored Dec 23, 2023
1 parent 1b8f9fc commit 60e1c08
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ kotlin
iterable
Guice
GuiceInjectionService
coroutine
coroutines
MutableCommandBuilder
DSL
54 changes: 54 additions & 0 deletions docs/kotlin/annotations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# cloud-kotlin-coroutines-annotations

This module adds coroutine support to [cloud-annotations](../annotations/index.md), allowing you to make
the annotated command methods and suggestion providers suspending.

The module also adds the ability to use Kotlin default values in both suspending and non-suspending methods.

```kotlin title="Example of a suspending command method"
@CommandMethod("command [argument]")
suspend fun yourCommand(
argument: String = "default value"
): Unit = withContext(Dispatchers.IO) {
// ...
}
```

## Installation

Cloud is available through [Maven Central](https://search.maven.org/search?q=cloud.commandframework).

<!-- prettier-ignore -->
=== "Gradle (Kotlin)"

```kotlin
implementation("cloud.commandframework:cloud-kotlin-coroutines-annotations:2.0.0-SNAPSHOT")
```

=== "Gradle (Groovy)"

```groovy
implementation 'cloud.commandframework:cloud-kotlin-coroutines-annotations:2.0.0-SNAPSHOT'
```

You then need to install the `AnnotationParser` extension:

```kotlin
annotationParser.installCoroutineSupport()
```

You may override the default coroutine scope (`GlobalScope`) and context (`EmptyCoroutineContext`)
when invoking `installCoroutineSupport`.

## Suggestion Providers

The coroutine support extends the allowed signatures for `@Suggestions`-annotated methods, letting you return
sequences of `Suggestion` objects as well as strings.

```kotlin title="Example of a suspending suggestion provider"
@Suggestions("custom-suggestions")
suspend fun suggestionMethod(
context: CommandContext<CommandSender>,
input: String
): Sequence<Suggestion> = sequenceOf("a", "b", "c").map(Suggestion::simple)
```
68 changes: 68 additions & 0 deletions docs/kotlin/coroutines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# cloud-kotlin-coroutines

This module adds coroutine support to commands using builders.
For suspending commands methods, see [cloud-kotlin-coroutines-annotations](./annotations.md).

## Installation

Cloud is available through [Maven Central](https://search.maven.org/search?q=cloud.commandframework).

<!-- prettier-ignore -->
=== "Gradle (Kotlin)"

```kotlin
implementation("cloud.commandframework:cloud-kotlin-coroutines:2.0.0-SNAPSHOT")
```

=== "Gradle (Groovy)"

```groovy
implementation 'cloud.commandframework:cloud-kotlin-coroutines:2.0.0-SNAPSHOT'
```

## Suspending command execution handlers

You may create a suspending command execution handler by implementing `SuspendingExecutionHandler`.
The `SuspendingExecutionHandler` interface does not extend `CommandExecutionHandler`, but you can convert
it to a `CommandExecutionHandler` by using `SuspendingExecutionHandler.asCommandExecutionHandler`.

There are extensions for both `Command.Builder` and `MutableCommandBuilder` that allow you to make direct
use of suspending execution handlers.

```kotlin title="Example"
manager.commandBuilder("command")
.suspendingHandler { context ->
// ...
}
```

## Suspending parsers

You may create a suspending argument parser by implementing `SuspendingArgumentParser`.
The `SuspendingArgumentParser` interface does not extend `ArgumentParser`, but you can convert
it to an `ArgumentParser` by using `SuspendingArgumentParser.asArgumentParser`.

You may also create a suspending argument parser by making use of the global factory function:

```kotlin title="Creating a suspending parser"
suspendingArgumentParser<CommandSender, Int> { ctx, input ->
ArgumentParseResult.success(input.readInteger())
}
```

## Suspending suggestion providers

You may create a suspending suggestion provider by implementing `SuspendingSuggestionProvider`.
The `SuspendingSuggestionProvider` interface does not extend `SuggestionProvider`, but you can convert
it to a `SuggestionProvider` by using `SuspendingSuggestionProvider.asSuggestionProvider`.

You may also create a suspending suggestion provider by making use of the global factory function:

```kotlin title="Creating a suspending suggestion provider"
suspendingSuggestionProvider<CommandSender> { ctx, input ->
(1..3).asSequence()
.map(Number::toString)
.map(Suggestion::simple)
.asIterable()
}
```
55 changes: 55 additions & 0 deletions docs/kotlin/extensions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# cloud-kotlin-extensions

This module contains extensions to different parts of Cloud.

## Installation

Cloud is available through [Maven Central](https://search.maven.org/search?q=cloud.commandframework).

<!-- prettier-ignore -->
=== "Gradle (Kotlin)"

```kotlin
implementation("cloud.commandframework:cloud-kotlin-extensions:2.0.0-SNAPSHOT")
```

=== "Gradle (Groovy)"

```groovy
implementation 'cloud.commandframework:cloud-kotlin-extensions:2.0.0-SNAPSHOT'
```

## MutableCommandBuilder

`MutableCommandBuilder` is a small DSL for `Command.Builder` which allows for the creation of commands
in more idiomatic Kotlin.

You can initiate the mutable command builder using `CommandManager.commandBuilder` or
create and register the command in one step by using `CommandManager.buildAndRegister`.

```kotlin title="Example MutableCommandBuilder usage"
manager.buildAndRegister("command") {
senderType<OverriddenSenderType>()

required("string", stringParser()) {
description(argumentDescription("A string argument"))
}

handler { ctx ->
// ...
}
}
```

## Extension Functions

The `cloud.commandframework.kotlin.extension` package contains extensions to:

- `CloudKeyContainer`
- `CloudKey`
- `Command.Builder`
- `CommandManager`
- `ExceptionController`
- `ArgumentParser`

The extensions exist to make it easier to use Cloud in Kotlin.
6 changes: 6 additions & 0 deletions docs/kotlin/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# cloud-kotlin

Cloud has three different modules for Kotlin:

- [cloud-kotlin-coroutines-annotations](./annotations.md)
- [cloud-kotlin-coroutines](./coroutines.md)
- [cloud-kotlin-extensions](./extensions.md)
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ theme:
- navigation.sections
- navigation.indexes
- navigation.top
# - navigation.footer
- navigation.footer
- toc.follow
- search.suggest
- search.highlight
Expand Down

0 comments on commit 60e1c08

Please sign in to comment.