src/Tools/VSCode/src/vscode_rendering.scala
author wenzelm
Thu Dec 29 22:10:29 2016 +0100 (2016-12-29)
changeset 64704 08c2d80428ff
parent 64702 d95b9117cb5b
child 64706 3ebf9f8299df
permissions -rw-r--r--
re-use options from resources;
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@64702
    40
    resources: VSCode_Resources)
wenzelm@64704
    41
  extends Rendering(snapshot, resources.options, resources)
wenzelm@64622
    42
{
wenzelm@64679
    43
  /* diagnostics */
wenzelm@64679
    44
wenzelm@64679
    45
  def diagnostics: List[Text.Info[Command.Results]] =
wenzelm@64679
    46
    snapshot.cumulate[Command.Results](
wenzelm@64688
    47
      model.doc.full_range, Command.Results.empty, VSCode_Rendering.diagnostics_elements, _ =>
wenzelm@64679
    48
      {
wenzelm@64679
    49
        case (res, Text.Info(_, msg @ XML.Elem(Markup(_, Markup.Serial(i)), body)))
wenzelm@64679
    50
        if body.nonEmpty => Some(res + (i -> msg))
wenzelm@64679
    51
wenzelm@64679
    52
        case _ => None
wenzelm@64679
    53
      })
wenzelm@64679
    54
wenzelm@64679
    55
  val diagnostics_margin = options.int("vscode_diagnostics_margin")
wenzelm@64679
    56
wenzelm@64679
    57
  def diagnostics_output(results: List[Text.Info[Command.Results]]): List[Protocol.Diagnostic] =
wenzelm@64679
    58
  {
wenzelm@64679
    59
    (for {
wenzelm@64679
    60
      Text.Info(text_range, res) <- results.iterator
wenzelm@64683
    61
      range = model.doc.range(text_range)
wenzelm@64679
    62
      (_, XML.Elem(Markup(name, _), body)) <- res.iterator
wenzelm@64679
    63
    } yield {
wenzelm@64679
    64
      val message = Pretty.string_of(body, margin = diagnostics_margin)
wenzelm@64679
    65
      val severity = VSCode_Rendering.message_severity.get(name)
wenzelm@64679
    66
      Protocol.Diagnostic(range, message, severity = severity)
wenzelm@64679
    67
    }).toList
wenzelm@64679
    68
  }
wenzelm@64679
    69
wenzelm@64679
    70
wenzelm@64622
    71
  /* tooltips */
wenzelm@64622
    72
wenzelm@64622
    73
  def tooltip_margin: Int = options.int("vscode_tooltip_margin")
wenzelm@64622
    74
  def timing_threshold: Double = options.real("vscode_timing_threshold")
wenzelm@64648
    75
wenzelm@64648
    76
wenzelm@64648
    77
  /* hyperlinks */
wenzelm@64648
    78
wenzelm@64667
    79
  def hyperlink_source_file(source_name: String, line1: Int, range: Symbol.Range)
wenzelm@64667
    80
    : Option[Line.Node_Range] =
wenzelm@64667
    81
  {
wenzelm@64667
    82
    for (name <- resources.source_file(source_name))
wenzelm@64667
    83
    yield {
wenzelm@64667
    84
      val opt_text =
wenzelm@64667
    85
        try { Some(File.read(Path.explode(name))) } // FIXME content from resources/models
wenzelm@64667
    86
        catch { case ERROR(_) => None }
wenzelm@64668
    87
      Line.Node_Range(File.platform_url(name),
wenzelm@64667
    88
        opt_text match {
wenzelm@64667
    89
          case Some(text) if range.start > 0 =>
wenzelm@64667
    90
            val chunk = Symbol.Text_Chunk(text)
wenzelm@64683
    91
            val doc = Line.Document(text, model.doc.text_length)
wenzelm@64683
    92
            doc.range(chunk.decode(range))
wenzelm@64667
    93
          case _ =>
wenzelm@64667
    94
            Line.Range(Line.Position((line1 - 1) max 0))
wenzelm@64667
    95
        })
wenzelm@64667
    96
    }
wenzelm@64667
    97
  }
wenzelm@64667
    98
wenzelm@64667
    99
  def hyperlink_command(id: Document_ID.Generic, range: Symbol.Range): Option[Line.Node_Range] =
wenzelm@64667
   100
  {
wenzelm@64667
   101
    if (snapshot.is_outdated) None
wenzelm@64667
   102
    else
wenzelm@64667
   103
      for {
wenzelm@64667
   104
        start <- snapshot.find_command_position(id, range.start)
wenzelm@64667
   105
        stop <- snapshot.find_command_position(id, range.stop)
wenzelm@64667
   106
      } yield Line.Node_Range(start.name, Line.Range(start.pos, stop.pos))
wenzelm@64667
   107
  }
wenzelm@64667
   108
wenzelm@64667
   109
  def hyperlink_position(pos: Position.T): Option[Line.Node_Range] =
wenzelm@64667
   110
    pos match {
wenzelm@64667
   111
      case Position.Item_File(name, line, range) => hyperlink_source_file(name, line, range)
wenzelm@64667
   112
      case Position.Item_Id(id, range) => hyperlink_command(id, range)
wenzelm@64667
   113
      case _ => None
wenzelm@64667
   114
    }
wenzelm@64667
   115
wenzelm@64667
   116
  def hyperlink_def_position(pos: Position.T): Option[Line.Node_Range] =
wenzelm@64667
   117
    pos match {
wenzelm@64667
   118
      case Position.Item_Def_File(name, line, range) => hyperlink_source_file(name, line, range)
wenzelm@64667
   119
      case Position.Item_Def_Id(id, range) => hyperlink_command(id, range)
wenzelm@64667
   120
      case _ => None
wenzelm@64667
   121
    }
wenzelm@64667
   122
wenzelm@64651
   123
  def hyperlinks(range: Text.Range): List[Line.Node_Range] =
wenzelm@64648
   124
  {
wenzelm@64651
   125
    snapshot.cumulate[List[Line.Node_Range]](
wenzelm@64648
   126
      range, Nil, VSCode_Rendering.hyperlink_elements, _ =>
wenzelm@64648
   127
        {
wenzelm@64648
   128
          case (links, Text.Info(_, XML.Elem(Markup.Path(name), _))) =>
wenzelm@64654
   129
            val file = resources.append_file_url(snapshot.node_name.master_dir, name)
wenzelm@64654
   130
            Some(Line.Node_Range(file) :: links)
wenzelm@64648
   131
wenzelm@64667
   132
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64667
   133
            hyperlink_def_position(props).map(_ :: links)
wenzelm@64667
   134
wenzelm@64667
   135
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
wenzelm@64667
   136
            hyperlink_position(props).map(_ :: links)
wenzelm@64648
   137
wenzelm@64648
   138
          case _ => None
wenzelm@64648
   139
        }) match { case Text.Info(_, links) :: _ => links.reverse case _ => Nil }
wenzelm@64648
   140
  }
wenzelm@64622
   141
}