src/Tools/jEdit/src/token_markup.scala
author wenzelm
Thu Jun 16 23:16:06 2011 +0200 (2011-06-16)
changeset 43416 e730cdd97dcf
parent 43414 f0770743b7ec
child 43429 095f90f8dca3
permissions -rw-r--r--
more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm@43414
     1
/*  Title:      Tools/jEdit/src/token_markup.scala
wenzelm@43414
     2
    Author:     Makarius
wenzelm@43414
     3
wenzelm@43414
     4
Outer syntax token markup.
wenzelm@43414
     5
*/
wenzelm@43414
     6
wenzelm@43414
     7
package isabelle.jedit
wenzelm@43414
     8
wenzelm@43414
     9
wenzelm@43414
    10
import isabelle._
wenzelm@43414
    11
wenzelm@43414
    12
import org.gjt.sp.jedit.Buffer
wenzelm@43414
    13
import org.gjt.sp.jedit.syntax.{Token => JToken, TokenMarker, TokenHandler, ParserRuleSet}
wenzelm@43414
    14
import javax.swing.text.Segment
wenzelm@43414
    15
wenzelm@43414
    16
wenzelm@43414
    17
object Token_Markup
wenzelm@43414
    18
{
wenzelm@43414
    19
  /* extended jEdit syntax styles */
wenzelm@43414
    20
wenzelm@43414
    21
  private val plain_range: Int = JToken.ID_COUNT
wenzelm@43414
    22
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@43414
    23
wenzelm@43414
    24
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@43414
    25
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@43414
    26
  val hidden: Byte = (3 * plain_range).toByte
wenzelm@43414
    27
wenzelm@43414
    28
wenzelm@43414
    29
  /* token marker */
wenzelm@43414
    30
wenzelm@43414
    31
  private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
wenzelm@43414
    32
wenzelm@43416
    33
  private class Line_Context(val context: Scan.Context, prev: Line_Context)
wenzelm@43416
    34
    extends TokenMarker.LineContext(isabelle_rules, prev)
wenzelm@43414
    35
  {
wenzelm@43414
    36
    override def hashCode: Int = context.hashCode
wenzelm@43414
    37
    override def equals(that: Any): Boolean =
wenzelm@43414
    38
      that match {
wenzelm@43414
    39
        case other: Line_Context => context == other.context
wenzelm@43414
    40
        case _ => false
wenzelm@43414
    41
      }
wenzelm@43414
    42
  }
wenzelm@43414
    43
wenzelm@43414
    44
  def token_marker(session: Session, buffer: Buffer): TokenMarker =
wenzelm@43414
    45
    new TokenMarker {
wenzelm@43416
    46
      override def markTokens(raw_context: TokenMarker.LineContext,
wenzelm@43414
    47
          handler: TokenHandler, line: Segment): TokenMarker.LineContext =
wenzelm@43414
    48
      {
wenzelm@43416
    49
        val syntax = session.current_syntax()
wenzelm@43414
    50
wenzelm@43416
    51
        val context = raw_context.asInstanceOf[Line_Context]
wenzelm@43416
    52
        val ctxt = if (context == null) Scan.Finished else context.context
wenzelm@43414
    53
wenzelm@43416
    54
        val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
wenzelm@43416
    55
        val context1 = new Line_Context(ctxt1, context)
wenzelm@43414
    56
wenzelm@43416
    57
        var offset = 0
wenzelm@43416
    58
        for (token <- tokens) {
wenzelm@43416
    59
          val style = Isabelle_Markup.token_markup(syntax, token)
wenzelm@43416
    60
          val length = token.source.length
wenzelm@43416
    61
          handler.handleToken(line, style, offset, length, context1)
wenzelm@43416
    62
          offset += length
wenzelm@43414
    63
        }
wenzelm@43416
    64
        handler.handleToken(line, JToken.END, line.count, 0, context1)
wenzelm@43416
    65
wenzelm@43416
    66
        val context2 = context1.intern
wenzelm@43416
    67
        handler.setLineContext(context2)
wenzelm@43416
    68
        context2
wenzelm@43414
    69
      }
wenzelm@43414
    70
    }
wenzelm@43414
    71
}
wenzelm@43414
    72