src/Tools/jEdit/src/token_markup.scala
author wenzelm
Thu, 16 Jun 2011 22:05:40 +0200
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.);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/token_markup.scala
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     3
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     4
Outer syntax token markup.
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     5
*/
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     6
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     8
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     9
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    10
import isabelle._
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    11
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    12
import org.gjt.sp.jedit.Buffer
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    13
import org.gjt.sp.jedit.syntax.{Token => JToken, TokenMarker, TokenHandler, ParserRuleSet}
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    14
import javax.swing.text.Segment
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    15
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    16
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    17
object Token_Markup
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    18
{
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    19
  /* extended jEdit syntax styles */
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    20
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    21
  private val plain_range: Int = JToken.ID_COUNT
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    22
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    23
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    24
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    25
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    26
  val hidden: Byte = (3 * plain_range).toByte
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    27
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    28
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    29
  /* token marker */
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    30
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    31
  private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    32
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    33
  private class Line_Context(val context: Scan.Context)
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    34
    extends TokenMarker.LineContext(isabelle_rules, null)
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    35
  {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    36
    override def hashCode: Int = context.hashCode
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    37
    override def equals(that: Any): Boolean =
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    38
      that match {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    39
        case other: Line_Context => context == other.context
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    40
        case _ => false
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    41
      }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    42
  }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    43
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    44
  def token_marker(session: Session, buffer: Buffer): TokenMarker =
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    45
    new TokenMarker {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    46
      override def markTokens(context: TokenMarker.LineContext,
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    47
          handler: TokenHandler, line: Segment): TokenMarker.LineContext =
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    48
      {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    49
        Isabelle.swing_buffer_lock(buffer) {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    50
          val syntax = session.current_syntax()
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    51
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    52
          val ctxt =
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    53
            context match {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    54
              case c: Line_Context => c.context
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    55
              case _ => Scan.Finished
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    56
            }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    57
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    58
          val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    59
          val context1 = new Line_Context(ctxt1)
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    60
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    61
          var offset = 0
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    62
          for (token <- tokens) {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    63
            val style = Isabelle_Markup.token_markup(syntax, token)
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    64
            val length = token.source.length
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    65
            handler.handleToken(line, style, offset, length, context1)
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    66
            offset += length
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    67
          }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    68
          handler.handleToken(line, JToken.END, line.count, 0, context1)
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    69
          handler.setLineContext(context1)
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    70
          context1
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    71
        }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    72
      }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    73
    }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    74
}
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    75