src/Tools/jEdit/src/jedit/DynamicTokenMarker.scala
author immler@in.tum.de
Fri, 20 Mar 2009 12:46:57 +0100
changeset 34541 e3ca0658fb6a
parent 34538 20bfcca24658
child 34542 e647f063ffad
permissions -rw-r--r--
changes of text with unique id
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
     1
/*
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
     2
 * include isabelle's command- and keyword-declarations
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
     3
 * live in jEdits syntax-highlighting
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
     4
 *
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
     5
 * @author Fabian Immler, TU Munich
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
     6
 */
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
     7
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
     8
package isabelle.jedit
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
     9
34538
20bfcca24658 Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents: 34524
diff changeset
    10
import isabelle.prover.{Command, MarkupNode, Prover}
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    11
import isabelle.Markup
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
    12
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    13
import org.gjt.sp.jedit.buffer.JEditBuffer
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    14
import org.gjt.sp.jedit.syntax._
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
    15
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    16
import java.awt.Color
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    17
import java.awt.Font
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    18
import javax.swing.text.Segment;
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    19
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    20
object DynamicTokenMarker {
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
    21
34524
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    22
  var styles : Array[SyntaxStyle] = null
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    23
  def reload_styles: Array[SyntaxStyle] = {
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    24
    styles = new Array[SyntaxStyle](256)
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    25
    //jEdit styles
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    26
    for(i <- 0 to Token.ID_COUNT) styles(i) = new SyntaxStyle(Color.black, Color.white, Isabelle.plugin.font)
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    27
    //isabelle styles
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    28
    def from_property(kind : String, spec : String, default : Color) : Color = 
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    29
      try {Color.decode(Isabelle.Property("styles." + kind + "." + spec))} catch {case _ => default}
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    30
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    31
    for((kind, i) <- kinds) styles(i + FIRST_BYTE) = new SyntaxStyle(
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    32
      from_property(kind, "foreground", Color.black),
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    33
      from_property(kind, "background", Color.white),
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    34
      Isabelle.plugin.font)
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    35
    return styles
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    36
  }
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
    37
34524
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    38
  private final val FIRST_BYTE : Byte = 30
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    39
  val kinds = List(// TODO Markups as Enumeration?
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    40
     // default style
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    41
    null,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    42
    // logical entities
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    43
    Markup.TCLASS, Markup.TYCON, Markup.FIXED_DECL, Markup.FIXED, Markup.CONST_DECL,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    44
    Markup.CONST, Markup.FACT_DECL, Markup.FACT, Markup.DYNAMIC_FACT,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    45
    Markup.LOCAL_FACT_DECL, Markup.LOCAL_FACT,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    46
    // inner syntax
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    47
    Markup.TFREE, Markup.FREE,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    48
    Markup.TVAR, Markup.SKOLEM, Markup.BOUND, Markup.VAR,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    49
    Markup.NUM, Markup.FLOAT, Markup.XNUM, Markup.XSTR, Markup.LITERAL,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    50
    Markup.INNER_COMMENT,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    51
    Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    52
    Markup.ATTRIBUTE, Markup.METHOD,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    53
    // embedded source text
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    54
    Markup.ML_SOURCE, Markup.DOC_SOURCE, Markup.ANTIQ, Markup.ML_ANTIQ,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    55
    Markup.DOC_ANTIQ,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    56
    // outer syntax
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    57
    Markup.IDENT, Markup.COMMAND, Markup.KEYWORD, Markup.VERBATIM, Markup.COMMENT,
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    58
    Markup.CONTROL, Markup.MALFORMED, Markup.STRING, Markup.ALTSTRING
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    59
  ).zipWithIndex
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    60
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    61
  def choose_byte(kind : String) : Byte = (kinds.find (k => kind == k._1)) match {
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    62
    case Some((kind, index)) => (index + FIRST_BYTE).asInstanceOf[Byte]
06a18bcf4e74 syntax styles in properties
immler@in.tum.de
parents: 34523
diff changeset
    63
    case _ => FIRST_BYTE
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
    64
  }
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    65
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    66
  def choose_color(kind : String) : Color = styles((choose_byte(kind).asInstanceOf[Byte])).getForegroundColor
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    67
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
    68
}
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    69
34538
20bfcca24658 Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents: 34524
diff changeset
    70
class DynamicTokenMarker(buffer: JEditBuffer, prover: Prover) extends TokenMarker {
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    71
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    72
  override def markTokens(prev: TokenMarker.LineContext,
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    73
    handler: TokenHandler, line_segment: Segment): TokenMarker.LineContext = {
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    74
    val previous = prev.asInstanceOf[IndexLineContext]
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    75
    val line = if(prev == null) 0 else previous.line + 1
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    76
    val context = new IndexLineContext(line, previous)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    77
    val start = buffer.getLineStartOffset(line)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    78
    val stop = start + line_segment.count
34541
e3ca0658fb6a changes of text with unique id
immler@in.tum.de
parents: 34538
diff changeset
    79
e3ca0658fb6a changes of text with unique id
immler@in.tum.de
parents: 34538
diff changeset
    80
    val (current_document, current_version) = synchronized (prover.document, prover.document_id)
e3ca0658fb6a changes of text with unique id
immler@in.tum.de
parents: 34538
diff changeset
    81
   
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    82
    def to = Isabelle.prover_setup(buffer).get.theory_view.to_current(_)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    83
    def from = Isabelle.prover_setup(buffer).get.theory_view.from_current(_)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    84
34522
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    85
    var next_x = start
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    86
    for {
34538
20bfcca24658 Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents: 34524
diff changeset
    87
      command <- prover.document.commands.dropWhile(_.stop <= from(start)).takeWhile(_.start < from(stop))
34522
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    88
      markup <- command.root_node.flatten
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    89
      if(to(markup.abs_stop) > start)
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    90
      if(to(markup.abs_start) < stop)
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    91
      byte = DynamicTokenMarker.choose_byte(markup.kind)
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    92
      token_start = to(markup.abs_start) - start max 0
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    93
      token_length = to(markup.abs_stop) - to(markup.abs_start) -
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    94
                     (start - to(markup.abs_start) max 0) -
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    95
                     (to(markup.abs_stop) - stop max 0)
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    96
    } {
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    97
      if (start + token_start > next_x)
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    98
        handler.handleToken(line_segment, 1, next_x - start, start + token_start - next_x, context)
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    99
      handler.handleToken(line_segment, byte, token_start, token_length, context)
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
   100
      next_x = start + token_start + token_length
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   101
    }
34522
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
   102
    if (next_x < stop)
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
   103
      handler.handleToken(line_segment, 1, next_x - start, stop - next_x, context)
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   104
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   105
    handler.handleToken(line_segment,Token.END, line_segment.count, 0, context)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   106
    handler.setLineContext(context)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   107
    return context
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   108
  }
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   109
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   110
}
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   111
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   112
class IndexLineContext(val line: Int, prev: IndexLineContext)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   113
  extends TokenMarker.LineContext(new ParserRuleSet("isabelle", "MAIN"), prev)