clarified signature;
authorwenzelm
Sat, 23 May 2020 11:27:35 +0200
changeset 71868 06ec50d9fc0a
parent 71867 3ee14fc25736
child 71869 2b7840fb2f90
clarified signature;
src/Pure/System/scala.scala
src/Tools/jEdit/src/scala_console.scala
--- a/src/Pure/System/scala.scala	Sat May 23 11:25:34 2020 +0200
+++ b/src/Pure/System/scala.scala	Sat May 23 11:27:35 2020 +0200
@@ -11,7 +11,7 @@
 import java.io.{File => JFile, StringWriter, PrintWriter}
 
 import scala.util.matching.Regex
-import scala.tools.nsc.GenericRunnerSettings
+import scala.tools.nsc.{GenericRunnerSettings, ConsoleWriter, NewLinePrintWriter}
 import scala.tools.nsc.interpreter.IMain
 
 
@@ -21,7 +21,9 @@
 
   object Compiler
   {
-    def classpath(jar_dirs: List[JFile]): String =
+    def context(
+      error: String => Unit = Exn.error,
+      jar_dirs: List[JFile] = Nil): Context =
     {
       def find_jars(dir: JFile): List[String] =
         File.find_files(dir, file => file.getName.endsWith(".jar")).
@@ -34,34 +36,45 @@
           elem <- space_explode(JFile.pathSeparatorChar, path)
         } yield elem
 
-      (class_path ::: jar_dirs.flatMap(find_jars)).mkString(JFile.pathSeparator)
-    }
-
-    type Settings = scala.tools.nsc.Settings
+      val settings = new GenericRunnerSettings(error)
+      settings.classpath.value =
+        (class_path ::: jar_dirs.flatMap(find_jars)).mkString(JFile.pathSeparator)
 
-    def settings(
-      error: String => Unit = Exn.error,
-      jar_dirs: List[JFile] = Nil): Settings =
-    {
-      val settings = new GenericRunnerSettings(error)
-      settings.classpath.value = classpath(jar_dirs)
-      settings
+      new Context(settings)
     }
 
-    def toplevel(settings: Settings, source: String): List[String] =
+    def default_print_writer: PrintWriter =
+      new NewLinePrintWriter(new ConsoleWriter, true)
+
+    class Context private [Compiler](val settings: GenericRunnerSettings)
     {
-      val out = new StringWriter
-      val interp = new IMain(settings, new PrintWriter(out))
-      val rep = new interp.ReadEvalPrint
-      val ok = interp.withLabel("\u0001") { rep.compile(source) }
-      out.close
+      def interpreter(
+        print_writer: PrintWriter = default_print_writer,
+        class_loader: ClassLoader = null): IMain =
+      {
+        new IMain(settings, print_writer)
+        {
+          override def parentClassLoader: ClassLoader =
+            if (class_loader == null) super.parentClassLoader
+            else class_loader
+        }
+      }
 
-      val Error = """(?s)^\S* error: (.*)$""".r
-      val errors =
-        space_explode('\u0001', Library.strip_ansi_color(out.toString)).
-          collect({ case Error(msg) => "Scala error: " + Library.trim_line(msg) })
+      def toplevel(source: String): List[String] =
+      {
+        val out = new StringWriter
+        val interp = interpreter(new PrintWriter(out))
+        val rep = new interp.ReadEvalPrint
+        val ok = interp.withLabel("\u0001") { rep.compile(source) }
+        out.close
 
-      if (!ok && errors.isEmpty) List("Error") else errors
+        val Error = """(?s)^\S* error: (.*)$""".r
+        val errors =
+          space_explode('\u0001', Library.strip_ansi_color(out.toString)).
+            collect({ case Error(msg) => "Scala error: " + Library.trim_line(msg) })
+
+        if (!ok && errors.isEmpty) List("Error") else errors
+      }
     }
   }
 
--- a/src/Tools/jEdit/src/scala_console.scala	Sat May 23 11:25:34 2020 +0200
+++ b/src/Tools/jEdit/src/scala_console.scala	Sat May 23 11:27:35 2020 +0200
@@ -12,7 +12,6 @@
 import console.{Console, ConsolePane, Shell, Output}
 import org.gjt.sp.jedit.JARClassLoader
 import java.io.{OutputStream, Writer, PrintWriter}
-import scala.tools.nsc.interpreter.IMain
 
 
 class Scala_Console extends Shell("Scala")
@@ -100,13 +99,11 @@
     private val running = Synchronized[Option[Thread]](None)
     def interrupt { running.change(opt => { opt.foreach(_.interrupt); opt }) }
 
-    private val settings =
-      Scala.Compiler.settings(error = report_error, jar_dirs = JEdit_Lib.directories)
-
-    private val interp = new IMain(settings, new PrintWriter(console_writer, true))
-    {
-      override def parentClassLoader = new JARClassLoader
-    }
+    private val interp =
+      Scala.Compiler.context(error = report_error, jar_dirs = JEdit_Lib.directories).
+        interpreter(
+          print_writer = new PrintWriter(console_writer, true),
+          class_loader = new JARClassLoader)
 
     val thread: Consumer_Thread[Request] = Consumer_Thread.fork("Scala_Console")
     {