src/Tools/VSCode/src/vscode_rendering.scala
author wenzelm
Wed Jan 04 12:03:45 2017 +0100 (2017-01-04)
changeset 64775 dd3797f1e0d6
parent 64762 cd545bec3fd0
child 64777 ca09695eb43c
permissions -rw-r--r--
clarified file URIs;
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@64762
    19
      Markup.WRITELN -> Protocol.DiagnosticSeverity.Information,
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@64775
    82
    for {
wenzelm@64775
    83
      name <- resources.source_file(source_name)
wenzelm@64775
    84
      if Path.is_wellformed(name)
wenzelm@64775
    85
    }
wenzelm@64667
    86
    yield {
wenzelm@64775
    87
      val file = Path.explode(name).file
wenzelm@64667
    88
      val opt_text =
wenzelm@64775
    89
        try { Some(File.read(file)) } // FIXME content from resources/models
wenzelm@64667
    90
        catch { case ERROR(_) => None }
wenzelm@64775
    91
      Line.Node_Range(Url.print_file(file),
wenzelm@64667
    92
        opt_text match {
wenzelm@64667
    93
          case Some(text) if range.start > 0 =>
wenzelm@64667
    94
            val chunk = Symbol.Text_Chunk(text)
wenzelm@64706
    95
            val doc = Line.Document(text, resources.text_length)
wenzelm@64683
    96
            doc.range(chunk.decode(range))
wenzelm@64667
    97
          case _ =>
wenzelm@64667
    98
            Line.Range(Line.Position((line1 - 1) max 0))
wenzelm@64667
    99
        })
wenzelm@64667
   100
    }
wenzelm@64667
   101
  }
wenzelm@64667
   102
wenzelm@64667
   103
  def hyperlink_command(id: Document_ID.Generic, range: Symbol.Range): Option[Line.Node_Range] =
wenzelm@64667
   104
  {
wenzelm@64667
   105
    if (snapshot.is_outdated) None
wenzelm@64667
   106
    else
wenzelm@64667
   107
      for {
wenzelm@64667
   108
        start <- snapshot.find_command_position(id, range.start)
wenzelm@64667
   109
        stop <- snapshot.find_command_position(id, range.stop)
wenzelm@64667
   110
      } yield Line.Node_Range(start.name, Line.Range(start.pos, stop.pos))
wenzelm@64667
   111
  }
wenzelm@64667
   112
wenzelm@64667
   113
  def hyperlink_position(pos: Position.T): Option[Line.Node_Range] =
wenzelm@64667
   114
    pos match {
wenzelm@64667
   115
      case Position.Item_File(name, line, range) => hyperlink_source_file(name, line, range)
wenzelm@64667
   116
      case Position.Item_Id(id, range) => hyperlink_command(id, range)
wenzelm@64667
   117
      case _ => None
wenzelm@64667
   118
    }
wenzelm@64667
   119
wenzelm@64667
   120
  def hyperlink_def_position(pos: Position.T): Option[Line.Node_Range] =
wenzelm@64667
   121
    pos match {
wenzelm@64667
   122
      case Position.Item_Def_File(name, line, range) => hyperlink_source_file(name, line, range)
wenzelm@64667
   123
      case Position.Item_Def_Id(id, range) => hyperlink_command(id, range)
wenzelm@64667
   124
      case _ => None
wenzelm@64667
   125
    }
wenzelm@64667
   126
wenzelm@64651
   127
  def hyperlinks(range: Text.Range): List[Line.Node_Range] =
wenzelm@64648
   128
  {
wenzelm@64651
   129
    snapshot.cumulate[List[Line.Node_Range]](
wenzelm@64648
   130
      range, Nil, VSCode_Rendering.hyperlink_elements, _ =>
wenzelm@64648
   131
        {
wenzelm@64648
   132
          case (links, Text.Info(_, XML.Elem(Markup.Path(name), _))) =>
wenzelm@64759
   133
            val file = resources.append_file(snapshot.node_name.master_dir, name)
wenzelm@64654
   134
            Some(Line.Node_Range(file) :: links)
wenzelm@64648
   135
wenzelm@64667
   136
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _))) =>
wenzelm@64667
   137
            hyperlink_def_position(props).map(_ :: links)
wenzelm@64667
   138
wenzelm@64667
   139
          case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) =>
wenzelm@64667
   140
            hyperlink_position(props).map(_ :: links)
wenzelm@64648
   141
wenzelm@64648
   142
          case _ => None
wenzelm@64648
   143
        }) match { case Text.Info(_, links) :: _ => links.reverse case _ => Nil }
wenzelm@64648
   144
  }
wenzelm@64622
   145
}