Skip to content

Commit 310a90b

Browse files
committed
fix: don't add extra newlines when there aren't extra sections in doc markdown
Currently if you have a simple scaladoc and you use `renderAsMarkdown` (which Metals does) to render markdown for hovers, there are extra newlines added that aren't necessary. Normally you can't tell as it seems some editors just trim off the extra newlines, but you can see in certain editors and in the REPL as well. _Notice how it removes two lines_ ```diff scala> :doc CanEqual Companion object containing a few universally known `CanEqual` instances. CanEqual instances involving primitive types or the Null type are handled directly in the compiler (see Implicits.synthesizedCanEqual), so they are not included here. - - ``` This just ensures that we add the extra two newlines only when we know we have more sections coming. fixes scalameta/metals#3740
1 parent b90752d commit 310a90b

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

compiler/src/dotty/tools/dotc/util/ParsedComment.scala

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,26 @@ class ParsedComment(val comment: Comment) {
5353
*
5454
* The different sections are formatted according to the mapping in `knownTags`.
5555
*/
56-
def renderAsMarkdown(using Context): String = {
56+
def renderAsMarkdown(using Context): String =
5757
val buf = new StringBuilder
58-
buf.append(mainDoc + System.lineSeparator + System.lineSeparator)
58+
buf.append(mainDoc)
5959
val groupedSections = CommentParsing.groupedSections(content, tagIndex)
6060

61-
for {
61+
val sections = for {
6262
(tag, formatter) <- ParsedComment.knownTags
6363
boundss <- groupedSections.get(tag)
6464
texts = boundss.map { case (start, end) => clean(content.slice(start, end)) }
6565
formatted <- formatter(texts)
66-
}
67-
{
68-
buf.append(formatted)
69-
buf.append(System.lineSeparator)
70-
}
71-
66+
} yield formatted
67+
68+
if sections.nonEmpty then
69+
buf.append(System.lineSeparator + System.lineSeparator)
70+
sections.foreach { section =>
71+
buf.append(section)
72+
buf.append(System.lineSeparator)
73+
}
7274
buf.toString
73-
}
75+
end renderAsMarkdown
7476

7577
/**
7678
* The `@param` section corresponding to `name`.

0 commit comments

Comments
 (0)