Skip to content

Commit

Permalink
add class KDoc generator
Browse files Browse the repository at this point in the history
  • Loading branch information
siosio committed Jan 5, 2019
1 parent 377185e commit 2503e96
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 21 deletions.
28 changes: 8 additions & 20 deletions src/main/kotlin/siosio/kodkod/EnterAfterKDocGenHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ import com.intellij.psi.*
import com.intellij.psi.codeStyle.*
import com.intellij.psi.util.*
import com.intellij.util.text.*
import org.jetbrains.kotlin.idea.core.*
import org.jetbrains.kotlin.idea.kdoc.*
import org.jetbrains.kotlin.kdoc.psi.api.*
import org.jetbrains.kotlin.kdoc.psi.impl.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*

/**
*
*/
class EnterAfterKDocGenHandler : EnterHandlerDelegateAdapter() {


override fun postProcessEnter(file: PsiFile,
editor: Editor,
dataContext: DataContext): EnterHandlerDelegate.Result {
Expand Down Expand Up @@ -53,21 +48,14 @@ class EnterAfterKDocGenHandler : EnterHandlerDelegateAdapter() {

val parent = kdoc.parent
when (parent) {
is KtNamedFunction -> {
val params: List<PsiNameIdentifierOwner> = parent.typeParameters + parent.valueParameters
if (!params.isEmpty()) {
params.map { it.name }
.map { "@param $it" }
.joinToString("\n", transform = { "* $it" })
.let { "/**\n* TODO\n$it\n*/" }
.let { kDocElementFactory.createKDocFromText(it) }
.let { kdoc.replace(it) }
.let { CodeStyleManager.getInstance(project).reformat(it) }
} else {
null
}
}
is KtNamedFunction -> NamedFunctionKDocGenerator(parent)
is KtClass -> ClassKDocGenerator(parent)
else -> null
}?.generate()
?.let {
kDocElementFactory.createKDocFromText(it)
.let { kdoc.replace(it) }
.let { CodeStyleManager.getInstance(project).reformat(it) }
}?.let {
it.getChildOfType<KDocSection>()?.let {
caretModel.moveToOffset(it.textOffset + 6)
Expand All @@ -87,4 +75,4 @@ class EnterAfterKDocGenHandler : EnterHandlerDelegateAdapter() {
}
return false
}
}
}
68 changes: 68 additions & 0 deletions src/main/kotlin/siosio/kodkod/KDocGenerator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package siosio.kodkod

import com.intellij.psi.*
import org.jetbrains.kotlin.psi.*

interface KDocGenerator {

companion object {
const val LF = "\n"
}

fun generate(): String

fun toParamsKdoc(keyword: String = "@param", params: List<PsiNameIdentifierOwner>): String =
params.map { "$keyword ${it.name}" }
.joinToString(LF, transform = { "* $it" })

fun StringBuilder.appendLine(text: String): StringBuilder = append(text).append(LF)
}

class NamedFunctionKDocGenerator(private val function: KtNamedFunction) : KDocGenerator {
override fun generate(): String {
val builder = StringBuilder()
builder.appendLine("/**")
.appendLine("* TODO")
.appendLine("*")
if (function.typeParameters.isNotEmpty()) {
builder.appendLine(toParamsKdoc(params = function.typeParameters))
}
if (function.valueParameters.isNotEmpty()) {
builder.appendLine(toParamsKdoc(params = function.valueParameters))
}
builder.appendLine("*/")
return builder.toString()
}
}
data class Hoge(val hoge:String)

class ClassKDocGenerator(private val klass: KtClass) : KDocGenerator {
override fun generate(): String {
val builder = StringBuilder()
builder.appendLine("/**")
.appendLine("* TODO")
.appendLine("*")

if (klass.typeParameters.isNotEmpty()) {
builder.appendLine(toParamsKdoc(params = klass.typeParameters))
}

val (properties, parameters) = klass.primaryConstructor?.valueParameters?.partition {
it.hasValOrVar()
} ?: Pair(emptyList(), emptyList())

if (properties.isNotEmpty()) {
builder.appendLine(toParamsKdoc(keyword = "@property", params = properties))
}

if (parameters.isNotEmpty()) {
builder.appendLine("* @constructor")
.appendLine("* TODO")
.appendLine("*")
.appendLine(toParamsKdoc(params = parameters))
}

builder.appendLine("*/")
return builder.toString()
}
}
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<name>kodkod</name>
<vendor email="[email protected]" url="https://github.com/siosio">siosio</vendor>

<description><![CDATA[
<description>
<![CDATA[
generate kdoc plugin.
]]></description>

Expand Down

0 comments on commit 2503e96

Please sign in to comment.