src/Pure/PIDE/rendering.scala
author wenzelm
Wed Dec 21 23:54:21 2016 +0100 (2016-12-21)
changeset 64654 31b681e38c70
parent 64648 5d7f741aaccb
child 64660 ef85bb6491b3
permissions -rw-r--r--
clarified signature;
     1 /*  Title:      Pure/PIDE/rendering.scala
     2     Author:     Makarius
     3 
     4 Isabelle-specific implementation of quasi-abstract rendering and
     5 markup interpretation.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 object Rendering
    12 {
    13   private val tooltip_descriptions =
    14     Map(
    15       Markup.CITATION -> "citation",
    16       Markup.TOKEN_RANGE -> "inner syntax token",
    17       Markup.FREE -> "free variable",
    18       Markup.SKOLEM -> "skolem variable",
    19       Markup.BOUND -> "bound variable",
    20       Markup.VAR -> "schematic variable",
    21       Markup.TFREE -> "free type variable",
    22       Markup.TVAR -> "schematic type variable")
    23 
    24   private val tooltip_elements =
    25     Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
    26       Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
    27       Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
    28       Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
    29 
    30   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
    31     Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
    32 }
    33 
    34 abstract class Rendering(
    35   val snapshot: Document.Snapshot,
    36   val options: Options,
    37   val resources: Resources)
    38 {
    39   override def toString: String = "Rendering(" + snapshot.toString + ")"
    40 
    41 
    42   /* tooltips */
    43 
    44   def tooltip_margin: Int
    45   def timing_threshold: Double
    46 
    47   def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
    48   {
    49     def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
    50       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
    51     {
    52       val r = snapshot.convert(r0)
    53       val (t, info) = prev.info
    54       if (prev.range == r)
    55         Text.Info(r, (t, info :+ p))
    56       else Text.Info(r, (t, Vector(p)))
    57     }
    58 
    59     val results =
    60       snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
    61         range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
    62         {
    63           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
    64             Some(Text.Info(r, (t1 + t2, info)))
    65 
    66           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
    67           if kind != "" &&
    68             kind != Markup.ML_DEF &&
    69             kind != Markup.ML_OPEN &&
    70             kind != Markup.ML_STRUCTURE =>
    71             val kind1 = Word.implode(Word.explode('_', kind))
    72             val txt1 =
    73               if (name == "") kind1
    74               else if (kind1 == "") quote(name)
    75               else kind1 + " " + quote(name)
    76             val t = prev.info._1
    77             val txt2 =
    78               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
    79                 "\n" + t.message
    80               else ""
    81             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
    82 
    83           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
    84             val file = resources.append_file(snapshot.node_name.master_dir, name)
    85             val text =
    86               if (name == file) "file " + quote(file)
    87               else "path " + quote(name) + "\nfile " + quote(file)
    88             Some(add(prev, r, (true, XML.Text(text))))
    89 
    90           case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
    91             val text = "doc " + quote(name)
    92             Some(add(prev, r, (true, XML.Text(text))))
    93 
    94           case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
    95             Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
    96 
    97           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
    98           if name == Markup.SORTING || name == Markup.TYPING =>
    99             Some(add(prev, r, (true, Rendering.pretty_typing("::", body))))
   100 
   101           case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
   102             Some(add(prev, r, (true, Pretty.block(0, body))))
   103 
   104           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   105             Some(add(prev, r, (false, Rendering.pretty_typing("ML:", body))))
   106 
   107           case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
   108             val text =
   109               if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
   110               else "breakpoint (disabled)"
   111             Some(add(prev, r, (true, XML.Text(text))))
   112 
   113           case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
   114             val lang = Word.implode(Word.explode('_', language))
   115             Some(add(prev, r, (true, XML.Text("language: " + lang))))
   116 
   117           case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
   118             val descr = if (kind == "") "expression" else "expression: " + kind
   119             Some(add(prev, r, (true, XML.Text(descr))))
   120 
   121           case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
   122             Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
   123           case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
   124             Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
   125 
   126           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   127             Rendering.tooltip_descriptions.get(name).
   128               map(descr => add(prev, r, (true, XML.Text(descr))))
   129         }).map(_.info)
   130 
   131     results.map(_.info).flatMap(res => res._2.toList) match {
   132       case Nil => None
   133       case tips =>
   134         val r = Text.Range(results.head.range.start, results.last.range.stop)
   135         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   136         Some(Text.Info(r, Library.separate(Pretty.fbrk, all_tips)))
   137     }
   138   }
   139 }