Update to Piston 0.5.2 + Doctools/Deprecation improvements (#523)

* Update to Piston 0.5.2

* [Doctools] Fix output, be verbose about deprecations

* Improve deprecation system, doctools output
This commit is contained in:
Kenzie Togami
2019-10-05 02:06:18 -07:00
committed by Matthew Miller
parent d8d25fbff1
commit 03c0cce53e
19 changed files with 205 additions and 67 deletions

View File

@ -24,7 +24,6 @@ import com.sk89q.worldedit.WorldEdit
import com.sk89q.worldedit.command.BiomeCommands
import com.sk89q.worldedit.command.ChunkCommands
import com.sk89q.worldedit.command.ClipboardCommands
import com.sk89q.worldedit.command.ExpandCommands
import com.sk89q.worldedit.command.GeneralCommands
import com.sk89q.worldedit.command.GenerationCommands
import com.sk89q.worldedit.command.HistoryCommands
@ -37,11 +36,10 @@ import com.sk89q.worldedit.command.ToolCommands
import com.sk89q.worldedit.command.ToolUtilCommands
import com.sk89q.worldedit.command.UtilityCommands
import com.sk89q.worldedit.command.util.PermissionCondition
import com.sk89q.worldedit.internal.command.CommandUtil
import com.sk89q.worldedit.util.formatting.text.TextComponent
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent
import com.sk89q.worldedit.util.formatting.text.serializer.plain.PlainComponentSerializer
import org.enginehub.piston.Command
import org.enginehub.piston.TextConfig
import org.enginehub.piston.config.TextConfig
import org.enginehub.piston.part.SubCommandPart
import org.enginehub.piston.util.HelpGenerator
import java.nio.file.Files
@ -52,7 +50,6 @@ import kotlin.streams.toList
class DocumentationPrinter private constructor() {
private val nameRegex = Regex("name = \"(.+?)\"")
private val serializer = PlainComponentSerializer({ "" }, TranslatableComponent::key)
private val commands = WorldEdit.getInstance().platformManager.platformCommandManager.commandManager.allCommands
.map { it.name to it }.toList().toMap()
private val cmdOutput = StringBuilder()
@ -111,6 +108,7 @@ class DocumentationPrinter private constructor() {
}
dumpSection("Tool Commands") {
yield("tool")
yieldAllCommandsIn<ToolCommands>()
yieldAllCommandsIn<ToolUtilCommands>()
}
@ -216,7 +214,7 @@ Other Permissions
private fun dumpSection(title: String, addCommandNames: suspend SequenceScope<String>.() -> Unit) {
cmdOutput.append("\n").append(title).append("\n").append(Strings.repeat("~", title.length)).append("\n")
val prefix = TextConfig.getCommandPrefix()
val prefix = reduceToRst(TextConfig.commandPrefixValue())
val commands = sequence(addCommandNames).map { this.commands.getValue(it) }.toList()
matchedCommands.addAll(commands.map { it.name })
@ -269,14 +267,19 @@ Other Permissions
}
cmdOutput.appendln()
cmdOutput.appendln(" :class: command-topic").appendln()
CommandUtil.deprecationWarning(command).ifPresent { warning ->
cmdOutput.appendln("""
| .. WARNING::
| ${reduceToRst(warning).makeRstSafe("\n\n")}
""".trimMargin())
}
cmdOutput.appendln("""
| .. csv-table::
| :widths: 8, 15
""".trimMargin())
cmdOutput.appendln()
for ((k, v) in entries) {
val rstSafe = v.trim().replace("\"", "\\\"").replace("\n", "\n" + " ".repeat(2))
.lineSequence().map { line -> line.ifBlank { "" } }.joinToString(separator = "\n")
val rstSafe = v.makeRstSafe("\n")
cmdOutput.append(" ".repeat(2))
.append(k)
.append(",")
@ -287,32 +290,39 @@ Other Permissions
cmdOutput.appendln()
}
private fun String.makeRstSafe(lineJoiner: String) = trim()
.replace("\"", "\\\"").replace("\n", "\n" + " ".repeat(2))
.lineSequence()
.map { line -> line.ifBlank { "" } }
.joinToString(separator = lineJoiner)
private fun linkSafe(text: String) = text.replace(" ", "-")
private fun commandTableEntries(command: Command, parents: Stream<Command>): Map<String, String> {
return sequence {
val desc = command.description.run {
val footer = CommandUtil.footerWithoutDeprecation(command)
when {
command.footer.isPresent -> append(
TextComponent.builder("\n\n").append(command.footer.get())
footer.isPresent -> append(
TextComponent.builder("\n\n").append(footer.get())
)
else -> this
}
}
yield("**Description**" to serializer.serialize(desc))
yield("**Description**" to reduceToRst(desc))
val cond = command.condition
if (cond is PermissionCondition && cond.permissions.isNotEmpty()) {
val perms = cond.permissions.joinToString(", ") { "``$it``" }
yield("**Permissions**" to perms)
}
val usage = serializer.serialize(HelpGenerator.create(Stream.concat(parents, Stream.of(command)).toList()).usage)
val usage = reduceToRst(HelpGenerator.create(Stream.concat(parents, Stream.of(command)).toList()).usage)
yield("**Usage**" to "``$usage``")
// Part descriptions
command.parts.filterNot { it is SubCommandPart }
.forEach {
val title = "\u2001\u2001``" + serializer.serialize(it.textRepresentation) + "``"
yield(title to serializer.serialize(it.description))
val title = "\u2001\u2001``" + reduceToRst(it.textRepresentation) + "``"
yield(title to reduceToRst(it.description))
}
}.toMap()
}
@ -324,13 +334,15 @@ Other Permissions
*/
@JvmStatic
fun main(args: Array<String>) {
val printer = DocumentationPrinter()
try {
val printer = DocumentationPrinter()
printer.writeAllCommands()
writeOutput("commands.rst", printer.cmdOutput.toString())
writeOutput("permissions.rst", printer.permsOutput.toString())
WorldEdit.getInstance().sessionManager.unload()
printer.writeAllCommands()
writeOutput("commands.rst", printer.cmdOutput.toString())
writeOutput("permissions.rst", printer.permsOutput.toString())
} finally {
WorldEdit.getInstance().sessionManager.unload()
}
}
private fun writeOutput(file: String, output: String) {

View File

@ -0,0 +1,69 @@
package com.sk89q.worldedit.internal.util
import com.sk89q.worldedit.util.formatting.WorldEditText
import com.sk89q.worldedit.util.formatting.text.Component
import com.sk89q.worldedit.util.formatting.text.TextComponent
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent
import com.sk89q.worldedit.util.formatting.text.format.TextDecoration
import org.enginehub.piston.util.TextHelper
fun reduceToRst(component: Component): String {
val formatted = WorldEditText.format(component)
return formatAsRst(formatted).toString()
}
private fun formatAsRst(component: Component, currentDeco: String? = null): CharSequence {
return when (component) {
is TextComponent -> {
val content = StringBuilder(component.content())
val deco = when {
// Actions that suggest themselves as commands are marked as code
component.isSuggestingAsCommand(content.toString()) -> "``"
component.decorations().any { it == TextDecoration.BOLD } -> "**"
component.decorations().any { it == TextDecoration.ITALIC } -> "*"
else -> null
}
component.children().joinTo(content, separator = "") {
formatAsRst(it, deco ?: currentDeco)
}
deco?.let {
require(currentDeco == null) {
"Nested decorations are hell in RST. \n" +
"Existing: $currentDeco; New: $deco\n" +
"Offender: ${TextHelper.reduceToText(component)}"
}
content.rstDeco(deco)
}
content
}
is TranslatableComponent -> {
val content = StringBuilder(component.key())
component.children().joinTo(content, separator = "") { formatAsRst(it, currentDeco) }
content
}
else -> component.children().joinToString(separator = "") { formatAsRst(it, currentDeco) }
}
}
private fun Component.isSuggestingAsCommand(text: String): Boolean {
val ce = clickEvent()
return when (ce?.action()) {
ClickEvent.Action.RUN_COMMAND,
ClickEvent.Action.SUGGEST_COMMAND ->
ce.value() == text
else -> false
}
}
private fun StringBuilder.rstDeco(deco: String): StringBuilder = apply {
ensureCapacity(length + deco.length * 2)
val insertionPoint = indexOfFirst { !it.isWhitespace() }.coerceAtLeast(0)
insert(insertionPoint, deco)
val insertionPointEnd = (indexOfLast { !it.isWhitespace() } + 1).takeUnless { it < 1 } ?: length
insert(insertionPointEnd, deco)
}