diff --git a/scaladoc/src/dotty/tools/scaladoc/DocContext.scala b/scaladoc/src/dotty/tools/scaladoc/DocContext.scala index daddd1a70d4a..ee1444effc4a 100644 --- a/scaladoc/src/dotty/tools/scaladoc/DocContext.scala +++ b/scaladoc/src/dotty/tools/scaladoc/DocContext.scala @@ -76,13 +76,7 @@ case class DocContext(args: Scaladoc.Args, compilerContext: CompilerContext): lazy val snippetCompilerArgs = snippets.SnippetCompilerArgs.load(args.snippetCompiler, args.snippetCompilerDebug)(using compilerContext) - lazy val snippetChecker = snippets.SnippetChecker( - args.classpath, - args.bootclasspath, - args.tastyFiles, - compilerContext.settings.scalajs.value(using compilerContext), - compilerContext.settings.usejavacp.value(using compilerContext) - ) + lazy val snippetChecker = snippets.SnippetChecker(args)(using compilerContext) lazy val staticSiteContext = args.docsRoot.map(path => StaticSiteContext( File(path).getAbsoluteFile(), diff --git a/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetChecker.scala b/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetChecker.scala index 3fe12ea6f1b3..506adccbec0e 100644 --- a/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetChecker.scala +++ b/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetChecker.scala @@ -7,27 +7,28 @@ import java.io.File import dotty.tools.io.AbstractFile import dotty.tools.dotc.fromtasty.TastyFileUtil +import dotty.tools.dotc.config.Settings._ +import dotty.tools.dotc.config.ScalaSettings -class SnippetChecker(val classpath: String, val bootclasspath: String, val tastyFiles: Seq[File], isScalajs: Boolean, useJavaCp: Boolean): +class SnippetChecker(val args: Scaladoc.Args)(using cctx: CompilerContext): + +// (val classpath: String, val bootclasspath: String, val tastyFiles: Seq[File], isScalajs: Boolean, useJavaCp: Boolean): private val sep = System.getProperty("path.separator") private val fullClasspath = List( - tastyFiles + args.tastyFiles .map(_.getAbsolutePath()) .map(AbstractFile.getFile(_)) .flatMap(t => try { TastyFileUtil.getClassPath(t) } catch { case e: AssertionError => Seq() }) .distinct.mkString(sep), - classpath + args.classpath ).mkString(sep) - private val scalacOptions = Seq( - Option.when(!fullClasspath.isEmpty)("-cp " + fullClasspath), - Option.when(!bootclasspath.isEmpty)("-bootclasspath " + bootclasspath), - Option.when(isScalajs)("-scalajs"), - Option.when(useJavaCp)("-usejavacp") - ).flatten.mkString(" ") + private val snippetCompilerSettings: Seq[SnippetCompilerSetting[_]] = cctx.settings.userSetSettings(cctx.settingsState).map( s => + SnippetCompilerSetting(s, s.valueIn(cctx.settingsState)) + ) :+ SnippetCompilerSetting(cctx.settings.classpath, fullClasspath) - private val compiler: SnippetCompiler = SnippetCompiler(scalacOptions = scalacOptions) + private val compiler: SnippetCompiler = SnippetCompiler(snippetCompilerSettings = snippetCompilerSettings) // These constants were found empirically to make snippet compiler // report errors in the same position as main compiler. diff --git a/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompiler.scala b/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompiler.scala index 46b202722070..5033e42d4e2d 100644 --- a/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompiler.scala +++ b/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompiler.scala @@ -2,10 +2,9 @@ package dotty.tools.scaladoc package snippets import dotty.tools.io.{AbstractFile, VirtualDirectory} -import dotty.tools.dotc.interactive.InteractiveDriver -import dotty.tools.dotc.interactive.Interactive -import dotty.tools.dotc.interactive.InteractiveCompiler +import dotty.tools.dotc.Driver import dotty.tools.dotc.core.Contexts.Context +import dotty.tools.dotc.core.Mode import dotty.tools.dotc.config.Settings.Setting._ import dotty.tools.dotc.interfaces.SourcePosition import dotty.tools.dotc.ast.Trees.Tree @@ -21,21 +20,28 @@ import dotty.tools.dotc.interfaces.Diagnostic._ import scala.util.{ Try, Success, Failure } class SnippetCompiler( - val scalacOptions: String, + val snippetCompilerSettings: Seq[SnippetCompilerSetting[_]], + val scalacOptions: String = "", target: AbstractFile = new VirtualDirectory("(memory)") ): - private def newDriver: InteractiveDriver = { - val defaultFlags = - List("-color:never", "-unchecked", "-deprecation", "-Ximport-suggestion-timeout", "0") - val options = scalacOptions.split("\\s+").toList - val settings = - options ::: defaultFlags ::: Nil - - new InteractiveDriver(settings) - } - - private val driver = newDriver + object SnippetDriver extends Driver: + val currentCtx = + val rootCtx = initCtx.fresh.addMode(Mode.ReadPositions).addMode(Mode.Interactive) + rootCtx.setSetting(rootCtx.settings.YretainTrees, true) + rootCtx.setSetting(rootCtx.settings.YcookComments, true) + rootCtx.setSetting(rootCtx.settings.YreadComments, true) + val defaultFlags = + List("-color:never", "-unchecked", "-deprecation", "-Ximport-suggestion-timeout", "0") + val options = scalacOptions.split("\\s+").toList + val settings = options ::: defaultFlags ::: Nil + val ctx = setup(settings.toArray, rootCtx) match + case Some((_, ctx)) => ctx + case None => rootCtx + ctx.initialize()(using ctx) + snippetCompilerSettings.foldLeft(ctx.fresh) { (ctx, setting) => + ctx.setSetting(setting.setting, setting.value) + } private val scala3Compiler = new Compiler @@ -85,9 +91,9 @@ class SnippetCompiler( wrappedSnippet: WrappedSnippet, arg: SnippetCompilerArg ): SnippetCompilationResult = { - val context = driver.currentCtx.fresh + val context = SnippetDriver.currentCtx.fresh .setSetting( - driver.currentCtx.settings.outputDir, + SnippetDriver.currentCtx.settings.outputDir, target ) .setReporter(new StoreReporter) diff --git a/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompilerSetting.scala b/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompilerSetting.scala new file mode 100644 index 000000000000..4c5ef500c83a --- /dev/null +++ b/scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompilerSetting.scala @@ -0,0 +1,8 @@ +package dotty.tools.scaladoc +package snippets + +import dotty.tools.scaladoc.DocContext +import dotty.tools.dotc.config.Settings._ +import dotty.tools.dotc.config.ScalaSettings + +case class SnippetCompilerSetting[T](setting: Setting[T], value: T) \ No newline at end of file