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