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;
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@64767
    61
wenzelm@64767
    62
  val caret_focus_elements = Markup.Elements(Markup.ENTITY)
wenzelm@64622
    63
}
wenzelm@64622
    64
wenzelm@64622
    65
abstract class Rendering(
wenzelm@64622
    66
  val snapshot: Document.Snapshot,
wenzelm@64622
    67
  val options: Options,
wenzelm@64622
    68
  val resources: Resources)
wenzelm@64622
    69
{
wenzelm@64622
    70
  override def toString: String = "Rendering(" + snapshot.toString + ")"
wenzelm@64622
    71
wenzelm@64622
    72
wenzelm@64622
    73
  /* tooltips */
wenzelm@64622
    74
wenzelm@64622
    75
  def tooltip_margin: Int
wenzelm@64622
    76
  def timing_threshold: Double
wenzelm@64622
    77
wenzelm@64748
    78
  def tooltips(range: Text.Range): Option[Text.Info[List[XML.Tree]]] =
wenzelm@64622
    79
  {
wenzelm@64622
    80
    def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
wenzelm@64622
    81
      r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
wenzelm@64622
    82
    {
wenzelm@64622
    83
      val r = snapshot.convert(r0)
wenzelm@64622
    84
      val (t, info) = prev.info
wenzelm@64622
    85
      if (prev.range == r)
wenzelm@64622
    86
        Text.Info(r, (t, info :+ p))
wenzelm@64622
    87
      else Text.Info(r, (t, Vector(p)))
wenzelm@64622
    88
    }
wenzelm@64622
    89
wenzelm@64622
    90
    val results =
wenzelm@64622
    91
      snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
wenzelm@64622
    92
        range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
wenzelm@64622
    93
        {
wenzelm@64622
    94
          case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
wenzelm@64622
    95
            Some(Text.Info(r, (t1 + t2, info)))
wenzelm@64622
    96
wenzelm@64622
    97
          case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
wenzelm@64660
    98
          if kind != "" && kind != Markup.ML_DEF =>
wenzelm@64622
    99
            val kind1 = Word.implode(Word.explode('_', kind))
wenzelm@64622
   100
            val txt1 =
wenzelm@64622
   101
              if (name == "") kind1
wenzelm@64622
   102
              else if (kind1 == "") quote(name)
wenzelm@64622
   103
              else kind1 + " " + quote(name)
wenzelm@64622
   104
            val t = prev.info._1
wenzelm@64622
   105
            val txt2 =
wenzelm@64622
   106
              if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
wenzelm@64622
   107
                "\n" + t.message
wenzelm@64622
   108
              else ""
wenzelm@64622
   109
            Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
wenzelm@64622
   110
wenzelm@64622
   111
          case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
wenzelm@64654
   112
            val file = resources.append_file(snapshot.node_name.master_dir, name)
wenzelm@64622
   113
            val text =
wenzelm@64622
   114
              if (name == file) "file " + quote(file)
wenzelm@64622
   115
              else "path " + quote(name) + "\nfile " + quote(file)
wenzelm@64622
   116
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64622
   117
wenzelm@64622
   118
          case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@64622
   119
            val text = "doc " + quote(name)
wenzelm@64622
   120
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64622
   121
wenzelm@64622
   122
          case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
wenzelm@64622
   123
            Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
wenzelm@64622
   124
wenzelm@64622
   125
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@64622
   126
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@64622
   127
            Some(add(prev, r, (true, Rendering.pretty_typing("::", body))))
wenzelm@64622
   128
wenzelm@64622
   129
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
wenzelm@64622
   130
            Some(add(prev, r, (true, Pretty.block(0, body))))
wenzelm@64622
   131
wenzelm@64622
   132
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@64622
   133
            Some(add(prev, r, (false, Rendering.pretty_typing("ML:", body))))
wenzelm@64622
   134
wenzelm@64622
   135
          case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
wenzelm@64622
   136
            val text =
wenzelm@64622
   137
              if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
wenzelm@64622
   138
              else "breakpoint (disabled)"
wenzelm@64622
   139
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64622
   140
wenzelm@64622
   141
          case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
wenzelm@64622
   142
            val lang = Word.implode(Word.explode('_', language))
wenzelm@64622
   143
            Some(add(prev, r, (true, XML.Text("language: " + lang))))
wenzelm@64622
   144
wenzelm@64622
   145
          case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
wenzelm@64622
   146
            val descr = if (kind == "") "expression" else "expression: " + kind
wenzelm@64622
   147
            Some(add(prev, r, (true, XML.Text(descr))))
wenzelm@64622
   148
wenzelm@64622
   149
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
wenzelm@64622
   150
            Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
wenzelm@64622
   151
          case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
wenzelm@64622
   152
            Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
wenzelm@64622
   153
wenzelm@64622
   154
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
wenzelm@64622
   155
            Rendering.tooltip_descriptions.get(name).
wenzelm@64622
   156
              map(descr => add(prev, r, (true, XML.Text(descr))))
wenzelm@64622
   157
        }).map(_.info)
wenzelm@64622
   158
wenzelm@64622
   159
    results.map(_.info).flatMap(res => res._2.toList) match {
wenzelm@64622
   160
      case Nil => None
wenzelm@64622
   161
      case tips =>
wenzelm@64622
   162
        val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@64622
   163
        val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@64748
   164
        Some(Text.Info(r, all_tips))
wenzelm@64622
   165
    }
wenzelm@64622
   166
  }
wenzelm@64767
   167
wenzelm@64767
   168
wenzelm@64767
   169
  /* caret focus */
wenzelm@64767
   170
wenzelm@64767
   171
  private def entity_focus(range: Text.Range,
wenzelm@64767
   172
    check: (Boolean, Long) => Boolean = (is_def: Boolean, i: Long) => is_def): Set[Long] =
wenzelm@64767
   173
  {
wenzelm@64767
   174
    val results =
wenzelm@64767
   175
      snapshot.cumulate[Set[Long]](range, Set.empty, Rendering.caret_focus_elements, _ =>
wenzelm@64767
   176
          {
wenzelm@64767
   177
            case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64767
   178
              props match {
wenzelm@64767
   179
                case Markup.Entity.Def(i) if check(true, i) => Some(serials + i)
wenzelm@64767
   180
                case Markup.Entity.Ref(i) if check(false, i) => Some(serials + i)
wenzelm@64767
   181
                case _ => None
wenzelm@64767
   182
              }
wenzelm@64767
   183
            case _ => None
wenzelm@64767
   184
          })
wenzelm@64767
   185
    (Set.empty[Long] /: results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
wenzelm@64767
   186
  }
wenzelm@64767
   187
wenzelm@64767
   188
  def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
wenzelm@64767
   189
  {
wenzelm@64767
   190
    val focus_defs = entity_focus(caret_range)
wenzelm@64767
   191
    if (focus_defs.nonEmpty) focus_defs
wenzelm@64767
   192
    else {
wenzelm@64767
   193
      val visible_defs = entity_focus(visible_range)
wenzelm@64767
   194
      entity_focus(caret_range, (is_def: Boolean, i: Long) => !is_def && visible_defs.contains(i))
wenzelm@64767
   195
    }
wenzelm@64767
   196
  }
wenzelm@64767
   197
wenzelm@64767
   198
  def caret_focus_ranges(caret_range: Text.Range, visible_range: Text.Range): List[Text.Range] =
wenzelm@64767
   199
  {
wenzelm@64767
   200
    val focus = caret_focus(caret_range, visible_range)
wenzelm@64767
   201
    if (focus.nonEmpty) {
wenzelm@64767
   202
      val results =
wenzelm@64767
   203
        snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
wenzelm@64767
   204
          {
wenzelm@64767
   205
            case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64767
   206
              props match {
wenzelm@64767
   207
                case Markup.Entity.Def(i) if focus(i) => Some(true)
wenzelm@64767
   208
                case Markup.Entity.Ref(i) if focus(i) => Some(true)
wenzelm@64767
   209
                case _ => None
wenzelm@64767
   210
              }
wenzelm@64767
   211
          })
wenzelm@64767
   212
      for (info <- results if info.info) yield info.range
wenzelm@64767
   213
    }
wenzelm@64767
   214
    else Nil
wenzelm@64767
   215
  }
wenzelm@64622
   216
}