Skip to content

Instantly share code, notes, and snippets.

@IgnatBeresnev
Last active April 24, 2024 09:35
Show Gist options
  • Save IgnatBeresnev/d65a3486f64729fe69ed817df279a308 to your computer and use it in GitHub Desktop.
Save IgnatBeresnev/d65a3486f64729fe69ed817df279a308 to your computer and use it in GitHub Desktop.
@file:OptIn(InternalDokkaApi::class)
package template
import org.jetbrains.dokka.CoreExtensions
import org.jetbrains.dokka.DokkaException
import org.jetbrains.dokka.InternalDokkaApi
import org.jetbrains.dokka.model.DModule
import org.jetbrains.dokka.model.WithSources
import org.jetbrains.dokka.model.dfs
import org.jetbrains.dokka.model.doc.CustomTagWrapper
import org.jetbrains.dokka.model.doc.Text
import org.jetbrains.dokka.model.withDescendants
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.DokkaPlugin
import org.jetbrains.dokka.plugability.DokkaPluginApiPreview
import org.jetbrains.dokka.plugability.PluginApiPreviewAcknowledgement
import org.jetbrains.dokka.transformers.documentation.DocumentableTransformer
import org.jetbrains.dokka.utilities.firstIsInstanceOrNull
import java.time.LocalDate
import java.time.format.DateTimeFormatter
class MyAwesomeDokkaPlugin : DokkaPlugin() {
val documentableTransformerCheck by extending {
CoreExtensions.documentableTransformer providing {
TodoCheckDocumentableTransformer()
}
}
}
class TodoCheckDocumentableTransformer : DocumentableTransformer {
override fun invoke(original: DModule, context: DokkaContext): DModule {
original.dfs { documentable ->
for ((sourceSet, documentation) in documentable.documentation) {
val todoTag = documentation.children
.firstOrNull { it is CustomTagWrapper && it.name.equals("TODO", ignoreCase = true) }
?: continue
val (date, reason) = todoTag.withDescendants()
.firstIsInstanceOrNull<Text>()
?.let { it.body.split(" ", limit = 2).takeIf { it.size == 2 } }
?: throw DokkaException("TODO comments must have an ISO date and a reason")
val dueDate = LocalDate.parse(date, DateTimeFormatter.ISO_DATE)
if (dueDate.isBefore(LocalDate.now())) {
val lineNum = (documentable as WithSources).sources.getValue(sourceSet).computeLineNumber()
val location = with(documentable.dri) {
listOfNotNull(packageName, classNames, callable?.name).joinToString(separator = ".")
}
context.logger.warn("[warning] TODO [$location:$lineNum] is overdue: $reason")
}
}
false
}
return original
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment