src/Tools/jEdit/src/jedit/DynamicTokenMarker.scala
author immler@in.tum.de
Tue, 02 Jun 2009 22:22:03 +0200
changeset 34596 2b46d92e4642
parent 34579 f5c3ad245539
child 34597 a0c84b0edb9a
permissions -rw-r--r--
linearset works faster here
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
34549
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    22
  // Mapping to jEdits token types
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    23
  def choose_byte(kind: String): Byte = {
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    24
    // TODO: as properties
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    25
    kind match {
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    26
      // logical entities
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    27
      case Markup.TCLASS | Markup.TYCON | Markup.FIXED_DECL | Markup.FIXED | Markup.CONST_DECL
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    28
        | Markup.CONST | Markup.FACT_DECL | Markup.FACT | Markup.DYNAMIC_FACT
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    29
        | Markup.LOCAL_FACT_DECL | Markup.LOCAL_FACT => Token.DIGIT
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    30
      // inner syntax
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    31
      case Markup.TFREE | Markup.FREE => Token.LITERAL2
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    32
      case Markup.TVAR | Markup.SKOLEM | Markup.BOUND | Markup.VAR => Token.LITERAL3
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    33
      case Markup.NUM | Markup.FLOAT | Markup.XNUM | Markup.XSTR | Markup.LITERAL
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    34
        | Markup.INNER_COMMENT => Token.LITERAL4
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    35
      case Markup.SORT | Markup.TYP | Markup.TERM | Markup.PROP
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    36
        | Markup.ATTRIBUTE | Markup.METHOD => Token.FUNCTION
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    37
      // ML syntax
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    38
      case Markup.ML_KEYWORD => Token.KEYWORD2
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    39
      case Markup.ML_IDENT => Token.KEYWORD3
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    40
      case Markup.ML_TVAR => Token.LITERAL3
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    41
      case Markup.ML_NUMERAL => Token.LITERAL4
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    42
      case Markup.ML_CHAR | Markup.ML_STRING => Token.LITERAL1
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    43
      case Markup.ML_COMMENT => Token.COMMENT1
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    44
      case Markup.ML_MALFORMED => Token.INVALID
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    45
      // embedded source text
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    46
      case Markup.ML_SOURCE | Markup.DOC_SOURCE | Markup.ANTIQ | Markup.ML_ANTIQ
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    47
        | Markup.DOC_ANTIQ => Token.COMMENT4
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    48
      // outer syntax
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    49
      case Markup.IDENT => Token.KEYWORD3
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    50
      case Markup.COMMAND => Token.KEYWORD1
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    51
      case Markup.KEYWORD => Token.KEYWORD2
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    52
      case Markup.VERBATIM => Token.COMMENT1
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    53
      case Markup.COMMENT => Token.COMMENT2
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    54
      case Markup.CONTROL => Token.COMMENT3
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    55
      case Markup.MALFORMED => Token.INVALID
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    56
      case Markup.STRING | Markup.ALTSTRING => Token.LITERAL1
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    57
      // other
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    58
      case _ => 0
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    59
    }
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    60
  }
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
    61
34579
f5c3ad245539 outer syntax could clash with status on removed commands
immler@in.tum.de
parents: 34564
diff changeset
    62
  def is_outer(kind: String) =
f5c3ad245539 outer syntax could clash with status on removed commands
immler@in.tum.de
parents: 34564
diff changeset
    63
    List(Markup.IDENT, Markup.COMMAND, Markup.KEYWORD, Markup.VERBATIM, Markup.COMMENT,
f5c3ad245539 outer syntax could clash with status on removed commands
immler@in.tum.de
parents: 34564
diff changeset
    64
         Markup.CONTROL, Markup.MALFORMED, Markup.STRING, Markup.ALTSTRING).exists(kind == _)
f5c3ad245539 outer syntax could clash with status on removed commands
immler@in.tum.de
parents: 34564
diff changeset
    65
34549
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    66
  def choose_color(kind : String, styles: Array[SyntaxStyle]) : Color =
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    67
    styles((choose_byte(kind).asInstanceOf[Byte])).getForegroundColor
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    68
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
    69
}
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    70
34538
20bfcca24658 Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents: 34524
diff changeset
    71
class DynamicTokenMarker(buffer: JEditBuffer, prover: Prover) extends TokenMarker {
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    72
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    73
  override def markTokens(prev: TokenMarker.LineContext,
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    74
    handler: TokenHandler, line_segment: Segment): TokenMarker.LineContext = {
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    75
    val previous = prev.asInstanceOf[IndexLineContext]
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    76
    val line = if(prev == null) 0 else previous.line + 1
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    77
    val context = new IndexLineContext(line, previous)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    78
    val start = buffer.getLineStartOffset(line)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    79
    val stop = start + line_segment.count
34541
e3ca0658fb6a changes of text with unique id
immler@in.tum.de
parents: 34538
diff changeset
    80
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34549
diff changeset
    81
    val document = prover.document
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34549
diff changeset
    82
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34549
diff changeset
    83
    def to: Int => Int = Isabelle.prover_setup(buffer).get.theory_view.to_current(document.id, _)
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34549
diff changeset
    84
    def from: Int => Int = Isabelle.prover_setup(buffer).get.theory_view.from_current(document.id, _)
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    85
34522
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
    86
    var next_x = start
34596
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    87
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    88
    var command = document.find_command_at(from(start))
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    89
    while (command != null && command.start(document) < from(stop)) {
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    90
      for {
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    91
        markup <- command.highlight_node.flatten
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    92
        if(to(markup.abs_stop(document)) > start)
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    93
        if(to(markup.abs_start(document)) < stop)
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    94
        byte = DynamicTokenMarker.choose_byte(markup.info.toString)
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    95
        token_start = to(markup.abs_start(document)) - start max 0
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    96
        token_length = to(markup.abs_stop(document)) - to(markup.abs_start(document)) -
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    97
                       (start - to(markup.abs_start(document)) max 0) -
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    98
                       (to(markup.abs_stop(document)) - stop max 0)
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
    99
      } {
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
   100
        if (start + token_start > next_x)
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
   101
          handler.handleToken(line_segment, 1, next_x - start, start + token_start - next_x, context)
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
   102
        handler.handleToken(line_segment, byte, token_start, token_length, context)
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
   103
        next_x = start + token_start + token_length
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
   104
      }
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34579
diff changeset
   105
      command = document.commands.next(command).getOrElse(null)
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   106
    }
34522
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
   107
    if (next_x < stop)
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
   108
      handler.handleToken(line_segment, 1, next_x - start, stop - next_x, context)
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   109
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   110
    handler.handleToken(line_segment,Token.END, line_segment.count, 0, context)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   111
    handler.setLineContext(context)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   112
    return context
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   113
  }
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   114
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   115
}
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   116
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   117
class IndexLineContext(val line: Int, prev: IndexLineContext)
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   118
  extends TokenMarker.LineContext(new ParserRuleSet("isabelle", "MAIN"), prev)