src/Tools/jEdit/src/token_markup.scala
author wenzelm
Mon Jun 20 22:43:56 2011 +0200 (2011-06-20)
changeset 43482 ebb90ff55b79
parent 43464 265d9300d523
child 43487 98cd7e83fc5b
permissions -rw-r--r--
added SyntaxUtilities.StyleExtender hook, with actual functionality in Isabelle/Scala;
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@43482
    12
import java.awt.{Font, Color}
wenzelm@43482
    13
import java.awt.font.TextAttribute
wenzelm@43482
    14
wenzelm@43482
    15
import org.gjt.sp.util.SyntaxUtilities
wenzelm@43452
    16
import org.gjt.sp.jedit.Mode
wenzelm@43452
    17
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
wenzelm@43482
    18
  ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
wenzelm@43452
    19
wenzelm@43414
    20
import javax.swing.text.Segment
wenzelm@43414
    21
wenzelm@43414
    22
wenzelm@43414
    23
object Token_Markup
wenzelm@43414
    24
{
wenzelm@43443
    25
  /* extended syntax styles */
wenzelm@43414
    26
wenzelm@43440
    27
  private val plain_range: Int = JEditToken.ID_COUNT
wenzelm@43482
    28
  private val full_range: Int = 4 * plain_range + 1
wenzelm@43414
    29
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@43414
    30
wenzelm@43414
    31
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@43414
    32
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@43460
    33
  def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
wenzelm@43460
    34
  val hidden: Byte = (4 * plain_range).toByte
wenzelm@43414
    35
wenzelm@43482
    36
  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
wenzelm@43482
    37
  {
wenzelm@43482
    38
    import scala.collection.JavaConversions._
wenzelm@43482
    39
    val font = style.getFont.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
wenzelm@43482
    40
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, font)
wenzelm@43482
    41
  }
wenzelm@43482
    42
wenzelm@43482
    43
  private def bold_style(style: SyntaxStyle): SyntaxStyle =
wenzelm@43482
    44
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor,
wenzelm@43482
    45
      style.getFont.deriveFont(Font.BOLD))
wenzelm@43482
    46
wenzelm@43482
    47
  class Style_Extender extends SyntaxUtilities.StyleExtender
wenzelm@43482
    48
  {
wenzelm@43482
    49
    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
wenzelm@43482
    50
    {
wenzelm@43482
    51
      val new_styles = new Array[SyntaxStyle](full_range)
wenzelm@43482
    52
      for (i <- 0 until plain_range) {
wenzelm@43482
    53
        val style = styles(i)
wenzelm@43482
    54
        new_styles(i) = style
wenzelm@43482
    55
        new_styles(subscript(i.toByte)) = script_style(style, -1)
wenzelm@43482
    56
        new_styles(superscript(i.toByte)) = script_style(style, 1)
wenzelm@43482
    57
        new_styles(bold(i.toByte)) = bold_style(style)
wenzelm@43482
    58
      }
wenzelm@43482
    59
      new_styles(hidden) =
wenzelm@43482
    60
        new SyntaxStyle(Color.white, null, new Font(styles(0).getFont.getFamily, 0, 1))
wenzelm@43482
    61
      new_styles
wenzelm@43482
    62
    }
wenzelm@43482
    63
  }
wenzelm@43482
    64
wenzelm@43443
    65
  private def extended_styles(symbols: Symbol.Interpretation, text: CharSequence)
wenzelm@43443
    66
    : Map[Text.Offset, Byte => Byte] =
wenzelm@43443
    67
  {
wenzelm@43482
    68
    // FIXME \\<^bsub> \\<^esub> \\<^bsup> \\<^esup>
wenzelm@43482
    69
    def ctrl_style(sym: String): Option[Byte => Byte] =
wenzelm@43482
    70
      if (symbols.is_subscript_decoded(sym)) Some(subscript(_))
wenzelm@43482
    71
      else if (symbols.is_superscript_decoded(sym)) Some(superscript(_))
wenzelm@43482
    72
      else if (symbols.is_bold_decoded(sym)) Some(bold(_))
wenzelm@43482
    73
      else None
wenzelm@43455
    74
wenzelm@43482
    75
    var result = Map[Text.Offset, Byte => Byte]()
wenzelm@43482
    76
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
wenzelm@43482
    77
    {
wenzelm@43482
    78
      for (i <- start until stop) result += (i -> style)
wenzelm@43482
    79
    }
wenzelm@43482
    80
    var offset = 0
wenzelm@43482
    81
    var ctrl = ""
wenzelm@43482
    82
    for (sym <- Symbol.iterator(text).map(_.toString)) {
wenzelm@43482
    83
      if (ctrl_style(sym).isDefined) ctrl = sym
wenzelm@43482
    84
      else if (ctrl != "") {
wenzelm@43482
    85
        if (symbols.is_controllable(sym) && sym != "\"") {
wenzelm@43482
    86
          mark(offset - ctrl.length, offset, _ => hidden)
wenzelm@43482
    87
          mark(offset, offset + sym.length, ctrl_style(ctrl).get)
wenzelm@43482
    88
        }
wenzelm@43482
    89
        ctrl = ""
wenzelm@43443
    90
      }
wenzelm@43482
    91
      offset += sym.length
wenzelm@43443
    92
    }
wenzelm@43482
    93
    result
wenzelm@43443
    94
  }
wenzelm@43443
    95
wenzelm@43414
    96
wenzelm@43414
    97
  /* token marker */
wenzelm@43414
    98
wenzelm@43414
    99
  private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
wenzelm@43414
   100
wenzelm@43429
   101
  private class Line_Context(val context: Scan.Context)
wenzelm@43429
   102
    extends TokenMarker.LineContext(isabelle_rules, null)
wenzelm@43414
   103
  {
wenzelm@43414
   104
    override def hashCode: Int = context.hashCode
wenzelm@43414
   105
    override def equals(that: Any): Boolean =
wenzelm@43414
   106
      that match {
wenzelm@43414
   107
        case other: Line_Context => context == other.context
wenzelm@43414
   108
        case _ => false
wenzelm@43414
   109
      }
wenzelm@43414
   110
  }
wenzelm@43414
   111
wenzelm@43452
   112
  class Marker extends TokenMarker
wenzelm@43452
   113
  {
wenzelm@43452
   114
    override def markTokens(context: TokenMarker.LineContext,
wenzelm@43452
   115
        handler: TokenHandler, line: Segment): TokenMarker.LineContext =
wenzelm@43452
   116
    {
wenzelm@43452
   117
      val symbols = Isabelle.system.symbols
wenzelm@43452
   118
      val syntax = Isabelle.session.current_syntax()
wenzelm@43414
   119
wenzelm@43452
   120
      val ctxt =
wenzelm@43452
   121
        context match {
wenzelm@43452
   122
          case c: Line_Context => c.context
wenzelm@43452
   123
          case _ => Scan.Finished
wenzelm@43452
   124
        }
wenzelm@43452
   125
      val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
wenzelm@43452
   126
      val context1 = new Line_Context(ctxt1)
wenzelm@43452
   127
wenzelm@43452
   128
      val extended = extended_styles(symbols, line)
wenzelm@43443
   129
wenzelm@43452
   130
      var offset = 0
wenzelm@43452
   131
      for (token <- tokens) {
wenzelm@43452
   132
        val style = Isabelle_Markup.token_markup(syntax, token)
wenzelm@43452
   133
        val length = token.source.length
wenzelm@43452
   134
        val end_offset = offset + length
wenzelm@43452
   135
        if ((offset until end_offset) exists extended.isDefinedAt) {
wenzelm@43452
   136
          for (i <- offset until end_offset) {
wenzelm@43452
   137
            val style1 =
wenzelm@43452
   138
              extended.get(i) match {
wenzelm@43452
   139
                case None => style
wenzelm@43452
   140
                case Some(ext) => ext(style)
wenzelm@43452
   141
              }
wenzelm@43452
   142
            handler.handleToken(line, style1, i, 1, context1)
wenzelm@43443
   143
          }
wenzelm@43414
   144
        }
wenzelm@43452
   145
        else handler.handleToken(line, style, offset, length, context1)
wenzelm@43452
   146
        offset += length
wenzelm@43452
   147
      }
wenzelm@43452
   148
      handler.handleToken(line, JEditToken.END, line.count, 0, context1)
wenzelm@43452
   149
wenzelm@43452
   150
      val context2 = context1.intern
wenzelm@43452
   151
      handler.setLineContext(context2)
wenzelm@43452
   152
      context2
wenzelm@43452
   153
    }
wenzelm@43452
   154
  }
wenzelm@43452
   155
wenzelm@43416
   156
wenzelm@43452
   157
  /* mode provider */
wenzelm@43452
   158
wenzelm@43452
   159
  class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
wenzelm@43452
   160
  {
wenzelm@43452
   161
    for (mode <- orig_provider.getModes) addMode(mode)
wenzelm@43452
   162
wenzelm@43452
   163
    val isabelle_token_marker = new Token_Markup.Marker
wenzelm@43452
   164
wenzelm@43452
   165
    override def loadMode(mode: Mode, xmh: XModeHandler)
wenzelm@43452
   166
    {
wenzelm@43452
   167
      super.loadMode(mode, xmh)
wenzelm@43452
   168
      if (mode.getName == "isabelle")
wenzelm@43452
   169
        mode.setTokenMarker(isabelle_token_marker)
wenzelm@43414
   170
    }
wenzelm@43452
   171
  }
wenzelm@43414
   172
}
wenzelm@43414
   173