src/Tools/jEdit/src/token_markup.scala
author wenzelm
Sat Jun 18 11:22:03 2011 +0200 (2011-06-18)
changeset 43429 095f90f8dca3
parent 43416 e730cdd97dcf
child 43440 a1db9a251a03
permissions -rw-r--r--
simplified Line_Context (again);
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@43429
    33
  private class Line_Context(val context: Scan.Context)
wenzelm@43429
    34
    extends TokenMarker.LineContext(isabelle_rules, null)
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@43429
    46
      override def markTokens(context: TokenMarker.LineContext,
wenzelm@43414
    47
          handler: TokenHandler, line: Segment): TokenMarker.LineContext =
wenzelm@43414
    48
      {
wenzelm@43416
    49
        val syntax = session.current_syntax()
wenzelm@43429
    50
        val ctxt =
wenzelm@43429
    51
          context match {
wenzelm@43429
    52
            case c: Line_Context => c.context
wenzelm@43429
    53
            case _ => Scan.Finished
wenzelm@43429
    54
          }
wenzelm@43416
    55
        val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
wenzelm@43429
    56
        val context1 = new Line_Context(ctxt1)
wenzelm@43414
    57
wenzelm@43416
    58
        var offset = 0
wenzelm@43416
    59
        for (token <- tokens) {
wenzelm@43416
    60
          val style = Isabelle_Markup.token_markup(syntax, token)
wenzelm@43416
    61
          val length = token.source.length
wenzelm@43416
    62
          handler.handleToken(line, style, offset, length, context1)
wenzelm@43416
    63
          offset += length
wenzelm@43414
    64
        }
wenzelm@43416
    65
        handler.handleToken(line, JToken.END, line.count, 0, context1)
wenzelm@43416
    66
wenzelm@43416
    67
        val context2 = context1.intern
wenzelm@43416
    68
        handler.setLineContext(context2)
wenzelm@43416
    69
        context2
wenzelm@43414
    70
      }
wenzelm@43414
    71
    }
wenzelm@43414
    72
}
wenzelm@43414
    73