Commit 9e6d0f35 authored by Setako's avatar Setako

[Add] Picocli autocomplete integration

parent 8babba4d
......@@ -18,31 +18,42 @@ class CommandTree(
.setErr(out);
}
/**
* For getting info only
*/
fun getDummyCommandLine() = constructCommandLineRecursively(rootCommandProvider, null, null, null)
private fun constructCommandLineRecursively(commandProvider: () -> Runnable,
sender: CommandSender, stdOut: StdOut, stdErr: StdOut): CommandLine {
sender: CommandSender?, stdOut: StdOut?, stdErr: StdOut?): CommandLine {
var commandLine: CommandLine? = null;
val commandRunnable = commandProvider().apply {
if (this is ReactantCommand) {
this.sender = sender
this.stdout = stdOut
this.stderr = stdErr
sender?.let { this.sender = it }
stdOut?.let { this.stdout = it }
stdErr?.let { this.stderr = it }
}
}
commandLine = CommandLine(commandRunnable).setTrimQuotes(true)
.setColorScheme(CommandLine.Help.defaultColorScheme(CommandLine.Help.Ansi.OFF))
// .let { it.setResourceBundle() }
.also { it.commandSpec.parser().collectErrors(true) }
.apply {
getCommand<Runnable>().let { if (it is ReactantCommand) it.commandLine = this }
};
.apply { getCommand<Runnable>().let { if (it is ReactantCommand) it.commandLine = this } }
subCommandMap[commandProvider]?.forEach { subCommandProvider ->
constructCommandLineRecursively(subCommandProvider,
sender, stdOut, stdErr).let {
commandLine.addSubcommand(it.commandSpec.name(), it, *it.commandSpec.aliases())
}
constructCommandLineRecursively(subCommandProvider, sender, stdOut, stdErr)
.let { commandLine.addSubcommand(it.commandSpec.name(), it, *it.commandSpec.aliases()) }
}
return commandLine
}
//
// private fun constructCommandSpecRecursively(commandProvider: () -> Runnable) {
// val commandSpec = CommandLine.Model.CommandSpec.forAnnotatedObject(rootCommandProvider)
//
// subCommandMap[commandProvider]?.forEach { subCommandProvider ->
// constructCommandSpecRecursively(subCommandProvider).let {
// }
// }
// }
fun addSubcommand(superCommandProvider: () -> Runnable, subCommandProvider: () -> Runnable) {
subCommandMap.getOrPut(superCommandProvider) { arrayListOf() }.add(subCommandProvider)
......
......@@ -13,6 +13,7 @@ import dev.reactant.reactant.service.spec.profiler.ProfilerDataProvider
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.command.SimpleCommandMap
import picocli.AutoComplete
import picocli.CommandLine.Model
import kotlin.reflect.KType
......@@ -134,7 +135,7 @@ interface PicocliCommandService : Registrable<PicocliCommandService.CommandRegis
private val argsGroupingRegex = Regex("(\"(?:\\\\\"|[^\"])+\")|((?:\\\\\"|\\\\ |[^\" ])+)");
class PicocliBukkitCommand(
commandSpec: Model.CommandSpec,
val commandSpec: Model.CommandSpec,
private val profilerDataProvider: PublishingProfilerDataProvider,
private val requester: Provider,
private val commandTree: CommandTree
......@@ -154,4 +155,12 @@ class PicocliBukkitCommand(
}
return true
}
override fun tabComplete(sender: CommandSender, alias: String, args: Array<out String>): MutableList<String> {
val candidates = arrayListOf<String>()
AutoComplete.complete(commandTree.getDummyCommandLine().commandSpec, args, args.size - 1, args.lastOrNull()?.length
?: 0, 0, candidates as List<String>)
val result = candidates.map { (args.lastOrNull() ?: "") + it }.toMutableList()
return result
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment