Skip to content

Commit 7f818bc

Browse files
committed
Filter non-scala snippets. Report errors in parsing override flags
1 parent c8f03db commit 7f818bc

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

scaladoc/src/dotty/tools/scaladoc/site/templates.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ case class TemplateFile(
9393
// Snippet compiler currently supports markdown only
9494
val parser: Parser = Parser.builder(defaultMarkdownOptions).build()
9595
val parsedMd = parser.parse(rendered)
96-
val processed = FlexmarkSnippetProcessor.processSnippets(parsedMd, ssctx.snippetCompilerArgs.debug, snippetCheckingFunc)
96+
val processed = FlexmarkSnippetProcessor.processSnippets(parsedMd, ssctx.snippetCompilerArgs.debug, snippetCheckingFunc)(using ssctx.outerCtx)
9797
HtmlRenderer.builder(defaultMarkdownOptions).build().render(processed)
9898

9999
layoutTemplate match

scaladoc/src/dotty/tools/scaladoc/snippets/FlexmarkSnippetProcessor.scala

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import collection.JavaConverters._
1010
import dotty.tools.scaladoc.tasty.comments.markdown.ExtendedFencedCodeBlock
1111

1212
object FlexmarkSnippetProcessor:
13-
def processSnippets(root: mdu.Node, debug: Boolean, checkingFunc: => SnippetChecker.SnippetCheckingFunc): mdu.Node = {
13+
def processSnippets(root: mdu.Node, debug: Boolean, checkingFunc: => SnippetChecker.SnippetCheckingFunc)(using CompilerContext): mdu.Node = {
1414
lazy val cf: SnippetChecker.SnippetCheckingFunc = checkingFunc
1515

1616
val nodes = root.getDescendants().asScala.collect {
@@ -20,27 +20,37 @@ object FlexmarkSnippetProcessor:
2020
nodes.foreach { node =>
2121
val snippet = node.getContentChars.toString
2222
val lineOffset = node.getStartLineNumber
23-
val info = node.getInfo.toString
24-
val argOverride =
25-
info.split(" ")
26-
.find(_.startsWith("sc:"))
27-
.map(_.stripPrefix("sc:"))
28-
.map(SCFlagsParser.parse)
29-
.flatMap(_.toOption)
30-
val snippetCompilationResult = cf(snippet, lineOffset, argOverride) match {
31-
case result@Some(SnippetCompilationResult(wrapped, _, _, _)) if debug =>
32-
val s = sequence.BasedSequence.EmptyBasedSequence()
33-
.append(wrapped)
34-
.append(sequence.BasedSequence.EOL)
35-
val content = mdu.BlockContent()
36-
content.add(s, 0)
37-
node.setContent(content)
38-
result
39-
case result => result
40-
}
23+
val info = node.getInfo.toString.split(" ")
24+
if info.contains("scala") then {
25+
val argOverride =
26+
info
27+
.find(_.startsWith("sc:"))
28+
.map(_.stripPrefix("sc:"))
29+
.map(SCFlagsParser.parse)
30+
.flatMap(_ match {
31+
case Right(flags) => Some(flags)
32+
case Left(error) =>
33+
report.warning(
34+
s"""|Error occured during parsing flags in snippet:
35+
|$error""".stripMargin
36+
)
37+
None
38+
})
39+
val snippetCompilationResult = cf(snippet, lineOffset, argOverride) match {
40+
case result@Some(SnippetCompilationResult(wrapped, _, _, _)) if debug =>
41+
val s = sequence.BasedSequence.EmptyBasedSequence()
42+
.append(wrapped)
43+
.append(sequence.BasedSequence.EOL)
44+
val content = mdu.BlockContent()
45+
content.add(s, 0)
46+
node.setContent(content)
47+
result
48+
case result => result
49+
}
4150

42-
node.insertBefore(new ExtendedFencedCodeBlock(node, snippetCompilationResult))
43-
node.unlink()
51+
node.insertBefore(new ExtendedFencedCodeBlock(node, snippetCompilationResult))
52+
node.unlink()
53+
}
4454
}
4555

4656
root

0 commit comments

Comments
 (0)