src/Pure/PIDE/rendering.scala
author wenzelm
Fri Mar 10 21:47:48 2017 +0100 (2017-03-10)
changeset 65176 908d8be90533
parent 65150 fa299b4e50c3
child 65190 0dd2ad9dbfc2
permissions -rw-r--r--
suppress irrelevant markup for VSCode;
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@65143
    29
    val background_colors = values
wenzelm@65104
    30
wenzelm@65104
    31
    // foreground
wenzelm@65104
    32
    val quoted = Value("quoted")
wenzelm@65104
    33
    val antiquoted = Value("antiquoted")
wenzelm@65143
    34
    val foreground_colors = values -- background_colors
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@65143
    42
    val message_underline_colors = values -- background_colors -- foreground_colors
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@65143
    51
    val message_background_colors =
wenzelm@65143
    52
      values -- background_colors -- foreground_colors -- message_underline_colors
wenzelm@65144
    53
wenzelm@65144
    54
    // text
wenzelm@65145
    55
    val main = Value("main")
wenzelm@65144
    56
    val keyword1 = Value("keyword1")
wenzelm@65144
    57
    val keyword2 = Value("keyword2")
wenzelm@65144
    58
    val keyword3 = Value("keyword3")
wenzelm@65144
    59
    val quasi_keyword = Value("quasi_keyword")
wenzelm@65144
    60
    val improper = Value("improper")
wenzelm@65144
    61
    val operator = Value("operator")
wenzelm@65144
    62
    val tfree = Value("tfree")
wenzelm@65144
    63
    val tvar = Value("tvar")
wenzelm@65144
    64
    val free = Value("free")
wenzelm@65144
    65
    val skolem = Value("skolem")
wenzelm@65144
    66
    val bound = Value("bound")
wenzelm@65144
    67
    val var_ = Value("var")
wenzelm@65144
    68
    val inner_numeral = Value("inner_numeral")
wenzelm@65144
    69
    val inner_quoted = Value("inner_quoted")
wenzelm@65144
    70
    val inner_cartouche = Value("inner_cartouche")
wenzelm@65144
    71
    val inner_comment = Value("inner_comment")
wenzelm@65144
    72
    val dynamic = Value("dynamic")
wenzelm@65144
    73
    val class_parameter = Value("class_parameter")
wenzelm@65144
    74
    val antiquote = Value("antiquote")
wenzelm@65144
    75
    val text_colors =
wenzelm@65144
    76
      values -- background_colors -- foreground_colors -- message_underline_colors --
wenzelm@65144
    77
      message_background_colors
wenzelm@65101
    78
  }
wenzelm@65101
    79
wenzelm@65101
    80
wenzelm@64676
    81
  /* message priorities */
wenzelm@64676
    82
wenzelm@64676
    83
  val state_pri = 1
wenzelm@64676
    84
  val writeln_pri = 2
wenzelm@64676
    85
  val information_pri = 3
wenzelm@64676
    86
  val tracing_pri = 4
wenzelm@64676
    87
  val warning_pri = 5
wenzelm@64676
    88
  val legacy_pri = 6
wenzelm@64676
    89
  val error_pri = 7
wenzelm@64676
    90
wenzelm@64676
    91
  val message_pri = Map(
wenzelm@64676
    92
    Markup.STATE -> state_pri,
wenzelm@64676
    93
    Markup.STATE_MESSAGE -> state_pri,
wenzelm@64676
    94
    Markup.WRITELN -> writeln_pri,
wenzelm@64676
    95
    Markup.WRITELN_MESSAGE -> writeln_pri,
wenzelm@64676
    96
    Markup.INFORMATION -> information_pri,
wenzelm@64676
    97
    Markup.INFORMATION_MESSAGE -> information_pri,
wenzelm@64676
    98
    Markup.TRACING -> tracing_pri,
wenzelm@64676
    99
    Markup.TRACING_MESSAGE -> tracing_pri,
wenzelm@64676
   100
    Markup.WARNING -> warning_pri,
wenzelm@64676
   101
    Markup.WARNING_MESSAGE -> warning_pri,
wenzelm@64676
   102
    Markup.LEGACY -> legacy_pri,
wenzelm@64676
   103
    Markup.LEGACY_MESSAGE -> legacy_pri,
wenzelm@64676
   104
    Markup.ERROR -> error_pri,
wenzelm@65133
   105
    Markup.ERROR_MESSAGE -> error_pri
wenzelm@65133
   106
  ).withDefaultValue(0)
wenzelm@64676
   107
wenzelm@65121
   108
  val message_underline_color = Map(
wenzelm@65121
   109
    writeln_pri -> Color.writeln,
wenzelm@65121
   110
    information_pri -> Color.information,
wenzelm@65121
   111
    warning_pri -> Color.warning,
wenzelm@65121
   112
    legacy_pri -> Color.legacy,
wenzelm@65121
   113
    error_pri -> Color.error)
wenzelm@65121
   114
wenzelm@65124
   115
  val message_background_color = Map(
wenzelm@65124
   116
    writeln_pri -> Color.writeln_message,
wenzelm@65124
   117
    information_pri -> Color.information_message,
wenzelm@65124
   118
    tracing_pri -> Color.tracing_message,
wenzelm@65124
   119
    warning_pri -> Color.warning_message,
wenzelm@65124
   120
    legacy_pri -> Color.legacy_message,
wenzelm@65124
   121
    error_pri -> Color.error_message)
wenzelm@65124
   122
wenzelm@64676
   123
wenzelm@65144
   124
  /* text color */
wenzelm@65144
   125
wenzelm@65144
   126
  val text_color = Map(
wenzelm@65144
   127
    Markup.KEYWORD1 -> Color.keyword1,
wenzelm@65144
   128
    Markup.KEYWORD2 -> Color.keyword2,
wenzelm@65144
   129
    Markup.KEYWORD3 -> Color.keyword3,
wenzelm@65144
   130
    Markup.QUASI_KEYWORD -> Color.quasi_keyword,
wenzelm@65144
   131
    Markup.IMPROPER -> Color.improper,
wenzelm@65144
   132
    Markup.OPERATOR -> Color.operator,
wenzelm@65145
   133
    Markup.STRING -> Color.main,
wenzelm@65145
   134
    Markup.ALT_STRING -> Color.main,
wenzelm@65145
   135
    Markup.VERBATIM -> Color.main,
wenzelm@65145
   136
    Markup.CARTOUCHE -> Color.main,
wenzelm@65144
   137
    Markup.LITERAL -> Color.keyword1,
wenzelm@65145
   138
    Markup.DELIMITER -> Color.main,
wenzelm@65144
   139
    Markup.TFREE -> Color.tfree,
wenzelm@65144
   140
    Markup.TVAR -> Color.tvar,
wenzelm@65144
   141
    Markup.FREE -> Color.free,
wenzelm@65144
   142
    Markup.SKOLEM -> Color.skolem,
wenzelm@65144
   143
    Markup.BOUND -> Color.bound,
wenzelm@65144
   144
    Markup.VAR -> Color.var_,
wenzelm@65144
   145
    Markup.INNER_STRING -> Color.inner_quoted,
wenzelm@65144
   146
    Markup.INNER_CARTOUCHE -> Color.inner_cartouche,
wenzelm@65144
   147
    Markup.INNER_COMMENT -> Color.inner_comment,
wenzelm@65144
   148
    Markup.DYNAMIC_FACT -> Color.dynamic,
wenzelm@65144
   149
    Markup.CLASS_PARAMETER -> Color.class_parameter,
wenzelm@65144
   150
    Markup.ANTIQUOTE -> Color.antiquote,
wenzelm@65144
   151
    Markup.ML_KEYWORD1 -> Color.keyword1,
wenzelm@65144
   152
    Markup.ML_KEYWORD2 -> Color.keyword2,
wenzelm@65144
   153
    Markup.ML_KEYWORD3 -> Color.keyword3,
wenzelm@65145
   154
    Markup.ML_DELIMITER -> Color.main,
wenzelm@65144
   155
    Markup.ML_NUMERAL -> Color.inner_numeral,
wenzelm@65144
   156
    Markup.ML_CHAR -> Color.inner_quoted,
wenzelm@65144
   157
    Markup.ML_STRING -> Color.inner_quoted,
wenzelm@65144
   158
    Markup.ML_COMMENT -> Color.inner_comment,
wenzelm@65144
   159
    Markup.SML_STRING -> Color.inner_quoted,
wenzelm@65144
   160
    Markup.SML_COMMENT -> Color.inner_comment)
wenzelm@65144
   161
wenzelm@65144
   162
wenzelm@65149
   163
  /* tooltips */
wenzelm@65101
   164
wenzelm@65149
   165
  val tooltip_descriptions =
wenzelm@64622
   166
    Map(
wenzelm@64622
   167
      Markup.CITATION -> "citation",
wenzelm@64622
   168
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@64622
   169
      Markup.FREE -> "free variable",
wenzelm@64622
   170
      Markup.SKOLEM -> "skolem variable",
wenzelm@64622
   171
      Markup.BOUND -> "bound variable",
wenzelm@64622
   172
      Markup.VAR -> "schematic variable",
wenzelm@64622
   173
      Markup.TFREE -> "free type variable",
wenzelm@64622
   174
      Markup.TVAR -> "schematic type variable")
wenzelm@64622
   175
wenzelm@65149
   176
wenzelm@65149
   177
  /* markup elements */
wenzelm@65149
   178
wenzelm@65149
   179
  val active_elements =
wenzelm@65149
   180
    Markup.Elements(Markup.DIALOG, Markup.BROWSER, Markup.GRAPHVIEW,
wenzelm@65149
   181
      Markup.SENDBACK, Markup.JEDIT_ACTION, Markup.SIMP_TRACE_PANEL)
wenzelm@65149
   182
wenzelm@65149
   183
  val background_elements =
wenzelm@65149
   184
    Protocol.proper_status_elements + Markup.WRITELN_MESSAGE +
wenzelm@65149
   185
      Markup.STATE_MESSAGE + Markup.INFORMATION_MESSAGE +
wenzelm@65149
   186
      Markup.TRACING_MESSAGE + Markup.WARNING_MESSAGE +
wenzelm@65149
   187
      Markup.LEGACY_MESSAGE + Markup.ERROR_MESSAGE +
wenzelm@65149
   188
      Markup.BAD + Markup.INTENSIFY + Markup.ENTITY +
wenzelm@65149
   189
      Markup.Markdown_Item.name ++ active_elements
wenzelm@65149
   190
wenzelm@65149
   191
  val foreground_elements =
wenzelm@65149
   192
    Markup.Elements(Markup.STRING, Markup.ALT_STRING, Markup.VERBATIM,
wenzelm@65149
   193
      Markup.CARTOUCHE, Markup.ANTIQUOTED)
wenzelm@65149
   194
wenzelm@65149
   195
  val semantic_completion_elements =
wenzelm@65149
   196
    Markup.Elements(Markup.COMPLETION, Markup.NO_COMPLETION)
wenzelm@65149
   197
wenzelm@65129
   198
  val tooltip_elements =
wenzelm@64622
   199
    Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
wenzelm@64622
   200
      Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
wenzelm@64622
   201
      Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
wenzelm@64622
   202
      Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
wenzelm@64622
   203
wenzelm@65129
   204
  val tooltip_message_elements =
wenzelm@65129
   205
    Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR,
wenzelm@65129
   206
      Markup.BAD)
wenzelm@65129
   207
wenzelm@64767
   208
  val caret_focus_elements = Markup.Elements(Markup.ENTITY)
wenzelm@65144
   209
wenzelm@65144
   210
  val text_color_elements = Markup.Elements(text_color.keySet)
wenzelm@64622
   211
}
wenzelm@64622
   212
wenzelm@64622
   213
abstract class Rendering(
wenzelm@64622
   214
  val snapshot: Document.Snapshot,
wenzelm@64622
   215
  val options: Options,
wenzelm@64622
   216
  val resources: Resources)
wenzelm@64622
   217
{
wenzelm@64622
   218
  override def toString: String = "Rendering(" + snapshot.toString + ")"
wenzelm@64622
   219
wenzelm@64622
   220
wenzelm@64877
   221
  /* completion */
wenzelm@64877
   222
wenzelm@64877
   223
  def semantic_completion(completed_range: Option[Text.Range], range: Text.Range)
wenzelm@64877
   224
      : Option[Text.Info[Completion.Semantic]] =
wenzelm@64877
   225
    if (snapshot.is_outdated) None
wenzelm@64877
   226
    else {
wenzelm@64877
   227
      snapshot.select(range, Rendering.semantic_completion_elements, _ =>
wenzelm@64877
   228
        {
wenzelm@64877
   229
          case Completion.Semantic.Info(info) =>
wenzelm@64877
   230
            completed_range match {
wenzelm@64877
   231
              case Some(range0) if range0.contains(info.range) && range0 != info.range => None
wenzelm@64877
   232
              case _ => Some(info)
wenzelm@64877
   233
            }
wenzelm@64877
   234
          case _ => None
wenzelm@64877
   235
        }).headOption.map(_.info)
wenzelm@64877
   236
    }
wenzelm@64877
   237
wenzelm@64877
   238
wenzelm@65139
   239
  /* spell checker */
wenzelm@65139
   240
wenzelm@65139
   241
  private lazy val spell_checker_elements =
wenzelm@65139
   242
    Markup.Elements(space_explode(',', options.string("spell_checker_elements")): _*)
wenzelm@65139
   243
wenzelm@65139
   244
  def spell_checker_ranges(range: Text.Range): List[Text.Range] =
wenzelm@65139
   245
    snapshot.select(range, spell_checker_elements, _ => _ => Some(())).map(_.range)
wenzelm@65139
   246
wenzelm@65139
   247
  def spell_checker_point(range: Text.Range): Option[Text.Range] =
wenzelm@65139
   248
    snapshot.select(range, spell_checker_elements, _ =>
wenzelm@65139
   249
      {
wenzelm@65139
   250
        case info => Some(snapshot.convert(info.range))
wenzelm@65139
   251
      }).headOption.map(_.info)
wenzelm@65139
   252
wenzelm@65139
   253
wenzelm@65101
   254
  /* text background */
wenzelm@65101
   255
wenzelm@65176
   256
  def background(elements: Markup.Elements, range: Text.Range, focus: Set[Long])
wenzelm@65176
   257
    : List[Text.Info[Rendering.Color.Value]] =
wenzelm@65101
   258
  {
wenzelm@65101
   259
    for {
wenzelm@65101
   260
      Text.Info(r, result) <-
wenzelm@65101
   261
        snapshot.cumulate[(List[Markup], Option[Rendering.Color.Value])](
wenzelm@65101
   262
          range, (List(Markup.Empty), None), Rendering.background_elements,
wenzelm@65101
   263
          command_states =>
wenzelm@65101
   264
            {
wenzelm@65101
   265
              case (((markups, color), Text.Info(_, XML.Elem(markup, _))))
wenzelm@65101
   266
              if markups.nonEmpty && Protocol.proper_status_elements(markup.name) =>
wenzelm@65101
   267
                Some((markup :: markups, color))
wenzelm@65101
   268
              case (_, Text.Info(_, XML.Elem(Markup(Markup.BAD, _), _))) =>
wenzelm@65101
   269
                Some((Nil, Some(Rendering.Color.bad)))
wenzelm@65101
   270
              case (_, Text.Info(_, XML.Elem(Markup(Markup.INTENSIFY, _), _))) =>
wenzelm@65101
   271
                Some((Nil, Some(Rendering.Color.intensify)))
wenzelm@65101
   272
              case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@65101
   273
                props match {
wenzelm@65101
   274
                  case Markup.Entity.Def(i) if focus(i) => Some((Nil, Some(Rendering.Color.entity)))
wenzelm@65101
   275
                  case Markup.Entity.Ref(i) if focus(i) => Some((Nil, Some(Rendering.Color.entity)))
wenzelm@65101
   276
                  case _ => None
wenzelm@65101
   277
                }
wenzelm@65101
   278
              case (_, Text.Info(_, XML.Elem(Markup.Markdown_Item(depth), _))) =>
wenzelm@65101
   279
                val color =
wenzelm@65150
   280
                  depth % 4 match {
wenzelm@65101
   281
                    case 1 => Rendering.Color.markdown_item1
wenzelm@65101
   282
                    case 2 => Rendering.Color.markdown_item2
wenzelm@65101
   283
                    case 3 => Rendering.Color.markdown_item3
wenzelm@65101
   284
                    case _ => Rendering.Color.markdown_item4
wenzelm@65101
   285
                  }
wenzelm@65101
   286
                Some((Nil, Some(color)))
wenzelm@65101
   287
              case (acc, Text.Info(_, Protocol.Dialog(_, serial, result))) =>
wenzelm@65101
   288
                command_states.collectFirst(
wenzelm@65101
   289
                  { case st if st.results.defined(serial) => st.results.get(serial).get }) match
wenzelm@65101
   290
                {
wenzelm@65101
   291
                  case Some(Protocol.Dialog_Result(res)) if res == result =>
wenzelm@65101
   292
                    Some((Nil, Some(Rendering.Color.active_result)))
wenzelm@65101
   293
                  case _ =>
wenzelm@65101
   294
                    Some((Nil, Some(Rendering.Color.active)))
wenzelm@65101
   295
                }
wenzelm@65101
   296
              case (_, Text.Info(_, elem)) =>
wenzelm@65101
   297
                if (Rendering.active_elements(elem.name))
wenzelm@65101
   298
                  Some((Nil, Some(Rendering.Color.active)))
wenzelm@65101
   299
                else None
wenzelm@65101
   300
            })
wenzelm@65101
   301
      color <-
wenzelm@65101
   302
        (result match {
wenzelm@65101
   303
          case (markups, opt_color) if markups.nonEmpty =>
wenzelm@65101
   304
            val status = Protocol.Status.make(markups.iterator)
wenzelm@65101
   305
            if (status.is_unprocessed) Some(Rendering.Color.unprocessed1)
wenzelm@65101
   306
            else if (status.is_running) Some(Rendering.Color.running1)
wenzelm@65101
   307
            else opt_color
wenzelm@65101
   308
          case (_, opt_color) => opt_color
wenzelm@65101
   309
        })
wenzelm@65101
   310
    } yield Text.Info(r, color)
wenzelm@65101
   311
  }
wenzelm@65101
   312
wenzelm@65101
   313
wenzelm@65101
   314
  /* text foreground */
wenzelm@65101
   315
wenzelm@65101
   316
  def foreground(range: Text.Range): List[Text.Info[Rendering.Color.Value]] =
wenzelm@65101
   317
    snapshot.select(range, Rendering.foreground_elements, _ =>
wenzelm@65101
   318
      {
wenzelm@65101
   319
        case Text.Info(_, elem) =>
wenzelm@65101
   320
          if (elem.name == Markup.ANTIQUOTED) Some(Rendering.Color.antiquoted)
wenzelm@65101
   321
          else Some(Rendering.Color.quoted)
wenzelm@65101
   322
      })
wenzelm@65101
   323
wenzelm@65101
   324
wenzelm@64767
   325
  /* caret focus */
wenzelm@64767
   326
wenzelm@64767
   327
  private def entity_focus(range: Text.Range,
wenzelm@64767
   328
    check: (Boolean, Long) => Boolean = (is_def: Boolean, i: Long) => is_def): Set[Long] =
wenzelm@64767
   329
  {
wenzelm@64767
   330
    val results =
wenzelm@64767
   331
      snapshot.cumulate[Set[Long]](range, Set.empty, Rendering.caret_focus_elements, _ =>
wenzelm@64767
   332
          {
wenzelm@64767
   333
            case (serials, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64767
   334
              props match {
wenzelm@64767
   335
                case Markup.Entity.Def(i) if check(true, i) => Some(serials + i)
wenzelm@64767
   336
                case Markup.Entity.Ref(i) if check(false, i) => Some(serials + i)
wenzelm@64767
   337
                case _ => None
wenzelm@64767
   338
              }
wenzelm@64767
   339
            case _ => None
wenzelm@64767
   340
          })
wenzelm@64767
   341
    (Set.empty[Long] /: results){ case (s1, Text.Info(_, s2)) => s1 ++ s2 }
wenzelm@64767
   342
  }
wenzelm@64767
   343
wenzelm@64767
   344
  def caret_focus(caret_range: Text.Range, visible_range: Text.Range): Set[Long] =
wenzelm@64767
   345
  {
wenzelm@64767
   346
    val focus_defs = entity_focus(caret_range)
wenzelm@64767
   347
    if (focus_defs.nonEmpty) focus_defs
wenzelm@64767
   348
    else {
wenzelm@64767
   349
      val visible_defs = entity_focus(visible_range)
wenzelm@64767
   350
      entity_focus(caret_range, (is_def: Boolean, i: Long) => !is_def && visible_defs.contains(i))
wenzelm@64767
   351
    }
wenzelm@64767
   352
  }
wenzelm@64767
   353
wenzelm@64767
   354
  def caret_focus_ranges(caret_range: Text.Range, visible_range: Text.Range): List[Text.Range] =
wenzelm@64767
   355
  {
wenzelm@64767
   356
    val focus = caret_focus(caret_range, visible_range)
wenzelm@64767
   357
    if (focus.nonEmpty) {
wenzelm@64767
   358
      val results =
wenzelm@64767
   359
        snapshot.cumulate[Boolean](visible_range, false, Rendering.caret_focus_elements, _ =>
wenzelm@64767
   360
          {
wenzelm@64767
   361
            case (_, Text.Info(_, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64767
   362
              props match {
wenzelm@64767
   363
                case Markup.Entity.Def(i) if focus(i) => Some(true)
wenzelm@64767
   364
                case Markup.Entity.Ref(i) if focus(i) => Some(true)
wenzelm@64767
   365
                case _ => None
wenzelm@64767
   366
              }
wenzelm@64767
   367
          })
wenzelm@64767
   368
      for (info <- results if info.info) yield info.range
wenzelm@64767
   369
    }
wenzelm@64767
   370
    else Nil
wenzelm@64767
   371
  }
wenzelm@65121
   372
wenzelm@65121
   373
wenzelm@65121
   374
  /* message underline color */
wenzelm@65121
   375
wenzelm@65129
   376
  def message_underline_color(elements: Markup.Elements, range: Text.Range)
wenzelm@65129
   377
    : List[Text.Info[Rendering.Color.Value]] =
wenzelm@65121
   378
  {
wenzelm@65121
   379
    val results =
wenzelm@65121
   380
      snapshot.cumulate[Int](range, 0, elements, _ =>
wenzelm@65121
   381
        {
wenzelm@65121
   382
          case (pri, Text.Info(_, elem)) => Some(pri max Rendering.message_pri(elem.name))
wenzelm@65121
   383
        })
wenzelm@65121
   384
    for {
wenzelm@65121
   385
      Text.Info(r, pri) <- results
wenzelm@65121
   386
      color <- Rendering.message_underline_color.get(pri)
wenzelm@65121
   387
    } yield Text.Info(r, color)
wenzelm@65121
   388
  }
wenzelm@65149
   389
wenzelm@65149
   390
wenzelm@65149
   391
  /* tooltips */
wenzelm@65149
   392
wenzelm@65149
   393
  def timing_threshold: Double
wenzelm@65149
   394
wenzelm@65149
   395
  private sealed case class Tooltip_Info(
wenzelm@65149
   396
    range: Text.Range,
wenzelm@65149
   397
    timing: Timing = Timing.zero,
wenzelm@65149
   398
    messages: List[Command.Results.Entry] = Nil,
wenzelm@65149
   399
    rev_infos: List[(Boolean, XML.Tree)] = Nil)
wenzelm@65149
   400
  {
wenzelm@65149
   401
    def + (t: Timing): Tooltip_Info = copy(timing = timing + t)
wenzelm@65149
   402
    def + (r0: Text.Range, serial: Long, tree: XML.Tree): Tooltip_Info =
wenzelm@65149
   403
    {
wenzelm@65149
   404
      val r = snapshot.convert(r0)
wenzelm@65149
   405
      if (range == r) copy(messages = (serial -> tree) :: messages)
wenzelm@65149
   406
      else copy(range = r, messages = List(serial -> tree))
wenzelm@65149
   407
    }
wenzelm@65149
   408
    def + (r0: Text.Range, important: Boolean, tree: XML.Tree): Tooltip_Info =
wenzelm@65149
   409
    {
wenzelm@65149
   410
      val r = snapshot.convert(r0)
wenzelm@65149
   411
      if (range == r) copy(rev_infos = (important -> tree) :: rev_infos)
wenzelm@65149
   412
      else copy (range = r, rev_infos = List(important -> tree))
wenzelm@65149
   413
    }
wenzelm@65149
   414
    def infos(important: Boolean): List[XML.Tree] =
wenzelm@65149
   415
      rev_infos.filter(p => p._1 == important).reverse.map(_._2)
wenzelm@65149
   416
  }
wenzelm@65149
   417
wenzelm@65149
   418
  def tooltips(elements: Markup.Elements, range: Text.Range): Option[Text.Info[List[XML.Tree]]] =
wenzelm@65149
   419
  {
wenzelm@65149
   420
    val results =
wenzelm@65149
   421
      snapshot.cumulate[Tooltip_Info](range, Tooltip_Info(range), elements, _ =>
wenzelm@65149
   422
        {
wenzelm@65149
   423
          case (info, Text.Info(_, XML.Elem(Markup.Timing(t), _))) => Some(info + t)
wenzelm@65149
   424
wenzelm@65149
   425
          case (info, Text.Info(r0, XML.Elem(Markup(name, props @ Markup.Serial(serial)), body)))
wenzelm@65149
   426
          if Rendering.tooltip_message_elements(name) && body.nonEmpty =>
wenzelm@65149
   427
            Some(info + (r0, serial, XML.Elem(Markup(Markup.message(name), props), body)))
wenzelm@65149
   428
wenzelm@65149
   429
          case (info, Text.Info(r0, XML.Elem(Markup.Entity(kind, name), _)))
wenzelm@65149
   430
          if kind != "" && kind != Markup.ML_DEF =>
wenzelm@65149
   431
            val kind1 = Word.implode(Word.explode('_', kind))
wenzelm@65149
   432
            val txt1 =
wenzelm@65149
   433
              if (name == "") kind1
wenzelm@65149
   434
              else if (kind1 == "") quote(name)
wenzelm@65149
   435
              else kind1 + " " + quote(name)
wenzelm@65149
   436
            val txt2 =
wenzelm@65149
   437
              if (kind == Markup.COMMAND && info.timing.elapsed.seconds >= timing_threshold)
wenzelm@65149
   438
                "\n" + info.timing.message
wenzelm@65149
   439
              else ""
wenzelm@65149
   440
            Some(info + (r0, true, XML.Text(txt1 + txt2)))
wenzelm@65149
   441
wenzelm@65149
   442
          case (info, Text.Info(r0, XML.Elem(Markup.Path(name), _))) =>
wenzelm@65149
   443
            val file = resources.append_file(snapshot.node_name.master_dir, name)
wenzelm@65149
   444
            val text =
wenzelm@65149
   445
              if (name == file) "file " + quote(file)
wenzelm@65149
   446
              else "path " + quote(name) + "\nfile " + quote(file)
wenzelm@65149
   447
            Some(info + (r0, true, XML.Text(text)))
wenzelm@65149
   448
wenzelm@65149
   449
          case (info, Text.Info(r0, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@65149
   450
            val text = "doc " + quote(name)
wenzelm@65149
   451
            Some(info + (r0, true, XML.Text(text)))
wenzelm@65149
   452
wenzelm@65149
   453
          case (info, Text.Info(r0, XML.Elem(Markup.Url(name), _))) =>
wenzelm@65149
   454
            Some(info + (r0, true, XML.Text("URL " + quote(name))))
wenzelm@65149
   455
wenzelm@65149
   456
          case (info, Text.Info(r0, XML.Elem(Markup(name, _), body)))
wenzelm@65149
   457
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@65149
   458
            Some(info + (r0, true, Pretty.block(XML.Text("::") :: Pretty.brk(1) :: body)))
wenzelm@65149
   459
wenzelm@65149
   460
          case (info, Text.Info(r0, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
wenzelm@65149
   461
            Some(info + (r0, true, Pretty.block(0, body)))
wenzelm@65149
   462
wenzelm@65149
   463
          case (info, Text.Info(r0, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@65149
   464
            Some(info + (r0, false, Pretty.block(XML.Text("ML:") :: Pretty.brk(1) :: body)))
wenzelm@65149
   465
wenzelm@65149
   466
          case (info, Text.Info(r0, Protocol.ML_Breakpoint(breakpoint))) =>
wenzelm@65149
   467
            val text =
wenzelm@65149
   468
              if (Debugger.breakpoint_state(breakpoint)) "breakpoint (enabled)"
wenzelm@65149
   469
              else "breakpoint (disabled)"
wenzelm@65149
   470
            Some(info + (r0, true, XML.Text(text)))
wenzelm@65149
   471
wenzelm@65149
   472
          case (info, Text.Info(r0, XML.Elem(Markup.Language(language, _, _, _), _))) =>
wenzelm@65149
   473
            val lang = Word.implode(Word.explode('_', language))
wenzelm@65149
   474
            Some(info + (r0, true, XML.Text("language: " + lang)))
wenzelm@65149
   475
wenzelm@65149
   476
          case (info, Text.Info(r0, XML.Elem(Markup.Expression(kind), _))) =>
wenzelm@65149
   477
            val descr = if (kind == "") "expression" else "expression: " + kind
wenzelm@65149
   478
            Some(info + (r0, true, XML.Text(descr)))
wenzelm@65149
   479
wenzelm@65149
   480
          case (info, Text.Info(r0, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
wenzelm@65149
   481
            Some(info + (r0, true, XML.Text("Markdown: paragraph")))
wenzelm@65149
   482
          case (info, Text.Info(r0, XML.Elem(Markup.Markdown_List(kind), _))) =>
wenzelm@65149
   483
            Some(info + (r0, true, XML.Text("Markdown: " + kind)))
wenzelm@65149
   484
wenzelm@65149
   485
          case (info, Text.Info(r0, XML.Elem(Markup(name, _), _))) =>
wenzelm@65149
   486
            Rendering.tooltip_descriptions.get(name).map(desc => info + (r0, true, XML.Text(desc)))
wenzelm@65149
   487
        }).map(_.info)
wenzelm@65149
   488
wenzelm@65149
   489
    if (results.isEmpty) None
wenzelm@65149
   490
    else {
wenzelm@65149
   491
      val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@65149
   492
      val all_tips =
wenzelm@65149
   493
        Command.Results.make(results.flatMap(_.messages)).iterator.map(_._2).toList :::
wenzelm@65149
   494
        results.flatMap(res => res.infos(true)) :::
wenzelm@65149
   495
        results.flatMap(res => res.infos(false)).lastOption.toList
wenzelm@65149
   496
      if (all_tips.isEmpty) None else Some(Text.Info(r, all_tips))
wenzelm@65149
   497
    }
wenzelm@65149
   498
  }
wenzelm@64622
   499
}