src/Pure/PIDE/rendering.scala
author wenzelm
Fri Dec 23 11:21:38 2016 +0100 (2016-12-23)
changeset 64660 ef85bb6491b3
parent 64654 31b681e38c70
child 64676 fd2df1ea3bb4
permissions -rw-r--r--
omit unused markup;
     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 != "" && kind != Markup.ML_DEF =>
    68             val kind1 = Word.implode(Word.explode('_', kind))
    69             val txt1 =
    70               if (name == "") kind1
    71               else if (kind1 == "") quote(name)
    72               else kind1 + " " + quote(name)
    73             val t = prev.info._1
    74             val txt2 =
    75               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
    76                 "\n" + t.message
    77               else ""
    78             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
    79 
    80           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
    81             val file = resources.append_file(snapshot.node_name.master_dir, name)
    82             val text =
    83               if (name == file) "file " + quote(file)
    84               else "path " + quote(name) + "\nfile " + quote(file)
    85             Some(add(prev, r, (true, XML.Text(text))))
    86 
    87           case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
    88             val text = "doc " + quote(name)
    89             Some(add(prev, r, (true, XML.Text(text))))
    90 
    91           case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
    92             Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
    93 
    94           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
    95           if name == Markup.SORTING || name == Markup.TYPING =>
    96             Some(add(prev, r, (true, Rendering.pretty_typing("::", body))))
    97 
    98           case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
    99             Some(add(prev, r, (true, Pretty.block(0, body))))
   100 
   101           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   102             Some(add(prev, r, (false, Rendering.pretty_typing("ML:", body))))
   103 
   104           case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
   105             val text =
   106               if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
   107               else "breakpoint (disabled)"
   108             Some(add(prev, r, (true, XML.Text(text))))
   109 
   110           case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
   111             val lang = Word.implode(Word.explode('_', language))
   112             Some(add(prev, r, (true, XML.Text("language: " + lang))))
   113 
   114           case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
   115             val descr = if (kind == "") "expression" else "expression: " + kind
   116             Some(add(prev, r, (true, XML.Text(descr))))
   117 
   118           case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
   119             Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
   120           case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
   121             Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
   122 
   123           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   124             Rendering.tooltip_descriptions.get(name).
   125               map(descr => add(prev, r, (true, XML.Text(descr))))
   126         }).map(_.info)
   127 
   128     results.map(_.info).flatMap(res => res._2.toList) match {
   129       case Nil => None
   130       case tips =>
   131         val r = Text.Range(results.head.range.start, results.last.range.stop)
   132         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   133         Some(Text.Info(r, Library.separate(Pretty.fbrk, all_tips)))
   134     }
   135   }
   136 }