Skip to content

Commit

Permalink
Review CRs 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pikinier20 committed May 21, 2021
1 parent ac9beef commit 6c1086c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
8 changes: 1 addition & 7 deletions scaladoc/src/dotty/tools/scaladoc/DocContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
21 changes: 11 additions & 10 deletions scaladoc/src/dotty/tools/scaladoc/snippets/SnippetChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 23 additions & 17 deletions scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6c1086c

Please sign in to comment.