src/Tools/jEdit/src/token_markup.scala
author wenzelm
Wed Mar 14 15:37:51 2012 +0100 (2012-03-14)
changeset 46920 5f44c8bea84e
parent 46210 553ec602d337
child 46941 c0f776b661fa
permissions -rw-r--r--
more explicit indication of swing thread context;
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@43502
    13
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
wenzelm@43491
    14
import java.awt.geom.AffineTransform
wenzelm@43482
    15
wenzelm@43482
    16
import org.gjt.sp.util.SyntaxUtilities
wenzelm@44358
    17
import org.gjt.sp.jedit.{jEdit, Mode}
wenzelm@43452
    18
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler,
wenzelm@43482
    19
  ParserRuleSet, ModeProvider, XModeHandler, SyntaxStyle}
wenzelm@44358
    20
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@43452
    21
wenzelm@43414
    22
import javax.swing.text.Segment
wenzelm@43414
    23
wenzelm@43414
    24
wenzelm@43414
    25
object Token_Markup
wenzelm@43414
    26
{
wenzelm@43502
    27
  /* font operations */
wenzelm@43502
    28
wenzelm@43502
    29
  private def font_metrics(font: Font): LineMetrics =
wenzelm@43502
    30
    font.getLineMetrics("", new FontRenderContext(null, false, false))
wenzelm@43502
    31
wenzelm@43502
    32
  private def imitate_font(family: String, font: Font): Font =
wenzelm@43502
    33
  {
wenzelm@43502
    34
    val font1 = new Font (family, font.getStyle, font.getSize)
wenzelm@43502
    35
    font1.deriveFont(font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize)
wenzelm@43502
    36
  }
wenzelm@43502
    37
wenzelm@43502
    38
  private def transform_font(font: Font, transform: AffineTransform): Font =
wenzelm@43502
    39
  {
wenzelm@43502
    40
    import scala.collection.JavaConversions._
wenzelm@43502
    41
    font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform)))
wenzelm@43502
    42
  }
wenzelm@43502
    43
wenzelm@43502
    44
wenzelm@43443
    45
  /* extended syntax styles */
wenzelm@43414
    46
wenzelm@43440
    47
  private val plain_range: Int = JEditToken.ID_COUNT
wenzelm@43488
    48
  private val full_range = 6 * plain_range + 1
wenzelm@43414
    49
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@43414
    50
wenzelm@43414
    51
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@43414
    52
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@43460
    53
  def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte }
wenzelm@43487
    54
  def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte }
wenzelm@43487
    55
  val hidden: Byte = (6 * plain_range).toByte
wenzelm@43487
    56
wenzelm@43487
    57
  private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle =
wenzelm@43487
    58
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont))
wenzelm@43414
    59
wenzelm@43482
    60
  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
wenzelm@43482
    61
  {
wenzelm@43502
    62
    font_style(style, font0 =>
wenzelm@43502
    63
      {
wenzelm@43502
    64
        import scala.collection.JavaConversions._
wenzelm@43502
    65
        val font1 = font0.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
wenzelm@43502
    66
wenzelm@43502
    67
        def shift(y: Float): Font =
wenzelm@43502
    68
          transform_font(font1, AffineTransform.getTranslateInstance(0.0, y.toDouble))
wenzelm@43502
    69
wenzelm@43502
    70
        val m0 = font_metrics(font0)
wenzelm@43502
    71
        val m1 = font_metrics(font1)
wenzelm@43502
    72
        val a = m1.getAscent - m0.getAscent
wenzelm@43502
    73
        val b = (m1.getDescent + m1.getLeading) - (m0.getDescent + m0.getLeading)
wenzelm@43502
    74
        if (a > 0.0f) shift(a)
wenzelm@43502
    75
        else if (b > 0.0f) shift(- b)
wenzelm@43502
    76
        else font1
wenzelm@43502
    77
      })
wenzelm@43482
    78
  }
wenzelm@43482
    79
wenzelm@43482
    80
  private def bold_style(style: SyntaxStyle): SyntaxStyle =
wenzelm@43487
    81
    font_style(style, _.deriveFont(Font.BOLD))
wenzelm@43482
    82
wenzelm@44356
    83
  private def hidden_color: Color = new Color(255, 255, 255, 0)
wenzelm@44356
    84
wenzelm@43661
    85
  class Style_Extender extends SyntaxUtilities.StyleExtender
wenzelm@43482
    86
  {
wenzelm@44355
    87
    val max_user_fonts = 2
wenzelm@44355
    88
    if (Symbol.font_names.length > max_user_fonts)
wenzelm@44355
    89
      error("Too many user symbol fonts (max " + max_user_fonts + " permitted): " +
wenzelm@44355
    90
        Symbol.font_names.mkString(", "))
wenzelm@43487
    91
wenzelm@43482
    92
    override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
wenzelm@43482
    93
    {
wenzelm@43482
    94
      val new_styles = new Array[SyntaxStyle](full_range)
wenzelm@43482
    95
      for (i <- 0 until plain_range) {
wenzelm@43482
    96
        val style = styles(i)
wenzelm@43482
    97
        new_styles(i) = style
wenzelm@43482
    98
        new_styles(subscript(i.toByte)) = script_style(style, -1)
wenzelm@43482
    99
        new_styles(superscript(i.toByte)) = script_style(style, 1)
wenzelm@43482
   100
        new_styles(bold(i.toByte)) = bold_style(style)
wenzelm@44355
   101
        for (idx <- 0 until max_user_fonts)
wenzelm@44355
   102
          new_styles(user_font(idx, i.toByte)) = style
wenzelm@43695
   103
        for ((family, idx) <- Symbol.font_index)
wenzelm@43502
   104
          new_styles(user_font(idx, i.toByte)) = font_style(style, imitate_font(family, _))
wenzelm@43482
   105
      }
wenzelm@43502
   106
      new_styles(hidden) =
wenzelm@44356
   107
        new SyntaxStyle(hidden_color, null,
wenzelm@43502
   108
          { val font = styles(0).getFont
wenzelm@43502
   109
            transform_font(new Font(font.getFamily, 0, 1),
wenzelm@43502
   110
              AffineTransform.getScaleInstance(1.0, font.getSize.toDouble)) })
wenzelm@43482
   111
      new_styles
wenzelm@43482
   112
    }
wenzelm@43482
   113
  }
wenzelm@43482
   114
wenzelm@43661
   115
  def extended_styles(text: CharSequence): Map[Text.Offset, Byte => Byte] =
wenzelm@43443
   116
  {
wenzelm@44238
   117
    // FIXME Symbol.bsub_decoded etc.
wenzelm@43482
   118
    def ctrl_style(sym: String): Option[Byte => Byte] =
wenzelm@44238
   119
      if (sym == Symbol.sub_decoded || sym == Symbol.isub_decoded) Some(subscript(_))
wenzelm@44238
   120
      else if (sym == Symbol.sup_decoded || sym == Symbol.isup_decoded) Some(superscript(_))
wenzelm@44238
   121
      else if (sym == Symbol.bold_decoded) Some(bold(_))
wenzelm@43482
   122
      else None
wenzelm@43455
   123
wenzelm@43482
   124
    var result = Map[Text.Offset, Byte => Byte]()
wenzelm@43482
   125
    def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte)
wenzelm@43482
   126
    {
wenzelm@43482
   127
      for (i <- start until stop) result += (i -> style)
wenzelm@43482
   128
    }
wenzelm@43482
   129
    var offset = 0
wenzelm@43482
   130
    var ctrl = ""
wenzelm@43675
   131
    for (sym <- Symbol.iterator(text)) {
wenzelm@43482
   132
      if (ctrl_style(sym).isDefined) ctrl = sym
wenzelm@43482
   133
      else if (ctrl != "") {
wenzelm@43695
   134
        if (Symbol.is_controllable(sym) && sym != "\"" && !Symbol.fonts.isDefinedAt(sym)) {
wenzelm@43482
   135
          mark(offset - ctrl.length, offset, _ => hidden)
wenzelm@43482
   136
          mark(offset, offset + sym.length, ctrl_style(ctrl).get)
wenzelm@43482
   137
        }
wenzelm@43482
   138
        ctrl = ""
wenzelm@43443
   139
      }
wenzelm@43695
   140
      Symbol.lookup_font(sym) match {
wenzelm@43488
   141
        case Some(idx) => mark(offset, offset + sym.length, user_font(idx, _))
wenzelm@43487
   142
        case _ =>
wenzelm@43487
   143
      }
wenzelm@43482
   144
      offset += sym.length
wenzelm@43443
   145
    }
wenzelm@43482
   146
    result
wenzelm@43443
   147
  }
wenzelm@43443
   148
wenzelm@43414
   149
wenzelm@43414
   150
  /* token marker */
wenzelm@43414
   151
wenzelm@43414
   152
  private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
wenzelm@43414
   153
wenzelm@44358
   154
  private class Line_Context(val context: Option[Scan.Context])
wenzelm@43429
   155
    extends TokenMarker.LineContext(isabelle_rules, null)
wenzelm@43414
   156
  {
wenzelm@43414
   157
    override def hashCode: Int = context.hashCode
wenzelm@43414
   158
    override def equals(that: Any): Boolean =
wenzelm@43414
   159
      that match {
wenzelm@43414
   160
        case other: Line_Context => context == other.context
wenzelm@43414
   161
        case _ => false
wenzelm@43414
   162
      }
wenzelm@43414
   163
  }
wenzelm@43414
   164
wenzelm@43452
   165
  class Marker extends TokenMarker
wenzelm@43452
   166
  {
wenzelm@43452
   167
    override def markTokens(context: TokenMarker.LineContext,
wenzelm@46210
   168
        handler: TokenHandler, raw_line: Segment): TokenMarker.LineContext =
wenzelm@43452
   169
    {
wenzelm@44358
   170
      val line_ctxt =
wenzelm@44358
   171
        context match {
wenzelm@44358
   172
          case c: Line_Context => c.context
wenzelm@44358
   173
          case _ => Some(Scan.Finished)
wenzelm@44358
   174
        }
wenzelm@46210
   175
      val line = if (raw_line == null) new Segment else raw_line
wenzelm@46210
   176
wenzelm@43553
   177
      val context1 =
wenzelm@44702
   178
      {
wenzelm@44702
   179
        val (styled_tokens, context1) =
wenzelm@44702
   180
          if (line_ctxt.isDefined && Isabelle.session.is_ready) {
wenzelm@44702
   181
            val syntax = Isabelle.session.current_syntax()
wenzelm@44702
   182
            val (tokens, ctxt1) = syntax.scan_context(line, line_ctxt.get)
wenzelm@46178
   183
            val styled_tokens =
wenzelm@46178
   184
              tokens.map(tok => (Isabelle_Rendering.token_markup(syntax, tok), tok))
wenzelm@44702
   185
            (styled_tokens, new Line_Context(Some(ctxt1)))
wenzelm@44702
   186
          }
wenzelm@44702
   187
          else {
wenzelm@44702
   188
            val token = Token(Token.Kind.UNPARSED, line.subSequence(0, line.count).toString)
wenzelm@44702
   189
            (List((JEditToken.NULL, token)), new Line_Context(None))
wenzelm@44702
   190
          }
wenzelm@44702
   191
wenzelm@44702
   192
        val extended = extended_styles(line)
wenzelm@44702
   193
wenzelm@44702
   194
        var offset = 0
wenzelm@44702
   195
        for ((style, token) <- styled_tokens) {
wenzelm@44702
   196
          val length = token.source.length
wenzelm@44702
   197
          val end_offset = offset + length
wenzelm@44702
   198
          if ((offset until end_offset) exists
wenzelm@44702
   199
              (i => extended.isDefinedAt(i) || line.charAt(i) == '\t')) {
wenzelm@44702
   200
            for (i <- offset until end_offset) {
wenzelm@44702
   201
              val style1 =
wenzelm@44702
   202
                extended.get(i) match {
wenzelm@44702
   203
                  case None => style
wenzelm@44702
   204
                  case Some(ext) => ext(style)
wenzelm@44702
   205
                }
wenzelm@44702
   206
              handler.handleToken(line, style1, i, 1, context1)
wenzelm@43553
   207
            }
wenzelm@43553
   208
          }
wenzelm@44702
   209
          else handler.handleToken(line, style, offset, length, context1)
wenzelm@44702
   210
          offset += length
wenzelm@43452
   211
        }
wenzelm@44702
   212
        handler.handleToken(line, JEditToken.END, line.count, 0, context1)
wenzelm@44702
   213
        context1
wenzelm@44702
   214
      }
wenzelm@43452
   215
      val context2 = context1.intern
wenzelm@43452
   216
      handler.setLineContext(context2)
wenzelm@43452
   217
      context2
wenzelm@43452
   218
    }
wenzelm@43452
   219
  }
wenzelm@43452
   220
wenzelm@43416
   221
wenzelm@43452
   222
  /* mode provider */
wenzelm@43452
   223
wenzelm@44358
   224
  private val isabelle_token_marker = new Token_Markup.Marker
wenzelm@44358
   225
wenzelm@43452
   226
  class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
wenzelm@43452
   227
  {
wenzelm@43452
   228
    for (mode <- orig_provider.getModes) addMode(mode)
wenzelm@43452
   229
wenzelm@43452
   230
    override def loadMode(mode: Mode, xmh: XModeHandler)
wenzelm@43452
   231
    {
wenzelm@43452
   232
      super.loadMode(mode, xmh)
wenzelm@43452
   233
      if (mode.getName == "isabelle")
wenzelm@43452
   234
        mode.setTokenMarker(isabelle_token_marker)
wenzelm@43414
   235
    }
wenzelm@43452
   236
  }
wenzelm@44358
   237
wenzelm@44358
   238
  def refresh_buffer(buffer: JEditBuffer)
wenzelm@44358
   239
  {
wenzelm@44358
   240
    buffer.setTokenMarker(jEdit.getMode("text").getTokenMarker)
wenzelm@44358
   241
    buffer.setTokenMarker(isabelle_token_marker)
wenzelm@44358
   242
    buffer.propertiesChanged
wenzelm@44358
   243
  }
wenzelm@43414
   244
}
wenzelm@43414
   245