src/Tools/jEdit/src/token_markup.scala
author wenzelm
Tue Aug 23 12:20:12 2011 +0200 (2011-08-23)
changeset 44385 e7fdb008aa7d
parent 44358 2a2df4de1bbe
child 44701 0fd2bf8eaa9f
permissions -rw-r--r--
propagate editor perspective through document model;
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@43452
   168
        handler: TokenHandler, 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@43553
   175
      val context1 =
wenzelm@44358
   176
        if (line_ctxt.isDefined && Isabelle.session.is_ready) {
wenzelm@43553
   177
          val syntax = Isabelle.session.current_syntax()
wenzelm@43553
   178
    
wenzelm@44358
   179
          val (tokens, ctxt1) = syntax.scan_context(line, line_ctxt.get)
wenzelm@44358
   180
          val context1 = new Line_Context(Some(ctxt1))
wenzelm@43553
   181
    
wenzelm@43661
   182
          val extended = extended_styles(line)
wenzelm@43553
   183
    
wenzelm@43553
   184
          var offset = 0
wenzelm@43553
   185
          for (token <- tokens) {
wenzelm@43553
   186
            val style = Isabelle_Markup.token_markup(syntax, token)
wenzelm@43553
   187
            val length = token.source.length
wenzelm@43553
   188
            val end_offset = offset + length
wenzelm@43553
   189
            if ((offset until end_offset) exists extended.isDefinedAt) {
wenzelm@43553
   190
              for (i <- offset until end_offset) {
wenzelm@43553
   191
                val style1 =
wenzelm@43553
   192
                  extended.get(i) match {
wenzelm@43553
   193
                    case None => style
wenzelm@43553
   194
                    case Some(ext) => ext(style)
wenzelm@43553
   195
                  }
wenzelm@43553
   196
                handler.handleToken(line, style1, i, 1, context1)
wenzelm@43553
   197
              }
wenzelm@43553
   198
            }
wenzelm@43553
   199
            else handler.handleToken(line, style, offset, length, context1)
wenzelm@43553
   200
            offset += length
wenzelm@43553
   201
          }
wenzelm@43553
   202
          handler.handleToken(line, JEditToken.END, line.count, 0, context1)
wenzelm@43553
   203
          context1
wenzelm@43452
   204
        }
wenzelm@43553
   205
        else {
wenzelm@44358
   206
          val context1 = new Line_Context(None)
wenzelm@43553
   207
          handler.handleToken(line, JEditToken.NULL, 0, line.count, context1)
wenzelm@43553
   208
          handler.handleToken(line, JEditToken.END, line.count, 0, context1)
wenzelm@43553
   209
          context1
wenzelm@43414
   210
        }
wenzelm@43452
   211
      val context2 = context1.intern
wenzelm@43452
   212
      handler.setLineContext(context2)
wenzelm@43452
   213
      context2
wenzelm@43452
   214
    }
wenzelm@43452
   215
  }
wenzelm@43452
   216
wenzelm@43416
   217
wenzelm@43452
   218
  /* mode provider */
wenzelm@43452
   219
wenzelm@44358
   220
  private val isabelle_token_marker = new Token_Markup.Marker
wenzelm@44358
   221
wenzelm@43452
   222
  class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider
wenzelm@43452
   223
  {
wenzelm@43452
   224
    for (mode <- orig_provider.getModes) addMode(mode)
wenzelm@43452
   225
wenzelm@43452
   226
    override def loadMode(mode: Mode, xmh: XModeHandler)
wenzelm@43452
   227
    {
wenzelm@43452
   228
      super.loadMode(mode, xmh)
wenzelm@43452
   229
      if (mode.getName == "isabelle")
wenzelm@43452
   230
        mode.setTokenMarker(isabelle_token_marker)
wenzelm@43414
   231
    }
wenzelm@43452
   232
  }
wenzelm@44358
   233
wenzelm@44358
   234
  def refresh_buffer(buffer: JEditBuffer)
wenzelm@44358
   235
  {
wenzelm@44358
   236
    buffer.setTokenMarker(jEdit.getMode("text").getTokenMarker)
wenzelm@44358
   237
    buffer.setTokenMarker(isabelle_token_marker)
wenzelm@44358
   238
    buffer.propertiesChanged
wenzelm@44358
   239
  }
wenzelm@43414
   240
}
wenzelm@43414
   241