<commit_before>package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.Driver
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.StdNames._
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
import dotty.tools.repl.AbstractFileClassLoader
import dotty.tools.dotc.printing.DecompilerPrinter

import scala.quoted.Expr

class QuoteDriver extends Driver {
  import tpd._

  def run[T](expr: Expr[T], settings: Runners.RunSettings): T = {
    val ctx: Context = initCtx.fresh
    ctx.settings.optimise.update(settings.optimise)(ctx)

    val outDir: AbstractFile = settings.outDir match {
      case Some(out) =>
        val dir = Directory(out)
        dir.createDirectory()
        new PlainDirectory(Directory(out))
      case None =>
        new VirtualDirectory("(memory)", None)
    }

    val driver = new ExprCompiler(outDir)
    driver.newRun(ctx).compileExpr(expr)

    val classLoader = new AbstractFileClassLoader(outDir, this.getClass.getClassLoader)

    val clazz = classLoader.loadClass(driver.outputClassName.toString)
    val method = clazz.getMethod("apply")
    val instance = clazz.newInstance()

    method.invoke(instance).asInstanceOf[T]
  }

  def show(expr: Expr[_]): String = {
    def show(tree: Tree, ctx: Context): String = {
      val printer = new DecompilerPrinter(ctx)
      val pageWidth = ctx.settings.pageWidth.value(ctx)
      printer.toText(tree).mkString(pageWidth, false)
    }
    withTree(expr, show)
  }

  def withTree[T](expr: Expr[_], f: (Tree, Context) => T): T = {
    val ctx: Context = initCtx.fresh
    ctx.settings.color.update("never")(ctx) // TODO support colored show
    var output: Option[T] = None
    def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {
      assert(output.isEmpty)
      output = Some(f(tree, ctx))
    }
    new ExprDecompiler(registerTree).newRun(ctx).compileExpr(expr)
    output.getOrElse(throw new Exception("Could not extact " + expr))
  }

  override def initCtx: Context = {
    val ictx = super.initCtx.fresh
    val classpath = System.getProperty("java.class.path")
    ictx.settings.classpath.update(classpath)(ictx)
    ictx
  }

}<commit_msg>Fix classpath in QuoteDriver when executing from sbt<commit_after>package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.Driver
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
import dotty.tools.repl.AbstractFileClassLoader
import dotty.tools.dotc.printing.DecompilerPrinter

import scala.quoted.Expr

import java.net.URLClassLoader

class QuoteDriver extends Driver {
  import tpd._

  def run[T](expr: Expr[T], settings: Runners.RunSettings): T = {
    val ctx: Context = initCtx.fresh
    ctx.settings.optimise.update(settings.optimise)(ctx)

    val outDir: AbstractFile = settings.outDir match {
      case Some(out) =>
        val dir = Directory(out)
        dir.createDirectory()
        new PlainDirectory(Directory(out))
      case None =>
        new VirtualDirectory("(memory)", None)
    }

    val driver = new ExprCompiler(outDir)
    driver.newRun(ctx).compileExpr(expr)

    val classLoader = new AbstractFileClassLoader(outDir, this.getClass.getClassLoader)

    val clazz = classLoader.loadClass(driver.outputClassName.toString)
    val method = clazz.getMethod("apply")
    val instance = clazz.newInstance()

    method.invoke(instance).asInstanceOf[T]
  }

  def show(expr: Expr[_]): String = {
    def show(tree: Tree, ctx: Context): String = {
      val printer = new DecompilerPrinter(ctx)
      val pageWidth = ctx.settings.pageWidth.value(ctx)
      printer.toText(tree).mkString(pageWidth, false)
    }
    withTree(expr, show)
  }

  def withTree[T](expr: Expr[_], f: (Tree, Context) => T): T = {
    val ctx: Context = initCtx.fresh
    ctx.settings.color.update("never")(ctx) // TODO support colored show
    var output: Option[T] = None
    def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {
      assert(output.isEmpty)
      output = Some(f(tree, ctx))
    }
    new ExprDecompiler(registerTree).newRun(ctx).compileExpr(expr)
    output.getOrElse(throw new Exception("Could not extact " + expr))
  }

  override def initCtx: Context = {
    val ictx = super.initCtx.fresh
    var classpath = System.getProperty("java.class.path")
    this.getClass.getClassLoader match {
      case cl: URLClassLoader =>
        // Loads the classes loaded by this class loader
        // When executing `run` or `test` in sbt the classpath is not in the property java.class.path
        val newClasspath = cl.getURLs.map(_.getFile())
        classpath = newClasspath.mkString("", ":", if (classpath == "") "" else ":" + classpath)
      case _ =>
    }
    ictx.settings.classpath.update(classpath)(ictx)
    ictx
  }

}
{'fraction_non_alphanumeric': 0.11787617876178762, 'fraction_numerical': 0.0, 'mean_word_length': 3.5898400752587016, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3430954', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1536, 'n_words': 339}