Skip to content
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
11 changes: 11 additions & 0 deletions scaladoc-testcases/docs/_docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ val renderer: Renderer = Renderer()
extension (x: Self) def combine(y: Self): Self
```

```scala sc:fail sc-opts:-Werror:true
def exampleShouldError(input: Option[String]): Unit =
input match
case Some("foo") => ???
```

```scala sc:compile sc-opts:-Werror:false
def exampleShouldWarn(input: Option[String]): Unit =
input match
case Some("foo") => ???
```
4 changes: 2 additions & 2 deletions scaladoc/src/dotty/tools/scaladoc/site/templates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ case class TemplateFile(
val path = Some(Paths.get(file.getAbsolutePath))
val pathBasedArg = ssctx.snippetCompilerArgs.get(path)
val sourceFile = dotty.tools.dotc.util.SourceFile(dotty.tools.io.AbstractFile.getFile(path.get), scala.io.Codec.UTF8)
(str: String, lineOffset: SnippetChecker.LineOffset, argOverride: Option[SCFlags]) => {
val arg = argOverride.fold(pathBasedArg)(pathBasedArg.overrideFlag(_))
(str: String, lineOffset: SnippetChecker.LineOffset, argOverride: Option[SnippetCompilerArg]) => {
val arg = argOverride.fold(pathBasedArg)(pathBasedArg.merge(_))
val compilerData = SnippetCompilerData(
"staticsitesnippet",
SnippetCompilerData.Position(configOffset - 1, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object FlexmarkSnippetProcessor:
val lineOffset = node.getStartLineNumber + preparsed.fold(0)(_.strippedLinesBeforeNo)
val info = node.getInfo.toString.split(" ")
if info.contains("scala") then {
val argOverride = info
val flagOverride = info
.find(_.startsWith("sc:"))
.map(_.stripPrefix("sc:"))
.map(SCFlagsParser.parse)
Expand All @@ -34,6 +34,19 @@ object FlexmarkSnippetProcessor:
)
None
})

val scalacOptions: Seq[String] = info
.toIndexedSeq
.filter(_.startsWith("sc-opts:"))
.flatMap(_.stripPrefix("sc-opts:").split(",").map(_.trim).toSeq)

val argOverride: Option[SnippetCompilerArg] =
(flagOverride, scalacOptions) match {
case (None, Seq()) => None
case (Some(flag), opts) => Some(SnippetCompilerArg(flag, opts))
case (None, opts) => Some(SnippetCompilerArg(SCFlags.Compile, opts))
}

val id = info
.find(_.startsWith("sc-name:"))
.map(_.stripPrefix("sc-name:"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ class SnippetChecker(val args: Scaladoc.Args)(using cctx: CompilerContext):

object SnippetChecker:
type LineOffset = Int
type SnippetCheckingFunc = (String, LineOffset, Option[SCFlags]) => Option[SnippetCompilationResult]
type SnippetCheckingFunc = (String, LineOffset, Option[SnippetCompilerArg]) => Option[SnippetCompilationResult]
13 changes: 11 additions & 2 deletions scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ class SnippetCompiler(

private def additionalMessages(wrappedSnippet: WrappedSnippet, arg: SnippetCompilerArg, sourceFile: SourceFile, context: Context): Seq[SnippetCompilerMessage] = {
Option.when(arg.flag == SCFlags.Fail && !context.reporter.hasErrors)(
SnippetCompilerMessage(None, "Snippet should not compile but compiled successfully", MessageLevel.Error)
SnippetCompilerMessage(
Some(Position(SourcePosition(sourceFile, NoSpan), wrappedSnippet.outerLineOffset)),
"Snippet should not compile but compiled successfully", MessageLevel.Error)
).toList
}

Expand All @@ -100,12 +102,19 @@ class SnippetCompiler(
arg: SnippetCompilerArg,
sourceFile: SourceFile
): SnippetCompilationResult = {
val context = SnippetDriver.currentCtx.fresh
val baseContext = SnippetDriver.currentCtx.fresh
.setSetting(
SnippetDriver.currentCtx.settings.outputDir,
target
)
.setReporter(new StoreReporter)
val context =
if arg.scalacOptions.isEmpty then baseContext
else
val args = arg.scalacOptions.toArray
SnippetDriver.setup(args, baseContext) match
case Some((_, ctx)) => ctx
case None => baseContext
val run = newRun(using context)
run.compileFromStrings(List(wrappedSnippet.snippet))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package snippets

import java.nio.file.Path

case class SnippetCompilerArg(flag: SCFlags):
case class SnippetCompilerArg(flag: SCFlags, scalacOptions: Seq[String] = Seq.empty):
def overrideFlag(f: SCFlags): SnippetCompilerArg = copy(flag = f)
def withScalacOptions(opts: Seq[String]): SnippetCompilerArg = copy(scalacOptions = scalacOptions ++ opts)
def merge(other: SnippetCompilerArg): SnippetCompilerArg =
SnippetCompilerArg(other.flag, scalacOptions ++ other.scalacOptions)

enum SCFlags(val flagName: String):
case Compile extends SCFlags("compile")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ abstract class MarkupConversion[T](val repr: Repr)(using dctx: DocContext) {
val scDataCollector = SnippetCompilerDataCollector[qctx.type](qctx)
val data = scDataCollector.getSnippetCompilerData(s, s)
val sourceFile = scDataCollector.getSourceFile(s)
(str: String, lineOffset: SnippetChecker.LineOffset, argOverride: Option[SCFlags]) => {
val arg = argOverride.fold(pathBasedArg)(pathBasedArg.overrideFlag(_))
(str: String, lineOffset: SnippetChecker.LineOffset, argOverride: Option[SnippetCompilerArg]) => {
val arg = argOverride.fold(pathBasedArg)(pathBasedArg.merge(_))
val res = snippetChecker.checkSnippet(str, Some(data), arg, lineOffset, sourceFile)
res.filter(r => !r.isSuccessful).foreach(_.reportMessages()(using compilerContext))
res
Expand Down
Loading