diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Kotlin/ContentBuilder.kt | 64 | ||||
-rw-r--r-- | src/Kotlin/DocumentationBuilder.kt | 52 | ||||
-rw-r--r-- | src/Markdown/MarkdownProcessor.kt | 33 |
3 files changed, 87 insertions, 62 deletions
diff --git a/src/Kotlin/ContentBuilder.kt b/src/Kotlin/ContentBuilder.kt index 8079fb4c..462e886e 100644 --- a/src/Kotlin/ContentBuilder.kt +++ b/src/Kotlin/ContentBuilder.kt @@ -5,17 +5,16 @@ import org.jetbrains.jet.lang.descriptors.* import org.jetbrains.jet.lang.resolve.* import org.jetbrains.jet.lang.resolve.scopes.* import org.jetbrains.jet.lang.resolve.name.* -import net.nicoulaj.idea.markdown.lang.* +import org.intellij.markdown.* public fun DocumentationBuilder.buildContent(tree: MarkdownNode, descriptor: DeclarationDescriptor): Content { +// println(tree.toTestString()) val nodeStack = ArrayDeque<ContentNode>() nodeStack.push(Content()) tree.visit {(node, processChildren) -> val parent = nodeStack.peek()!! - val nodeType = node.type - val nodeText = tree.text - when (nodeType) { + when (node.type) { MarkdownElementTypes.UNORDERED_LIST -> { nodeStack.push(ContentList()) processChildren() @@ -46,33 +45,40 @@ public fun DocumentationBuilder.buildContent(tree: MarkdownNode, descriptor: Dec processChildren() parent.append(nodeStack.pop()) } - /* MarkdownElementTypes.ANONYMOUS_SECTION -> { - nodeStack.push(ContentSection("")) - 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)) - } - } - MarkdownElementTypes.NAMED_SECTION -> { - val label = tree.findChildByType(node, MarkdownElementTypes.SECTION_NAME)?.let { tree.getNodeText(it) } ?: "" - nodeStack.push(ContentSection(label)) - processChildren() - parent.append(nodeStack.pop()) - }*/ - MarkdownElementTypes.INLINE_LINK -> { - val target = node.child(MarkdownElementTypes.LINK_TITLE)?.let { it.text } ?: "" - val href = node.child(MarkdownElementTypes.LINK_DESTINATION)?.let { it.text } - val link = if (href != null) ContentExternalLink(href) else ContentExternalLink(target) - link.append(ContentText(target)) - parent.append(link) +/* + 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)) + } + } +*/ + MarkdownElementTypes.SECTION -> { + val label = node.child(MarkdownTokenTypes.SECTION_ID)?.let { it.text.trimLeading("$").trim("{","}") } ?: "" + nodeStack.push(ContentSection(label)) + processChildren() + parent.append(nodeStack.pop()) + } + MarkdownElementTypes.SHORT_REFERENCE_LINK -> { + val label = node.child(MarkdownElementTypes.LINK_LABEL) + val target = label?.child(MarkdownTokenTypes.TEXT) + if (target != null) { + val link = ContentExternalLink(target.text) + link.append(ContentText(target.text)) + parent.append(link) + } + } + MarkdownTokenTypes.WHITE_SPACE, + MarkdownTokenTypes.EOL -> { + if (nodeStack.peek() is ContentParagraph && node.parent?.children?.last() != node) { + nodeStack.push(ContentText(node.text)) + processChildren() + parent.append(nodeStack.pop()) + } } MarkdownTokenTypes.TEXT -> { - nodeStack.push(ContentText(nodeText)) + nodeStack.push(ContentText(node.text)) processChildren() parent.append(nodeStack.pop()) } diff --git a/src/Kotlin/DocumentationBuilder.kt b/src/Kotlin/DocumentationBuilder.kt index d8be9d5a..c2d28312 100644 --- a/src/Kotlin/DocumentationBuilder.kt +++ b/src/Kotlin/DocumentationBuilder.kt @@ -4,7 +4,6 @@ import org.jetbrains.jet.lang.descriptors.* import org.jetbrains.dokka.DocumentationNode.* import org.jetbrains.jet.lang.types.* import org.jetbrains.jet.lang.types.lang.* -import org.jetbrains.jet.lang.resolve.scopes.* import org.jetbrains.jet.lang.resolve.name.* import org.jetbrains.jet.lang.resolve.lazy.* @@ -218,6 +217,7 @@ class DocumentationBuilder(val session: ResolveSession, val options: Documentati fun ValueParameterDescriptor.build(): DocumentationNode { val node = DocumentationNode(this, Kind.Parameter) node.appendType(getType()) + register(this, node) return node } @@ -301,9 +301,9 @@ class DocumentationBuilder(val session: ResolveSession, val options: Documentati } } - fun getResolutionScope(node: DocumentationNode): JetScope { + fun getResolutionScope(node: DocumentationNode): DeclarationDescriptor { val descriptor = nodeToDescriptor[node] ?: throw IllegalArgumentException("Node is not known to this context") - return getResolutionScope(descriptor) + return descriptor } fun resolveContentLinks(node: DocumentationNode, content: ContentNode) { @@ -311,26 +311,38 @@ class DocumentationBuilder(val session: ResolveSession, val options: Documentati for (child in snapshot) { if (child is ContentExternalLink) { val referenceText = child.href - if (Name.isValidIdentifier(referenceText)) { - val scope = getResolutionScope(node) - val symbolName = Name.guess(referenceText) - val symbol = scope.getLocalVariable(symbolName) ?: - scope.getProperties(symbolName).firstOrNull() ?: - scope.getFunctions(symbolName).firstOrNull() ?: - scope.getClassifier(symbolName) - - if (symbol != null) { - val targetNode = descriptorToNode[symbol] - val contentLink = if (targetNode != null) ContentNodeLink(targetNode) else ContentExternalLink("#") - - val index = content.children.indexOf(child) - content.children.remove(index) - contentLink.children.addAll(child.children) - content.children.add(index, contentLink) - } + val symbol = resolveReference(getResolutionScope(node), referenceText) + if (symbol != null) { + val targetNode = descriptorToNode[symbol] + val contentLink = if (targetNode != null) ContentNodeLink(targetNode) else ContentExternalLink("#") + + val index = content.children.indexOf(child) + content.children.remove(index) + contentLink.children.addAll(child.children) + content.children.add(index, contentLink) } + } resolveContentLinks(node, child) } } + + private fun resolveReference(context: DeclarationDescriptor, reference: String): DeclarationDescriptor? { + if (Name.isValidIdentifier(reference)) { + val scope = getResolutionScope(context) + val symbolName = Name.guess(reference) + return scope.getLocalVariable(symbolName) ?: + scope.getProperties(symbolName).firstOrNull() ?: + scope.getFunctions(symbolName).firstOrNull() ?: + scope.getClassifier(symbolName) + + } + + val names = reference.split('.') + val result = names.fold<String, DeclarationDescriptor?>(context) {(nextContext, name) -> + nextContext?.let { resolveReference(it, name) } + } + + return result + } }
\ No newline at end of file diff --git a/src/Markdown/MarkdownProcessor.kt b/src/Markdown/MarkdownProcessor.kt index b5e18f92..05c4a7ec 100644 --- a/src/Markdown/MarkdownProcessor.kt +++ b/src/Markdown/MarkdownProcessor.kt @@ -1,17 +1,19 @@ package org.jetbrains.dokka -import net.nicoulaj.idea.markdown.lang.ast.* -import net.nicoulaj.idea.markdown.lang.parser.dialects.commonmark.* -import net.nicoulaj.idea.markdown.lang.parser.* -import net.nicoulaj.idea.markdown.lang.* +import org.intellij.markdown.* +import org.intellij.markdown.ast.* +import org.intellij.markdown.parser.* +import org.intellij.markdown.parser.dialects.KDocMarkerProcessor -class MarkdownNode(val node: ASTNode, val markdown: String) { - val children: List<MarkdownNode> get() = node.children.map { MarkdownNode(it, markdown) } +class MarkdownNode(val node: ASTNode, val parent: MarkdownNode?, val markdown: String) { + val children: List<MarkdownNode> = node.children.map { MarkdownNode(it, this, markdown) } val endOffset: Int get() = node.endOffset val startOffset: Int get() = node.startOffset val type: IElementType get() = node.type val text: String get() = markdown.substring(startOffset, endOffset) fun child(type: IElementType): MarkdownNode? = children.firstOrNull { it.type == type } + + override fun toString(): String = present() } fun MarkdownNode.visit(action: (MarkdownNode, () -> Unit) -> Unit) { @@ -27,9 +29,7 @@ public fun MarkdownNode.toTestString(): String { var level = 0 visit {(node, visitChildren) -> sb.append(" ".repeat(level * 2)) - sb.append(node.type.toString()) - sb.append(":" + node.text.replace("\n", "\u23CE")) - sb.appendln() + node.presentTo(sb) level++ visitChildren() level-- @@ -37,6 +37,13 @@ public fun MarkdownNode.toTestString(): String { return sb.toString() } +private fun MarkdownNode.present() = StringBuilder { presentTo(this) }.toString() +private fun MarkdownNode.presentTo(sb: StringBuilder) { + sb.append(type.toString()) + sb.append(":" + text.replace("\n", "\u23CE")) + sb.appendln() +} + public fun MarkdownNode.toHtml(): String { val sb = StringBuilder() visit {(node, processChildren) -> @@ -126,14 +133,14 @@ public fun MarkdownNode.toHtml(): String { fun parseMarkdown(markdown: String): MarkdownNode { if (markdown.isEmpty()) - return MarkdownNode(LeafASTNode(MarkdownElementTypes.MARKDOWN_FILE, 0, 0), markdown) - return MarkdownNode(MarkdownParser(CommonMarkMarkerProcessor()).buildMarkdownTreeFromString(markdown), markdown) + return MarkdownNode(LeafASTNode(MarkdownElementTypes.MARKDOWN_FILE, 0, 0), null, markdown) + return MarkdownNode(MarkdownParser(KDocMarkerProcessor.Factory()).buildMarkdownTreeFromString(markdown), null, markdown) } fun markdownToHtml(markdown: String): String { - val tree = MarkdownParser(CommonMarkMarkerProcessor()).buildMarkdownTreeFromString(markdown) - val markdownTree = MarkdownNode(tree, markdown) + val tree = MarkdownParser(KDocMarkerProcessor.Factory()).buildMarkdownTreeFromString(markdown) + val markdownTree = MarkdownNode(tree, null, markdown) val ast = markdownTree.toTestString() return markdownTree.toHtml() } |