Skip to content

Commit

Permalink
QDOC: improve documentation rendering for macro 2.0
Browse files Browse the repository at this point in the history
Previously, we didn't render header for macro 2.0 items as we do for other items.
Also, add some missing tests for macro documentation
  • Loading branch information
Undin committed Sep 10, 2021
1 parent adc136a commit c6cf9f4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
16 changes: 11 additions & 5 deletions src/main/kotlin/org/rust/ide/docs/RsDocumentationProvider.kt
Expand Up @@ -267,7 +267,7 @@ private fun RsDocAndAttributeOwner.header(buffer: StringBuilder) {
is RsNamedFieldDecl -> listOfNotNull((parent?.parent as? RsDocAndAttributeOwner)?.presentableQualifiedName)
is RsStructOrEnumItemElement,
is RsTraitItem,
is RsMacro -> listOfNotNull(presentableQualifiedModName)
is RsMacroDefinitionBase -> listOfNotNull(presentableQualifiedModName)
is RsAbstractable -> when (val owner = owner) {
is RsAbstractableOwner.Foreign,
is RsAbstractableOwner.Free -> listOfNotNull(presentableQualifiedModName)
Expand All @@ -287,8 +287,7 @@ fun RsDocAndAttributeOwner.signature(builder: StringBuilder) {
is RsNamedFieldDecl -> listOfNotNull(presentationInfo?.signatureText)
is RsFunction -> {
val buffer = StringBuilder()
declarationModifiers.joinTo(buffer, " ")
buffer += " "
declarationModifiers.joinTo(buffer, separator = " ", postfix = " ")
buffer.b { it += name }
typeParameterList?.generateDocumentation(buffer)
valueParameterList?.generateDocumentation(buffer)
Expand All @@ -297,7 +296,7 @@ fun RsDocAndAttributeOwner.signature(builder: StringBuilder) {
}
is RsConstant -> {
val buffer = StringBuilder()
declarationModifiers.joinTo(buffer, " ", "", " ")
declarationModifiers.joinTo(buffer, separator = " ", postfix = " ")
buffer.b { it += name }
typeReference?.generateDocumentation(buffer, ": ")
expr?.generateDocumentation(buffer, " = ")
Expand All @@ -308,14 +307,20 @@ fun RsDocAndAttributeOwner.signature(builder: StringBuilder) {
val name = name
if (name != null) {
val buffer = StringBuilder()
(this as RsItemElement).declarationModifiers.joinTo(buffer, " ", "", " ")
(this as RsItemElement).declarationModifiers.joinTo(buffer, separator = " ", postfix = " ")
buffer.b { it += name }
(this as RsGenericDeclaration).typeParameterList?.generateDocumentation(buffer)
(this as? RsTypeAlias)?.typeReference?.generateDocumentation(buffer, " = ")
listOf(buffer.toString()) + whereClause?.documentationText.orEmpty()
} else emptyList()
}
is RsMacro -> listOf("macro <b>$name</b>")
is RsMacro2 -> {
val buffer = StringBuilder()
declarationModifiers.joinTo(buffer, separator = " ", postfix = " ")
buffer.b { it += name }
listOf(buffer.toString())
}
is RsImplItem -> declarationText
else -> emptyList()
}
Expand Down Expand Up @@ -377,6 +382,7 @@ private val RsItemElement.declarationModifiers: List<String>
}
modifiers += "trait"
}
is RsMacro2 -> modifiers += "macro"
else -> error("unexpected type $javaClass")
}
return modifiers
Expand Down
43 changes: 37 additions & 6 deletions src/test/kotlin/org/rust/ide/docs/RsQuickDocumentationTest.kt
Expand Up @@ -732,8 +732,7 @@ class RsQuickDocumentationTest : RsDocumentationProviderTest() {
Module level docs</p></div>
""")

//
fun `test macro outer docstring`() = doTest("""
fun `test macro outer docstring 1`() = doTest("""
/// Outer documentation
macro_rules! makro {
//^
Expand All @@ -748,16 +747,48 @@ class RsQuickDocumentationTest : RsDocumentationProviderTest() {
<div class='content'><p>Outer documentation</p></div>
""")

fun `test macro 2 outer docs`() = doTest("""
fun `test macro outer docstring 2`() = doTest("""
pub mod foo {
/// Outer documentation
macro_rules! makro {
//^
() => { };
}
}
fn main() {
}
""", """
<div class='definition'><pre>test_package
macro <b>makro</b></pre></div>
<div class='content'><p>Outer documentation</p></div>
""")

fun `test macro 2 outer docs 1`() = doTest("""
pub struct Foo;
/// Outer doc
/// [link](Foo)
pub macro Bar() {}
pub macro bar() {}
//^
""", """
<div class='definition'><pre>test_package::Bar
</pre></div>
<div class='definition'><pre>test_package
pub macro <b>bar</b></pre></div>
<div class='content'><p>Outer doc
<a href="psi_element://Foo">link</a></p></div>
""")

fun `test macro 2 outer docs 2`() = doTest("""
pub mod foo_bar {
pub struct Foo;
/// Outer doc
/// [link](Foo)
pub macro bar() {}
} //^
""", """
<div class='definition'><pre>test_package::foo_bar
pub macro <b>bar</b></pre></div>
<div class='content'><p>Outer doc
<a href="psi_element://Foo">link</a></p></div>
""")
Expand Down

0 comments on commit c6cf9f4

Please sign in to comment.