aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Ryzhenkov <orangy@jetbrains.com>2014-07-12 03:55:20 +0400
committerIlya Ryzhenkov <orangy@jetbrains.com>2014-07-12 03:55:20 +0400
commita83488aae453f1bf01cfb5507317acf0b9ddfb82 (patch)
treeab1c3e2370480f182ac75a2016bf2dc95d1637bb
parentd627d2cfdfebcddd63669734efb82dfc66e7c7fe (diff)
downloaddokka-a83488aae453f1bf01cfb5507317acf0b9ddfb82.tar.gz
dokka-a83488aae453f1bf01cfb5507317acf0b9ddfb82.tar.bz2
dokka-a83488aae453f1bf01cfb5507317acf0b9ddfb82.zip
Implement section parsing.
-rw-r--r--src/Model/DocumentationContent.kt96
-rw-r--r--src/Model/Sections.kt43
-rw-r--r--test/data/comments/emptySection.kt6
-rw-r--r--test/data/comments/emptySectionOnOneLine.kt2
-rw-r--r--test/data/comments/multilineSection.kt7
-rw-r--r--test/data/comments/section1.kt5
-rw-r--r--test/data/comments/section2.kt6
-rw-r--r--test/data/comments/sectionOnOneLine.kt2
-rw-r--r--test/src/model/CommentTest.kt83
9 files changed, 207 insertions, 43 deletions
diff --git a/src/Model/DocumentationContent.kt b/src/Model/DocumentationContent.kt
new file mode 100644
index 00000000..ee91aed3
--- /dev/null
+++ b/src/Model/DocumentationContent.kt
@@ -0,0 +1,96 @@
+package org.jetbrains.dokka
+
+import org.jetbrains.jet.lang.descriptors.*
+import org.jetbrains.jet.lang.resolve.BindingContext
+
+class DocumentationContentSection(val label: String, val text: String) {
+ override fun toString(): String {
+ return "$label = $text"
+ }
+}
+
+class DocumentationContent(val summary: String, val sections: List<DocumentationContentSection>) {
+
+ override fun equals(other: Any?): Boolean {
+ if (other !is DocumentationContent)
+ return false
+ if (summary != other.summary)
+ return false
+ if (sections.size != other.sections.size)
+ return false
+ for (index in sections.indices)
+ if (sections[index] != other.sections[index])
+ return false
+
+ return true
+ }
+
+ override fun hashCode(): Int {
+ return summary.hashCode() + sections.map { it.hashCode() }.sum()
+ }
+
+ override fun toString(): String {
+ if (sections.isEmpty())
+ return summary
+ return "$summary | " + sections.joinToString()
+ }
+
+ class object {
+ val Empty = DocumentationContent("", listOf())
+ }
+}
+
+
+fun BindingContext.getDocumentation(descriptor: DeclarationDescriptor): DocumentationContent {
+ val docText = getDocumentationElements(descriptor).map { it.extractText() }.join("\n")
+
+ val sections = docText.parseSections()
+
+ return DocumentationContent(sections.first().text, sections.drop(1))
+}
+
+fun String.parseLabel(index: Int): Pair<String, Int> {
+ val c = get(index)
+ when {
+ Character.isJavaIdentifierStart(c) -> {
+ for (end in index + 1..length - 1) {
+ if (!Character.isJavaIdentifierPart(get(end))) {
+ return substring(index, end) to end
+ }
+ }
+ return substring(index, length) to length
+ }
+ c == '{' -> {
+ val end = indexOf('}', index + 1)
+ return substring(index, end) to index + 2
+ }
+ }
+ return "" to -1
+}
+
+fun String.parseSections(): List<DocumentationContentSection> {
+ val sections = arrayListOf<DocumentationContentSection>()
+ var currentLabel = ""
+ var currentSectionStart = 0
+ var currentIndex = 0
+
+ while (currentIndex < length) {
+ if (get(currentIndex) == '$') {
+ val (label, index) = parseLabel(currentIndex + 1)
+ if (index != -1) {
+ // section starts, add previous section
+ sections.add(DocumentationContentSection(currentLabel, substring(currentSectionStart, currentIndex).trim()))
+
+ currentLabel = label
+ currentIndex = index
+ currentSectionStart = currentIndex
+ continue
+ }
+ }
+ currentIndex++
+
+ }
+
+ sections.add(DocumentationContentSection(currentLabel, substring(currentSectionStart, currentIndex).trim()))
+ return sections
+} \ No newline at end of file
diff --git a/src/Model/Sections.kt b/src/Model/Sections.kt
deleted file mode 100644
index 733c0d2f..00000000
--- a/src/Model/Sections.kt
+++ /dev/null
@@ -1,43 +0,0 @@
-package org.jetbrains.dokka
-
-import org.jetbrains.jet.lang.descriptors.*
-import org.jetbrains.jet.lang.resolve.BindingContext
-
-fun BindingContext.getDocumentation(descriptor: DeclarationDescriptor): DocumentationContent {
- val docText = getDocumentationElements(descriptor).map { it.extractText() }.join("\n")
- return DocumentationContent(docText, listOf())
-}
-
-class DocumentationContentSection(val label: String, val text: String) {
-
-}
-
-class DocumentationContent(val summary: String, val sections: List<DocumentationContentSection>) {
-
- override fun equals(other: Any?): Boolean {
- if (other !is DocumentationContent)
- return false
- if (summary != other.summary)
- return false
- if (sections.size != other.sections.size)
- return false
- for (index in sections.indices)
- if (sections[index] != other.sections[index])
- return false
-
- return true
- }
-
- override fun hashCode(): Int {
- return summary.hashCode() + sections.map { it.hashCode() }.sum()
- }
-
- override fun toString(): String {
- return "$summary | " + sections.joinToString()
- }
-
- class object {
- val Empty = DocumentationContent("", listOf())
- }
-}
-
diff --git a/test/data/comments/emptySection.kt b/test/data/comments/emptySection.kt
new file mode 100644
index 00000000..0e618b2a
--- /dev/null
+++ b/test/data/comments/emptySection.kt
@@ -0,0 +1,6 @@
+
+/**
+ * Summary
+ * $one
+ */
+val property = "test" \ No newline at end of file
diff --git a/test/data/comments/emptySectionOnOneLine.kt b/test/data/comments/emptySectionOnOneLine.kt
new file mode 100644
index 00000000..4bb6c6f6
--- /dev/null
+++ b/test/data/comments/emptySectionOnOneLine.kt
@@ -0,0 +1,2 @@
+/** Summary $one*/
+val property = "test" \ No newline at end of file
diff --git a/test/data/comments/multilineSection.kt b/test/data/comments/multilineSection.kt
new file mode 100644
index 00000000..be83b641
--- /dev/null
+++ b/test/data/comments/multilineSection.kt
@@ -0,0 +1,7 @@
+/**
+ * Summary
+ * $one
+ * line one
+ * line two
+ */
+val property = "test" \ No newline at end of file
diff --git a/test/data/comments/section1.kt b/test/data/comments/section1.kt
new file mode 100644
index 00000000..0a27d394
--- /dev/null
+++ b/test/data/comments/section1.kt
@@ -0,0 +1,5 @@
+/**
+ * Summary
+ * $one section one
+ */
+val property = "test" \ No newline at end of file
diff --git a/test/data/comments/section2.kt b/test/data/comments/section2.kt
new file mode 100644
index 00000000..a34aa853
--- /dev/null
+++ b/test/data/comments/section2.kt
@@ -0,0 +1,6 @@
+/**
+ * Summary
+ * $one section one
+ * $two section two
+ */
+val property = "test" \ No newline at end of file
diff --git a/test/data/comments/sectionOnOneLine.kt b/test/data/comments/sectionOnOneLine.kt
new file mode 100644
index 00000000..cab5c69c
--- /dev/null
+++ b/test/data/comments/sectionOnOneLine.kt
@@ -0,0 +1,2 @@
+/** Summary $one same line */
+val property = "test" \ No newline at end of file
diff --git a/test/src/model/CommentTest.kt b/test/src/model/CommentTest.kt
index f7d313fe..6c835f8d 100644
--- a/test/src/model/CommentTest.kt
+++ b/test/src/model/CommentTest.kt
@@ -74,4 +74,87 @@ doc3""", doc.summary)
}
}
}
+
+ Test fun emptySection() {
+ verifyModel("test/data/comments/emptySection.kt") { model ->
+ with(model.nodes.single().members.single()) {
+ assertEquals("Summary", doc.summary)
+ assertEquals(1, doc.sections.count())
+ with (doc.sections.elementAt(0)) {
+ assertEquals("one", label)
+ assertEquals("", text)
+ }
+ }
+ }
+ }
+
+ Test fun section1() {
+ verifyModel("test/data/comments/section1.kt") { model ->
+ with(model.nodes.single().members.single()) {
+ assertEquals("Summary", doc.summary)
+ assertEquals(1, doc.sections.count())
+ with (doc.sections.elementAt(0)) {
+ assertEquals("one", label)
+ assertEquals("section one", text)
+ }
+ }
+ }
+ }
+
+ Test fun section2() {
+ verifyModel("test/data/comments/section2.kt") { model ->
+ with(model.nodes.single().members.single()) {
+ assertEquals("Summary", doc.summary)
+ assertEquals(2, doc.sections.count())
+ with (doc.sections.elementAt(0)) {
+ assertEquals("one", label)
+ assertEquals("section one", text)
+ }
+ with (doc.sections.elementAt(1)) {
+ assertEquals("two", label)
+ assertEquals("section two", text)
+ }
+ }
+ }
+ }
+
+ Test fun sectionOnOneLine() {
+ verifyModel("test/data/comments/sectionOnOneLine.kt") { model ->
+ with(model.nodes.single().members.single()) {
+ assertEquals("Summary", doc.summary)
+ assertEquals(1, doc.sections.count())
+ with (doc.sections.elementAt(0)) {
+ assertEquals("one", label)
+ assertEquals("same line", text)
+ }
+ }
+ }
+ }
+
+ Test fun emptySectionOnOneLine() {
+ verifyModel("test/data/comments/emptySectionOnOneLine.kt") { model ->
+ with(model.nodes.single().members.single()) {
+ assertEquals("Summary", doc.summary)
+ assertEquals(1, doc.sections.count())
+ with (doc.sections.elementAt(0)) {
+ assertEquals("one", label)
+ assertEquals("", text)
+ }
+ }
+ }
+ }
+
+ Test fun multilineSection() {
+ verifyModel("test/data/comments/multilineSection.kt") { model ->
+ with(model.nodes.single().members.single()) {
+ assertEquals("Summary", doc.summary)
+ assertEquals(1, doc.sections.count())
+ with (doc.sections.elementAt(0)) {
+ assertEquals("one", label)
+ assertEquals("""line one
+line two""", text)
+ }
+ }
+ }
+ }
}