src/Tools/VSCode/src/vscode_rendering.scala
author wenzelm
Wed Dec 28 16:45:00 2016 +0100 (2016-12-28)
changeset 64679 b2bf280b7e13
parent 64668 39a6c88c059b
child 64683 c0c09b6dfbe0
permissions -rw-r--r--
more uniform treatment of input/output wrt. client;
support for diagnistic messages;
misc tuning;
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@64648
    13
object VSCode_Rendering
wenzelm@64648
    14
{
wenzelm@64679
    15
  /* diagnostic messages */
wenzelm@64679
    16
wenzelm@64679
    17
  private val message_severity =
wenzelm@64679
    18
    Map(
wenzelm@64679
    19
      Markup.WRITELN -> Protocol.DiagnosticSeverity.Hint,
wenzelm@64679
    20
      Markup.INFORMATION -> Protocol.DiagnosticSeverity.Information,
wenzelm@64679
    21
      Markup.WARNING -> Protocol.DiagnosticSeverity.Warning,
wenzelm@64679
    22
      Markup.LEGACY -> Protocol.DiagnosticSeverity.Warning,
wenzelm@64679
    23
      Markup.ERROR -> Protocol.DiagnosticSeverity.Error,
wenzelm@64679
    24
      Markup.BAD -> Protocol.DiagnosticSeverity.Error)
wenzelm@64679
    25
wenzelm@64679
    26
wenzelm@64679
    27
  /* markup elements */
wenzelm@64679
    28
wenzelm@64679
    29
  private val diagnostics_elements =
wenzelm@64679
    30
    Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.LEGACY, Markup.ERROR,
wenzelm@64679
    31
      Markup.BAD)
wenzelm@64679
    32
wenzelm@64648
    33
  private val hyperlink_elements =
wenzelm@64652
    34
    Markup.Elements(Markup.ENTITY, Markup.PATH, Markup.POSITION)
wenzelm@64648
    35
}
wenzelm@64622
    36
wenzelm@64649
    37
class VSCode_Rendering(
wenzelm@64649
    38
    val model: Document_Model,
wenzelm@64649
    39
    snapshot: Document.Snapshot,
wenzelm@64649
    40
    options: Options,
wenzelm@64649
    41
    resources: Resources)
wenzelm@64622
    42
  extends Rendering(snapshot, options, resources)
wenzelm@64622
    43
{
wenzelm@64679
    44
  /* diagnostics */
wenzelm@64679
    45
wenzelm@64679
    46
  def diagnostics: List[Text.Info[Command.Results]] =
wenzelm@64679
    47
    snapshot.cumulate[Command.Results](
wenzelm@64679
    48
      Text.Range.full, Command.Results.empty, VSCode_Rendering.diagnostics_elements, _ =>
wenzelm@64679
    49
      {
wenzelm@64679
    50
        case (res, Text.Info(_, msg @ XML.Elem(Markup(_, Markup.Serial(i)), body)))
wenzelm@64679
    51
        if body.nonEmpty => Some(res + (i -> msg))
wenzelm@64679
    52
wenzelm@64679
    53
        case _ => None
wenzelm@64679
    54
      })
wenzelm@64679
    55
wenzelm@64679
    56
  val diagnostics_margin = options.int("vscode_diagnostics_margin")
wenzelm@64679
    57
wenzelm@64679
    58
  def diagnostics_output(results: List[Text.Info[Command.Results]]): List[Protocol.Diagnostic] =
wenzelm@64679
    59
  {
wenzelm@64679
    60
    (for {
wenzelm@64679
    61
      Text.Info(text_range, res) <- results.iterator
wenzelm@64679
    62
      range = model.doc.range(text_range, model.text_length)
wenzelm@64679
    63
      (_, XML.Elem(Markup(name, _), body)) <- res.iterator
wenzelm@64679
    64
    } yield {
wenzelm@64679
    65
      val message = Pretty.string_of(body, margin = diagnostics_margin)
wenzelm@64679
    66
      val severity = VSCode_Rendering.message_severity.get(name)
wenzelm@64679
    67
      Protocol.Diagnostic(range, message, severity = severity)
wenzelm@64679
    68
    }).toList
wenzelm@64679
    69
  }
wenzelm@64679
    70
wenzelm@64679
    71
wenzelm@64622
    72
  /* tooltips */
wenzelm@64622
    73
wenzelm@64622
    74
  def tooltip_margin: Int = options.int("vscode_tooltip_margin")
wenzelm@64622
    75
  def timing_threshold: Double = options.real("vscode_timing_threshold")
wenzelm@64648
    76
wenzelm@64648
    77
wenzelm@64648
    78
  /* hyperlinks */
wenzelm@64648
    79
wenzelm@64667
    80
  def hyperlink_source_file(source_name: String, line1: Int, range: Symbol.Range)
wenzelm@64667
    81
    : Option[Line.Node_Range] =
wenzelm@64667
    82
  {
wenzelm@64667
    83
    for (name <- resources.source_file(source_name))
wenzelm@64667
    84
    yield {
wenzelm@64667
    85
      val opt_text =
wenzelm@64667
    86
        try { Some(File.read(Path.explode(name))) } // FIXME content from resources/models
wenzelm@64667
    87
        catch { case ERROR(_) => None }
wenzelm@64668
    88
      Line.Node_Range(File.platform_url(name),
wenzelm@64667
    89
        opt_text match {
wenzelm@64667
    90
          case Some(text) if range.start > 0 =>
wenzelm@64667
    91
            val chunk = Symbol.Text_Chunk(text)
wenzelm@64667
    92
            val doc = Line.Document(text)
wenzelm@64679
    93
            def position(offset: Symbol.Offset) =
wenzelm@64679
    94
              doc.position(chunk.decode(offset), model.text_length)
wenzelm@64667
    95
            Line.Range(position(range.start), position(range.stop))
wenzelm@64667
    96
          case _ =>
wenzelm@64667
    97
            Line.Range(Line.Position((line1 - 1) max 0))
wenzelm@64667
    98
        })
wenzelm@64667
    99
    }
wenzelm@64667
   100
  }
wenzelm@64667
   101
wenzelm@64667
   102
  def hyperlink_command(id: Document_ID.Generic, range: Symbol.Range): Option[Line.Node_Range] =
wenzelm@64667
   103
  {
wenzelm@64667
   104
    if (snapshot.is_outdated) None
wenzelm@64667
   105
    else
wenzelm@64667
   106
      for {
wenzelm@64667
   107
        start <- snapshot.find_command_position(id, range.start)
wenzelm@64667
   108
        stop <- snapshot.find_command_position(id, range.stop)
wenzelm@64667
   109
      } yield Line.Node_Range(start.name, Line.Range(start.pos, stop.pos))
wenzelm@64667
   110
  }
wenzelm@64667
   111
wenzelm@64667
   112
  def hyperlink_position(pos: Position.T): Option[Line.Node_Range] =
wenzelm@64667
   113
    pos match {
wenzelm@64667
   114
      case Position.Item_File(name, line, range) => hyperlink_source_file(name, line, range)
wenzelm@64667
   115
      case Position.Item_Id(id, range) => hyperlink_command(id, range)
wenzelm@64667
   116
      case _ => None
wenzelm@64667
   117
    }
wenzelm@64667
   118
wenzelm@64667
   119
  def hyperlink_def_position(pos: Position.T): Option[Line.Node_Range] =
wenzelm@64667
   120
    pos match {
wenzelm@64667
   121
      case Position.Item_Def_File(name, line, range) => hyperlink_source_file(name, line, range)
wenzelm@64667
   122
      case Position.Item_Def_Id(id, range) => hyperlink_command(id, range)
wenzelm@64667
   123
      case _ => None
wenzelm@64667
   124
    }
wenzelm@64667
   125
wenzelm@64651
   126
  def hyperlinks(range: Text.Range): List[Line.Node_Range] =
wenzelm@64648
   127
  {
wenzelm@64651
   128
    snapshot.cumulate[List[Line.Node_Range]](
wenzelm@64648
   129
      range, Nil, VSCode_Rendering.hyperlink_elements, _ =>
wenzelm@64648
   130
        {
wenzelm@64648
   131
          case (links, Text.Info(_, XML.Elem(Markup.Path(name), _))) =>
wenzelm@64654
   132
            val file = resources.append_file_url(snapshot.node_name.master_dir, name)
wenzelm@64654
   133
            Some(Line.Node_Range(file) :: links)
wenzelm@64648
   134
wenzelm@64667
   135
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64667
   136
            hyperlink_def_position(props).map(_ :: links)
wenzelm@64667
   137
wenzelm@64667
   138
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
wenzelm@64667
   139
            hyperlink_position(props).map(_ :: links)
wenzelm@64648
   140
wenzelm@64648
   141
          case _ => None
wenzelm@64648
   142
        }) match { case Text.Info(_, links) :: _ => links.reverse case _ => Nil }
wenzelm@64648
   143
  }
wenzelm@64622
   144
}