src/Tools/jEdit/src/jedit/scala_console.scala
author wenzelm
Sat Jan 09 18:25:48 2010 +0100 (2010-01-09)
changeset 34846 ca76b3978540
parent 34845 6d64de27efa5
child 34849 96bcb91b45ce
permissions -rw-r--r--
pass all jEdit jars to compiler as classpath -- to enable proper referencing of application name space;
reduced predefined values, which take time and are static anyway;
wenzelm@34841
     1
/*
wenzelm@34841
     2
 * Scala instance of Console plugin
wenzelm@34841
     3
 *
wenzelm@34841
     4
 * @author Makarius
wenzelm@34841
     5
 */
wenzelm@34841
     6
wenzelm@34841
     7
package isabelle.jedit
wenzelm@34841
     8
wenzelm@34841
     9
wenzelm@34844
    10
import console.{Console, ConsolePane, Shell, Output}
wenzelm@34841
    11
wenzelm@34845
    12
import org.gjt.sp.jedit.{jEdit, JARClassLoader}
wenzelm@34841
    13
import org.gjt.sp.jedit.MiscUtilities
wenzelm@34841
    14
wenzelm@34846
    15
import java.io.{File, Writer, PrintWriter}
wenzelm@34841
    16
wenzelm@34841
    17
import scala.tools.nsc.{Interpreter, GenericRunnerSettings, NewLinePrintWriter, ConsoleWriter}
wenzelm@34846
    18
import scala.collection.mutable
wenzelm@34841
    19
wenzelm@34841
    20
wenzelm@34841
    21
class Scala_Console extends Shell("Scala")
wenzelm@34841
    22
{
wenzelm@34841
    23
  private var interpreters = Map[Console, Interpreter]()
wenzelm@34841
    24
wenzelm@34841
    25
  private var global_console: Console = null
wenzelm@34841
    26
  private var global_out: Output = null
wenzelm@34841
    27
  private var global_err: Output = null
wenzelm@34841
    28
wenzelm@34844
    29
  private def with_console[A](console: Console, out: Output, err: Output)(e: => A): A =
wenzelm@34844
    30
  {
wenzelm@34844
    31
    global_console = console
wenzelm@34844
    32
    global_out = out
wenzelm@34844
    33
    global_err = if (err == null) out else err
wenzelm@34844
    34
    val res = Exn.capture { e }
wenzelm@34844
    35
    global_console = null
wenzelm@34844
    36
    global_out = null
wenzelm@34844
    37
    global_err = null
wenzelm@34844
    38
    Exn.release(res)
wenzelm@34844
    39
  }
wenzelm@34844
    40
wenzelm@34841
    41
  private def report_error(str: String)
wenzelm@34841
    42
  {
wenzelm@34841
    43
    if (global_console == null || global_err == null) System.err.println(str)
wenzelm@34841
    44
    else global_err.print(global_console.getErrorColor, str)
wenzelm@34841
    45
  }
wenzelm@34841
    46
wenzelm@34846
    47
  private def construct_classpath(): String =
wenzelm@34846
    48
  {
wenzelm@34846
    49
    def find_jars(start: String): List[String] =
wenzelm@34846
    50
      if (start != null)
wenzelm@34846
    51
        Standard_System.find_files(new File(start),
wenzelm@34846
    52
          entry => entry.isFile && entry.getName.endsWith(".jar")).map(_.getAbsolutePath)
wenzelm@34846
    53
      else Nil
wenzelm@34846
    54
    val path =
wenzelm@34846
    55
      (jEdit.getJEditHome + File.separator + "jedit.jar") ::
wenzelm@34846
    56
        (find_jars(jEdit.getSettingsDirectory) ::: find_jars(jEdit.getJEditHome))
wenzelm@34846
    57
     path.mkString(File.pathSeparator)
wenzelm@34846
    58
  }
wenzelm@34846
    59
wenzelm@34841
    60
  private class Console_Writer extends Writer
wenzelm@34841
    61
  {
wenzelm@34841
    62
    def close {}
wenzelm@34841
    63
    def flush {}
wenzelm@34841
    64
wenzelm@34841
    65
    def write(cbuf: Array[Char], off: Int, len: Int)
wenzelm@34841
    66
    {
wenzelm@34841
    67
      if (len > 0) write(new String(cbuf.subArray(off, off + len)))
wenzelm@34841
    68
    }
wenzelm@34841
    69
wenzelm@34841
    70
    override def write(str: String)
wenzelm@34841
    71
    {
wenzelm@34841
    72
      if (global_out == null) System.out.println(str)
wenzelm@34841
    73
      else global_out.print(null, str)
wenzelm@34841
    74
    }
wenzelm@34841
    75
  }
wenzelm@34841
    76
wenzelm@34841
    77
  override def openConsole(console: Console)
wenzelm@34841
    78
  {
wenzelm@34841
    79
    val settings = new GenericRunnerSettings(report_error)
wenzelm@34846
    80
    settings.classpath.value = construct_classpath()
wenzelm@34841
    81
    val printer = new PrintWriter(new Console_Writer, true)
wenzelm@34845
    82
    val interp = new Interpreter(settings, printer)
wenzelm@34845
    83
    {
wenzelm@34845
    84
      override def parentClassLoader = new JARClassLoader
wenzelm@34845
    85
    }
wenzelm@34845
    86
    interp.setContextClassLoader
wenzelm@34846
    87
    interp.bind("view", "org.gjt.sp.jedit.View", console.getView)
wenzelm@34845
    88
    interpreters += (console -> interp)
wenzelm@34841
    89
  }
wenzelm@34841
    90
wenzelm@34841
    91
  override def closeConsole(console: Console)
wenzelm@34841
    92
  {
wenzelm@34841
    93
    interpreters -= console
wenzelm@34841
    94
  }
wenzelm@34841
    95
wenzelm@34841
    96
  override def printPrompt(console: Console, out: Output)
wenzelm@34841
    97
	{
wenzelm@34844
    98
    out.writeAttrs(ConsolePane.colorAttributes(console.getInfoColor), "scala>")
wenzelm@34845
    99
		out.writeAttrs(ConsolePane.colorAttributes(console.getPlainColor), " ")
wenzelm@34841
   100
	}
wenzelm@34841
   101
wenzelm@34841
   102
  override def execute(console: Console, input: String, out: Output, err: Output, command: String)
wenzelm@34841
   103
  {
wenzelm@34846
   104
    val interp = interpreters(console)
wenzelm@34846
   105
    with_console(console, out, err) { interp.interpret(command) }
wenzelm@34844
   106
    if (err != null) err.commandDone()
wenzelm@34844
   107
		out.commandDone()
wenzelm@34841
   108
  }
wenzelm@34841
   109
}