src/Pure/PIDE/rendering.scala
author wenzelm
Tue Jan 03 21:02:46 2017 +0100 (2017-01-03)
changeset 64767 f5c4ffdb1124
parent 64748 155bf8632104
child 64877 31e9920a0dc1
permissions -rw-r--r--
support VSCode DocumentHighlights;
clarified modules;
     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   /* message priorities */
    14 
    15   val state_pri = 1
    16   val writeln_pri = 2
    17   val information_pri = 3
    18   val tracing_pri = 4
    19   val warning_pri = 5
    20   val legacy_pri = 6
    21   val error_pri = 7
    22 
    23   val message_pri = Map(
    24     Markup.STATE -> state_pri,
    25     Markup.STATE_MESSAGE -> state_pri,
    26     Markup.WRITELN -> writeln_pri,
    27     Markup.WRITELN_MESSAGE -> writeln_pri,
    28     Markup.INFORMATION -> information_pri,
    29     Markup.INFORMATION_MESSAGE -> information_pri,
    30     Markup.TRACING -> tracing_pri,
    31     Markup.TRACING_MESSAGE -> tracing_pri,
    32     Markup.WARNING -> warning_pri,
    33     Markup.WARNING_MESSAGE -> warning_pri,
    34     Markup.LEGACY -> legacy_pri,
    35     Markup.LEGACY_MESSAGE -> legacy_pri,
    36     Markup.ERROR -> error_pri,
    37     Markup.ERROR_MESSAGE -> error_pri)
    38 
    39 
    40   /* markup elements */
    41 
    42   private val tooltip_descriptions =
    43     Map(
    44       Markup.CITATION -> "citation",
    45       Markup.TOKEN_RANGE -> "inner syntax token",
    46       Markup.FREE -> "free variable",
    47       Markup.SKOLEM -> "skolem variable",
    48       Markup.BOUND -> "bound variable",
    49       Markup.VAR -> "schematic variable",
    50       Markup.TFREE -> "free type variable",
    51       Markup.TVAR -> "schematic type variable")
    52 
    53   private val tooltip_elements =
    54     Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
    55       Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
    56       Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
    57       Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
    58 
    59   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
    60     Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
    61 
    62   val caret_focus_elements = Markup.Elements(Markup.ENTITY)
    63 }
    64 
    65 abstract class Rendering(
    66   val snapshot: Document.Snapshot,
    67   val options: Options,
    68   val resources: Resources)
    69 {
    70   override def toString: String = "Rendering(" + snapshot.toString + ")"
    71 
    72 
    73   /* tooltips */
    74 
    75   def tooltip_margin: Int
    76   def timing_threshold: Double
    77 
    78   def tooltips(range: Text.Range): Option[Text.Info[List[XML.Tree]]] =
    79   {
    80     def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
    81       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
    82     {
    83       val r = snapshot.convert(r0)
    84       val (t, info) = prev.info
    85       if (prev.range == r)
    86         Text.Info(r, (t, info :+ p))
    87       else Text.Info(r, (t, Vector(p)))
    88     }
    89 
    90     val results =
    91       snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
    92         range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
    93         {
    94           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
    95             Some(Text.Info(r, (t1 + t2, info)))
    96 
    97           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
    98           if kind != "" && kind != Markup.ML_DEF =>
    99             val kind1 = Word.implode(Word.explode('_', kind))
   100             val txt1 =
   101               if (name == "") kind1
   102               else if (kind1 == "") quote(name)
   103               else kind1 + " " + quote(name)
   104             val t = prev.info._1
   105             val txt2 =
   106               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   107                 "\n" + t.message
   108               else ""
   109             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   110 
   111           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
   112             val file = resources.append_file(snapshot.node_name.master_dir, name)
   113             val text =
   114               if (name == file) "file " + quote(file)
   115               else "path " + quote(name) + "\nfile " + quote(file)
   116             Some(add(prev, r, (true, XML.Text(text))))
   117 
   118           case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
   119             val text = "doc " + quote(name)
   120             Some(add(prev, r, (true, XML.Text(text))))
   121 
   122           case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
   123             Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
   124 
   125           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   126           if name == Markup.SORTING || name == Markup.TYPING =>
   127             Some(add(prev, r, (true, Rendering.pretty_typing("::", body))))
   128 
   129           case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
   130             Some(add(prev, r, (true, Pretty.block(0, body))))
   131 
   132           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   133             Some(add(prev, r, (false, Rendering.pretty_typing("ML:", body))))
   134 
   135           case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
   136             val text =
   137               if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
   138               else "breakpoint (disabled)"
   139             Some(add(prev, r, (true, XML.Text(text))))
   140 
   141           case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
   142             val lang = Word.implode(Word.explode('_', language))
   143             Some(add(prev, r, (true, XML.Text("language: " + lang))))
   144 
   145           case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
   146             val descr = if (kind == "") "expression" else "expression: " + kind
   147             Some(add(prev, r, (true, XML.Text(descr))))
   148 
   149           case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
   150             Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
   151           case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
   152             Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
   153 
   154           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   155             Rendering.tooltip_descriptions.get(name).
   156               map(descr => add(prev, r, (true, XML.Text(descr))))
   157         }).map(_.info)
   158 
   159     results.map(_.info).flatMap(res => res._2.toList) match {
   160       case Nil => None
   161       case tips =>
   162         val r = Text.Range(results.head.range.start, results.last.range.stop)
   163         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   164         Some(Text.Info(r, all_tips))
   165     }
   166   }
   167 
   168 
   169   /* caret focus */
   170 
   171   private def entity_focus(range: Text.Range,
   172     check: (Boolean, Long) => Boolean = (is_def: Boolean, i: Long) => is_def): Set[Long] =
   173   {
   174     val results =
   175       snapshot.cumulate[Set[Long]](range, Set.empty, Rendering.caret_focus_elements, _ =>
   176           {
   177             case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   178               props match {
   179                 case Markup.Entity.Def(i) if check(true, i) => Some(serials + i)
   180                 case Markup.Entity.Ref(i) if check(false, i) => Some(serials + i)
   181                 case _ => None
   182               }
   183             case _ => None
   184           })
   185     (Set.empty[Long] /: results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
   186   }
   187 
   188   def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
   189   {
   190     val focus_defs = entity_focus(caret_range)
   191     if (focus_defs.nonEmpty) focus_defs
   192     else {
   193       val visible_defs = entity_focus(visible_range)
   194       entity_focus(caret_range, (is_def: Boolean, i: Long) => !is_def && visible_defs.contains(i))
   195     }
   196   }
   197 
   198   def caret_focus_ranges(caret_range: Text.Range, visible_range: Text.Range): List[Text.Range] =
   199   {
   200     val focus = caret_focus(caret_range, visible_range)
   201     if (focus.nonEmpty) {
   202       val results =
   203         snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
   204           {
   205             case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   206               props match {
   207                 case Markup.Entity.Def(i) if focus(i) => Some(true)
   208                 case Markup.Entity.Ref(i) if focus(i) => Some(true)
   209                 case _ => None
   210               }
   211           })
   212       for (info <- results if info.info) yield info.range
   213     }
   214     else Nil
   215   }
   216 }