Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[scaladoc] fix: Only trim one newline when preprocessing the content of a markdown code snippet #21519

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions scaladoc-testcases/docs/_docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ class Renderer(using RenderingContext)
val renderer: Renderer = Renderer()
```

```scala
trait Ord:
type Self

trait SemiGroup:
type Self
extension (x: Self) def combine(y: Self): Self
```

131 changes: 66 additions & 65 deletions scaladoc/src/dotty/tools/scaladoc/renderers/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,71 +30,72 @@ abstract class Renderer(rootPackage: Member, val members: Map[DRI, Member], prot

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

val rootDocsPage: Option[Page] = staticSite match
case None => None
case Some(siteContext) =>
val rootTemplate = siteContext.staticSiteRoot.rootTemplate

// Below code is for walking in order the tree and modifing its nodes basing on its neighbours

// We add dummy guards
val notHidden: Seq[Option[LoadedTemplate]] = None +: siteContext.allTemplates.filterNot(_.hidden).map(Some(_)) :+ None

// Let's gather the list of maps for each template with its in-order neighbours
val newSettings: List[Map[String, Object]] = notHidden.sliding(size = 3, step = 1).map {
case None :: None :: Nil =>
Map.empty
case prev :: mid :: next :: Nil =>
def link(sibling: Option[LoadedTemplate]): Option[String] =
def realPath(path: Path) = if Files.isDirectory(path) then Paths.get(path.toString, "index.html") else path
sibling.map { n =>
val realMidPath = realPath(mid.get.file.toPath)
val realSiblingPath = realPath(n.file.toPath)
realMidPath.relativize(realSiblingPath).toString.stripPrefix("../")
}
List(
for {
link <- link(prev)
p <- prev
} yield (
"previous" -> Map(
"title" -> p.templateFile.title.name,
"url" -> link
)
),
for {
link <- link(next)
n <- next
} yield (
"next" -> Map(
"title" -> n.templateFile.title.name,
"url" -> link
)
),
).flatten.toMap
}.toList

def updateSettings(templates: Seq[LoadedTemplate], additionalSettings: ListBuffer[Map[String, Object]]): List[LoadedTemplate] =
val updatedTemplates = List.newBuilder[LoadedTemplate]
for template <- templates do
val head: Map[String, Object] =
if template.hidden then Map.empty
else additionalSettings.remove(0)
val current: Map[String, Object] = template.templateFile.settings.getOrElse("page", Map.empty).asInstanceOf[Map[String, Object]]
val updatedTemplateFile = template.templateFile.copy(settings = template.templateFile.settings.updated("page", head ++ current))
updatedTemplates += template.copy(
templateFile = updatedTemplateFile,
children = updateSettings(template.children, additionalSettings)
)
updatedTemplates.result()

val newTemplates = updateSettings(Seq(rootTemplate), newSettings.to(ListBuffer))
val templatePages = newTemplates.map(templateToPage(_, siteContext))

val newRoot = newTemplates.head

Some(newRoot).filter(r => r.children.nonEmpty || r.templateFile.rawCode.nonEmpty)
.map(templateToPage(_, siteContext))
val rootDocsPage: Option[Page] = staticSite match {
case None => None
case Some(siteContext) =>
val rootTemplate = siteContext.staticSiteRoot.rootTemplate

// Below code is for walking in order the tree and modifing its nodes basing on its neighbours

// We add dummy guards
val notHidden: Seq[Option[LoadedTemplate]] = None +: siteContext.allTemplates.filterNot(_.hidden).map(Some(_)) :+ None

// Let's gather the list of maps for each template with its in-order neighbours
val newSettings: List[Map[String, Object]] = notHidden.sliding(size = 3, step = 1).map {
case None :: None :: Nil =>
Map.empty
case prev :: mid :: next :: Nil =>
def link(sibling: Option[LoadedTemplate]): Option[String] =
def realPath(path: Path) = if Files.isDirectory(path) then Paths.get(path.toString, "index.html") else path
sibling.map { n =>
val realMidPath = realPath(mid.get.file.toPath)
val realSiblingPath = realPath(n.file.toPath)
realMidPath.relativize(realSiblingPath).toString.stripPrefix("../")
}
List(
for {
link <- link(prev)
p <- prev
} yield (
"previous" -> Map(
"title" -> p.templateFile.title.name,
"url" -> link
)
),
for {
link <- link(next)
n <- next
} yield (
"next" -> Map(
"title" -> n.templateFile.title.name,
"url" -> link
)
),
).flatten.toMap
}.toList

def updateSettings(templates: Seq[LoadedTemplate], additionalSettings: ListBuffer[Map[String, Object]]): List[LoadedTemplate] =
val updatedTemplates = List.newBuilder[LoadedTemplate]
for template <- templates do
val head: Map[String, Object] =
if template.hidden then Map.empty
else additionalSettings.remove(0)
val current: Map[String, Object] = template.templateFile.settings.getOrElse("page", Map.empty).asInstanceOf[Map[String, Object]]
val updatedTemplateFile = template.templateFile.copy(settings = template.templateFile.settings.updated("page", head ++ current))
updatedTemplates += template.copy(
templateFile = updatedTemplateFile,
children = updateSettings(template.children, additionalSettings)
)
updatedTemplates.result()

val newTemplates = updateSettings(Seq(rootTemplate), newSettings.to(ListBuffer))
val templatePages = newTemplates.map(templateToPage(_, siteContext))

val newRoot = newTemplates.head

Some(newRoot).filter(r => r.children.nonEmpty || r.templateFile.rawCode.nonEmpty)
.map(templateToPage(_, siteContext))
}
Comment on lines +33 to +98
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only changed a wrong indentation and added braces here.


val redirectPages: Seq[Page] = staticSite.fold(Seq.empty)(siteContext => siteContext.redirectTemplates.map {
case (template, driFrom, driTo) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ object FlexmarkSnippetProcessor:
content.add(s, 0)
node.setContent(content)

val fullSnippet = Seq(snippetImports, snippet).mkString("\n").trim
val fullSnippet = Seq(snippetImports, snippet).mkString("\n").stripPrefix("\n")
val snippetCompilationResult = cf(fullSnippet, lineOffset, argOverride) match {
case Some(result @ SnippetCompilationResult(wrapped, _, _, messages)) =>
node.setContentString(fullSnippet)
Expand Down
Loading