Skip to content

Commit fbeda00

Browse files
KacperFKorbanWojciechMazur
authored andcommitted
[scaladoc] fix: Only trim one newline when preprocessing the content of a markdown code snippet
[Cherry-picked 0dac210]
1 parent 4ee9d5a commit fbeda00

File tree

3 files changed

+74
-66
lines changed

3 files changed

+74
-66
lines changed

Diff for: scaladoc-testcases/docs/_docs/index.md

+7
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@ class Renderer(using RenderingContext)
1313
val renderer: Renderer = Renderer()
1414
```
1515

16+
```scala
17+
trait Ord:
18+
type Self
1619

20+
trait SemiGroup:
21+
type Self
22+
extension (x: Self) def combine(y: Self): Self
23+
```
1724

Diff for: scaladoc/src/dotty/tools/scaladoc/renderers/Renderer.scala

+66-65
Original file line numberDiff line numberDiff line change
@@ -30,71 +30,72 @@ abstract class Renderer(rootPackage: Member, val members: Map[DRI, Member], prot
3030

3131
val rootApiPage: Option[Page] = Some(memberPage(rootPackage)).filter(_.children.nonEmpty).map(_.withTitle(ctx.args.name))
3232

33-
val rootDocsPage: Option[Page] = staticSite match
34-
case None => None
35-
case Some(siteContext) =>
36-
val rootTemplate = siteContext.staticSiteRoot.rootTemplate
37-
38-
// Below code is for walking in order the tree and modifing its nodes basing on its neighbours
39-
40-
// We add dummy guards
41-
val notHidden: Seq[Option[LoadedTemplate]] = None +: siteContext.allTemplates.filterNot(_.hidden).map(Some(_)) :+ None
42-
43-
// Let's gather the list of maps for each template with its in-order neighbours
44-
val newSettings: List[Map[String, Object]] = notHidden.sliding(size = 3, step = 1).map {
45-
case None :: None :: Nil =>
46-
Map.empty
47-
case prev :: mid :: next :: Nil =>
48-
def link(sibling: Option[LoadedTemplate]): Option[String] =
49-
def realPath(path: Path) = if Files.isDirectory(path) then Paths.get(path.toString, "index.html") else path
50-
sibling.map { n =>
51-
val realMidPath = realPath(mid.get.file.toPath)
52-
val realSiblingPath = realPath(n.file.toPath)
53-
realMidPath.relativize(realSiblingPath).toString.stripPrefix("../")
54-
}
55-
List(
56-
for {
57-
link <- link(prev)
58-
p <- prev
59-
} yield (
60-
"previous" -> Map(
61-
"title" -> p.templateFile.title.name,
62-
"url" -> link
63-
)
64-
),
65-
for {
66-
link <- link(next)
67-
n <- next
68-
} yield (
69-
"next" -> Map(
70-
"title" -> n.templateFile.title.name,
71-
"url" -> link
72-
)
73-
),
74-
).flatten.toMap
75-
}.toList
76-
77-
def updateSettings(templates: Seq[LoadedTemplate], additionalSettings: ListBuffer[Map[String, Object]]): List[LoadedTemplate] =
78-
val updatedTemplates = List.newBuilder[LoadedTemplate]
79-
for template <- templates do
80-
val head: Map[String, Object] =
81-
if template.hidden then Map.empty
82-
else additionalSettings.remove(0)
83-
val current: Map[String, Object] = template.templateFile.settings.getOrElse("page", Map.empty).asInstanceOf[Map[String, Object]]
84-
val updatedTemplateFile = template.templateFile.copy(settings = template.templateFile.settings.updated("page", head ++ current))
85-
updatedTemplates += template.copy(
86-
templateFile = updatedTemplateFile,
87-
children = updateSettings(template.children, additionalSettings)
88-
)
89-
updatedTemplates.result()
90-
91-
val newTemplates = updateSettings(Seq(rootTemplate), newSettings.to(ListBuffer))
92-
val templatePages = newTemplates.map(templateToPage(_, siteContext))
93-
94-
val newRoot = newTemplates.head
95-
96-
Some(newRoot).filter(r => r.children.nonEmpty || r.templateFile.rawCode.nonEmpty)
97-
.map(templateToPage(_, siteContext))
33+
val rootDocsPage: Option[Page] = staticSite match {
34+
case None => None
35+
case Some(siteContext) =>
36+
val rootTemplate = siteContext.staticSiteRoot.rootTemplate
37+
38+
// Below code is for walking in order the tree and modifing its nodes basing on its neighbours
39+
40+
// We add dummy guards
41+
val notHidden: Seq[Option[LoadedTemplate]] = None +: siteContext.allTemplates.filterNot(_.hidden).map(Some(_)) :+ None
42+
43+
// Let's gather the list of maps for each template with its in-order neighbours
44+
val newSettings: List[Map[String, Object]] = notHidden.sliding(size = 3, step = 1).map {
45+
case None :: None :: Nil =>
46+
Map.empty
47+
case prev :: mid :: next :: Nil =>
48+
def link(sibling: Option[LoadedTemplate]): Option[String] =
49+
def realPath(path: Path) = if Files.isDirectory(path) then Paths.get(path.toString, "index.html") else path
50+
sibling.map { n =>
51+
val realMidPath = realPath(mid.get.file.toPath)
52+
val realSiblingPath = realPath(n.file.toPath)
53+
realMidPath.relativize(realSiblingPath).toString.stripPrefix("../")
54+
}
55+
List(
56+
for {
57+
link <- link(prev)
58+
p <- prev
59+
} yield (
60+
"previous" -> Map(
61+
"title" -> p.templateFile.title.name,
62+
"url" -> link
63+
)
64+
),
65+
for {
66+
link <- link(next)
67+
n <- next
68+
} yield (
69+
"next" -> Map(
70+
"title" -> n.templateFile.title.name,
71+
"url" -> link
72+
)
73+
),
74+
).flatten.toMap
75+
}.toList
76+
77+
def updateSettings(templates: Seq[LoadedTemplate], additionalSettings: ListBuffer[Map[String, Object]]): List[LoadedTemplate] =
78+
val updatedTemplates = List.newBuilder[LoadedTemplate]
79+
for template <- templates do
80+
val head: Map[String, Object] =
81+
if template.hidden then Map.empty
82+
else additionalSettings.remove(0)
83+
val current: Map[String, Object] = template.templateFile.settings.getOrElse("page", Map.empty).asInstanceOf[Map[String, Object]]
84+
val updatedTemplateFile = template.templateFile.copy(settings = template.templateFile.settings.updated("page", head ++ current))
85+
updatedTemplates += template.copy(
86+
templateFile = updatedTemplateFile,
87+
children = updateSettings(template.children, additionalSettings)
88+
)
89+
updatedTemplates.result()
90+
91+
val newTemplates = updateSettings(Seq(rootTemplate), newSettings.to(ListBuffer))
92+
val templatePages = newTemplates.map(templateToPage(_, siteContext))
93+
94+
val newRoot = newTemplates.head
95+
96+
Some(newRoot).filter(r => r.children.nonEmpty || r.templateFile.rawCode.nonEmpty)
97+
.map(templateToPage(_, siteContext))
98+
}
9899

99100
val redirectPages: Seq[Page] = staticSite.fold(Seq.empty)(siteContext => siteContext.redirectTemplates.map {
100101
case (template, driFrom, driTo) =>

Diff for: scaladoc/src/dotty/tools/scaladoc/snippets/FlexmarkSnippetProcessor.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ object FlexmarkSnippetProcessor:
6565
content.add(s, 0)
6666
node.setContent(content)
6767

68-
val fullSnippet = Seq(snippetImports, snippet).mkString("\n").trim
68+
val fullSnippet = Seq(snippetImports, snippet).mkString("\n").stripPrefix("\n")
6969
val snippetCompilationResult = cf(fullSnippet, lineOffset, argOverride) match {
7070
case Some(result @ SnippetCompilationResult(wrapped, _, _, messages)) =>
7171
node.setContentString(fullSnippet)

0 commit comments

Comments
 (0)