src/Tools/VSCode/src/vscode_rendering.scala
author wenzelm
Tue Mar 07 10:52:04 2017 +0100 (2017-03-07)
changeset 65137 812c35fbffa8
parent 65134 511bf19348d3
child 65140 1191df79aa1c
permissions -rw-r--r--
clarified options;
wenzelm@64622
     1
/*  Title:      Tools/VSCode/src/vscode_rendering.scala
wenzelm@64622
     2
    Author:     Makarius
wenzelm@64622
     3
wenzelm@64622
     4
Isabelle/VSCode-specific implementation of quasi-abstract rendering and
wenzelm@64622
     5
markup interpretation.
wenzelm@64622
     6
*/
wenzelm@64622
     7
wenzelm@64622
     8
package isabelle.vscode
wenzelm@64622
     9
wenzelm@64622
    10
wenzelm@64622
    11
import isabelle._
wenzelm@64622
    12
wenzelm@64778
    13
import java.io.{File => JFile}
wenzelm@64778
    14
wenzelm@64778
    15
wenzelm@64648
    16
object VSCode_Rendering
wenzelm@64648
    17
{
wenzelm@65104
    18
  /* decorations */
wenzelm@65104
    19
wenzelm@65122
    20
  private def color_decorations(prefix: String, types: Set[Rendering.Color.Value],
wenzelm@65104
    21
    colors: List[Text.Info[Rendering.Color.Value]]): List[Document_Model.Decoration] =
wenzelm@65104
    22
  {
wenzelm@65104
    23
    val color_ranges =
wenzelm@65104
    24
      (Map.empty[Rendering.Color.Value, List[Text.Range]] /: colors) {
wenzelm@65104
    25
        case (m, Text.Info(range, c)) => m + (c -> (range :: m.getOrElse(c, Nil)))
wenzelm@65104
    26
      }
wenzelm@65105
    27
    types.toList.map(c =>
wenzelm@65105
    28
      Document_Model.Decoration(prefix + c.toString,
wenzelm@65105
    29
        color_ranges.getOrElse(c, Nil).reverse.map(r => Text.Info(r, Nil: List[XML.Body]))))
wenzelm@65104
    30
  }
wenzelm@65104
    31
wenzelm@65122
    32
  private val dotted_colors =
wenzelm@65134
    33
    Set(Rendering.Color.writeln, Rendering.Color.information, Rendering.Color.warning)
wenzelm@65122
    34
wenzelm@65104
    35
wenzelm@64679
    36
  /* diagnostic messages */
wenzelm@64679
    37
wenzelm@64679
    38
  private val message_severity =
wenzelm@64679
    39
    Map(
wenzelm@64679
    40
      Markup.LEGACY -> Protocol.DiagnosticSeverity.Warning,
wenzelm@65095
    41
      Markup.ERROR -> Protocol.DiagnosticSeverity.Error)
wenzelm@64679
    42
wenzelm@64679
    43
wenzelm@64679
    44
  /* markup elements */
wenzelm@64679
    45
wenzelm@64679
    46
  private val diagnostics_elements =
wenzelm@65134
    47
    Markup.Elements(Markup.LEGACY, Markup.ERROR)
wenzelm@65095
    48
wenzelm@65122
    49
  private val dotted_elements =
wenzelm@65134
    50
    Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING)
wenzelm@65122
    51
wenzelm@65134
    52
  val tooltip_elements =
wenzelm@65134
    53
    Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.BAD) ++
wenzelm@65134
    54
    Rendering.tooltip_elements
wenzelm@64679
    55
wenzelm@64648
    56
  private val hyperlink_elements =
wenzelm@64833
    57
    Markup.Elements(Markup.ENTITY, Markup.PATH, Markup.POSITION, Markup.CITATION)
wenzelm@64648
    58
}
wenzelm@64622
    59
wenzelm@64649
    60
class VSCode_Rendering(
wenzelm@64649
    61
    val model: Document_Model,
wenzelm@64649
    62
    snapshot: Document.Snapshot,
wenzelm@64702
    63
    resources: VSCode_Resources)
wenzelm@64704
    64
  extends Rendering(snapshot, resources.options, resources)
wenzelm@64622
    65
{
wenzelm@64877
    66
  /* completion */
wenzelm@64877
    67
wenzelm@64877
    68
  def completion(range: Text.Range): List[Protocol.CompletionItem] =
wenzelm@64877
    69
    semantic_completion(None, range) match {
wenzelm@64877
    70
      case Some(Text.Info(complete_range, names: Completion.Names)) =>
wenzelm@64877
    71
        model.content.try_get_text(complete_range) match {
wenzelm@64877
    72
          case Some(original) =>
wenzelm@64877
    73
            names.complete(complete_range, Completion.History.empty,
wenzelm@65137
    74
                resources.unicode_symbols, original) match {
wenzelm@64877
    75
              case Some(result) =>
wenzelm@64877
    76
                result.items.map(item =>
wenzelm@64877
    77
                  Protocol.CompletionItem(
wenzelm@64877
    78
                    label = item.replacement,
wenzelm@64877
    79
                    detail = Some(item.description.mkString(" ")),
wenzelm@64877
    80
                    range = Some(model.content.doc.range(item.range))))
wenzelm@64877
    81
              case None => Nil
wenzelm@64877
    82
            }
wenzelm@64877
    83
          case None => Nil
wenzelm@64877
    84
        }
wenzelm@64877
    85
      case _ => Nil
wenzelm@64877
    86
    }
wenzelm@64877
    87
wenzelm@64877
    88
wenzelm@64679
    89
  /* diagnostics */
wenzelm@64679
    90
wenzelm@64679
    91
  def diagnostics: List[Text.Info[Command.Results]] =
wenzelm@64679
    92
    snapshot.cumulate[Command.Results](
wenzelm@64830
    93
      model.content.text_range, Command.Results.empty, VSCode_Rendering.diagnostics_elements, _ =>
wenzelm@64679
    94
      {
wenzelm@64679
    95
        case (res, Text.Info(_, msg @ XML.Elem(Markup(_, Markup.Serial(i)), body)))
wenzelm@64679
    96
        if body.nonEmpty => Some(res + (i -> msg))
wenzelm@64679
    97
wenzelm@64679
    98
        case _ => None
wenzelm@64802
    99
      }).filterNot(info => info.info.is_empty)
wenzelm@64679
   100
wenzelm@64679
   101
  def diagnostics_output(results: List[Text.Info[Command.Results]]): List[Protocol.Diagnostic] =
wenzelm@64679
   102
  {
wenzelm@64679
   103
    (for {
wenzelm@64679
   104
      Text.Info(text_range, res) <- results.iterator
wenzelm@64830
   105
      range = model.content.doc.range(text_range)
wenzelm@64679
   106
      (_, XML.Elem(Markup(name, _), body)) <- res.iterator
wenzelm@64679
   107
    } yield {
wenzelm@65107
   108
      val message = resources.output_pretty_message(body)
wenzelm@64679
   109
      val severity = VSCode_Rendering.message_severity.get(name)
wenzelm@64679
   110
      Protocol.Diagnostic(range, message, severity = severity)
wenzelm@64679
   111
    }).toList
wenzelm@64679
   112
  }
wenzelm@64679
   113
wenzelm@64679
   114
wenzelm@65122
   115
  /* dotted underline */
wenzelm@65122
   116
wenzelm@65122
   117
  def dotted(range: Text.Range): List[Text.Info[Rendering.Color.Value]] =
wenzelm@65122
   118
    message_underline_color(VSCode_Rendering.dotted_elements, range)
wenzelm@65122
   119
wenzelm@65122
   120
wenzelm@65095
   121
  /* decorations */
wenzelm@65095
   122
wenzelm@65120
   123
  def decorations: List[Document_Model.Decoration] = // list of canonical length and order
wenzelm@65104
   124
    VSCode_Rendering.color_decorations("background_", Rendering.Color.background,
wenzelm@65104
   125
      background(model.content.text_range, Set.empty)) :::
wenzelm@65104
   126
    VSCode_Rendering.color_decorations("foreground_", Rendering.Color.foreground,
wenzelm@65122
   127
      foreground(model.content.text_range)) :::
wenzelm@65122
   128
    VSCode_Rendering.color_decorations("dotted_", VSCode_Rendering.dotted_colors,
wenzelm@65122
   129
      dotted(model.content.text_range))
wenzelm@65095
   130
wenzelm@65095
   131
  def decoration_output(decoration: Document_Model.Decoration): Protocol.Decoration =
wenzelm@65095
   132
  {
wenzelm@65095
   133
    val content =
wenzelm@65105
   134
      for (Text.Info(text_range, msgs) <- decoration.content)
wenzelm@65095
   135
      yield {
wenzelm@65095
   136
        val range = model.content.doc.range(text_range)
wenzelm@65105
   137
        Protocol.DecorationOptions(range,
wenzelm@65107
   138
          msgs.map(msg => Protocol.MarkedString(resources.output_pretty_tooltip(msg))))
wenzelm@65095
   139
      }
wenzelm@65095
   140
    Protocol.Decoration(decoration.typ, content)
wenzelm@65095
   141
  }
wenzelm@65095
   142
wenzelm@65095
   143
wenzelm@64622
   144
  /* tooltips */
wenzelm@64622
   145
wenzelm@64622
   146
  def timing_threshold: Double = options.real("vscode_timing_threshold")
wenzelm@64648
   147
wenzelm@64648
   148
wenzelm@64648
   149
  /* hyperlinks */
wenzelm@64648
   150
wenzelm@64667
   151
  def hyperlink_source_file(source_name: String, line1: Int, range: Symbol.Range)
wenzelm@64667
   152
    : Option[Line.Node_Range] =
wenzelm@64667
   153
  {
wenzelm@64775
   154
    for {
wenzelm@64779
   155
      platform_path <- resources.source_file(source_name)
wenzelm@64778
   156
      file <-
wenzelm@64779
   157
        (try { Some(new JFile(platform_path).getCanonicalFile) }
wenzelm@64778
   158
         catch { case ERROR(_) => None })
wenzelm@64775
   159
    }
wenzelm@64667
   160
    yield {
wenzelm@64778
   161
      Line.Node_Range(file.getPath,
wenzelm@64841
   162
        if (range.start > 0) {
wenzelm@64841
   163
          resources.get_file_content(file) match {
wenzelm@64841
   164
            case Some(text) =>
wenzelm@64841
   165
              val chunk = Symbol.Text_Chunk(text)
wenzelm@64841
   166
              val doc = Line.Document(text, resources.text_length)
wenzelm@64841
   167
              doc.range(chunk.decode(range))
wenzelm@64841
   168
            case _ =>
wenzelm@64841
   169
              Line.Range(Line.Position((line1 - 1) max 0))
wenzelm@64841
   170
          }
wenzelm@64841
   171
        }
wenzelm@64841
   172
        else Line.Range(Line.Position((line1 - 1) max 0)))
wenzelm@64667
   173
    }
wenzelm@64667
   174
  }
wenzelm@64667
   175
wenzelm@64667
   176
  def hyperlink_command(id: Document_ID.Generic, range: Symbol.Range): Option[Line.Node_Range] =
wenzelm@64667
   177
  {
wenzelm@64667
   178
    if (snapshot.is_outdated) None
wenzelm@64667
   179
    else
wenzelm@64667
   180
      for {
wenzelm@64667
   181
        start <- snapshot.find_command_position(id, range.start)
wenzelm@64667
   182
        stop <- snapshot.find_command_position(id, range.stop)
wenzelm@64667
   183
      } yield Line.Node_Range(start.name, Line.Range(start.pos, stop.pos))
wenzelm@64667
   184
  }
wenzelm@64667
   185
wenzelm@64667
   186
  def hyperlink_position(pos: Position.T): Option[Line.Node_Range] =
wenzelm@64667
   187
    pos match {
wenzelm@64667
   188
      case Position.Item_File(name, line, range) => hyperlink_source_file(name, line, range)
wenzelm@64667
   189
      case Position.Item_Id(id, range) => hyperlink_command(id, range)
wenzelm@64667
   190
      case _ => None
wenzelm@64667
   191
    }
wenzelm@64667
   192
wenzelm@64667
   193
  def hyperlink_def_position(pos: Position.T): Option[Line.Node_Range] =
wenzelm@64667
   194
    pos match {
wenzelm@64667
   195
      case Position.Item_Def_File(name, line, range) => hyperlink_source_file(name, line, range)
wenzelm@64667
   196
      case Position.Item_Def_Id(id, range) => hyperlink_command(id, range)
wenzelm@64667
   197
      case _ => None
wenzelm@64667
   198
    }
wenzelm@64667
   199
wenzelm@64651
   200
  def hyperlinks(range: Text.Range): List[Line.Node_Range] =
wenzelm@64648
   201
  {
wenzelm@64651
   202
    snapshot.cumulate[List[Line.Node_Range]](
wenzelm@64648
   203
      range, Nil, VSCode_Rendering.hyperlink_elements, _ =>
wenzelm@64648
   204
        {
wenzelm@64648
   205
          case (links, Text.Info(_, XML.Elem(Markup.Path(name), _))) =>
wenzelm@64759
   206
            val file = resources.append_file(snapshot.node_name.master_dir, name)
wenzelm@64654
   207
            Some(Line.Node_Range(file) :: links)
wenzelm@64648
   208
wenzelm@64667
   209
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64667
   210
            hyperlink_def_position(props).map(_ :: links)
wenzelm@64667
   211
wenzelm@64667
   212
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
wenzelm@64667
   213
            hyperlink_position(props).map(_ :: links)
wenzelm@64648
   214
wenzelm@64833
   215
          case (links, Text.Info(info_range, XML.Elem(Markup.Citation(name), _))) =>
wenzelm@64833
   216
            val iterator =
wenzelm@64833
   217
              for {
wenzelm@64833
   218
                Text.Info(entry_range, (entry, model)) <- resources.bibtex_entries_iterator
wenzelm@64833
   219
                if entry == name
wenzelm@64833
   220
              } yield Line.Node_Range(model.node_name.node, model.content.doc.range(entry_range))
wenzelm@64833
   221
            if (iterator.isEmpty) None else Some((links /: iterator)(_ :+ _))
wenzelm@64833
   222
wenzelm@64648
   223
          case _ => None
wenzelm@64648
   224
        }) match { case Text.Info(_, links) :: _ => links.reverse case _ => Nil }
wenzelm@64648
   225
  }
wenzelm@64622
   226
}