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