src/Tools/jEdit/src/isabelle_markup.scala
author wenzelm
Sat Nov 12 19:44:56 2011 +0100 (2011-11-12)
changeset 45474 f793dd5d84b2
parent 45468 33e946d3f449
child 45489 9b6f55f34b70
permissions -rw-r--r--
index markup elements for more efficient cumulate/select operations;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/isabelle_markup.scala
wenzelm@39178
     2
    Author:     Makarius
wenzelm@39178
     3
wenzelm@39178
     4
Isabelle specific physical rendering and markup selection.
wenzelm@39178
     5
*/
wenzelm@39178
     6
wenzelm@39178
     7
package isabelle.jedit
wenzelm@39178
     8
wenzelm@39178
     9
wenzelm@39178
    10
import isabelle._
wenzelm@39178
    11
wenzelm@39178
    12
import java.awt.Color
wenzelm@39178
    13
wenzelm@43386
    14
import org.lobobrowser.util.gui.ColorFactory
wenzelm@43414
    15
import org.gjt.sp.jedit.syntax.{Token => JEditToken}
wenzelm@39178
    16
wenzelm@45460
    17
import scala.collection.immutable.SortedMap
wenzelm@45460
    18
wenzelm@39178
    19
wenzelm@39178
    20
object Isabelle_Markup
wenzelm@39178
    21
{
wenzelm@39178
    22
  /* physical rendering */
wenzelm@39178
    23
wenzelm@43377
    24
  // see http://www.w3schools.com/css/css_colornames.asp
wenzelm@43377
    25
wenzelm@43386
    26
  def get_color(s: String): Color = ColorFactory.getInstance.getColor(s)
wenzelm@43386
    27
wenzelm@44579
    28
  val outdated_color = new Color(238, 227, 227)
wenzelm@44611
    29
  val running_color = new Color(97, 0, 97)
wenzelm@44611
    30
  val running1_color = new Color(97, 0, 97, 100)
wenzelm@44866
    31
  val unprocessed_color = new Color(255, 160, 160)
wenzelm@44866
    32
  val unprocessed1_color = new Color(255, 160, 160, 50)
wenzelm@39178
    33
wenzelm@39690
    34
  val light_color = new Color(240, 240, 240)
wenzelm@39178
    35
  val regular_color = new Color(192, 192, 192)
wenzelm@39692
    36
  val warning_color = new Color(255, 140, 0)
wenzelm@39691
    37
  val error_color = new Color(178, 34, 34)
wenzelm@44866
    38
  val error1_color = new Color(178, 34, 34, 50)
wenzelm@39690
    39
  val bad_color = new Color(255, 106, 106, 100)
wenzelm@42171
    40
  val hilite_color = new Color(255, 204, 102, 100)
wenzelm@39178
    41
wenzelm@44546
    42
  val quoted_color = new Color(139, 139, 139, 25)
wenzelm@44545
    43
  val subexp_color = new Color(80, 80, 80, 50)
wenzelm@43435
    44
wenzelm@43388
    45
  val keyword1_color = get_color("#006699")
wenzelm@43388
    46
  val keyword2_color = get_color("#009966")
wenzelm@43388
    47
wenzelm@39181
    48
  class Icon(val priority: Int, val icon: javax.swing.Icon)
wenzelm@39181
    49
  {
wenzelm@39181
    50
    def >= (that: Icon): Boolean = this.priority >= that.priority
wenzelm@39181
    51
  }
wenzelm@44547
    52
  val warning_icon = new Icon(1, Isabelle.load_icon("16x16/status/dialog-information.png"))
wenzelm@44549
    53
  val legacy_icon = new Icon(2, Isabelle.load_icon("16x16/status/dialog-warning.png"))
wenzelm@44549
    54
  val error_icon = new Icon(3, Isabelle.load_icon("16x16/status/dialog-error.png"))
wenzelm@39178
    55
wenzelm@39178
    56
wenzelm@39178
    57
  /* command status */
wenzelm@39178
    58
wenzelm@39178
    59
  def status_color(snapshot: Document.Snapshot, command: Command): Option[Color] =
wenzelm@39178
    60
  {
wenzelm@44582
    61
    val state = snapshot.command_state(command)
wenzelm@44723
    62
    if (snapshot.is_outdated) Some(outdated_color)
wenzelm@39178
    63
    else
wenzelm@39178
    64
      Isar_Document.command_status(state.status) match {
wenzelm@44611
    65
        case Isar_Document.Forked(i) if i > 0 => Some(running1_color)
wenzelm@44866
    66
        case Isar_Document.Unprocessed => Some(unprocessed1_color)
wenzelm@39178
    67
        case _ => None
wenzelm@39178
    68
      }
wenzelm@39178
    69
  }
wenzelm@39178
    70
wenzelm@39178
    71
  def overview_color(snapshot: Document.Snapshot, command: Command): Option[Color] =
wenzelm@39178
    72
  {
wenzelm@44582
    73
    val state = snapshot.command_state(command)
wenzelm@39178
    74
    if (snapshot.is_outdated) None
wenzelm@39178
    75
    else
wenzelm@39178
    76
      Isar_Document.command_status(state.status) match {
wenzelm@44611
    77
        case Isar_Document.Forked(i) => if (i > 0) Some(running_color) else None
wenzelm@44866
    78
        case Isar_Document.Unprocessed => Some(unprocessed_color)
wenzelm@39178
    79
        case Isar_Document.Failed => Some(error_color)
wenzelm@39512
    80
        case Isar_Document.Finished =>
wenzelm@39512
    81
          if (state.results.exists(r => Isar_Document.is_error(r._2))) Some(error_color)
wenzelm@39512
    82
          else if (state.results.exists(r => Isar_Document.is_warning(r._2))) Some(warning_color)
wenzelm@39512
    83
          else None
wenzelm@39178
    84
      }
wenzelm@39178
    85
  }
wenzelm@39178
    86
wenzelm@39178
    87
wenzelm@39178
    88
  /* markup selectors */
wenzelm@39178
    89
wenzelm@45468
    90
  val message =
wenzelm@45468
    91
    Markup_Tree.Select[Color](
wenzelm@45474
    92
      {
wenzelm@45474
    93
        case Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _), _)) => regular_color
wenzelm@45474
    94
        case Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), _)) => warning_color
wenzelm@45474
    95
        case Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _)) => error_color
wenzelm@45474
    96
      },
wenzelm@45474
    97
      Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)))
wenzelm@39178
    98
wenzelm@45467
    99
  val tooltip_message =
wenzelm@45467
   100
    Markup_Tree.Cumulate[SortedMap[Long, String]](SortedMap.empty,
wenzelm@45467
   101
      {
wenzelm@45467
   102
        case (msgs, Text.Info(_, msg @ XML.Elem(Markup(markup, Markup.Serial(serial)), _)))
wenzelm@45467
   103
        if markup == Markup.WRITELN || markup == Markup.WARNING || markup == Markup.ERROR =>
wenzelm@45467
   104
          msgs + (serial ->
wenzelm@45467
   105
            Pretty.string_of(List(msg), margin = Isabelle.Int_Property("tooltip-margin")))
wenzelm@45474
   106
      },
wenzelm@45474
   107
      Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)))
wenzelm@39740
   108
wenzelm@45468
   109
  val gutter_message =
wenzelm@45468
   110
    Markup_Tree.Select[Icon](
wenzelm@45468
   111
      {
wenzelm@45468
   112
        case Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body)) =>
wenzelm@45468
   113
          body match {
wenzelm@45468
   114
            case List(XML.Elem(Markup(Markup.LEGACY, _), _)) => legacy_icon
wenzelm@45468
   115
            case _ => warning_icon
wenzelm@45468
   116
          }
wenzelm@45468
   117
        case Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _)) => error_icon
wenzelm@45474
   118
      },
wenzelm@45474
   119
      Some(Set(Markup.WARNING, Markup.ERROR)))
wenzelm@39181
   120
wenzelm@45468
   121
  val background1 =
wenzelm@45468
   122
    Markup_Tree.Select[Color](
wenzelm@45468
   123
      {
wenzelm@45468
   124
        case Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _)) => bad_color
wenzelm@45468
   125
        case Text.Info(_, XML.Elem(Markup(Markup.HILITE, _), _)) => hilite_color
wenzelm@45474
   126
      },
wenzelm@45474
   127
      Some(Set(Markup.BAD, Markup.HILITE)))
wenzelm@39178
   128
wenzelm@45468
   129
  val background2 =
wenzelm@45468
   130
    Markup_Tree.Select[Color](
wenzelm@45468
   131
      {
wenzelm@45468
   132
        case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => light_color
wenzelm@45474
   133
      },
wenzelm@45474
   134
      Some(Set(Markup.TOKEN_RANGE)))
wenzelm@39178
   135
wenzelm@45468
   136
  val foreground =
wenzelm@45468
   137
    Markup_Tree.Select[Color](
wenzelm@45468
   138
      {
wenzelm@45468
   139
        case Text.Info(_, XML.Elem(Markup(Markup.STRING, _), _)) => quoted_color
wenzelm@45468
   140
        case Text.Info(_, XML.Elem(Markup(Markup.ALTSTRING, _), _)) => quoted_color
wenzelm@45468
   141
        case Text.Info(_, XML.Elem(Markup(Markup.VERBATIM, _), _)) => quoted_color
wenzelm@45474
   142
      },
wenzelm@45474
   143
      Some(Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM)))
wenzelm@44545
   144
wenzelm@43434
   145
  private val text_colors: Map[String, Color] =
wenzelm@43386
   146
    Map(
wenzelm@44545
   147
      Markup.STRING -> get_color("black"),
wenzelm@44545
   148
      Markup.ALTSTRING -> get_color("black"),
wenzelm@44545
   149
      Markup.VERBATIM -> get_color("black"),
wenzelm@43436
   150
      Markup.LITERAL -> keyword1_color,
wenzelm@43436
   151
      Markup.DELIMITER -> get_color("black"),
wenzelm@43386
   152
      Markup.TFREE -> get_color("#A020F0"),
wenzelm@43386
   153
      Markup.TVAR -> get_color("#A020F0"),
wenzelm@43386
   154
      Markup.FREE -> get_color("blue"),
wenzelm@43386
   155
      Markup.SKOLEM -> get_color("#D2691E"),
wenzelm@43386
   156
      Markup.BOUND -> get_color("green"),
wenzelm@43386
   157
      Markup.VAR -> get_color("#00009B"),
wenzelm@43386
   158
      Markup.INNER_STRING -> get_color("#D2691E"),
wenzelm@43386
   159
      Markup.INNER_COMMENT -> get_color("#8B0000"),
wenzelm@44602
   160
      Markup.DYNAMIC_FACT -> get_color("#7BA428"),
wenzelm@43388
   161
      Markup.ML_KEYWORD -> keyword1_color,
wenzelm@43386
   162
      Markup.ML_DELIMITER -> get_color("black"),
wenzelm@43386
   163
      Markup.ML_NUMERAL -> get_color("red"),
wenzelm@43386
   164
      Markup.ML_CHAR -> get_color("#D2691E"),
wenzelm@43386
   165
      Markup.ML_STRING -> get_color("#D2691E"),
wenzelm@43386
   166
      Markup.ML_COMMENT -> get_color("#8B0000"),
wenzelm@43431
   167
      Markup.ML_MALFORMED -> get_color("#FF6A6A"),
wenzelm@43431
   168
      Markup.ANTIQ -> get_color("blue"))
wenzelm@43386
   169
wenzelm@45468
   170
  val text_color =
wenzelm@45468
   171
    Markup_Tree.Select[Color](
wenzelm@45468
   172
      {
wenzelm@45474
   173
        case Text.Info(_, XML.Elem(Markup(m, _), _))
wenzelm@45474
   174
        if text_colors.isDefinedAt(m) => text_colors(m)
wenzelm@45474
   175
      },
wenzelm@45474
   176
      Some(Set() ++ text_colors.keys))
wenzelm@43376
   177
wenzelm@43431
   178
  private val tooltips: Map[String, String] =
wenzelm@43431
   179
    Map(
wenzelm@43431
   180
      Markup.SORT -> "sort",
wenzelm@43431
   181
      Markup.TYP -> "type",
wenzelm@43431
   182
      Markup.TERM -> "term",
wenzelm@43431
   183
      Markup.PROP -> "proposition",
wenzelm@43431
   184
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@45460
   185
      Markup.FREE -> "free variable",
wenzelm@45460
   186
      Markup.SKOLEM -> "skolem variable",
wenzelm@45460
   187
      Markup.BOUND -> "bound variable",
wenzelm@45460
   188
      Markup.VAR -> "schematic variable",
wenzelm@45460
   189
      Markup.TFREE -> "free type variable",
wenzelm@45460
   190
      Markup.TVAR -> "schematic type variable",
wenzelm@43431
   191
      Markup.ML_SOURCE -> "ML source",
wenzelm@43431
   192
      Markup.DOC_SOURCE -> "document source")
wenzelm@43431
   193
wenzelm@45445
   194
  private def string_of_typing(kind: String, body: XML.Body): String =
wenzelm@45445
   195
    Pretty.string_of(List(Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)),
wenzelm@45445
   196
      margin = Isabelle.Int_Property("tooltip-margin"))
wenzelm@45445
   197
wenzelm@45468
   198
  val tooltip1 =
wenzelm@45468
   199
    Markup_Tree.Select[String](
wenzelm@45468
   200
      {
wenzelm@45468
   201
        case Text.Info(_, XML.Elem(Markup.Entity(kind, name), _)) => kind + " " + quote(name)
wenzelm@45474
   202
        case Text.Info(_, XML.Elem(Markup(Markup.ML_TYPING, _), body)) =>
wenzelm@45474
   203
          string_of_typing("ML:", body)
wenzelm@45468
   204
        case Text.Info(_, XML.Elem(Markup(name, _), _))
wenzelm@45468
   205
        if tooltips.isDefinedAt(name) => tooltips(name)
wenzelm@45474
   206
      },
wenzelm@45474
   207
      Some(Set(Markup.ENTITY, Markup.ML_TYPING) ++ tooltips.keys))
wenzelm@42302
   208
wenzelm@45468
   209
  val tooltip2 =
wenzelm@45468
   210
    Markup_Tree.Select[String](
wenzelm@45468
   211
      {
wenzelm@45468
   212
        case Text.Info(_, XML.Elem(Markup(Markup.TYPING, _), body)) => string_of_typing("::", body)
wenzelm@45474
   213
      },
wenzelm@45474
   214
      Some(Set(Markup.TYPING)))
wenzelm@45460
   215
wenzelm@42302
   216
  private val subexp_include =
wenzelm@42302
   217
    Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
wenzelm@42302
   218
      Markup.ENTITY, Markup.FREE, Markup.SKOLEM, Markup.BOUND, Markup.VAR,
wenzelm@43431
   219
      Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOC_SOURCE)
wenzelm@42302
   220
wenzelm@45468
   221
  val subexp =
wenzelm@45468
   222
    Markup_Tree.Select[(Text.Range, Color)](
wenzelm@45468
   223
      {
wenzelm@45468
   224
        case Text.Info(range, XML.Elem(Markup(name, _), _)) if subexp_include(name) =>
wenzelm@45468
   225
          (range, subexp_color)
wenzelm@45474
   226
      },
wenzelm@45474
   227
      Some(subexp_include))
wenzelm@39179
   228
wenzelm@39179
   229
wenzelm@39179
   230
  /* token markup -- text styles */
wenzelm@39179
   231
wenzelm@39179
   232
  private val command_style: Map[String, Byte] =
wenzelm@39179
   233
  {
wenzelm@43414
   234
    import JEditToken._
wenzelm@39179
   235
    Map[String, Byte](
wenzelm@39179
   236
      Keyword.THY_END -> KEYWORD2,
wenzelm@39179
   237
      Keyword.THY_SCRIPT -> LABEL,
wenzelm@39179
   238
      Keyword.PRF_SCRIPT -> LABEL,
wenzelm@39179
   239
      Keyword.PRF_ASM -> KEYWORD3,
wenzelm@39179
   240
      Keyword.PRF_ASM_GOAL -> KEYWORD3
wenzelm@39179
   241
    ).withDefaultValue(KEYWORD1)
wenzelm@39179
   242
  }
wenzelm@39179
   243
wenzelm@43414
   244
  private val token_style: Map[Token.Kind.Value, Byte] =
wenzelm@39179
   245
  {
wenzelm@43414
   246
    import JEditToken._
wenzelm@43414
   247
    Map[Token.Kind.Value, Byte](
wenzelm@43414
   248
      Token.Kind.KEYWORD -> KEYWORD2,
wenzelm@43414
   249
      Token.Kind.IDENT -> NULL,
wenzelm@43414
   250
      Token.Kind.LONG_IDENT -> NULL,
wenzelm@43414
   251
      Token.Kind.SYM_IDENT -> NULL,
wenzelm@43414
   252
      Token.Kind.VAR -> NULL,
wenzelm@43414
   253
      Token.Kind.TYPE_IDENT -> NULL,
wenzelm@43414
   254
      Token.Kind.TYPE_VAR -> NULL,
wenzelm@43414
   255
      Token.Kind.NAT -> NULL,
wenzelm@43414
   256
      Token.Kind.FLOAT -> NULL,
wenzelm@43431
   257
      Token.Kind.STRING -> LITERAL1,
wenzelm@43431
   258
      Token.Kind.ALT_STRING -> LITERAL2,
wenzelm@43414
   259
      Token.Kind.VERBATIM -> COMMENT3,
wenzelm@43414
   260
      Token.Kind.SPACE -> NULL,
wenzelm@43414
   261
      Token.Kind.COMMENT -> COMMENT1,
wenzelm@43414
   262
      Token.Kind.UNPARSED -> INVALID
wenzelm@39179
   263
    ).withDefaultValue(NULL)
wenzelm@39179
   264
  }
wenzelm@39179
   265
wenzelm@43414
   266
  def token_markup(syntax: Outer_Syntax, token: Token): Byte =
wenzelm@43430
   267
    if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
wenzelm@43430
   268
    else if (token.is_operator) JEditToken.OPERATOR
wenzelm@43414
   269
    else token_style(token.kind)
wenzelm@39178
   270
}