src/Tools/jEdit/src/token_markup.scala
author wenzelm
Thu Jun 16 22:05:40 2011 +0200 (2011-06-16)
changeset 43414 f0770743b7ec
child 43416 e730cdd97dcf
permissions -rw-r--r--
static token markup, based on outer syntax only;
eliminated obsolete buffer.propertiesChanged (expensive due to remarking of full buffer 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@43414
    33
  private class Line_Context(val context: Scan.Context)
wenzelm@43414
    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@43414
    46
      override def markTokens(context: TokenMarker.LineContext,
wenzelm@43414
    47
          handler: TokenHandler, line: Segment): TokenMarker.LineContext =
wenzelm@43414
    48
      {
wenzelm@43414
    49
        Isabelle.swing_buffer_lock(buffer) {
wenzelm@43414
    50
          val syntax = session.current_syntax()
wenzelm@43414
    51
wenzelm@43414
    52
          val ctxt =
wenzelm@43414
    53
            context match {
wenzelm@43414
    54
              case c: Line_Context => c.context
wenzelm@43414
    55
              case _ => Scan.Finished
wenzelm@43414
    56
            }
wenzelm@43414
    57
wenzelm@43414
    58
          val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
wenzelm@43414
    59
          val context1 = new Line_Context(ctxt1)
wenzelm@43414
    60
wenzelm@43414
    61
          var offset = 0
wenzelm@43414
    62
          for (token <- tokens) {
wenzelm@43414
    63
            val style = Isabelle_Markup.token_markup(syntax, token)
wenzelm@43414
    64
            val length = token.source.length
wenzelm@43414
    65
            handler.handleToken(line, style, offset, length, context1)
wenzelm@43414
    66
            offset += length
wenzelm@43414
    67
          }
wenzelm@43414
    68
          handler.handleToken(line, JToken.END, line.count, 0, context1)
wenzelm@43414
    69
          handler.setLineContext(context1)
wenzelm@43414
    70
          context1
wenzelm@43414
    71
        }
wenzelm@43414
    72
      }
wenzelm@43414
    73
    }
wenzelm@43414
    74
}
wenzelm@43414
    75