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