aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Kotlin/ContentBuilder.kt34
-rw-r--r--src/Languages/LanguageService.kt8
-rw-r--r--test/data/comments/directive.kt35
-rw-r--r--test/src/TestAPI.kt6
-rw-r--r--test/src/model/CommentTest.kt35
5 files changed, 108 insertions, 10 deletions
diff --git a/src/Kotlin/ContentBuilder.kt b/src/Kotlin/ContentBuilder.kt
index 0143a46e..e243ff2c 100644
--- a/src/Kotlin/ContentBuilder.kt
+++ b/src/Kotlin/ContentBuilder.kt
@@ -6,6 +6,7 @@ import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.name.*
import org.intellij.markdown.*
+import org.jetbrains.jet.lang.psi.*
public fun DocumentationBuilder.buildContent(tree: MarkdownNode, descriptor: DeclarationDescriptor): Content {
// println(tree.toTestString())
@@ -45,17 +46,18 @@ public fun DocumentationBuilder.buildContent(tree: MarkdownNode, descriptor: Dec
processChildren()
parent.append(nodeStack.pop())
}
-/*
- MarkdownElementTypes.DIRECTIVE -> {
- val name = tree.findChildByType(node, MarkdownElementTypes.DIRECTIVE_NAME)?.let { tree.getNodeText(it) } ?: ""
- val params = tree.findChildByType(node, MarkdownElementTypes.DIRECTIVE_PARAMS)?.let { tree.getNodeText(it) } ?: ""
- when (name) {
- "code" -> parent.append(functionBody(descriptor, params))
+ MarkdownTokenTypes.SECTION_ID -> {
+ if (parent !is ContentSection) {
+ val text = node.text.trimLeading("$").trim("{", "}")
+ val name = text.substringBefore(" ")
+ val params = text.substringAfter(" ")
+ when (name) {
+ "code" -> parent.append(functionBody(descriptor, params))
+ }
}
}
-*/
MarkdownElementTypes.SECTION -> {
- val label = node.child(MarkdownTokenTypes.SECTION_ID)?.let { it.text.trimLeading("$").trim("{","}") } ?: ""
+ val label = node.child(MarkdownTokenTypes.SECTION_ID)?.let { it.text.trimLeading("$").trim("{", "}") } ?: ""
nodeStack.push(ContentSection(label))
processChildren()
parent.append(nodeStack.pop())
@@ -132,7 +134,21 @@ fun DocumentationBuilder.functionBody(descriptor: DeclarationDescriptor, functio
if (psiElement == null)
return ContentBlockCode().let() { it.append(ContentText("Source not found: $functionName")); it }
- return ContentBlockCode().let() { it.append(ContentText(psiElement.getText())); it }
+ val text = when (psiElement) {
+ is JetDeclarationWithBody -> ContentBlockCode().let() {
+ val bodyExpression = psiElement.getBodyExpression()
+ when (bodyExpression) {
+ is JetBlockExpression -> bodyExpression.getText().trim("{", "}")
+ else -> bodyExpression.getText()
+ }
+ }
+ else -> psiElement.getText()
+ }
+
+ val lines = text.trimTrailing().split("\n").filterNot { it.length() == 0 }
+ val indent = lines.map { it.takeWhile { it.isWhitespace() }.count() }.min() ?: 0
+ val finalText = lines.map { it.drop(indent) }.join("\n")
+ return ContentBlockCode().let() { it.append(ContentText(finalText)); it }
}
private fun DocumentationBuilder.resolveInScope(functionName: String, scope: JetScope): DeclarationDescriptor? {
diff --git a/src/Languages/LanguageService.kt b/src/Languages/LanguageService.kt
index 191f12c4..97f8e621 100644
--- a/src/Languages/LanguageService.kt
+++ b/src/Languages/LanguageService.kt
@@ -14,9 +14,15 @@ trait LanguageService {
/**
* Renders [node] as a named representation in the target language
*
+ * For example:
+ * ${code org.jetbrains.dokka.example}
+ *
* $node: A [DocumentationNode] to render
* $returns: [String] which is a string representation of the node's name
*/
- fun renderName(node: DocumentationNode) : String
+ fun renderName(node: DocumentationNode): String
}
+fun example(service: LanguageService, node: DocumentationNode) {
+ println("Node name: ${service.renderName(node)}")
+} \ No newline at end of file
diff --git a/test/data/comments/directive.kt b/test/data/comments/directive.kt
new file mode 100644
index 00000000..9883b7bd
--- /dev/null
+++ b/test/data/comments/directive.kt
@@ -0,0 +1,35 @@
+/**
+ * Summary
+ *
+ * ${code example1}
+ * ${code example2}
+ * ${code X.example3}
+ * ${code X.Y.example4}
+ */
+val property = "test"
+
+fun example1(node: String) = if (true) {
+ println(property)
+}
+
+fun example2(node: String) {
+ if (true) {
+ println(property)
+ }
+}
+
+class X {
+ fun example3(node: String) {
+ if (true) {
+ println(property)
+ }
+ }
+
+ class Y {
+ fun example4(node: String) {
+ if (true) {
+ println(property)
+ }
+ }
+ }
+}
diff --git a/test/src/TestAPI.kt b/test/src/TestAPI.kt
index 901e78fc..7d0d3bdd 100644
--- a/test/src/TestAPI.kt
+++ b/test/src/TestAPI.kt
@@ -73,6 +73,12 @@ fun StringBuilder.appendNode(node: ContentNode): StringBuilder {
append(node.text)
}
is ContentEmphasis -> append("*").appendChildren(node).append("*")
+ is ContentBlockCode -> {
+ appendln("[code]")
+ appendChildren(node)
+ appendln()
+ appendln("[/code]")
+ }
is ContentNodeLink -> {
append("[")
appendChildren(node)
diff --git a/test/src/model/CommentTest.kt b/test/src/model/CommentTest.kt
index 2d87b928..63a957e7 100644
--- a/test/src/model/CommentTest.kt
+++ b/test/src/model/CommentTest.kt
@@ -142,4 +142,39 @@ line two""", toTestString())
}
}
}
+
+ Test fun directive() {
+ verifyModel("test/data/comments/directive.kt") { model ->
+ with(model.members.single().members.first()) {
+ assertEquals("Summary", content.summary.toTestString())
+ assertEquals(2, content.sections.count())
+ with (content.description) {
+ assertEquals("""[code]
+if (true) {
+ println(property)
+}
+[/code]
+
+[code]
+if (true) {
+ println(property)
+}
+[/code]
+
+[code]
+if (true) {
+ println(property)
+}
+[/code]
+
+[code]
+if (true) {
+ println(property)
+}
+[/code]
+""", toTestString())
+ }
+ }
+ }
+ }
}