src/Tools/jEdit/src/jedit/isabelle_token_marker.scala
author wenzelm
Tue, 08 Jun 2010 13:51:25 +0200
changeset 37366 5c6695de35ba
parent 37241 04d2521e79b0
child 38150 67fc24df3721
permissions -rw-r--r--
disable set_styles for now -- there are still some race conditions of PropertiesChanged vs. TextArea painting (NB: without it Isabelle_Token_Marker will crash if sub/superscript is actually used);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36760
b82a698ef6c9 tuned headers;
wenzelm
parents: 34867
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit/isabelle_token_marker.scala
b82a698ef6c9 tuned headers;
wenzelm
parents: 34867
diff changeset
     2
    Author:     Fabian Immler, TU Munich
b82a698ef6c9 tuned headers;
wenzelm
parents: 34867
diff changeset
     3
b82a698ef6c9 tuned headers;
wenzelm
parents: 34867
diff changeset
     4
Include isabelle's command- and keyword-declarations live in jEdits
b82a698ef6c9 tuned headers;
wenzelm
parents: 34867
diff changeset
     5
syntax-highlighting.
b82a698ef6c9 tuned headers;
wenzelm
parents: 34867
diff changeset
     6
*/
34467
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
34700
0e1d098940a7 tuned imports;
wenzelm
parents: 34687
diff changeset
    10
37196
wenzelm
parents: 37195
diff changeset
    11
import isabelle._
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
37241
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    14
import org.gjt.sp.jedit.syntax.{Token, TokenMarker, TokenHandler, ParserRuleSet, SyntaxStyle}
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    15
import org.gjt.sp.jedit.textarea.TextArea
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
    16
37241
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    17
import java.awt.Font
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    18
import java.awt.font.TextAttribute
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    19
import javax.swing.text.Segment;
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
    20
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34582
diff changeset
    21
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
    22
object Isabelle_Token_Marker
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34582
diff changeset
    23
{
37241
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    24
  /* extended token styles */
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    25
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    26
  private val plain_range: Int = Token.ID_COUNT
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    27
  private val full_range: Int = 3 * plain_range
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    28
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    29
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    30
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    31
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    32
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    33
  private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    34
  {
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    35
    import scala.collection.JavaConversions._
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    36
    val script_font =
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    37
      style.getFont.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    38
    new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, script_font)
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    39
  }
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    40
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    41
  def extend_styles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    42
  {
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    43
    val new_styles = new Array[SyntaxStyle](full_range)
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    44
    for (i <- 0 until plain_range) {
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    45
      val style = styles(i)
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    46
      new_styles(i) = style
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    47
      new_styles(subscript(i.toByte)) = script_style(style, -1)
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    48
      new_styles(superscript(i.toByte)) = script_style(style, 1)
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    49
    }
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    50
    new_styles
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    51
  }
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    52
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
    53
34709
2f0c18f9b6c7 minor tuning;
wenzelm
parents: 34704
diff changeset
    54
  /* line context */
2f0c18f9b6c7 minor tuning;
wenzelm
parents: 34704
diff changeset
    55
34727
3ec545c964d5 single instance of rule_set;
wenzelm
parents: 34717
diff changeset
    56
  private val rule_set = new ParserRuleSet("isabelle", "MAIN")
34709
2f0c18f9b6c7 minor tuning;
wenzelm
parents: 34704
diff changeset
    57
  private class LineContext(val line: Int, prev: LineContext)
34727
3ec545c964d5 single instance of rule_set;
wenzelm
parents: 34717
diff changeset
    58
    extends TokenMarker.LineContext(rule_set, prev)
34709
2f0c18f9b6c7 minor tuning;
wenzelm
parents: 34704
diff changeset
    59
2f0c18f9b6c7 minor tuning;
wenzelm
parents: 34704
diff changeset
    60
2f0c18f9b6c7 minor tuning;
wenzelm
parents: 34704
diff changeset
    61
  /* mapping to jEdit token types */
34638
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
    62
  // TODO: as properties or CSS style sheet
34709
2f0c18f9b6c7 minor tuning;
wenzelm
parents: 34704
diff changeset
    63
37197
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    64
  private val command_style: Map[String, Byte] =
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    65
  {
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    66
    import Token._
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    67
    Map[String, Byte](
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    68
      Keyword.THY_END -> KEYWORD2,
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    69
      Keyword.THY_SCRIPT -> LABEL,
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    70
      Keyword.PRF_SCRIPT -> LABEL,
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    71
      Keyword.PRF_ASM -> KEYWORD3,
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    72
      Keyword.PRF_ASM_GOAL -> KEYWORD3
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    73
    ).withDefaultValue(KEYWORD1)
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    74
  }
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    75
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
    76
  private val token_style: Map[String, Byte] =
34638
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
    77
  {
34861
309d545295d3 eliminated obsolete isabelle.proofdocument.Token;
wenzelm
parents: 34855
diff changeset
    78
    import Token._
34639
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
    79
    Map[String, Byte](
34549
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    80
      // logical entities
37194
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    81
      Markup.TCLASS -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    82
      Markup.TYCON -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    83
      Markup.FIXED_DECL -> FUNCTION,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    84
      Markup.FIXED -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    85
      Markup.CONST_DECL -> FUNCTION,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    86
      Markup.CONST -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    87
      Markup.FACT_DECL -> FUNCTION,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    88
      Markup.FACT -> NULL,
34639
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
    89
      Markup.DYNAMIC_FACT -> LABEL,
37194
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    90
      Markup.LOCAL_FACT_DECL -> FUNCTION,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    91
      Markup.LOCAL_FACT -> NULL,
34549
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
    92
      // inner syntax
37194
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    93
      Markup.TFREE -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    94
      Markup.FREE -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    95
      Markup.TVAR -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    96
      Markup.SKOLEM -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    97
      Markup.BOUND -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
    98
      Markup.VAR -> NULL,
34639
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
    99
      Markup.NUM -> DIGIT,
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
   100
      Markup.FLOAT -> DIGIT,
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
   101
      Markup.XNUM -> DIGIT,
34638
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   102
      Markup.XSTR -> LITERAL4,
37194
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   103
      Markup.LITERAL -> OPERATOR,
34639
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
   104
      Markup.INNER_COMMENT -> COMMENT1,
37194
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   105
      Markup.SORT -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   106
      Markup.TYP -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   107
      Markup.TERM -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   108
      Markup.PROP -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   109
      Markup.ATTRIBUTE -> NULL,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   110
      Markup.METHOD -> NULL,
34549
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
   111
      // ML syntax
37194
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   112
      Markup.ML_KEYWORD -> KEYWORD1,
37195
e87d305a4490 separate markup for ML delimiters;
wenzelm
parents: 37194
diff changeset
   113
      Markup.ML_DELIMITER -> OPERATOR,
34640
c620d8c7f6b3 use symbolic NULL;
wenzelm
parents: 34639
diff changeset
   114
      Markup.ML_IDENT -> NULL,
37194
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   115
      Markup.ML_TVAR -> NULL,
34639
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
   116
      Markup.ML_NUMERAL -> DIGIT,
34638
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   117
      Markup.ML_CHAR -> LITERAL1,
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   118
      Markup.ML_STRING -> LITERAL1,
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   119
      Markup.ML_COMMENT -> COMMENT1,
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   120
      Markup.ML_MALFORMED -> INVALID,
34549
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
   121
      // embedded source text
37194
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   122
      Markup.ML_SOURCE -> COMMENT3,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   123
      Markup.DOC_SOURCE -> COMMENT3,
34638
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   124
      Markup.ANTIQ -> COMMENT4,
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   125
      Markup.ML_ANTIQ -> COMMENT4,
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   126
      Markup.DOC_ANTIQ -> COMMENT4,
34549
5be7a165a9b9 use jEdits styles
immler@in.tum.de
parents: 34545
diff changeset
   127
      // outer syntax
37194
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   128
      Markup.KEYWORD -> KEYWORD2,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   129
      Markup.OPERATOR -> OPERATOR,
825456e5db30 less pschedelic token markup;
wenzelm
parents: 36990
diff changeset
   130
      Markup.COMMAND -> KEYWORD1,
34640
c620d8c7f6b3 use symbolic NULL;
wenzelm
parents: 34639
diff changeset
   131
      Markup.IDENT -> NULL,
34639
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
   132
      Markup.VERBATIM -> COMMENT3,
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
   133
      Markup.COMMENT -> COMMENT1,
34638
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   134
      Markup.CONTROL -> COMMENT3,
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   135
      Markup.MALFORMED -> INVALID,
34639
051635fa7b7e tuned token styles, according to some earlier Isabelle/jEdit experiments;
wenzelm
parents: 34638
diff changeset
   136
      Markup.STRING -> LITERAL3,
34638
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   137
      Markup.ALTSTRING -> LITERAL1
34640
c620d8c7f6b3 use symbolic NULL;
wenzelm
parents: 34639
diff changeset
   138
    ).withDefaultValue(NULL)
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   139
  }
34467
c7d7a92fe3d5 created DynamicTokenMarker
immler@in.tum.de
parents:
diff changeset
   140
}
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   141
34700
0e1d098940a7 tuned imports;
wenzelm
parents: 34687
diff changeset
   142
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34777
diff changeset
   143
class Isabelle_Token_Marker(model: Document_Model) extends TokenMarker
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34582
diff changeset
   144
{
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   145
  override def markTokens(prev: TokenMarker.LineContext,
34638
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   146
      handler: TokenHandler, line_segment: Segment): TokenMarker.LineContext =
c7de7b382318 use static Map/Set for token categorization;
wenzelm
parents: 34598
diff changeset
   147
  {
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   148
    val previous = prev.asInstanceOf[Isabelle_Token_Marker.LineContext]
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34579
diff changeset
   149
    val line = if (prev == null) 0 else previous.line + 1
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   150
    val context = new Isabelle_Token_Marker.LineContext(line, previous)
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34777
diff changeset
   151
    val start = model.buffer.getLineStartOffset(line)
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   152
    val stop = start + line_segment.count
34541
e3ca0658fb6a changes of text with unique id
immler@in.tum.de
parents: 34538
diff changeset
   153
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34784
diff changeset
   154
    val document = model.recent_document()
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34777
diff changeset
   155
    def to: Int => Int = model.to_current(document, _)
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34777
diff changeset
   156
    def from: Int => Int = model.from_current(document, _)
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   157
37366
5c6695de35ba disable set_styles for now -- there are still some race conditions of PropertiesChanged vs. TextArea painting (NB: without it Isabelle_Token_Marker will crash if sub/superscript is actually used);
wenzelm
parents: 37241
diff changeset
   158
    /* FIXME
37241
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   159
    for (text_area <- Isabelle.jedit_text_areas(model.buffer)
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   160
          if Document_View(text_area).isDefined)
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   161
      Document_View(text_area).get.set_styles()
37366
5c6695de35ba disable set_styles for now -- there are still some race conditions of PropertiesChanged vs. TextArea painting (NB: without it Isabelle_Token_Marker will crash if sub/superscript is actually used);
wenzelm
parents: 37241
diff changeset
   162
    */
37241
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   163
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   164
    def handle_token(style: Byte, offset: Int, length: Int) =
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   165
      handler.handleToken(line_segment, style, offset, length, context)
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   166
34522
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
   167
    var next_x = start
34855
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   168
    for {
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   169
      (command, command_start) <- document.command_range(from(start), from(stop))
36990
449628c148cf explicit Command.Status.UNDEFINED -- avoid fragile/cumbersome treatment of Option[State];
wenzelm
parents: 36760
diff changeset
   170
      markup <- document.current_state(command).highlight.flatten
34855
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   171
      val abs_start = to(command_start + markup.start)
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   172
      val abs_stop = to(command_start + markup.stop)
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   173
      if (abs_stop > start)
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   174
      if (abs_start < stop)
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   175
      val token_start = (abs_start - start) max 0
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   176
      val token_length =
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   177
        (abs_stop - abs_start) -
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   178
        ((start - abs_start) max 0) -
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   179
        ((abs_stop - stop) max 0)
37197
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   180
    }
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   181
    {
37241
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   182
      val token_type =
37197
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   183
        markup.info match {
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   184
          case Command.HighlightInfo(Markup.COMMAND, Some(kind)) =>
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   185
            Isabelle_Token_Marker.command_style(kind)
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   186
          case Command.HighlightInfo(kind, _) =>
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   187
            Isabelle_Token_Marker.token_style(kind)
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   188
          case _ => Token.NULL
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   189
        }
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   190
      if (start + token_start > next_x)
37241
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   191
        handle_token(Token.COMMENT1, next_x - start, start + token_start - next_x)
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   192
      handle_token(token_type, token_start, token_length)
37197
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   193
      next_x = start + token_start + token_length
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37196
diff changeset
   194
    }
34522
7cd619ee3917 removed redundant code
immler@in.tum.de
parents: 34518
diff changeset
   195
    if (next_x < stop)
37241
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   196
      handle_token(Token.COMMENT1, next_x - start, stop - next_x)
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   197
37241
04d2521e79b0 basic support for sub/superscript token markup -- NB: need to maintain extended token types eagerly, since jEdit occasionally reinstalls a style array that is too short;
wenzelm
parents: 37197
diff changeset
   198
    handle_token(Token.END, line_segment.count, 0)
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   199
    handler.setLineContext(context)
34855
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34832
diff changeset
   200
    context
34517
163cda249619 implemented markTokens;
immler@in.tum.de
parents: 34472
diff changeset
   201
  }
34700
0e1d098940a7 tuned imports;
wenzelm
parents: 34687
diff changeset
   202
}