src/Tools/jEdit/src/rendering.scala
author wenzelm
Mon Nov 26 16:16:47 2012 +0100 (2012-11-26 ago)
changeset 50215 97959912840a
parent 50206 6626bc5ed053
child 50450 358b6020f8b6
permissions -rw-r--r--
more general sendback properties;
support for padding of line boundary, e.g. for ad-hoc insertion of commands via 'help';
     1 /*  Title:      Tools/jEdit/src/rendering.scala
     2     Author:     Makarius
     3 
     4 Isabelle-specific implementation of quasi-abstract rendering and
     5 markup interpretation.
     6 */
     7 
     8 package isabelle.jedit
     9 
    10 
    11 import isabelle._
    12 
    13 import java.awt.Color
    14 import javax.swing.Icon
    15 
    16 import org.gjt.sp.jedit.syntax.{Token => JEditToken}
    17 import org.gjt.sp.jedit.{jEdit, GUIUtilities}
    18 
    19 import scala.collection.immutable.SortedMap
    20 
    21 
    22 object Rendering
    23 {
    24   def apply(snapshot: Document.Snapshot, options: Options): Rendering =
    25     new Rendering(snapshot, options)
    26 
    27 
    28   /* message priorities */
    29 
    30   private val writeln_pri = 1
    31   private val tracing_pri = 2
    32   private val warning_pri = 3
    33   private val legacy_pri = 4
    34   private val error_pri = 5
    35 
    36   private val message_pri = Map(
    37     Markup.WRITELN -> writeln_pri, Markup.WRITELN_MESSAGE -> writeln_pri,
    38     Markup.TRACING -> tracing_pri, Markup.TRACING_MESSAGE -> tracing_pri,
    39     Markup.WARNING -> warning_pri, Markup.WARNING_MESSAGE -> warning_pri,
    40     Markup.ERROR -> error_pri, Markup.ERROR_MESSAGE -> error_pri)
    41 
    42 
    43   /* icons */
    44 
    45   private def load_icon(name: String): Icon =
    46   {
    47     val icon = GUIUtilities.loadIcon(name)
    48     if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name)
    49     else icon
    50   }
    51 
    52   private val gutter_icons = Map(
    53     warning_pri -> load_icon("16x16/status/dialog-information.png"),
    54     legacy_pri -> load_icon("16x16/status/dialog-warning.png"),
    55     error_pri -> load_icon("16x16/status/dialog-error.png"))
    56 
    57   val tooltip_close_icon = load_icon("16x16/actions/document-close.png")
    58   val tooltip_detach_icon = load_icon("16x16/actions/window-new.png")
    59 
    60 
    61   /* jEdit font */
    62 
    63   def font_family(): String = jEdit.getProperty("view.font")
    64 
    65   def font_size(scale: String): Float =
    66     (jEdit.getIntegerProperty("view.fontsize", 16) * PIDE.options.real(scale)).toFloat
    67 
    68 
    69   /* token markup -- text styles */
    70 
    71   private val command_style: Map[String, Byte] =
    72   {
    73     import JEditToken._
    74     Map[String, Byte](
    75       Keyword.THY_END -> KEYWORD2,
    76       Keyword.THY_SCRIPT -> LABEL,
    77       Keyword.PRF_SCRIPT -> LABEL,
    78       Keyword.PRF_ASM -> KEYWORD3,
    79       Keyword.PRF_ASM_GOAL -> KEYWORD3
    80     ).withDefaultValue(KEYWORD1)
    81   }
    82 
    83   private val token_style: Map[Token.Kind.Value, Byte] =
    84   {
    85     import JEditToken._
    86     Map[Token.Kind.Value, Byte](
    87       Token.Kind.KEYWORD -> KEYWORD2,
    88       Token.Kind.IDENT -> NULL,
    89       Token.Kind.LONG_IDENT -> NULL,
    90       Token.Kind.SYM_IDENT -> NULL,
    91       Token.Kind.VAR -> NULL,
    92       Token.Kind.TYPE_IDENT -> NULL,
    93       Token.Kind.TYPE_VAR -> NULL,
    94       Token.Kind.NAT -> NULL,
    95       Token.Kind.FLOAT -> NULL,
    96       Token.Kind.STRING -> LITERAL1,
    97       Token.Kind.ALT_STRING -> LITERAL2,
    98       Token.Kind.VERBATIM -> COMMENT3,
    99       Token.Kind.SPACE -> NULL,
   100       Token.Kind.COMMENT -> COMMENT1,
   101       Token.Kind.ERROR -> INVALID
   102     ).withDefaultValue(NULL)
   103   }
   104 
   105   def token_markup(syntax: Outer_Syntax, token: Token): Byte =
   106     if (token.is_command) command_style(syntax.keyword_kind(token.content).getOrElse(""))
   107     else if (token.is_operator) JEditToken.OPERATOR
   108     else token_style(token.kind)
   109 }
   110 
   111 
   112 class Rendering private(val snapshot: Document.Snapshot, val options: Options)
   113 {
   114   /* colors */
   115 
   116   def color_value(s: String): Color = Color_Value(options.string(s))
   117 
   118   val outdated_color = color_value("outdated_color")
   119   val unprocessed_color = color_value("unprocessed_color")
   120   val unprocessed1_color = color_value("unprocessed1_color")
   121   val running_color = color_value("running_color")
   122   val running1_color = color_value("running1_color")
   123   val light_color = color_value("light_color")
   124   val tooltip_color = color_value("tooltip_color")
   125   val writeln_color = color_value("writeln_color")
   126   val warning_color = color_value("warning_color")
   127   val error_color = color_value("error_color")
   128   val error1_color = color_value("error1_color")
   129   val writeln_message_color = color_value("writeln_message_color")
   130   val tracing_message_color = color_value("tracing_message_color")
   131   val warning_message_color = color_value("warning_message_color")
   132   val error_message_color = color_value("error_message_color")
   133   val bad_color = color_value("bad_color")
   134   val intensify_color = color_value("intensify_color")
   135   val quoted_color = color_value("quoted_color")
   136   val highlight_color = color_value("highlight_color")
   137   val hyperlink_color = color_value("hyperlink_color")
   138   val sendback_color = color_value("sendback_color")
   139   val sendback_active_color = color_value("sendback_active_color")
   140   val keyword1_color = color_value("keyword1_color")
   141   val keyword2_color = color_value("keyword2_color")
   142 
   143   val tfree_color = color_value("tfree_color")
   144   val tvar_color = color_value("tvar_color")
   145   val free_color = color_value("free_color")
   146   val skolem_color = color_value("skolem_color")
   147   val bound_color = color_value("bound_color")
   148   val var_color = color_value("var_color")
   149   val inner_numeral_color = color_value("inner_numeral_color")
   150   val inner_quoted_color = color_value("inner_quoted_color")
   151   val inner_comment_color = color_value("inner_comment_color")
   152   val antiquotation_color = color_value("antiquotation_color")
   153   val dynamic_color = color_value("dynamic_color")
   154 
   155 
   156   /* command overview */
   157 
   158   val overview_limit = options.int("jedit_text_overview_limit")
   159 
   160   def overview_color(range: Text.Range): Option[Color] =
   161   {
   162     if (snapshot.is_outdated) None
   163     else {
   164       val results =
   165         snapshot.cumulate_markup[(Protocol.Status, Int)](
   166           range, (Protocol.Status.init, 0),
   167           Some(Protocol.command_status_markup + Markup.WARNING + Markup.ERROR),
   168           {
   169             case ((status, pri), Text.Info(_, XML.Elem(markup, _))) =>
   170               if (markup.name == Markup.WARNING || markup.name == Markup.ERROR)
   171                 (status, pri max Rendering.message_pri(markup.name))
   172               else (Protocol.command_status(status, markup), pri)
   173           })
   174       if (results.isEmpty) None
   175       else {
   176         val (status, pri) =
   177           ((Protocol.Status.init, 0) /: results) {
   178             case ((s1, p1), Text.Info(_, (s2, p2))) => (s1 + s2, p1 max p2) }
   179 
   180         if (pri == Rendering.warning_pri) Some(warning_color)
   181         else if (pri == Rendering.error_pri) Some(error_color)
   182         else if (status.is_unprocessed) Some(unprocessed_color)
   183         else if (status.is_running) Some(running_color)
   184         else if (status.is_failed) Some(error_color)
   185         else None
   186       }
   187     }
   188   }
   189 
   190 
   191   /* markup selectors */
   192 
   193   private val highlight_include =
   194     Set(Markup.SORT, Markup.TYP, Markup.TERM, Markup.PROP, Markup.ML_TYPING, Markup.TOKEN_RANGE,
   195       Markup.ENTITY, Markup.PATH, Markup.SORTING, Markup.TYPING, Markup.FREE, Markup.SKOLEM,
   196       Markup.BOUND, Markup.VAR, Markup.TFREE, Markup.TVAR, Markup.ML_SOURCE, Markup.DOCUMENT_SOURCE)
   197 
   198   def highlight(range: Text.Range): Option[Text.Info[Color]] =
   199   {
   200     snapshot.select_markup(range, Some(highlight_include),
   201         {
   202           case Text.Info(info_range, XML.Elem(Markup(name, _), _)) if highlight_include(name) =>
   203             Text.Info(snapshot.convert(info_range), highlight_color)
   204         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   205   }
   206 
   207 
   208   private val hyperlink_include = Set(Markup.ENTITY, Markup.PATH)
   209 
   210   def hyperlink(range: Text.Range): Option[Text.Info[Hyperlink]] =
   211   {
   212     snapshot.cumulate_markup[List[Text.Info[Hyperlink]]](range, Nil, Some(hyperlink_include),
   213         {
   214           case (links, Text.Info(info_range, XML.Elem(Markup.Path(name), _)))
   215           if Path.is_ok(name) =>
   216             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   217             Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, 0, 0)) :: links
   218 
   219           case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _)))
   220           if !props.exists(
   221             { case (Markup.KIND, Markup.ML_OPEN) => true
   222               case (Markup.KIND, Markup.ML_STRUCT) => true
   223               case _ => false }) =>
   224 
   225             props match {
   226               case Position.Def_Line_File(line, name) if Path.is_ok(name) =>
   227                 Isabelle_System.source_file(Path.explode(name)) match {
   228                   case Some(path) =>
   229                     val jedit_file = Isabelle_System.platform_path(path)
   230                     Text.Info(snapshot.convert(info_range), Hyperlink(jedit_file, line, 0)) :: links
   231                   case None => links
   232                 }
   233 
   234               case Position.Def_Id_Offset(id, offset) =>
   235                 snapshot.state.find_command(snapshot.version, id) match {
   236                   case Some((node, command)) =>
   237                     val sources =
   238                       node.commands.iterator.takeWhile(_ != command).map(_.source) ++
   239                         Iterator.single(command.source(Text.Range(0, command.decode(offset))))
   240                     val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
   241                     Text.Info(snapshot.convert(info_range),
   242                       Hyperlink(command.node_name.node, line, column)) :: links
   243                   case None => links
   244                 }
   245 
   246               case _ => links
   247             }
   248         }) match { case Text.Info(_, info :: _) #:: _ => Some(info) case _ => None }
   249   }
   250 
   251 
   252   def sendback(range: Text.Range): Option[Text.Info[Properties.T]] =
   253     snapshot.select_markup(range, Some(Set(Markup.SENDBACK)),
   254         {
   255           case Text.Info(info_range, XML.Elem(Markup(Markup.SENDBACK, props), _)) =>
   256             Text.Info(snapshot.convert(info_range), props)
   257         }) match { case Text.Info(_, info) #:: _ => Some(info) case _ => None }
   258 
   259 
   260   def tooltip_message(range: Text.Range): XML.Body =
   261   {
   262     val msgs =
   263       snapshot.cumulate_markup[SortedMap[Long, XML.Tree]](range, SortedMap.empty,
   264         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR, Markup.BAD)),
   265         {
   266           case (msgs, Text.Info(_, XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
   267           if name == Markup.WRITELN ||
   268               name == Markup.WARNING ||
   269               name == Markup.ERROR =>
   270             msgs + (serial -> XML.Elem(Markup(Markup.message(name), props), body))
   271           case (msgs, Text.Info(_, msg @ XML.Elem(Markup(Markup.BAD, _), body)))
   272           if !body.isEmpty => msgs + (Document.new_id() -> msg)
   273         }).toList.flatMap(_.info)
   274     Pretty.separate(msgs.iterator.map(_._2).toList)
   275   }
   276 
   277 
   278   private val tooltips: Map[String, String] =
   279     Map(
   280       Markup.SORT -> "sort",
   281       Markup.TYP -> "type",
   282       Markup.TERM -> "term",
   283       Markup.PROP -> "proposition",
   284       Markup.TOKEN_RANGE -> "inner syntax token",
   285       Markup.FREE -> "free variable",
   286       Markup.SKOLEM -> "skolem variable",
   287       Markup.BOUND -> "bound variable",
   288       Markup.VAR -> "schematic variable",
   289       Markup.TFREE -> "free type variable",
   290       Markup.TVAR -> "schematic type variable",
   291       Markup.ML_SOURCE -> "ML source",
   292       Markup.DOCUMENT_SOURCE -> "document source")
   293 
   294   private val tooltip_elements =
   295     Set(Markup.ENTITY, Markup.SORTING, Markup.TYPING, Markup.ML_TYPING, Markup.PATH) ++
   296       tooltips.keys
   297 
   298   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   299     Pretty.block(XML.Text(kind) :: Pretty.Break(1) :: body)
   300 
   301   def tooltip(range: Text.Range): XML.Body =
   302   {
   303     def add(prev: Text.Info[List[(Boolean, XML.Tree)]], r: Text.Range, p: (Boolean, XML.Tree)) =
   304       if (prev.range == r) Text.Info(r, p :: prev.info) else Text.Info(r, List(p))
   305 
   306     val tips =
   307       snapshot.cumulate_markup[Text.Info[(List[(Boolean, XML.Tree)])]](
   308         range, Text.Info(range, Nil), Some(tooltip_elements),
   309         {
   310           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _))) =>
   311             val kind1 = space_explode('_', kind).mkString(" ")
   312             add(prev, r, (true, XML.Text(kind1 + " " + quote(name))))
   313           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _)))
   314           if Path.is_ok(name) =>
   315             val jedit_file = PIDE.thy_load.append(snapshot.node_name.dir, Path.explode(name))
   316             add(prev, r, (true, XML.Text("file " + quote(jedit_file))))
   317           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   318           if name == Markup.SORTING || name == Markup.TYPING =>
   319             add(prev, r, (true, pretty_typing("::", body)))
   320           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   321             add(prev, r, (false, pretty_typing("ML:", body)))
   322           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _)))
   323           if tooltips.isDefinedAt(name) => add(prev, r, (true, XML.Text(tooltips(name))))
   324         }).toList.flatMap(_.info.info)
   325 
   326     val all_tips =
   327       (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   328     Library.separate(Pretty.FBreak, all_tips.toList)
   329   }
   330 
   331 
   332   def gutter_message(range: Text.Range): Option[Icon] =
   333   {
   334     val results =
   335       snapshot.cumulate_markup[Int](range, 0, Some(Set(Markup.WARNING, Markup.ERROR)),
   336         {
   337           case (pri, Text.Info(_, XML.Elem(Markup(Markup.WARNING, _), body))) =>
   338             body match {
   339               case List(XML.Elem(Markup(Markup.LEGACY, _), _)) =>
   340                 pri max Rendering.legacy_pri
   341               case _ => pri max Rendering.warning_pri
   342             }
   343           case (pri, Text.Info(_, XML.Elem(Markup(Markup.ERROR, _), _))) =>
   344             pri max Rendering.error_pri
   345         })
   346     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   347     Rendering.gutter_icons.get(pri)
   348   }
   349 
   350 
   351   private val squiggly_colors = Map(
   352     Rendering.writeln_pri -> writeln_color,
   353     Rendering.warning_pri -> warning_color,
   354     Rendering.error_pri -> error_color)
   355 
   356   def squiggly_underline(range: Text.Range): Stream[Text.Info[Color]] =
   357   {
   358     val results =
   359       snapshot.cumulate_markup[Int](range, 0,
   360         Some(Set(Markup.WRITELN, Markup.WARNING, Markup.ERROR)),
   361         {
   362           case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
   363           if name == Markup.WRITELN ||
   364             name == Markup.WARNING ||
   365             name == Markup.ERROR => pri max Rendering.message_pri(name)
   366         })
   367     for {
   368       Text.Info(r, pri) <- results
   369       color <- squiggly_colors.get(pri)
   370     } yield Text.Info(r, color)
   371   }
   372 
   373 
   374   private val messages_include =
   375     Set(Markup.WRITELN_MESSAGE, Markup.TRACING_MESSAGE, Markup.WARNING_MESSAGE, Markup.ERROR_MESSAGE)
   376 
   377   private val message_colors = Map(
   378     Rendering.writeln_pri -> writeln_message_color,
   379     Rendering.tracing_pri -> tracing_message_color,
   380     Rendering.warning_pri -> warning_message_color,
   381     Rendering.error_pri -> error_message_color)
   382 
   383   def line_background(range: Text.Range): Option[(Color, Boolean)] =
   384   {
   385     val results =
   386       snapshot.cumulate_markup[Int](range, 0, Some(messages_include),
   387         {
   388           case (pri, Text.Info(_, XML.Elem(Markup(name, _), _)))
   389           if name == Markup.WRITELN_MESSAGE ||
   390             name == Markup.TRACING_MESSAGE ||
   391             name == Markup.WARNING_MESSAGE ||
   392             name == Markup.ERROR_MESSAGE => pri max Rendering.message_pri(name)
   393         })
   394     val pri = (0 /: results) { case (p1, Text.Info(_, p2)) => p1 max p2 }
   395 
   396     val is_separator = pri > 0 &&
   397       snapshot.cumulate_markup[Boolean](range, false, Some(Set(Markup.SEPARATOR)),
   398         {
   399           case (_, Text.Info(_, XML.Elem(Markup(Markup.SEPARATOR, _), _))) => true
   400         }).exists(_.info)
   401 
   402     message_colors.get(pri).map((_, is_separator))
   403   }
   404 
   405 
   406   private val background1_include =
   407     Protocol.command_status_markup + Markup.WRITELN_MESSAGE + Markup.TRACING_MESSAGE +
   408       Markup.WARNING_MESSAGE + Markup.ERROR_MESSAGE + Markup.BAD + Markup.INTENSIFY +
   409       Markup.SENDBACK
   410 
   411   def background1(range: Text.Range): Stream[Text.Info[Color]] =
   412   {
   413     if (snapshot.is_outdated) Stream(Text.Info(range, outdated_color))
   414     else
   415       for {
   416         Text.Info(r, result) <-
   417           snapshot.cumulate_markup[(Option[Protocol.Status], Option[Color])](
   418             range, (Some(Protocol.Status.init), None), Some(background1_include),
   419             {
   420               case (((Some(status), color), Text.Info(_, XML.Elem(markup, _))))
   421               if (Protocol.command_status_markup(markup.name)) =>
   422                 (Some(Protocol.command_status(status, markup)), color)
   423               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   424                 (None, Some(bad_color))
   425               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   426                 (None, Some(intensify_color))
   427               case (_, Text.Info(_, XML.Elem(Markup(Markup.SENDBACK, _), _))) =>
   428                 (None, Some(sendback_color))
   429             })
   430         color <-
   431           (result match {
   432             case (Some(status), opt_color) =>
   433               if (status.is_unprocessed) Some(unprocessed1_color)
   434               else if (status.is_running) Some(running1_color)
   435               else opt_color
   436             case (_, opt_color) => opt_color
   437           })
   438       } yield Text.Info(r, color)
   439   }
   440 
   441 
   442   def background2(range: Text.Range): Stream[Text.Info[Color]] =
   443     snapshot.select_markup(range, Some(Set(Markup.TOKEN_RANGE)),
   444       {
   445         case Text.Info(_, XML.Elem(Markup(Markup.TOKEN_RANGE, _), _)) => light_color
   446       })
   447 
   448 
   449   def foreground(range: Text.Range): Stream[Text.Info[Color]] =
   450     snapshot.select_markup(range, Some(Set(Markup.STRING, Markup.ALTSTRING, Markup.VERBATIM)),
   451       {
   452         case Text.Info(_, XML.Elem(Markup(Markup.STRING, _), _)) => quoted_color
   453         case Text.Info(_, XML.Elem(Markup(Markup.ALTSTRING, _), _)) => quoted_color
   454         case Text.Info(_, XML.Elem(Markup(Markup.VERBATIM, _), _)) => quoted_color
   455       })
   456 
   457 
   458   private val text_colors: Map[String, Color] = Map(
   459       Markup.KEYWORD1 -> keyword1_color,
   460       Markup.KEYWORD2 -> keyword2_color,
   461       Markup.STRING -> Color.BLACK,
   462       Markup.ALTSTRING -> Color.BLACK,
   463       Markup.VERBATIM -> Color.BLACK,
   464       Markup.LITERAL -> keyword1_color,
   465       Markup.DELIMITER -> Color.BLACK,
   466       Markup.TFREE -> tfree_color,
   467       Markup.TVAR -> tvar_color,
   468       Markup.FREE -> free_color,
   469       Markup.SKOLEM -> skolem_color,
   470       Markup.BOUND -> bound_color,
   471       Markup.VAR -> var_color,
   472       Markup.INNER_STRING -> inner_quoted_color,
   473       Markup.INNER_COMMENT -> inner_comment_color,
   474       Markup.DYNAMIC_FACT -> dynamic_color,
   475       Markup.ML_KEYWORD -> keyword1_color,
   476       Markup.ML_DELIMITER -> Color.BLACK,
   477       Markup.ML_NUMERAL -> inner_numeral_color,
   478       Markup.ML_CHAR -> inner_quoted_color,
   479       Markup.ML_STRING -> inner_quoted_color,
   480       Markup.ML_COMMENT -> inner_comment_color,
   481       Markup.ANTIQ -> antiquotation_color)
   482 
   483   private val text_color_elements = Set.empty[String] ++ text_colors.keys
   484 
   485   def text_color(range: Text.Range, color: Color)
   486       : Stream[Text.Info[Color]] =
   487   {
   488     if (color == Token_Markup.hidden_color) Stream(Text.Info(range, color))
   489     else
   490       snapshot.cumulate_markup(range, color, Some(text_color_elements),
   491         {
   492           case (_, Text.Info(_, XML.Elem(Markup(m, _), _)))
   493           if text_colors.isDefinedAt(m) => text_colors(m)
   494         })
   495   }
   496 }