src/Pure/PIDE/rendering.scala
author wenzelm
Mon Mar 06 11:38:06 2017 +0100 (2017-03-06)
changeset 65124 759c64c39a6f
parent 65121 12c6774a8f65
child 65126 45ccb8ee3d08
permissions -rw-r--r--
more generic colors;
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
wenzelm@65124
    46
    val writeln_message = Value("writeln_message")
wenzelm@65124
    47
    val information_message = Value("information_message")
wenzelm@65124
    48
    val tracing_message = Value("tracing_message")
wenzelm@65124
    49
    val warning_message = Value("warning_message")
wenzelm@65124
    50
    val legacy_message = Value("legacy_message")
wenzelm@65124
    51
    val error_message = Value("error_message")
wenzelm@65124
    52
    val message_background = values -- background -- foreground -- message_underline
wenzelm@65101
    53
  }
wenzelm@65101
    54
wenzelm@65101
    55
wenzelm@64676
    56
  /* message priorities */
wenzelm@64676
    57
wenzelm@64676
    58
  val state_pri = 1
wenzelm@64676
    59
  val writeln_pri = 2
wenzelm@64676
    60
  val information_pri = 3
wenzelm@64676
    61
  val tracing_pri = 4
wenzelm@64676
    62
  val warning_pri = 5
wenzelm@64676
    63
  val legacy_pri = 6
wenzelm@64676
    64
  val error_pri = 7
wenzelm@64676
    65
wenzelm@64676
    66
  val message_pri = Map(
wenzelm@64676
    67
    Markup.STATE -> state_pri,
wenzelm@64676
    68
    Markup.STATE_MESSAGE -> state_pri,
wenzelm@64676
    69
    Markup.WRITELN -> writeln_pri,
wenzelm@64676
    70
    Markup.WRITELN_MESSAGE -> writeln_pri,
wenzelm@64676
    71
    Markup.INFORMATION -> information_pri,
wenzelm@64676
    72
    Markup.INFORMATION_MESSAGE -> information_pri,
wenzelm@64676
    73
    Markup.TRACING -> tracing_pri,
wenzelm@64676
    74
    Markup.TRACING_MESSAGE -> tracing_pri,
wenzelm@64676
    75
    Markup.WARNING -> warning_pri,
wenzelm@64676
    76
    Markup.WARNING_MESSAGE -> warning_pri,
wenzelm@64676
    77
    Markup.LEGACY -> legacy_pri,
wenzelm@64676
    78
    Markup.LEGACY_MESSAGE -> legacy_pri,
wenzelm@64676
    79
    Markup.ERROR -> error_pri,
wenzelm@64676
    80
    Markup.ERROR_MESSAGE -> error_pri)
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@64622
   130
  private 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@64622
   136
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@64622
   137
    Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
wenzelm@64767
   138
wenzelm@64767
   139
  val caret_focus_elements = Markup.Elements(Markup.ENTITY)
wenzelm@64622
   140
}
wenzelm@64622
   141
wenzelm@64622
   142
abstract class Rendering(
wenzelm@64622
   143
  val snapshot: Document.Snapshot,
wenzelm@64622
   144
  val options: Options,
wenzelm@64622
   145
  val resources: Resources)
wenzelm@64622
   146
{
wenzelm@64622
   147
  override def toString: String = "Rendering(" + snapshot.toString + ")"
wenzelm@64622
   148
wenzelm@64622
   149
wenzelm@64877
   150
  /* completion */
wenzelm@64877
   151
wenzelm@64877
   152
  def semantic_completion(completed_range: Option[Text.Range], range: Text.Range)
wenzelm@64877
   153
      : Option[Text.Info[Completion.Semantic]] =
wenzelm@64877
   154
    if (snapshot.is_outdated) None
wenzelm@64877
   155
    else {
wenzelm@64877
   156
      snapshot.select(range, Rendering.semantic_completion_elements, _ =>
wenzelm@64877
   157
        {
wenzelm@64877
   158
          case Completion.Semantic.Info(info) =>
wenzelm@64877
   159
            completed_range match {
wenzelm@64877
   160
              case Some(range0) if range0.contains(info.range) && range0 != info.range => None
wenzelm@64877
   161
              case _ => Some(info)
wenzelm@64877
   162
            }
wenzelm@64877
   163
          case _ => None
wenzelm@64877
   164
        }).headOption.map(_.info)
wenzelm@64877
   165
    }
wenzelm@64877
   166
wenzelm@64877
   167
wenzelm@64622
   168
  /* tooltips */
wenzelm@64622
   169
wenzelm@64622
   170
  def timing_threshold: Double
wenzelm@64622
   171
wenzelm@64748
   172
  def tooltips(range: Text.Range): Option[Text.Info[List[XML.Tree]]] =
wenzelm@64622
   173
  {
wenzelm@64622
   174
    def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
wenzelm@64622
   175
      r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
wenzelm@64622
   176
    {
wenzelm@64622
   177
      val r = snapshot.convert(r0)
wenzelm@64622
   178
      val (t, info) = prev.info
wenzelm@64622
   179
      if (prev.range == r)
wenzelm@64622
   180
        Text.Info(r, (t, info :+ p))
wenzelm@64622
   181
      else Text.Info(r, (t, Vector(p)))
wenzelm@64622
   182
    }
wenzelm@64622
   183
wenzelm@64622
   184
    val results =
wenzelm@64622
   185
      snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
wenzelm@64622
   186
        range, Text.Info(range, (Timing.zero, Vector.empty)), Rendering.tooltip_elements, _ =>
wenzelm@64622
   187
        {
wenzelm@64622
   188
          case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
wenzelm@64622
   189
            Some(Text.Info(r, (t1 + t2, info)))
wenzelm@64622
   190
wenzelm@64622
   191
          case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
wenzelm@64660
   192
          if kind != "" && kind != Markup.ML_DEF =>
wenzelm@64622
   193
            val kind1 = Word.implode(Word.explode('_', kind))
wenzelm@64622
   194
            val txt1 =
wenzelm@64622
   195
              if (name == "") kind1
wenzelm@64622
   196
              else if (kind1 == "") quote(name)
wenzelm@64622
   197
              else kind1 + " " + quote(name)
wenzelm@64622
   198
            val t = prev.info._1
wenzelm@64622
   199
            val txt2 =
wenzelm@64622
   200
              if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
wenzelm@64622
   201
                "\n" + t.message
wenzelm@64622
   202
              else ""
wenzelm@64622
   203
            Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
wenzelm@64622
   204
wenzelm@64622
   205
          case (prev, Text.Info(r, XML.Elem(Markup.Path(name), _))) =>
wenzelm@64654
   206
            val file = resources.append_file(snapshot.node_name.master_dir, name)
wenzelm@64622
   207
            val text =
wenzelm@64622
   208
              if (name == file) "file " + quote(file)
wenzelm@64622
   209
              else "path " + quote(name) + "\nfile " + quote(file)
wenzelm@64622
   210
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64622
   211
wenzelm@64622
   212
          case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@64622
   213
            val text = "doc " + quote(name)
wenzelm@64622
   214
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64622
   215
wenzelm@64622
   216
          case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
wenzelm@64622
   217
            Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
wenzelm@64622
   218
wenzelm@64622
   219
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@64622
   220
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@64622
   221
            Some(add(prev, r, (true, Rendering.pretty_typing("::", body))))
wenzelm@64622
   222
wenzelm@64622
   223
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
wenzelm@64622
   224
            Some(add(prev, r, (true, Pretty.block(0, body))))
wenzelm@64622
   225
wenzelm@64622
   226
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@64622
   227
            Some(add(prev, r, (false, Rendering.pretty_typing("ML:", body))))
wenzelm@64622
   228
wenzelm@64622
   229
          case (prev, Text.Info(r, Protocol.ML_Breakpoint(breakpoint))) =>
wenzelm@64622
   230
            val text =
wenzelm@64622
   231
              if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
wenzelm@64622
   232
              else "breakpoint (disabled)"
wenzelm@64622
   233
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64622
   234
wenzelm@64622
   235
          case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
wenzelm@64622
   236
            val lang = Word.implode(Word.explode('_', language))
wenzelm@64622
   237
            Some(add(prev, r, (true, XML.Text("language: " + lang))))
wenzelm@64622
   238
wenzelm@64622
   239
          case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
wenzelm@64622
   240
            val descr = if (kind == "") "expression" else "expression: " + kind
wenzelm@64622
   241
            Some(add(prev, r, (true, XML.Text(descr))))
wenzelm@64622
   242
wenzelm@64622
   243
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
wenzelm@64622
   244
            Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
wenzelm@64622
   245
          case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
wenzelm@64622
   246
            Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
wenzelm@64622
   247
wenzelm@64622
   248
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
wenzelm@64622
   249
            Rendering.tooltip_descriptions.get(name).
wenzelm@64622
   250
              map(descr => add(prev, r, (true, XML.Text(descr))))
wenzelm@64622
   251
        }).map(_.info)
wenzelm@64622
   252
wenzelm@64622
   253
    results.map(_.info).flatMap(res => res._2.toList) match {
wenzelm@64622
   254
      case Nil => None
wenzelm@64622
   255
      case tips =>
wenzelm@64622
   256
        val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@64622
   257
        val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@64748
   258
        Some(Text.Info(r, all_tips))
wenzelm@64622
   259
    }
wenzelm@64622
   260
  }
wenzelm@64767
   261
wenzelm@64767
   262
wenzelm@65101
   263
  /* text background */
wenzelm@65101
   264
wenzelm@65101
   265
  def background(range: Text.Range, focus: Set[Long]): List[Text.Info[Rendering.Color.Value]] =
wenzelm@65101
   266
  {
wenzelm@65101
   267
    for {
wenzelm@65101
   268
      Text.Info(r, result) <-
wenzelm@65101
   269
        snapshot.cumulate[(List[Markup], Option[Rendering.Color.Value])](
wenzelm@65101
   270
          range, (List(Markup.Empty), None), Rendering.background_elements,
wenzelm@65101
   271
          command_states =>
wenzelm@65101
   272
            {
wenzelm@65101
   273
              case (((markups, color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@65101
   274
              if markups.nonEmpty && Protocol.proper_status_elements(markup.name) =>
wenzelm@65101
   275
                Some((markup :: markups, color))
wenzelm@65101
   276
              case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
wenzelm@65101
   277
                Some((Nil, Some(Rendering.Color.bad)))
wenzelm@65101
   278
              case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
wenzelm@65101
   279
                Some((Nil, Some(Rendering.Color.intensify)))
wenzelm@65101
   280
              case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@65101
   281
                props match {
wenzelm@65101
   282
                  case Markup.Entity.Def(i) if focus(i) => Some((Nil, Some(Rendering.Color.entity)))
wenzelm@65101
   283
                  case Markup.Entity.Ref(i) if focus(i) => Some((Nil, Some(Rendering.Color.entity)))
wenzelm@65101
   284
                  case _ => None
wenzelm@65101
   285
                }
wenzelm@65101
   286
              case (_, Text.Info(_, XML.Elem(Markup.Markdown_Item(depth), _))) =>
wenzelm@65101
   287
                val color =
wenzelm@65101
   288
                  depth match {
wenzelm@65101
   289
                    case 1 => Rendering.Color.markdown_item1
wenzelm@65101
   290
                    case 2 => Rendering.Color.markdown_item2
wenzelm@65101
   291
                    case 3 => Rendering.Color.markdown_item3
wenzelm@65101
   292
                    case _ => Rendering.Color.markdown_item4
wenzelm@65101
   293
                  }
wenzelm@65101
   294
                Some((Nil, Some(color)))
wenzelm@65101
   295
              case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
wenzelm@65101
   296
                command_states.collectFirst(
wenzelm@65101
   297
                  { case st if st.results.defined(serial) => st.results.get(serial).get }) match
wenzelm@65101
   298
                {
wenzelm@65101
   299
                  case Some(Protocol.Dialog_Result(res)) if res == result =>
wenzelm@65101
   300
                    Some((Nil, Some(Rendering.Color.active_result)))
wenzelm@65101
   301
                  case _ =>
wenzelm@65101
   302
                    Some((Nil, Some(Rendering.Color.active)))
wenzelm@65101
   303
                }
wenzelm@65101
   304
              case (_, Text.Info(_, elem)) =>
wenzelm@65101
   305
                if (Rendering.active_elements(elem.name))
wenzelm@65101
   306
                  Some((Nil, Some(Rendering.Color.active)))
wenzelm@65101
   307
                else None
wenzelm@65101
   308
            })
wenzelm@65101
   309
      color <-
wenzelm@65101
   310
        (result match {
wenzelm@65101
   311
          case (markups, opt_color) if markups.nonEmpty =>
wenzelm@65101
   312
            val status = Protocol.Status.make(markups.iterator)
wenzelm@65101
   313
            if (status.is_unprocessed) Some(Rendering.Color.unprocessed1)
wenzelm@65101
   314
            else if (status.is_running) Some(Rendering.Color.running1)
wenzelm@65101
   315
            else opt_color
wenzelm@65101
   316
          case (_, opt_color) => opt_color
wenzelm@65101
   317
        })
wenzelm@65101
   318
    } yield Text.Info(r, color)
wenzelm@65101
   319
  }
wenzelm@65101
   320
wenzelm@65101
   321
wenzelm@65101
   322
  /* text foreground */
wenzelm@65101
   323
wenzelm@65101
   324
  def foreground(range: Text.Range): List[Text.Info[Rendering.Color.Value]] =
wenzelm@65101
   325
    snapshot.select(range, Rendering.foreground_elements, _ =>
wenzelm@65101
   326
      {
wenzelm@65101
   327
        case Text.Info(_, elem) =>
wenzelm@65101
   328
          if (elem.name == Markup.ANTIQUOTED) Some(Rendering.Color.antiquoted)
wenzelm@65101
   329
          else Some(Rendering.Color.quoted)
wenzelm@65101
   330
      })
wenzelm@65101
   331
wenzelm@65101
   332
wenzelm@64767
   333
  /* caret focus */
wenzelm@64767
   334
wenzelm@64767
   335
  private def entity_focus(range: Text.Range,
wenzelm@64767
   336
    check: (Boolean, Long) => Boolean = (is_def: Boolean, i: Long) => is_def): Set[Long] =
wenzelm@64767
   337
  {
wenzelm@64767
   338
    val results =
wenzelm@64767
   339
      snapshot.cumulate[Set[Long]](range, Set.empty, Rendering.caret_focus_elements, _ =>
wenzelm@64767
   340
          {
wenzelm@64767
   341
            case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64767
   342
              props match {
wenzelm@64767
   343
                case Markup.Entity.Def(i) if check(true, i) => Some(serials + i)
wenzelm@64767
   344
                case Markup.Entity.Ref(i) if check(false, i) => Some(serials + i)
wenzelm@64767
   345
                case _ => None
wenzelm@64767
   346
              }
wenzelm@64767
   347
            case _ => None
wenzelm@64767
   348
          })
wenzelm@64767
   349
    (Set.empty[Long] /: results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
wenzelm@64767
   350
  }
wenzelm@64767
   351
wenzelm@64767
   352
  def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
wenzelm@64767
   353
  {
wenzelm@64767
   354
    val focus_defs = entity_focus(caret_range)
wenzelm@64767
   355
    if (focus_defs.nonEmpty) focus_defs
wenzelm@64767
   356
    else {
wenzelm@64767
   357
      val visible_defs = entity_focus(visible_range)
wenzelm@64767
   358
      entity_focus(caret_range, (is_def: Boolean, i: Long) => !is_def && visible_defs.contains(i))
wenzelm@64767
   359
    }
wenzelm@64767
   360
  }
wenzelm@64767
   361
wenzelm@64767
   362
  def caret_focus_ranges(caret_range: Text.Range, visible_range: Text.Range): List[Text.Range] =
wenzelm@64767
   363
  {
wenzelm@64767
   364
    val focus = caret_focus(caret_range, visible_range)
wenzelm@64767
   365
    if (focus.nonEmpty) {
wenzelm@64767
   366
      val results =
wenzelm@64767
   367
        snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
wenzelm@64767
   368
          {
wenzelm@64767
   369
            case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64767
   370
              props match {
wenzelm@64767
   371
                case Markup.Entity.Def(i) if focus(i) => Some(true)
wenzelm@64767
   372
                case Markup.Entity.Ref(i) if focus(i) => Some(true)
wenzelm@64767
   373
                case _ => None
wenzelm@64767
   374
              }
wenzelm@64767
   375
          })
wenzelm@64767
   376
      for (info <- results if info.info) yield info.range
wenzelm@64767
   377
    }
wenzelm@64767
   378
    else Nil
wenzelm@64767
   379
  }
wenzelm@65121
   380
wenzelm@65121
   381
wenzelm@65121
   382
  /* message underline color */
wenzelm@65121
   383
wenzelm@65121
   384
  def message_underline_color(
wenzelm@65121
   385
    elements: Markup.Elements, range: Text.Range): List[Text.Info[Rendering.Color.Value]] =
wenzelm@65121
   386
  {
wenzelm@65121
   387
    val results =
wenzelm@65121
   388
      snapshot.cumulate[Int](range, 0, elements, _ =>
wenzelm@65121
   389
        {
wenzelm@65121
   390
          case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
wenzelm@65121
   391
        })
wenzelm@65121
   392
    for {
wenzelm@65121
   393
      Text.Info(r, pri) <- results
wenzelm@65121
   394
      color <- Rendering.message_underline_color.get(pri)
wenzelm@65121
   395
    } yield Text.Info(r, color)
wenzelm@65121
   396
  }
wenzelm@64622
   397
}