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