src/Pure/PIDE/rendering.scala
author wenzelm
Tue Dec 27 17:33:57 2016 +0100 (2016-12-27)
changeset 64676 fd2df1ea3bb4
parent 64660 ef85bb6491b3
child 64748 155bf8632104
permissions -rw-r--r--
clarified modules;
wenzelm@64622
     1
/*  Title:      Pure/PIDE/rendering.scala
wenzelm@64622
     2
    Author:     Makarius
wenzelm@64622
     3
wenzelm@64622
     4
Isabelle-specific implementation of quasi-abstract rendering and
wenzelm@64622
     5
markup interpretation.
wenzelm@64622
     6
*/
wenzelm@64622
     7
wenzelm@64622
     8
package isabelle
wenzelm@64622
     9
wenzelm@64622
    10
wenzelm@64622
    11
object Rendering
wenzelm@64622
    12
{
wenzelm@64676
    13
  /* message priorities */
wenzelm@64676
    14
wenzelm@64676
    15
  val state_pri = 1
wenzelm@64676
    16
  val writeln_pri = 2
wenzelm@64676
    17
  val information_pri = 3
wenzelm@64676
    18
  val tracing_pri = 4
wenzelm@64676
    19
  val warning_pri = 5
wenzelm@64676
    20
  val legacy_pri = 6
wenzelm@64676
    21
  val error_pri = 7
wenzelm@64676
    22
wenzelm@64676
    23
  val message_pri = Map(
wenzelm@64676
    24
    Markup.STATE -> state_pri,
wenzelm@64676
    25
    Markup.STATE_MESSAGE -> state_pri,
wenzelm@64676
    26
    Markup.WRITELN -> writeln_pri,
wenzelm@64676
    27
    Markup.WRITELN_MESSAGE -> writeln_pri,
wenzelm@64676
    28
    Markup.INFORMATION -> information_pri,
wenzelm@64676
    29
    Markup.INFORMATION_MESSAGE -> information_pri,
wenzelm@64676
    30
    Markup.TRACING -> tracing_pri,
wenzelm@64676
    31
    Markup.TRACING_MESSAGE -> tracing_pri,
wenzelm@64676
    32
    Markup.WARNING -> warning_pri,
wenzelm@64676
    33
    Markup.WARNING_MESSAGE -> warning_pri,
wenzelm@64676
    34
    Markup.LEGACY -> legacy_pri,
wenzelm@64676
    35
    Markup.LEGACY_MESSAGE -> legacy_pri,
wenzelm@64676
    36
    Markup.ERROR -> error_pri,
wenzelm@64676
    37
    Markup.ERROR_MESSAGE -> error_pri)
wenzelm@64676
    38
wenzelm@64676
    39
wenzelm@64676
    40
  /* markup elements */
wenzelm@64676
    41
wenzelm@64622
    42
  private val tooltip_descriptions =
wenzelm@64622
    43
    Map(
wenzelm@64622
    44
      Markup.CITATION -> "citation",
wenzelm@64622
    45
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@64622
    46
      Markup.FREE -> "free variable",
wenzelm@64622
    47
      Markup.SKOLEM -> "skolem variable",
wenzelm@64622
    48
      Markup.BOUND -> "bound variable",
wenzelm@64622
    49
      Markup.VAR -> "schematic variable",
wenzelm@64622
    50
      Markup.TFREE -> "free type variable",
wenzelm@64622
    51
      Markup.TVAR -> "schematic type variable")
wenzelm@64622
    52
wenzelm@64622
    53
  private val tooltip_elements =
wenzelm@64622
    54
    Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
wenzelm@64622
    55
      Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
wenzelm@64622
    56
      Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
wenzelm@64622
    57
      Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
wenzelm@64622
    58
wenzelm@64622
    59
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@64622
    60
    Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
wenzelm@64622
    61
}
wenzelm@64622
    62
wenzelm@64622
    63
abstract class Rendering(
wenzelm@64622
    64
  val snapshot: Document.Snapshot,
wenzelm@64622
    65
  val options: Options,
wenzelm@64622
    66
  val resources: Resources)
wenzelm@64622
    67
{
wenzelm@64622
    68
  override def toString: String = "Rendering(" + snapshot.toString + ")"
wenzelm@64622
    69
wenzelm@64622
    70
wenzelm@64622
    71
  /* tooltips */
wenzelm@64622
    72
wenzelm@64622
    73
  def tooltip_margin: Int
wenzelm@64622
    74
  def timing_threshold: Double
wenzelm@64622
    75
wenzelm@64622
    76
  def tooltip(range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@64622
    77
  {
wenzelm@64622
    78
    def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
wenzelm@64622
    79
      r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
wenzelm@64622
    80
    {
wenzelm@64622
    81
      val r = snapshot.convert(r0)
wenzelm@64622
    82
      val (t, info) = prev.info
wenzelm@64622
    83
      if (prev.range == r)
wenzelm@64622
    84
        Text.Info(r, (t, info :+ p))
wenzelm@64622
    85
      else Text.Info(r, (t, Vector(p)))
wenzelm@64622
    86
    }
wenzelm@64622
    87
wenzelm@64622
    88
    val results =
wenzelm@64622
    89
      snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
wenzelm@64622
    90
        range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
wenzelm@64622
    91
        {
wenzelm@64622
    92
          case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
wenzelm@64622
    93
            Some(Text.Info(r, (t1 + t2, info)))
wenzelm@64622
    94
wenzelm@64622
    95
          case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
wenzelm@64660
    96
          if kind != "" && kind != Markup.ML_DEF =>
wenzelm@64622
    97
            val kind1 = Word.implode(Word.explode('_', kind))
wenzelm@64622
    98
            val txt1 =
wenzelm@64622
    99
              if (name == "") kind1
wenzelm@64622
   100
              else if (kind1 == "") quote(name)
wenzelm@64622
   101
              else kind1 + " " + quote(name)
wenzelm@64622
   102
            val t = prev.info._1
wenzelm@64622
   103
            val txt2 =
wenzelm@64622
   104
              if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
wenzelm@64622
   105
                "\n" + t.message
wenzelm@64622
   106
              else ""
wenzelm@64622
   107
            Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
wenzelm@64622
   108
wenzelm@64622
   109
          case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
wenzelm@64654
   110
            val file = resources.append_file(snapshot.node_name.master_dir, name)
wenzelm@64622
   111
            val text =
wenzelm@64622
   112
              if (name == file) "file " + quote(file)
wenzelm@64622
   113
              else "path " + quote(name) + "\nfile " + quote(file)
wenzelm@64622
   114
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64622
   115
wenzelm@64622
   116
          case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@64622
   117
            val text = "doc " + quote(name)
wenzelm@64622
   118
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64622
   119
wenzelm@64622
   120
          case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
wenzelm@64622
   121
            Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
wenzelm@64622
   122
wenzelm@64622
   123
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@64622
   124
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@64622
   125
            Some(add(prev, r, (true, Rendering.pretty_typing("::", body))))
wenzelm@64622
   126
wenzelm@64622
   127
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
wenzelm@64622
   128
            Some(add(prev, r, (true, Pretty.block(0, body))))
wenzelm@64622
   129
wenzelm@64622
   130
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@64622
   131
            Some(add(prev, r, (false, Rendering.pretty_typing("ML:", body))))
wenzelm@64622
   132
wenzelm@64622
   133
          case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
wenzelm@64622
   134
            val text =
wenzelm@64622
   135
              if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
wenzelm@64622
   136
              else "breakpoint (disabled)"
wenzelm@64622
   137
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64622
   138
wenzelm@64622
   139
          case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
wenzelm@64622
   140
            val lang = Word.implode(Word.explode('_', language))
wenzelm@64622
   141
            Some(add(prev, r, (true, XML.Text("language: " + lang))))
wenzelm@64622
   142
wenzelm@64622
   143
          case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
wenzelm@64622
   144
            val descr = if (kind == "") "expression" else "expression: " + kind
wenzelm@64622
   145
            Some(add(prev, r, (true, XML.Text(descr))))
wenzelm@64622
   146
wenzelm@64622
   147
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
wenzelm@64622
   148
            Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
wenzelm@64622
   149
          case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
wenzelm@64622
   150
            Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
wenzelm@64622
   151
wenzelm@64622
   152
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
wenzelm@64622
   153
            Rendering.tooltip_descriptions.get(name).
wenzelm@64622
   154
              map(descr => add(prev, r, (true, XML.Text(descr))))
wenzelm@64622
   155
        }).map(_.info)
wenzelm@64622
   156
wenzelm@64622
   157
    results.map(_.info).flatMap(res => res._2.toList) match {
wenzelm@64622
   158
      case Nil => None
wenzelm@64622
   159
      case tips =>
wenzelm@64622
   160
        val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@64622
   161
        val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@64622
   162
        Some(Text.Info(r, Library.separate(Pretty.fbrk, all_tips)))
wenzelm@64622
   163
    }
wenzelm@64622
   164
  }
wenzelm@64622
   165
}