src/Tools/jEdit/src/isabelle_markup.scala
author wenzelm
Tue Aug 23 12:20:12 2011 +0200 (2011-08-23)
changeset 44385 e7fdb008aa7d
parent 44181 bbce0417236d
child 44545 3c40007aa031
permissions -rw-r--r--
propagate editor perspective through document model;
     1 /*  Title:      Tools/jEdit/src/isabelle_markup.scala
     2     Author:     Makarius
     3 
     4 Isabelle specific physical rendering and markup selection.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import java.awt.Color
    13 
    14 import org.lobobrowser.util.gui.ColorFactory
    15 import org.gjt.sp.jedit.syntax.{Token => JEditToken}
    16 
    17 
    18 object Isabelle_Markup
    19 {
    20   /* physical rendering */
    21 
    22   // see http://www.w3schools.com/css/css_colornames.asp
    23 
    24   def get_color(s: String): Color = ColorFactory.getInstance.getColor(s)
    25 
    26   val outdated_color = new Color(240, 240, 240)
    27   val unfinished_color = new Color(255, 228, 225)
    28 
    29   val light_color = new Color(240, 240, 240)
    30   val regular_color = new Color(192, 192, 192)
    31   val warning_color = new Color(255, 140, 0)
    32   val error_color = new Color(178, 34, 34)
    33   val bad_color = new Color(255, 106, 106, 100)
    34   val hilite_color = new Color(255, 204, 102, 100)
    35 
    36   val subexp_color = new Color(0xC0, 0xC0, 0xC0, 100)
    37 
    38   val keyword1_color = get_color("#006699")
    39   val keyword2_color = get_color("#009966")
    40 
    41   class Icon(val priority: Int, val icon: javax.swing.Icon)
    42   {
    43     def >= (that: Icon): Boolean = this.priority >= that.priority
    44   }
    45   val warning_icon = new Icon(1, Isabelle.load_icon("16x16/status/dialog-warning.png"))
    46   val error_icon = new Icon(2, Isabelle.load_icon("16x16/status/dialog-error.png"))
    47 
    48 
    49   /* command status */
    50 
    51   def status_color(snapshot: Document.Snapshot, command: Command): Option[Color] =
    52   {
    53     val state = snapshot.state(command)
    54     if (snapshot.is_outdated) Some(outdated_color)
    55     else
    56       Isar_Document.command_status(state.status) match {
    57         case Isar_Document.Forked(i) if i > 0 => Some(unfinished_color)
    58         case Isar_Document.Unprocessed => Some(unfinished_color)
    59         case _ => None
    60       }
    61   }
    62 
    63   def overview_color(snapshot: Document.Snapshot, command: Command): Option[Color] =
    64   {
    65     val state = snapshot.state(command)
    66     if (snapshot.is_outdated) None
    67     else
    68       Isar_Document.command_status(state.status) match {
    69         case Isar_Document.Forked(i) => if (i > 0) Some(unfinished_color) else None
    70         case Isar_Document.Unprocessed => Some(unfinished_color)
    71         case Isar_Document.Failed => Some(error_color)
    72         case Isar_Document.Finished =>
    73           if (state.results.exists(r => Isar_Document.is_error(r._2))) Some(error_color)
    74           else if (state.results.exists(r => Isar_Document.is_warning(r._2))) Some(warning_color)
    75           else None
    76       }
    77   }
    78 
    79 
    80   /* markup selectors */
    81 
    82   val message: Markup_Tree.Select[Color] =
    83   {
    84     case Text.Info(_, XML.Elem(Markup(Markup.WRITELN, _), _)) => regular_color
    85     case Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), _)) => warning_color
    86     case Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _)) => error_color
    87   }
    88 
    89   val popup: Markup_Tree.Select[String] =
    90   {
    91     case Text.Info(_, msg @ XML.Elem(Markup(markup, _), _))
    92     if markup == Markup.WRITELN || markup == Markup.WARNING || markup == Markup.ERROR =>
    93       Pretty.string_of(List(msg), margin = Isabelle.Int_Property("tooltip-margin"))
    94   }
    95 
    96   val gutter_message: Markup_Tree.Select[Icon] =
    97   {
    98     case Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), _)) => warning_icon
    99     case Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _)) => error_icon
   100   }
   101 
   102   val background1: Markup_Tree.Select[Color] =
   103   {
   104     case Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _)) => bad_color
   105     case Text.Info(_, XML.Elem(Markup(Markup.HILITE, _), _)) => hilite_color
   106   }
   107 
   108   val background2: Markup_Tree.Select[Color] =
   109   {
   110     case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => light_color
   111   }
   112 
   113   private val text_entity_colors: Map[String, Color] =
   114     Map(
   115       Markup.CLASS -> get_color("red"),
   116       Markup.TYPE -> get_color("black"),
   117       Markup.CONSTANT -> get_color("black"))
   118 
   119   private val text_colors: Map[String, Color] =
   120     Map(
   121       Markup.LITERAL -> keyword1_color,
   122       Markup.DELIMITER -> get_color("black"),
   123       Markup.IDENT -> get_color("black"),
   124       Markup.TFREE -> get_color("#A020F0"),
   125       Markup.TVAR -> get_color("#A020F0"),
   126       Markup.FREE -> get_color("blue"),
   127       Markup.SKOLEM -> get_color("#D2691E"),
   128       Markup.BOUND -> get_color("green"),
   129       Markup.VAR -> get_color("#00009B"),
   130       Markup.INNER_STRING -> get_color("#D2691E"),
   131       Markup.INNER_COMMENT -> get_color("#8B0000"),
   132       Markup.DYNAMIC_FACT -> get_color("yellowgreen"),
   133       Markup.ML_KEYWORD -> keyword1_color,
   134       Markup.ML_DELIMITER -> get_color("black"),
   135       Markup.ML_NUMERAL -> get_color("red"),
   136       Markup.ML_CHAR -> get_color("#D2691E"),
   137       Markup.ML_STRING -> get_color("#D2691E"),
   138       Markup.ML_COMMENT -> get_color("#8B0000"),
   139       Markup.ML_MALFORMED -> get_color("#FF6A6A"),
   140       Markup.ANTIQ -> get_color("blue"))
   141 
   142   val text_color: Markup_Tree.Select[Color] =
   143   {
   144     case Text.Info(_, XML.Elem(Markup.Entity(kind, _), _))
   145     if text_entity_colors.isDefinedAt(kind) => text_entity_colors(kind)
   146     case Text.Info(_, XML.Elem(Markup(m, _), _))
   147     if text_colors.isDefinedAt(m) => text_colors(m)
   148   }
   149 
   150   private val tooltips: Map[String, String] =
   151     Map(
   152       Markup.SORT -> "sort",
   153       Markup.TYP -> "type",
   154       Markup.TERM -> "term",
   155       Markup.PROP -> "proposition",
   156       Markup.TOKEN_RANGE -> "inner syntax token",
   157       Markup.FREE -> "free variable (globally fixed)",
   158       Markup.SKOLEM -> "skolem variable (locally fixed)",
   159       Markup.BOUND -> "bound variable (internally fixed)",
   160       Markup.VAR -> "schematic variable",
   161       Markup.TFREE -> "free type variable",
   162       Markup.TVAR -> "schematic type variable",
   163       Markup.ML_SOURCE -> "ML source",
   164       Markup.DOC_SOURCE -> "document source")
   165 
   166   val tooltip: Markup_Tree.Select[String] =
   167   {
   168     case Text.Info(_, XML.Elem(Markup.Entity(kind, name), _)) => kind + " " + quote(name)
   169     case Text.Info(_, XML.Elem(Markup(Markup.ML_TYPING, _), body)) =>
   170       Pretty.string_of(List(Pretty.block(XML.Text("ML:") :: Pretty.Break(1) :: body)),
   171         margin = Isabelle.Int_Property("tooltip-margin"))
   172     case Text.Info(_, XML.Elem(Markup(name, _), _))
   173     if tooltips.isDefinedAt(name) => tooltips(name)
   174   }
   175 
   176   private val subexp_include =
   177     Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   178       Markup.ENTITY, Markup.FREE, Markup.SKOLEM, Markup.BOUND, Markup.VAR,
   179       Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOC_SOURCE)
   180 
   181   val subexp: Markup_Tree.Select[(Text.Range, Color)] =
   182   {
   183     case Text.Info(range, XML.Elem(Markup(name, _), _)) if subexp_include(name) =>
   184       (range, subexp_color)
   185   }
   186 
   187 
   188   /* token markup -- text styles */
   189 
   190   private val command_style: Map[String, Byte] =
   191   {
   192     import JEditToken._
   193     Map[String, Byte](
   194       Keyword.THY_END -> KEYWORD2,
   195       Keyword.THY_SCRIPT -> LABEL,
   196       Keyword.PRF_SCRIPT -> LABEL,
   197       Keyword.PRF_ASM -> KEYWORD3,
   198       Keyword.PRF_ASM_GOAL -> KEYWORD3
   199     ).withDefaultValue(KEYWORD1)
   200   }
   201 
   202   private val token_style: Map[Token.Kind.Value, Byte] =
   203   {
   204     import JEditToken._
   205     Map[Token.Kind.Value, Byte](
   206       Token.Kind.KEYWORD -> KEYWORD2,
   207       Token.Kind.IDENT -> NULL,
   208       Token.Kind.LONG_IDENT -> NULL,
   209       Token.Kind.SYM_IDENT -> NULL,
   210       Token.Kind.VAR -> NULL,
   211       Token.Kind.TYPE_IDENT -> NULL,
   212       Token.Kind.TYPE_VAR -> NULL,
   213       Token.Kind.NAT -> NULL,
   214       Token.Kind.FLOAT -> NULL,
   215       Token.Kind.STRING -> LITERAL1,
   216       Token.Kind.ALT_STRING -> LITERAL2,
   217       Token.Kind.VERBATIM -> COMMENT3,
   218       Token.Kind.SPACE -> NULL,
   219       Token.Kind.COMMENT -> COMMENT1,
   220       Token.Kind.UNPARSED -> INVALID
   221     ).withDefaultValue(NULL)
   222   }
   223 
   224   def token_markup(syntax: Outer_Syntax, token: Token): Byte =
   225     if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
   226     else if (token.is_operator) JEditToken.OPERATOR
   227     else token_style(token.kind)
   228 }