src/Tools/jEdit/src/isabelle_markup.scala
author wenzelm
Fri Nov 11 22:05:18 2011 +0100 (2011-11-11 ago)
changeset 45460 dcd02d1a25d7
parent 45454 388fb71623dd
child 45467 3f290b6288cf
permissions -rw-r--r--
more tooltip content;
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@39178
    90
  val message: Markup_Tree.Select[Color] =
wenzelm@39178
    91
  {
wenzelm@39178
    92
    case Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _), _)) => regular_color
wenzelm@39178
    93
    case Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), _)) => warning_color
wenzelm@39178
    94
    case Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _)) => error_color
wenzelm@39178
    95
  }
wenzelm@39178
    96
wenzelm@45460
    97
  val tooltip_message: Markup_Tree.Cumulate[SortedMap[Long, String]] =
wenzelm@39740
    98
  {
wenzelm@45460
    99
    case (msgs, Text.Info(_, msg @ XML.Elem(Markup(markup, Markup.Serial(serial)), _)))
wenzelm@40338
   100
    if markup == Markup.WRITELN || markup == Markup.WARNING || markup == Markup.ERROR =>
wenzelm@45460
   101
      msgs + (serial ->
wenzelm@45460
   102
        Pretty.string_of(List(msg), margin = Isabelle.Int_Property("tooltip-margin")))
wenzelm@39740
   103
  }
wenzelm@39740
   104
wenzelm@39181
   105
  val gutter_message: Markup_Tree.Select[Icon] =
wenzelm@39181
   106
  {
wenzelm@44549
   107
    case Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body)) =>
wenzelm@44549
   108
      body match {
wenzelm@44549
   109
        case List(XML.Elem(Markup(Markup.LEGACY, _), _)) => legacy_icon
wenzelm@44549
   110
        case _ => warning_icon
wenzelm@44549
   111
      }
wenzelm@39181
   112
    case Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _)) => error_icon
wenzelm@39181
   113
  }
wenzelm@39181
   114
wenzelm@39700
   115
  val background1: Markup_Tree.Select[Color] =
wenzelm@39178
   116
  {
wenzelm@39178
   117
    case Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _)) => bad_color
wenzelm@42171
   118
    case Text.Info(_, XML.Elem(Markup(Markup.HILITE, _), _)) => hilite_color
wenzelm@39178
   119
  }
wenzelm@39178
   120
wenzelm@39700
   121
  val background2: Markup_Tree.Select[Color] =
wenzelm@39178
   122
  {
wenzelm@39690
   123
    case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => light_color
wenzelm@39178
   124
  }
wenzelm@39178
   125
wenzelm@44545
   126
  val foreground: Markup_Tree.Select[Color] =
wenzelm@44545
   127
  {
wenzelm@44546
   128
    case Text.Info(_, XML.Elem(Markup(Markup.STRING, _), _)) => quoted_color
wenzelm@44546
   129
    case Text.Info(_, XML.Elem(Markup(Markup.ALTSTRING, _), _)) => quoted_color
wenzelm@44546
   130
    case Text.Info(_, XML.Elem(Markup(Markup.VERBATIM, _), _)) => quoted_color
wenzelm@44545
   131
  }
wenzelm@44545
   132
wenzelm@43434
   133
  private val text_colors: Map[String, Color] =
wenzelm@43386
   134
    Map(
wenzelm@44545
   135
      Markup.STRING -> get_color("black"),
wenzelm@44545
   136
      Markup.ALTSTRING -> get_color("black"),
wenzelm@44545
   137
      Markup.VERBATIM -> get_color("black"),
wenzelm@43436
   138
      Markup.LITERAL -> keyword1_color,
wenzelm@43436
   139
      Markup.DELIMITER -> get_color("black"),
wenzelm@43386
   140
      Markup.TFREE -> get_color("#A020F0"),
wenzelm@43386
   141
      Markup.TVAR -> get_color("#A020F0"),
wenzelm@43386
   142
      Markup.FREE -> get_color("blue"),
wenzelm@43386
   143
      Markup.SKOLEM -> get_color("#D2691E"),
wenzelm@43386
   144
      Markup.BOUND -> get_color("green"),
wenzelm@43386
   145
      Markup.VAR -> get_color("#00009B"),
wenzelm@43386
   146
      Markup.INNER_STRING -> get_color("#D2691E"),
wenzelm@43386
   147
      Markup.INNER_COMMENT -> get_color("#8B0000"),
wenzelm@44602
   148
      Markup.DYNAMIC_FACT -> get_color("#7BA428"),
wenzelm@43388
   149
      Markup.ML_KEYWORD -> keyword1_color,
wenzelm@43386
   150
      Markup.ML_DELIMITER -> get_color("black"),
wenzelm@43386
   151
      Markup.ML_NUMERAL -> get_color("red"),
wenzelm@43386
   152
      Markup.ML_CHAR -> get_color("#D2691E"),
wenzelm@43386
   153
      Markup.ML_STRING -> get_color("#D2691E"),
wenzelm@43386
   154
      Markup.ML_COMMENT -> get_color("#8B0000"),
wenzelm@43431
   155
      Markup.ML_MALFORMED -> get_color("#FF6A6A"),
wenzelm@43431
   156
      Markup.ANTIQ -> get_color("blue"))
wenzelm@43386
   157
wenzelm@43434
   158
  val text_color: Markup_Tree.Select[Color] =
wenzelm@43376
   159
  {
wenzelm@45454
   160
    case Text.Info(_, XML.Elem(Markup(m, _), _)) if text_colors.isDefinedAt(m) => text_colors(m)
wenzelm@43376
   161
  }
wenzelm@43376
   162
wenzelm@43431
   163
  private val tooltips: Map[String, String] =
wenzelm@43431
   164
    Map(
wenzelm@43431
   165
      Markup.SORT -> "sort",
wenzelm@43431
   166
      Markup.TYP -> "type",
wenzelm@43431
   167
      Markup.TERM -> "term",
wenzelm@43431
   168
      Markup.PROP -> "proposition",
wenzelm@43431
   169
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@45460
   170
      Markup.FREE -> "free variable",
wenzelm@45460
   171
      Markup.SKOLEM -> "skolem variable",
wenzelm@45460
   172
      Markup.BOUND -> "bound variable",
wenzelm@45460
   173
      Markup.VAR -> "schematic variable",
wenzelm@45460
   174
      Markup.TFREE -> "free type variable",
wenzelm@45460
   175
      Markup.TVAR -> "schematic type variable",
wenzelm@43431
   176
      Markup.ML_SOURCE -> "ML source",
wenzelm@43431
   177
      Markup.DOC_SOURCE -> "document source")
wenzelm@43431
   178
wenzelm@45445
   179
  private def string_of_typing(kind: String, body: XML.Body): String =
wenzelm@45445
   180
    Pretty.string_of(List(Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)),
wenzelm@45445
   181
      margin = Isabelle.Int_Property("tooltip-margin"))
wenzelm@45445
   182
wenzelm@45460
   183
  val tooltip1: Markup_Tree.Select[String] =
wenzelm@39178
   184
  {
wenzelm@44181
   185
    case Text.Info(_, XML.Elem(Markup.Entity(kind, name), _)) => kind + " " + quote(name)
wenzelm@45445
   186
    case Text.Info(_, XML.Elem(Markup(Markup.ML_TYPING, _), body)) => string_of_typing("ML:", body)
wenzelm@43431
   187
    case Text.Info(_, XML.Elem(Markup(name, _), _))
wenzelm@43431
   188
    if tooltips.isDefinedAt(name) => tooltips(name)
wenzelm@42302
   189
  }
wenzelm@42302
   190
wenzelm@45460
   191
  val tooltip2: Markup_Tree.Select[String] =
wenzelm@45460
   192
  {
wenzelm@45460
   193
    case Text.Info(_, XML.Elem(Markup(Markup.TYPING, _), body)) => string_of_typing("::", body)
wenzelm@45460
   194
  }
wenzelm@45460
   195
wenzelm@42302
   196
  private val subexp_include =
wenzelm@42302
   197
    Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
wenzelm@42302
   198
      Markup.ENTITY, Markup.FREE, Markup.SKOLEM, Markup.BOUND, Markup.VAR,
wenzelm@43431
   199
      Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOC_SOURCE)
wenzelm@42302
   200
wenzelm@42302
   201
  val subexp: Markup_Tree.Select[(Text.Range, Color)] =
wenzelm@42302
   202
  {
wenzelm@42302
   203
    case Text.Info(range, XML.Elem(Markup(name, _), _)) if subexp_include(name) =>
wenzelm@43435
   204
      (range, subexp_color)
wenzelm@39178
   205
  }
wenzelm@39179
   206
wenzelm@39179
   207
wenzelm@39179
   208
  /* token markup -- text styles */
wenzelm@39179
   209
wenzelm@39179
   210
  private val command_style: Map[String, Byte] =
wenzelm@39179
   211
  {
wenzelm@43414
   212
    import JEditToken._
wenzelm@39179
   213
    Map[String, Byte](
wenzelm@39179
   214
      Keyword.THY_END -> KEYWORD2,
wenzelm@39179
   215
      Keyword.THY_SCRIPT -> LABEL,
wenzelm@39179
   216
      Keyword.PRF_SCRIPT -> LABEL,
wenzelm@39179
   217
      Keyword.PRF_ASM -> KEYWORD3,
wenzelm@39179
   218
      Keyword.PRF_ASM_GOAL -> KEYWORD3
wenzelm@39179
   219
    ).withDefaultValue(KEYWORD1)
wenzelm@39179
   220
  }
wenzelm@39179
   221
wenzelm@43414
   222
  private val token_style: Map[Token.Kind.Value, Byte] =
wenzelm@39179
   223
  {
wenzelm@43414
   224
    import JEditToken._
wenzelm@43414
   225
    Map[Token.Kind.Value, Byte](
wenzelm@43414
   226
      Token.Kind.KEYWORD -> KEYWORD2,
wenzelm@43414
   227
      Token.Kind.IDENT -> NULL,
wenzelm@43414
   228
      Token.Kind.LONG_IDENT -> NULL,
wenzelm@43414
   229
      Token.Kind.SYM_IDENT -> NULL,
wenzelm@43414
   230
      Token.Kind.VAR -> NULL,
wenzelm@43414
   231
      Token.Kind.TYPE_IDENT -> NULL,
wenzelm@43414
   232
      Token.Kind.TYPE_VAR -> NULL,
wenzelm@43414
   233
      Token.Kind.NAT -> NULL,
wenzelm@43414
   234
      Token.Kind.FLOAT -> NULL,
wenzelm@43431
   235
      Token.Kind.STRING -> LITERAL1,
wenzelm@43431
   236
      Token.Kind.ALT_STRING -> LITERAL2,
wenzelm@43414
   237
      Token.Kind.VERBATIM -> COMMENT3,
wenzelm@43414
   238
      Token.Kind.SPACE -> NULL,
wenzelm@43414
   239
      Token.Kind.COMMENT -> COMMENT1,
wenzelm@43414
   240
      Token.Kind.UNPARSED -> INVALID
wenzelm@39179
   241
    ).withDefaultValue(NULL)
wenzelm@39179
   242
  }
wenzelm@39179
   243
wenzelm@43414
   244
  def token_markup(syntax: Outer_Syntax, token: Token): Byte =
wenzelm@43430
   245
    if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
wenzelm@43430
   246
    else if (token.is_operator) JEditToken.OPERATOR
wenzelm@43414
   247
    else token_style(token.kind)
wenzelm@39178
   248
}