src/Pure/PIDE/rendering.scala
author wenzelm
Sun Mar 05 22:06:13 2017 +0100 (2017-03-05)
changeset 65121 12c6774a8f65
parent 65107 70b0113fa4ef
child 65124 759c64c39a6f
permissions -rw-r--r--
more generic rendering;
     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   /* color */
    14 
    15   object Color extends Enumeration
    16   {
    17     // background
    18     val unprocessed1 = Value("unprocessed1")
    19     val running1 = Value("running1")
    20     val bad = Value("bad")
    21     val intensify = Value("intensify")
    22     val entity = Value("entity")
    23     val active = Value("active")
    24     val active_result = Value("active_result")
    25     val markdown_item1 = Value("markdown_item1")
    26     val markdown_item2 = Value("markdown_item2")
    27     val markdown_item3 = Value("markdown_item3")
    28     val markdown_item4 = Value("markdown_item4")
    29     val background = values
    30 
    31     // foreground
    32     val quoted = Value("quoted")
    33     val antiquoted = Value("antiquoted")
    34     val foreground = values -- background
    35 
    36     // underline
    37     val writeln = Value("writeln")
    38     val information = Value("information")
    39     val warning = Value("warning")
    40     val legacy = Value("legacy")
    41     val error = Value("error")
    42     val underline = values -- background -- foreground
    43   }
    44 
    45 
    46   /* message priorities */
    47 
    48   val state_pri = 1
    49   val writeln_pri = 2
    50   val information_pri = 3
    51   val tracing_pri = 4
    52   val warning_pri = 5
    53   val legacy_pri = 6
    54   val error_pri = 7
    55 
    56   val message_pri = Map(
    57     Markup.STATE -> state_pri,
    58     Markup.STATE_MESSAGE -> state_pri,
    59     Markup.WRITELN -> writeln_pri,
    60     Markup.WRITELN_MESSAGE -> writeln_pri,
    61     Markup.INFORMATION -> information_pri,
    62     Markup.INFORMATION_MESSAGE -> information_pri,
    63     Markup.TRACING -> tracing_pri,
    64     Markup.TRACING_MESSAGE -> tracing_pri,
    65     Markup.WARNING -> warning_pri,
    66     Markup.WARNING_MESSAGE -> warning_pri,
    67     Markup.LEGACY -> legacy_pri,
    68     Markup.LEGACY_MESSAGE -> legacy_pri,
    69     Markup.ERROR -> error_pri,
    70     Markup.ERROR_MESSAGE -> error_pri)
    71 
    72   val message_underline_color = Map(
    73     writeln_pri -> Color.writeln,
    74     information_pri -> Color.information,
    75     warning_pri -> Color.warning,
    76     legacy_pri -> Color.legacy,
    77     error_pri -> Color.error)
    78 
    79 
    80   /* markup elements */
    81 
    82   val active_elements =
    83     Markup.Elements(Markup.DIALOG, Markup.BROWSER, Markup.GRAPHVIEW,
    84       Markup.SENDBACK, Markup.JEDIT_ACTION, Markup.SIMP_TRACE_PANEL)
    85 
    86   private val background_elements =
    87     Protocol.proper_status_elements + Markup.WRITELN_MESSAGE +
    88       Markup.STATE_MESSAGE + Markup.INFORMATION_MESSAGE +
    89       Markup.TRACING_MESSAGE + Markup.WARNING_MESSAGE +
    90       Markup.LEGACY_MESSAGE + Markup.ERROR_MESSAGE +
    91       Markup.BAD + Markup.INTENSIFY + Markup.ENTITY +
    92       Markup.Markdown_Item.name ++ active_elements
    93 
    94   private val foreground_elements =
    95     Markup.Elements(Markup.STRING, Markup.ALT_STRING, Markup.VERBATIM,
    96       Markup.CARTOUCHE, Markup.ANTIQUOTED)
    97 
    98   private val semantic_completion_elements =
    99     Markup.Elements(Markup.COMPLETION, Markup.NO_COMPLETION)
   100 
   101   private val tooltip_descriptions =
   102     Map(
   103       Markup.CITATION -> "citation",
   104       Markup.TOKEN_RANGE -> "inner syntax token",
   105       Markup.FREE -> "free variable",
   106       Markup.SKOLEM -> "skolem variable",
   107       Markup.BOUND -> "bound variable",
   108       Markup.VAR -> "schematic variable",
   109       Markup.TFREE -> "free type variable",
   110       Markup.TVAR -> "schematic type variable")
   111 
   112   private val tooltip_elements =
   113     Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
   114       Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
   115       Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
   116       Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
   117 
   118   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   119     Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
   120 
   121   val caret_focus_elements = Markup.Elements(Markup.ENTITY)
   122 }
   123 
   124 abstract class Rendering(
   125   val snapshot: Document.Snapshot,
   126   val options: Options,
   127   val resources: Resources)
   128 {
   129   override def toString: String = "Rendering(" + snapshot.toString + ")"
   130 
   131 
   132   /* completion */
   133 
   134   def semantic_completion(completed_range: Option[Text.Range], range: Text.Range)
   135       : Option[Text.Info[Completion.Semantic]] =
   136     if (snapshot.is_outdated) None
   137     else {
   138       snapshot.select(range, Rendering.semantic_completion_elements, _ =>
   139         {
   140           case Completion.Semantic.Info(info) =>
   141             completed_range match {
   142               case Some(range0) if range0.contains(info.range) && range0 != info.range => None
   143               case _ => Some(info)
   144             }
   145           case _ => None
   146         }).headOption.map(_.info)
   147     }
   148 
   149 
   150   /* tooltips */
   151 
   152   def timing_threshold: Double
   153 
   154   def tooltips(range: Text.Range): Option[Text.Info[List[XML.Tree]]] =
   155   {
   156     def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
   157       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
   158     {
   159       val r = snapshot.convert(r0)
   160       val (t, info) = prev.info
   161       if (prev.range == r)
   162         Text.Info(r, (t, info :+ p))
   163       else Text.Info(r, (t, Vector(p)))
   164     }
   165 
   166     val results =
   167       snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
   168         range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
   169         {
   170           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   171             Some(Text.Info(r, (t1 + t2, info)))
   172 
   173           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
   174           if kind != "" && kind != Markup.ML_DEF =>
   175             val kind1 = Word.implode(Word.explode('_', kind))
   176             val txt1 =
   177               if (name == "") kind1
   178               else if (kind1 == "") quote(name)
   179               else kind1 + " " + quote(name)
   180             val t = prev.info._1
   181             val txt2 =
   182               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   183                 "\n" + t.message
   184               else ""
   185             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   186 
   187           case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
   188             val file = resources.append_file(snapshot.node_name.master_dir, name)
   189             val text =
   190               if (name == file) "file " + quote(file)
   191               else "path " + quote(name) + "\nfile " + quote(file)
   192             Some(add(prev, r, (true, XML.Text(text))))
   193 
   194           case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
   195             val text = "doc " + quote(name)
   196             Some(add(prev, r, (true, XML.Text(text))))
   197 
   198           case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
   199             Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
   200 
   201           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   202           if name == Markup.SORTING || name == Markup.TYPING =>
   203             Some(add(prev, r, (true, Rendering.pretty_typing("::", body))))
   204 
   205           case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
   206             Some(add(prev, r, (true, Pretty.block(0, body))))
   207 
   208           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   209             Some(add(prev, r, (false, Rendering.pretty_typing("ML:", body))))
   210 
   211           case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
   212             val text =
   213               if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
   214               else "breakpoint (disabled)"
   215             Some(add(prev, r, (true, XML.Text(text))))
   216 
   217           case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
   218             val lang = Word.implode(Word.explode('_', language))
   219             Some(add(prev, r, (true, XML.Text("language: " + lang))))
   220 
   221           case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
   222             val descr = if (kind == "") "expression" else "expression: " + kind
   223             Some(add(prev, r, (true, XML.Text(descr))))
   224 
   225           case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
   226             Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
   227           case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
   228             Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
   229 
   230           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   231             Rendering.tooltip_descriptions.get(name).
   232               map(descr => add(prev, r, (true, XML.Text(descr))))
   233         }).map(_.info)
   234 
   235     results.map(_.info).flatMap(res => res._2.toList) match {
   236       case Nil => None
   237       case tips =>
   238         val r = Text.Range(results.head.range.start, results.last.range.stop)
   239         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   240         Some(Text.Info(r, all_tips))
   241     }
   242   }
   243 
   244 
   245   /* text background */
   246 
   247   def background(range: Text.Range, focus: Set[Long]): List[Text.Info[Rendering.Color.Value]] =
   248   {
   249     for {
   250       Text.Info(r, result) <-
   251         snapshot.cumulate[(List[Markup], Option[Rendering.Color.Value])](
   252           range, (List(Markup.Empty), None), Rendering.background_elements,
   253           command_states =>
   254             {
   255               case (((markups, color), Text.Info(_, XML.Elem(markup, _))))
   256               if markups.nonEmpty && Protocol.proper_status_elements(markup.name) =>
   257                 Some((markup :: markups, color))
   258               case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
   259                 Some((Nil, Some(Rendering.Color.bad)))
   260               case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
   261                 Some((Nil, Some(Rendering.Color.intensify)))
   262               case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   263                 props match {
   264                   case Markup.Entity.Def(i) if focus(i) => Some((Nil, Some(Rendering.Color.entity)))
   265                   case Markup.Entity.Ref(i) if focus(i) => Some((Nil, Some(Rendering.Color.entity)))
   266                   case _ => None
   267                 }
   268               case (_, Text.Info(_, XML.Elem(Markup.Markdown_Item(depth), _))) =>
   269                 val color =
   270                   depth match {
   271                     case 1 => Rendering.Color.markdown_item1
   272                     case 2 => Rendering.Color.markdown_item2
   273                     case 3 => Rendering.Color.markdown_item3
   274                     case _ => Rendering.Color.markdown_item4
   275                   }
   276                 Some((Nil, Some(color)))
   277               case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
   278                 command_states.collectFirst(
   279                   { case st if st.results.defined(serial) => st.results.get(serial).get }) match
   280                 {
   281                   case Some(Protocol.Dialog_Result(res)) if res == result =>
   282                     Some((Nil, Some(Rendering.Color.active_result)))
   283                   case _ =>
   284                     Some((Nil, Some(Rendering.Color.active)))
   285                 }
   286               case (_, Text.Info(_, elem)) =>
   287                 if (Rendering.active_elements(elem.name))
   288                   Some((Nil, Some(Rendering.Color.active)))
   289                 else None
   290             })
   291       color <-
   292         (result match {
   293           case (markups, opt_color) if markups.nonEmpty =>
   294             val status = Protocol.Status.make(markups.iterator)
   295             if (status.is_unprocessed) Some(Rendering.Color.unprocessed1)
   296             else if (status.is_running) Some(Rendering.Color.running1)
   297             else opt_color
   298           case (_, opt_color) => opt_color
   299         })
   300     } yield Text.Info(r, color)
   301   }
   302 
   303 
   304   /* text foreground */
   305 
   306   def foreground(range: Text.Range): List[Text.Info[Rendering.Color.Value]] =
   307     snapshot.select(range, Rendering.foreground_elements, _ =>
   308       {
   309         case Text.Info(_, elem) =>
   310           if (elem.name == Markup.ANTIQUOTED) Some(Rendering.Color.antiquoted)
   311           else Some(Rendering.Color.quoted)
   312       })
   313 
   314 
   315   /* caret focus */
   316 
   317   private def entity_focus(range: Text.Range,
   318     check: (Boolean, Long) => Boolean = (is_def: Boolean, i: Long) => is_def): Set[Long] =
   319   {
   320     val results =
   321       snapshot.cumulate[Set[Long]](range, Set.empty, Rendering.caret_focus_elements, _ =>
   322           {
   323             case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   324               props match {
   325                 case Markup.Entity.Def(i) if check(true, i) => Some(serials + i)
   326                 case Markup.Entity.Ref(i) if check(false, i) => Some(serials + i)
   327                 case _ => None
   328               }
   329             case _ => None
   330           })
   331     (Set.empty[Long] /: results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
   332   }
   333 
   334   def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
   335   {
   336     val focus_defs = entity_focus(caret_range)
   337     if (focus_defs.nonEmpty) focus_defs
   338     else {
   339       val visible_defs = entity_focus(visible_range)
   340       entity_focus(caret_range, (is_def: Boolean, i: Long) => !is_def && visible_defs.contains(i))
   341     }
   342   }
   343 
   344   def caret_focus_ranges(caret_range: Text.Range, visible_range: Text.Range): List[Text.Range] =
   345   {
   346     val focus = caret_focus(caret_range, visible_range)
   347     if (focus.nonEmpty) {
   348       val results =
   349         snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
   350           {
   351             case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
   352               props match {
   353                 case Markup.Entity.Def(i) if focus(i) => Some(true)
   354                 case Markup.Entity.Ref(i) if focus(i) => Some(true)
   355                 case _ => None
   356               }
   357           })
   358       for (info <- results if info.info) yield info.range
   359     }
   360     else Nil
   361   }
   362 
   363 
   364   /* message underline color */
   365 
   366   def message_underline_color(
   367     elements: Markup.Elements, range: Text.Range): List[Text.Info[Rendering.Color.Value]] =
   368   {
   369     val results =
   370       snapshot.cumulate[Int](range, 0, elements, _ =>
   371         {
   372           case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
   373         })
   374     for {
   375       Text.Info(r, pri) <- results
   376       color <- Rendering.message_underline_color.get(pri)
   377     } yield Text.Info(r, color)
   378   }
   379 }