src/Tools/VSCode/src/server.scala
author wenzelm
Tue Dec 20 17:09:40 2016 +0100 (2016-12-20)
changeset 64618 c81bd30839a6
parent 64605 9c1173a7e4cb
child 64622 529bbb8977c7
permissions -rw-r--r--
added option -T: text length encoding;
wenzelm@64605
     1
/*  Title:      Tools/VSCode/src/server.scala
wenzelm@64605
     2
    Author:     Makarius
wenzelm@64605
     3
wenzelm@64605
     4
Server for VS Code Language Server Protocol 2.0, see also
wenzelm@64605
     5
https://github.com/Microsoft/language-server-protocol
wenzelm@64605
     6
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
wenzelm@64605
     7
*/
wenzelm@64605
     8
wenzelm@64605
     9
package isabelle.vscode
wenzelm@64605
    10
wenzelm@64605
    11
wenzelm@64605
    12
import isabelle._
wenzelm@64605
    13
wenzelm@64605
    14
import java.io.{PrintStream, OutputStream}
wenzelm@64605
    15
wenzelm@64605
    16
import scala.annotation.tailrec
wenzelm@64605
    17
wenzelm@64605
    18
wenzelm@64605
    19
object Server
wenzelm@64605
    20
{
wenzelm@64605
    21
  /* Isabelle tool wrapper */
wenzelm@64605
    22
wenzelm@64618
    23
  private val default_text_length = "UTF-16"
wenzelm@64605
    24
  private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC")
wenzelm@64605
    25
wenzelm@64605
    26
  val isabelle_tool = Isabelle_Tool("vscode_server", "VSCode Language Server for PIDE", args =>
wenzelm@64605
    27
  {
wenzelm@64605
    28
    var log_file: Option[Path] = None
wenzelm@64618
    29
    var text_length = Length.encoding(default_text_length)
wenzelm@64605
    30
    var dirs: List[Path] = Nil
wenzelm@64605
    31
    var logic = default_logic
wenzelm@64605
    32
    var modes: List[String] = Nil
wenzelm@64605
    33
    var options = Options.init()
wenzelm@64605
    34
wenzelm@64618
    35
    def text_length_choice: String =
wenzelm@64618
    36
      commas(Length.encodings.map(
wenzelm@64618
    37
        { case (a, _) => if (a == default_text_length) a + " (default)" else a }))
wenzelm@64618
    38
wenzelm@64605
    39
    val getopts = Getopts("""
wenzelm@64605
    40
Usage: isabelle vscode_server [OPTIONS]
wenzelm@64605
    41
wenzelm@64605
    42
  Options are:
wenzelm@64605
    43
    -L FILE      enable logging on FILE
wenzelm@64618
    44
    -T LENGTH    text length encoding: """ + text_length_choice + """
wenzelm@64605
    45
    -d DIR       include session directory
wenzelm@64605
    46
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
wenzelm@64605
    47
    -m MODE      add print mode for output
wenzelm@64605
    48
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@64605
    49
wenzelm@64605
    50
  Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout.
wenzelm@64605
    51
""",
wenzelm@64605
    52
      "L:" -> (arg => log_file = Some(Path.explode(arg))),
wenzelm@64618
    53
      "T:" -> (arg => Length.encoding(arg)),
wenzelm@64605
    54
      "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
wenzelm@64605
    55
      "l:" -> (arg => logic = arg),
wenzelm@64605
    56
      "m:" -> (arg => modes = arg :: modes),
wenzelm@64605
    57
      "o:" -> (arg => options = options + arg))
wenzelm@64605
    58
wenzelm@64605
    59
    val more_args = getopts(args)
wenzelm@64605
    60
    if (more_args.nonEmpty) getopts.usage()
wenzelm@64605
    61
wenzelm@64605
    62
    if (!Build.build(options = options, build_heap = true, no_build = true,
wenzelm@64605
    63
          dirs = dirs, sessions = List(logic)).ok)
wenzelm@64605
    64
      error("Missing logic image " + quote(logic))
wenzelm@64605
    65
wenzelm@64605
    66
    val channel = new Channel(System.in, System.out, log_file)
wenzelm@64618
    67
    val server = new Server(channel, options, text_length, logic, dirs, modes)
wenzelm@64605
    68
wenzelm@64605
    69
    // prevent spurious garbage on the main protocol channel
wenzelm@64605
    70
    val orig_out = System.out
wenzelm@64605
    71
    try {
wenzelm@64605
    72
      System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} }))
wenzelm@64605
    73
      server.start()
wenzelm@64605
    74
    }
wenzelm@64605
    75
    finally { System.setOut(orig_out) }
wenzelm@64605
    76
  })
wenzelm@64605
    77
wenzelm@64605
    78
wenzelm@64605
    79
  /* server state */
wenzelm@64605
    80
wenzelm@64605
    81
  sealed case class State(
wenzelm@64605
    82
    session: Option[Session] = None,
wenzelm@64605
    83
    models: Map[String, Document_Model] = Map.empty)
wenzelm@64605
    84
}
wenzelm@64605
    85
wenzelm@64605
    86
class Server(
wenzelm@64605
    87
  channel: Channel,
wenzelm@64605
    88
  options: Options,
wenzelm@64618
    89
  text_length: Length = Length.encoding(Server.default_text_length),
wenzelm@64605
    90
  session_name: String = Server.default_logic,
wenzelm@64605
    91
  session_dirs: List[Path] = Nil,
wenzelm@64605
    92
  modes: List[String] = Nil)
wenzelm@64605
    93
{
wenzelm@64605
    94
  /* server state */
wenzelm@64605
    95
wenzelm@64605
    96
  private val state = Synchronized(Server.State())
wenzelm@64605
    97
wenzelm@64605
    98
  def session: Session = state.value.session getOrElse error("Session inactive")
wenzelm@64605
    99
  def resources: URI_Resources = session.resources.asInstanceOf[URI_Resources]
wenzelm@64605
   100
wenzelm@64605
   101
wenzelm@64605
   102
  /* init and exit */
wenzelm@64605
   103
wenzelm@64605
   104
  def initialize(reply: String => Unit)
wenzelm@64605
   105
  {
wenzelm@64605
   106
    val content = Build.session_content(options, false, session_dirs, session_name)
wenzelm@64605
   107
    val resources =
wenzelm@64605
   108
      new URI_Resources(content.loaded_theories, content.known_theories, content.syntax)
wenzelm@64605
   109
wenzelm@64605
   110
    val session =
wenzelm@64605
   111
      new Session(resources) {
wenzelm@64605
   112
        override def output_delay = options.seconds("editor_output_delay")
wenzelm@64605
   113
        override def prune_delay = options.seconds("editor_prune_delay")
wenzelm@64605
   114
        override def syslog_limit = options.int("editor_syslog_limit")
wenzelm@64605
   115
        override def reparse_limit = options.int("editor_reparse_limit")
wenzelm@64605
   116
      }
wenzelm@64605
   117
wenzelm@64605
   118
    var session_phase: Session.Consumer[Session.Phase] = null
wenzelm@64605
   119
    session_phase =
wenzelm@64605
   120
      Session.Consumer(getClass.getName) {
wenzelm@64605
   121
        case Session.Ready =>
wenzelm@64605
   122
          session.phase_changed -= session_phase
wenzelm@64605
   123
          session.update_options(options)
wenzelm@64605
   124
          reply("")
wenzelm@64605
   125
        case Session.Failed =>
wenzelm@64605
   126
          session.phase_changed -= session_phase
wenzelm@64605
   127
          reply("Prover startup failed")
wenzelm@64605
   128
        case _ =>
wenzelm@64605
   129
      }
wenzelm@64605
   130
    session.phase_changed += session_phase
wenzelm@64605
   131
wenzelm@64605
   132
    session.start(receiver =>
wenzelm@64605
   133
      Isabelle_Process(options = options, logic = session_name, dirs = session_dirs,
wenzelm@64605
   134
        modes = modes, receiver = receiver))
wenzelm@64605
   135
wenzelm@64605
   136
    state.change(_.copy(session = Some(session)))
wenzelm@64605
   137
  }
wenzelm@64605
   138
wenzelm@64605
   139
  def shutdown(reply: String => Unit)
wenzelm@64605
   140
  {
wenzelm@64605
   141
    state.change(st =>
wenzelm@64605
   142
      st.session match {
wenzelm@64605
   143
        case None => reply("Prover inactive"); st
wenzelm@64605
   144
        case Some(session) =>
wenzelm@64605
   145
          var session_phase: Session.Consumer[Session.Phase] = null
wenzelm@64605
   146
          session_phase =
wenzelm@64605
   147
            Session.Consumer(getClass.getName) {
wenzelm@64605
   148
              case Session.Inactive =>
wenzelm@64605
   149
                session.phase_changed -= session_phase
wenzelm@64605
   150
                reply("")
wenzelm@64605
   151
              case _ =>
wenzelm@64605
   152
            }
wenzelm@64605
   153
          session.phase_changed += session_phase
wenzelm@64605
   154
          session.stop()
wenzelm@64605
   155
          st.copy(session = None)
wenzelm@64605
   156
      })
wenzelm@64605
   157
  }
wenzelm@64605
   158
wenzelm@64605
   159
  def exit() { sys.exit(if (state.value.session.isDefined) 1 else 0) }
wenzelm@64605
   160
wenzelm@64605
   161
wenzelm@64605
   162
  /* document management */
wenzelm@64605
   163
wenzelm@64605
   164
  private val delay_flush =
wenzelm@64605
   165
    Standard_Thread.delay_last(options.seconds("editor_input_delay")) {
wenzelm@64605
   166
      state.change(st =>
wenzelm@64605
   167
        {
wenzelm@64605
   168
          val models = st.models
wenzelm@64605
   169
          val changed = (for { entry <- models.iterator if entry._2.changed } yield entry).toList
wenzelm@64605
   170
          val edits = for { (_, model) <- changed; edit <- model.node_edits } yield edit
wenzelm@64605
   171
          val models1 =
wenzelm@64605
   172
            (models /: changed)({ case (m, (uri, model)) => m + (uri -> model.unchanged) })
wenzelm@64605
   173
wenzelm@64605
   174
          session.update(Document.Blobs.empty, edits)
wenzelm@64605
   175
          st.copy(models = models1)
wenzelm@64605
   176
        })
wenzelm@64605
   177
    }
wenzelm@64605
   178
wenzelm@64605
   179
  def update_document(uri: String, text: String)
wenzelm@64605
   180
  {
wenzelm@64605
   181
    state.change(st =>
wenzelm@64605
   182
      {
wenzelm@64605
   183
        val node_name = resources.node_name(uri)
wenzelm@64605
   184
        val model = Document_Model(session, Line.Document(text), node_name)
wenzelm@64605
   185
        st.copy(models = st.models + (uri -> model))
wenzelm@64605
   186
      })
wenzelm@64605
   187
    delay_flush.invoke()
wenzelm@64605
   188
  }
wenzelm@64605
   189
wenzelm@64605
   190
wenzelm@64605
   191
  /* tooltips -- see $ISABELLE_HOME/src/Tools/jEdit/rendering.scala */
wenzelm@64605
   192
wenzelm@64605
   193
  def hover(uri: String, pos: Line.Position): Option[(Line.Range, List[String])] =
wenzelm@64605
   194
    for {
wenzelm@64605
   195
      model <- state.value.models.get(uri)
wenzelm@64605
   196
      snapshot = model.snapshot()
wenzelm@64618
   197
      offset <- model.doc.offset(pos, text_length)
wenzelm@64605
   198
      info <- tooltip(snapshot, Text.Range(offset, offset + 1))
wenzelm@64605
   199
    } yield {
wenzelm@64618
   200
      val start = model.doc.position(info.range.start, text_length)
wenzelm@64618
   201
      val stop = model.doc.position(info.range.stop, text_length)
wenzelm@64605
   202
      val s = Pretty.string_of(info.info, margin = tooltip_margin.toDouble)
wenzelm@64618
   203
      (Line.Range(start, stop), List("```\n" + s + "\n```"))
wenzelm@64605
   204
    }
wenzelm@64605
   205
wenzelm@64605
   206
  private val tooltip_descriptions =
wenzelm@64605
   207
    Map(
wenzelm@64605
   208
      Markup.CITATION -> "citation",
wenzelm@64605
   209
      Markup.TOKEN_RANGE -> "inner syntax token",
wenzelm@64605
   210
      Markup.FREE -> "free variable",
wenzelm@64605
   211
      Markup.SKOLEM -> "skolem variable",
wenzelm@64605
   212
      Markup.BOUND -> "bound variable",
wenzelm@64605
   213
      Markup.VAR -> "schematic variable",
wenzelm@64605
   214
      Markup.TFREE -> "free type variable",
wenzelm@64605
   215
      Markup.TVAR -> "schematic type variable")
wenzelm@64605
   216
wenzelm@64605
   217
  private val tooltip_elements =
wenzelm@64605
   218
    Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
wenzelm@64605
   219
      Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
wenzelm@64605
   220
      Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
wenzelm@64605
   221
      Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
wenzelm@64605
   222
wenzelm@64605
   223
  private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
wenzelm@64605
   224
    Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
wenzelm@64605
   225
wenzelm@64605
   226
  private def timing_threshold: Double = options.real("jedit_timing_threshold")
wenzelm@64605
   227
wenzelm@64605
   228
  def tooltip(snapshot: Document.Snapshot, range: Text.Range): Option[Text.Info[XML.Body]] =
wenzelm@64605
   229
  {
wenzelm@64605
   230
    def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
wenzelm@64605
   231
      r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
wenzelm@64605
   232
    {
wenzelm@64605
   233
      val r = snapshot.convert(r0)
wenzelm@64605
   234
      val (t, info) = prev.info
wenzelm@64605
   235
      if (prev.range == r)
wenzelm@64605
   236
        Text.Info(r, (t, info :+ p))
wenzelm@64605
   237
      else Text.Info(r, (t, Vector(p)))
wenzelm@64605
   238
    }
wenzelm@64605
   239
wenzelm@64605
   240
    val results =
wenzelm@64605
   241
      snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
wenzelm@64605
   242
        range, Text.Info(range, (Timing.zero, Vector.empty)), tooltip_elements, _ =>
wenzelm@64605
   243
        {
wenzelm@64605
   244
          case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
wenzelm@64605
   245
            Some(Text.Info(r, (t1 + t2, info)))
wenzelm@64605
   246
wenzelm@64605
   247
          case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
wenzelm@64605
   248
          if kind != "" &&
wenzelm@64605
   249
            kind != Markup.ML_DEF &&
wenzelm@64605
   250
            kind != Markup.ML_OPEN &&
wenzelm@64605
   251
            kind != Markup.ML_STRUCTURE =>
wenzelm@64605
   252
            val kind1 = Word.implode(Word.explode('_', kind))
wenzelm@64605
   253
            val txt1 =
wenzelm@64605
   254
              if (name == "") kind1
wenzelm@64605
   255
              else if (kind1 == "") quote(name)
wenzelm@64605
   256
              else kind1 + " " + quote(name)
wenzelm@64605
   257
            val t = prev.info._1
wenzelm@64605
   258
            val txt2 =
wenzelm@64605
   259
              if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
wenzelm@64605
   260
                "\n" + t.message
wenzelm@64605
   261
              else ""
wenzelm@64605
   262
            Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
wenzelm@64605
   263
wenzelm@64605
   264
          case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
wenzelm@64605
   265
            val text = "doc " + quote(name)
wenzelm@64605
   266
            Some(add(prev, r, (true, XML.Text(text))))
wenzelm@64605
   267
wenzelm@64605
   268
          case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
wenzelm@64605
   269
            Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
wenzelm@64605
   270
wenzelm@64605
   271
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
wenzelm@64605
   272
          if name == Markup.SORTING || name == Markup.TYPING =>
wenzelm@64605
   273
            Some(add(prev, r, (true, pretty_typing("::", body))))
wenzelm@64605
   274
wenzelm@64605
   275
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
wenzelm@64605
   276
            Some(add(prev, r, (true, Pretty.block(0, body))))
wenzelm@64605
   277
wenzelm@64605
   278
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
wenzelm@64605
   279
            Some(add(prev, r, (false, pretty_typing("ML:", body))))
wenzelm@64605
   280
wenzelm@64605
   281
          case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
wenzelm@64605
   282
            val lang = Word.implode(Word.explode('_', language))
wenzelm@64605
   283
            Some(add(prev, r, (true, XML.Text("language: " + lang))))
wenzelm@64605
   284
wenzelm@64605
   285
          case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
wenzelm@64605
   286
            val descr = if (kind == "") "expression" else "expression: " + kind
wenzelm@64605
   287
            Some(add(prev, r, (true, XML.Text(descr))))
wenzelm@64605
   288
wenzelm@64605
   289
          case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
wenzelm@64605
   290
            Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
wenzelm@64605
   291
          case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
wenzelm@64605
   292
            Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
wenzelm@64605
   293
wenzelm@64605
   294
          case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
wenzelm@64605
   295
            tooltip_descriptions.get(name).
wenzelm@64605
   296
              map(descr => add(prev, r, (true, XML.Text(descr))))
wenzelm@64605
   297
        }).map(_.info)
wenzelm@64605
   298
wenzelm@64605
   299
    results.map(_.info).flatMap(res => res._2.toList) match {
wenzelm@64605
   300
      case Nil => None
wenzelm@64605
   301
      case tips =>
wenzelm@64605
   302
        val r = Text.Range(results.head.range.start, results.last.range.stop)
wenzelm@64605
   303
        val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
wenzelm@64605
   304
        Some(Text.Info(r, Library.separate(Pretty.fbrk, all_tips)))
wenzelm@64605
   305
    }
wenzelm@64605
   306
  }
wenzelm@64605
   307
wenzelm@64605
   308
  def tooltip_margin: Int = options.int("jedit_tooltip_margin")
wenzelm@64605
   309
wenzelm@64605
   310
wenzelm@64605
   311
  /* main loop */
wenzelm@64605
   312
wenzelm@64605
   313
  def start()
wenzelm@64605
   314
  {
wenzelm@64605
   315
    channel.log("\nServer started " + Date.now())
wenzelm@64605
   316
wenzelm@64605
   317
    def handle(json: JSON.T)
wenzelm@64605
   318
    {
wenzelm@64605
   319
      try {
wenzelm@64605
   320
        json match {
wenzelm@64605
   321
          case Protocol.Initialize(id) =>
wenzelm@64605
   322
            def initialize_reply(err: String)
wenzelm@64605
   323
            {
wenzelm@64605
   324
              channel.write(Protocol.Initialize.reply(id, err))
wenzelm@64605
   325
              if (err == "") {
wenzelm@64605
   326
                channel.write(Protocol.DisplayMessage(Protocol.MessageType.Info,
wenzelm@64605
   327
                  "Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")"))
wenzelm@64605
   328
              }
wenzelm@64605
   329
              else channel.write(Protocol.DisplayMessage(Protocol.MessageType.Error, err))
wenzelm@64605
   330
            }
wenzelm@64605
   331
            initialize(initialize_reply _)
wenzelm@64605
   332
          case Protocol.Shutdown(id) =>
wenzelm@64605
   333
            shutdown((error: String) => channel.write(Protocol.Shutdown.reply(id, error)))
wenzelm@64605
   334
          case Protocol.Exit =>
wenzelm@64605
   335
            exit()
wenzelm@64605
   336
          case Protocol.DidOpenTextDocument(uri, lang, version, text) =>
wenzelm@64605
   337
            update_document(uri, text)
wenzelm@64605
   338
          case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
wenzelm@64605
   339
            update_document(uri, text)
wenzelm@64605
   340
          case Protocol.DidCloseTextDocument(uri) => channel.log("CLOSE " + uri)
wenzelm@64605
   341
          case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri)
wenzelm@64605
   342
          case Protocol.Hover(id, uri, pos) =>
wenzelm@64605
   343
            channel.write(Protocol.Hover.reply(id, hover(uri, pos)))
wenzelm@64605
   344
          case _ => channel.log("### IGNORED")
wenzelm@64605
   345
        }
wenzelm@64605
   346
      }
wenzelm@64605
   347
      catch { case exn: Throwable => channel.log("*** ERROR: " + Exn.message(exn)) }
wenzelm@64605
   348
    }
wenzelm@64605
   349
wenzelm@64605
   350
    @tailrec def loop()
wenzelm@64605
   351
    {
wenzelm@64605
   352
      channel.read() match {
wenzelm@64605
   353
        case Some(json) =>
wenzelm@64605
   354
          json match {
wenzelm@64605
   355
            case bulk: List[_] => bulk.foreach(handle(_))
wenzelm@64605
   356
            case _ => handle(json)
wenzelm@64605
   357
          }
wenzelm@64605
   358
          loop()
wenzelm@64605
   359
        case None => channel.log("### TERMINATE")
wenzelm@64605
   360
      }
wenzelm@64605
   361
    }
wenzelm@64605
   362
    loop()
wenzelm@64605
   363
  }
wenzelm@64605
   364
}