src/Tools/VSCode/src/server.scala
author wenzelm
Mon Jan 09 20:26:59 2017 +0100 (2017-01-09)
changeset 64854 f5aa712e6250
parent 64830 9bc44bef99e6
child 64856 5e9bf964510a
permissions -rw-r--r--
tuned signature;
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@64710
    14
import java.io.{PrintStream, OutputStream, File => JFile}
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@64644
    28
    try {
wenzelm@64644
    29
      var log_file: Option[Path] = None
wenzelm@64682
    30
      var text_length = Text.Length.encoding(default_text_length)
wenzelm@64644
    31
      var dirs: List[Path] = Nil
wenzelm@64644
    32
      var logic = default_logic
wenzelm@64644
    33
      var modes: List[String] = Nil
wenzelm@64644
    34
      var options = Options.init()
wenzelm@64733
    35
      var system_mode = false
wenzelm@64605
    36
wenzelm@64644
    37
      def text_length_choice: String =
wenzelm@64682
    38
        commas(Text.Length.encodings.map(
wenzelm@64644
    39
          { case (a, _) => if (a == default_text_length) a + " (default)" else a }))
wenzelm@64618
    40
wenzelm@64644
    41
      val getopts = Getopts("""
wenzelm@64605
    42
Usage: isabelle vscode_server [OPTIONS]
wenzelm@64605
    43
wenzelm@64605
    44
  Options are:
wenzelm@64605
    45
    -L FILE      enable logging on FILE
wenzelm@64618
    46
    -T LENGTH    text length encoding: """ + text_length_choice + """
wenzelm@64605
    47
    -d DIR       include session directory
wenzelm@64737
    48
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(default_logic) + """)
wenzelm@64605
    49
    -m MODE      add print mode for output
wenzelm@64605
    50
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@64733
    51
    -s           system build mode for session image
wenzelm@64605
    52
wenzelm@64605
    53
  Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout.
wenzelm@64605
    54
""",
wenzelm@64755
    55
        "L:" -> (arg => log_file = Some(Path.explode(File.standard_path(arg)))),
wenzelm@64682
    56
        "T:" -> (arg => Text.Length.encoding(arg)),
wenzelm@64755
    57
        "d:" -> (arg => dirs = dirs ::: List(Path.explode(File.standard_path(arg)))),
wenzelm@64644
    58
        "l:" -> (arg => logic = arg),
wenzelm@64644
    59
        "m:" -> (arg => modes = arg :: modes),
wenzelm@64733
    60
        "o:" -> (arg => options = options + arg),
wenzelm@64733
    61
        "s" -> (_ => system_mode = true))
wenzelm@64605
    62
wenzelm@64644
    63
      val more_args = getopts(args)
wenzelm@64644
    64
      if (more_args.nonEmpty) getopts.usage()
wenzelm@64644
    65
wenzelm@64717
    66
      val log = Logger.make(log_file)
wenzelm@64717
    67
      val channel = new Channel(System.in, System.out, log)
wenzelm@64733
    68
      val server = new Server(channel, options, text_length, logic, dirs, modes, system_mode, log)
wenzelm@64605
    69
wenzelm@64644
    70
      // prevent spurious garbage on the main protocol channel
wenzelm@64644
    71
      val orig_out = System.out
wenzelm@64644
    72
      try {
wenzelm@64644
    73
        System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} }))
wenzelm@64644
    74
        server.start()
wenzelm@64644
    75
      }
wenzelm@64644
    76
      finally { System.setOut(orig_out) }
wenzelm@64605
    77
    }
wenzelm@64644
    78
    catch {
wenzelm@64644
    79
      case exn: Throwable =>
wenzelm@64717
    80
        val channel = new Channel(System.in, System.out, No_Logger)
wenzelm@64644
    81
        channel.error_message(Exn.message(exn))
wenzelm@64644
    82
        throw(exn)
wenzelm@64644
    83
    }
wenzelm@64605
    84
  })
wenzelm@64605
    85
}
wenzelm@64605
    86
wenzelm@64605
    87
class Server(
wenzelm@64605
    88
  channel: Channel,
wenzelm@64605
    89
  options: Options,
wenzelm@64682
    90
  text_length: Text.Length = Text.Length.encoding(Server.default_text_length),
wenzelm@64605
    91
  session_name: String = Server.default_logic,
wenzelm@64605
    92
  session_dirs: List[Path] = Nil,
wenzelm@64717
    93
  modes: List[String] = Nil,
wenzelm@64733
    94
  system_mode: Boolean = false,
wenzelm@64717
    95
  log: Logger = No_Logger)
wenzelm@64605
    96
{
wenzelm@64725
    97
  /* prover session */
wenzelm@64605
    98
wenzelm@64703
    99
  private val session_ = Synchronized(None: Option[Session])
wenzelm@64703
   100
  def session: Session = session_.value getOrElse error("Server inactive")
wenzelm@64623
   101
  def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources]
wenzelm@64605
   102
wenzelm@64651
   103
  def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] =
wenzelm@64649
   104
    for {
wenzelm@64777
   105
      model <- resources.get_model(new JFile(node_pos.name))
wenzelm@64704
   106
      rendering = model.rendering()
wenzelm@64830
   107
      offset <- model.content.doc.offset(node_pos.pos)
wenzelm@64649
   108
    } yield (rendering, offset)
wenzelm@64649
   109
wenzelm@64605
   110
wenzelm@64725
   111
  /* input from client or file-system */
wenzelm@64725
   112
wenzelm@64800
   113
  private val delay_input: Standard_Thread.Delay =
wenzelm@64810
   114
    Standard_Thread.delay_last(options.seconds("vscode_input_delay"), channel.Error_Logger)
wenzelm@64725
   115
    { resources.flush_input(session) }
wenzelm@64725
   116
wenzelm@64800
   117
  private val delay_load: Standard_Thread.Delay =
wenzelm@64810
   118
    Standard_Thread.delay_last(options.seconds("vscode_load_delay"), channel.Error_Logger) {
wenzelm@64800
   119
      val (invoke_input, invoke_load) = resources.resolve_dependencies(session, watcher)
wenzelm@64800
   120
      if (invoke_input) delay_input.invoke()
wenzelm@64800
   121
      if (invoke_load) delay_load.invoke
wenzelm@64800
   122
    }
wenzelm@64727
   123
wenzelm@64727
   124
  private val watcher =
wenzelm@64727
   125
    File_Watcher(sync_documents(_), options.seconds("vscode_load_delay"))
wenzelm@64725
   126
wenzelm@64725
   127
  private def sync_documents(changed: Set[JFile]): Unit =
wenzelm@64725
   128
    if (resources.sync_models(changed)) delay_input.invoke()
wenzelm@64679
   129
wenzelm@64777
   130
  private def update_document(file: JFile, text: String)
wenzelm@64679
   131
  {
wenzelm@64777
   132
    resources.update_model(session, file, text)
wenzelm@64679
   133
    delay_input.invoke()
wenzelm@64679
   134
  }
wenzelm@64679
   135
wenzelm@64777
   136
  private def close_document(file: JFile)
wenzelm@64710
   137
  {
wenzelm@64777
   138
    resources.close_model(file) match {
wenzelm@64710
   139
      case Some(model) =>
wenzelm@64782
   140
        watcher.register_parent(file)
wenzelm@64777
   141
        sync_documents(Set(file))
wenzelm@64710
   142
      case None =>
wenzelm@64710
   143
    }
wenzelm@64710
   144
  }
wenzelm@64710
   145
wenzelm@64679
   146
wenzelm@64679
   147
  /* output to client */
wenzelm@64679
   148
wenzelm@64691
   149
  private val delay_output: Standard_Thread.Delay =
wenzelm@64810
   150
    Standard_Thread.delay_last(options.seconds("vscode_output_delay"), channel.Error_Logger)
wenzelm@64703
   151
    {
wenzelm@64691
   152
      if (session.current_state().stable_tip_version.isEmpty) delay_output.invoke()
wenzelm@64703
   153
      else resources.flush_output(channel)
wenzelm@64679
   154
    }
wenzelm@64679
   155
wenzelm@64725
   156
  private val prover_output =
wenzelm@64725
   157
    Session.Consumer[Session.Commands_Changed](getClass.getName) {
wenzelm@64725
   158
      case changed if changed.nodes.nonEmpty =>
wenzelm@64777
   159
        resources.update_output(changed.nodes.toList.map(resources.node_file(_)))
wenzelm@64725
   160
        delay_output.invoke()
wenzelm@64725
   161
      case _ =>
wenzelm@64725
   162
    }
wenzelm@64710
   163
wenzelm@64725
   164
  private val syslog =
wenzelm@64692
   165
    Session.Consumer[Prover.Message](getClass.getName) {
wenzelm@64692
   166
      case output: Prover.Output if output.is_syslog =>
wenzelm@64692
   167
        channel.log_writeln(XML.content(output.message))
wenzelm@64692
   168
      case _ =>
wenzelm@64692
   169
    }
wenzelm@64692
   170
wenzelm@64692
   171
wenzelm@64605
   172
  /* init and exit */
wenzelm@64605
   173
wenzelm@64641
   174
  def init(id: Protocol.Id)
wenzelm@64605
   175
  {
wenzelm@64641
   176
    def reply(err: String)
wenzelm@64641
   177
    {
wenzelm@64641
   178
      channel.write(Protocol.Initialize.reply(id, err))
wenzelm@64642
   179
      if (err == "")
wenzelm@64642
   180
        channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")")
wenzelm@64642
   181
      else channel.error_message(err)
wenzelm@64641
   182
    }
wenzelm@64641
   183
wenzelm@64643
   184
    val try_session =
wenzelm@64643
   185
      try {
wenzelm@64733
   186
        if (!Build.build(options = options, build_heap = true, no_build = true,
wenzelm@64733
   187
              system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok)
wenzelm@64733
   188
        {
wenzelm@64733
   189
          val start_msg = "Build started for Isabelle/" + session_name + " ..."
wenzelm@64733
   190
          val fail_msg = "Session build failed -- prover process remains inactive!"
wenzelm@64733
   191
wenzelm@64733
   192
          val progress = channel.make_progress(verbose = true)
wenzelm@64733
   193
          progress.echo(start_msg); channel.writeln(start_msg)
wenzelm@64733
   194
wenzelm@64733
   195
          if (!Build.build(options = options, progress = progress, build_heap = true,
wenzelm@64733
   196
              system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok)
wenzelm@64733
   197
          {
wenzelm@64733
   198
            progress.echo(fail_msg); error(fail_msg)
wenzelm@64733
   199
          }
wenzelm@64733
   200
        }
wenzelm@64733
   201
wenzelm@64643
   202
        val resources =
wenzelm@64854
   203
          new VSCode_Resources(options, text_length,
wenzelm@64854
   204
            Build.session_content(options, false, session_dirs, session_name), log)
wenzelm@64854
   205
          {
wenzelm@64727
   206
            override def commit(change: Session.Change): Unit =
wenzelm@64800
   207
              if (change.deps_changed || undefined_blobs(change.version.nodes).nonEmpty)
wenzelm@64800
   208
                delay_load.invoke()
wenzelm@64727
   209
          }
wenzelm@64605
   210
wenzelm@64643
   211
        Some(new Session(resources) {
wenzelm@64643
   212
          override def output_delay = options.seconds("editor_output_delay")
wenzelm@64643
   213
          override def prune_delay = options.seconds("editor_prune_delay")
wenzelm@64643
   214
          override def syslog_limit = options.int("editor_syslog_limit")
wenzelm@64643
   215
          override def reparse_limit = options.int("editor_reparse_limit")
wenzelm@64643
   216
        })
wenzelm@64605
   217
      }
wenzelm@64643
   218
      catch { case ERROR(msg) => reply(msg); None }
wenzelm@64605
   219
wenzelm@64643
   220
    for (session <- try_session) {
wenzelm@64643
   221
      var session_phase: Session.Consumer[Session.Phase] = null
wenzelm@64643
   222
      session_phase =
wenzelm@64643
   223
        Session.Consumer(getClass.getName) {
wenzelm@64643
   224
          case Session.Ready =>
wenzelm@64643
   225
            session.phase_changed -= session_phase
wenzelm@64643
   226
            session.update_options(options)
wenzelm@64643
   227
            reply("")
wenzelm@64643
   228
          case Session.Failed =>
wenzelm@64643
   229
            session.phase_changed -= session_phase
wenzelm@64643
   230
            reply("Prover startup failed")
wenzelm@64643
   231
          case _ =>
wenzelm@64643
   232
        }
wenzelm@64643
   233
      session.phase_changed += session_phase
wenzelm@64605
   234
wenzelm@64725
   235
      session.commands_changed += prover_output
wenzelm@64725
   236
      session.all_messages += syslog
wenzelm@64679
   237
wenzelm@64643
   238
      session.start(receiver =>
wenzelm@64643
   239
        Isabelle_Process(options = options, logic = session_name, dirs = session_dirs,
wenzelm@64643
   240
          modes = modes, receiver = receiver))
wenzelm@64605
   241
wenzelm@64703
   242
      session_.change(_ => Some(session))
wenzelm@64643
   243
    }
wenzelm@64605
   244
  }
wenzelm@64605
   245
wenzelm@64641
   246
  def shutdown(id: Protocol.Id)
wenzelm@64605
   247
  {
wenzelm@64641
   248
    def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err))
wenzelm@64641
   249
wenzelm@64703
   250
    session_.change({
wenzelm@64703
   251
      case Some(session) =>
wenzelm@64703
   252
        var session_phase: Session.Consumer[Session.Phase] = null
wenzelm@64703
   253
        session_phase =
wenzelm@64703
   254
          Session.Consumer(getClass.getName) {
wenzelm@64703
   255
            case Session.Inactive =>
wenzelm@64703
   256
              session.phase_changed -= session_phase
wenzelm@64725
   257
              session.commands_changed -= prover_output
wenzelm@64725
   258
              session.all_messages -= syslog
wenzelm@64703
   259
              reply("")
wenzelm@64703
   260
            case _ =>
wenzelm@64703
   261
          }
wenzelm@64703
   262
        session.phase_changed += session_phase
wenzelm@64703
   263
        session.stop()
wenzelm@64703
   264
        delay_input.revoke()
wenzelm@64703
   265
        delay_output.revoke()
wenzelm@64710
   266
        watcher.shutdown()
wenzelm@64703
   267
        None
wenzelm@64703
   268
      case None =>
wenzelm@64703
   269
        reply("Prover inactive")
wenzelm@64703
   270
        None
wenzelm@64703
   271
    })
wenzelm@64605
   272
  }
wenzelm@64605
   273
wenzelm@64646
   274
  def exit() {
wenzelm@64717
   275
    log("\n")
wenzelm@64703
   276
    sys.exit(if (session_.value.isDefined) 1 else 0)
wenzelm@64646
   277
  }
wenzelm@64605
   278
wenzelm@64605
   279
wenzelm@64622
   280
  /* hover */
wenzelm@64605
   281
wenzelm@64651
   282
  def hover(id: Protocol.Id, node_pos: Line.Node_Position)
wenzelm@64641
   283
  {
wenzelm@64641
   284
    val result =
wenzelm@64641
   285
      for {
wenzelm@64651
   286
        (rendering, offset) <- rendering_offset(node_pos)
wenzelm@64748
   287
        info <- rendering.tooltips(Text.Range(offset, offset + 1))
wenzelm@64641
   288
      } yield {
wenzelm@64830
   289
        val doc = rendering.model.content.doc
wenzelm@64683
   290
        val range = doc.range(info.range)
wenzelm@64748
   291
        val contents =
wenzelm@64748
   292
          info.info.map(tree => Pretty.string_of(List(tree), margin = rendering.tooltip_margin))
wenzelm@64748
   293
        (range, contents)
wenzelm@64641
   294
      }
wenzelm@64641
   295
    channel.write(Protocol.Hover.reply(id, result))
wenzelm@64641
   296
  }
wenzelm@64605
   297
wenzelm@64605
   298
wenzelm@64648
   299
  /* goto definition */
wenzelm@64648
   300
wenzelm@64651
   301
  def goto_definition(id: Protocol.Id, node_pos: Line.Node_Position)
wenzelm@64648
   302
  {
wenzelm@64648
   303
    val result =
wenzelm@64651
   304
      (for ((rendering, offset) <- rendering_offset(node_pos))
wenzelm@64649
   305
        yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil
wenzelm@64648
   306
    channel.write(Protocol.GotoDefinition.reply(id, result))
wenzelm@64648
   307
  }
wenzelm@64648
   308
wenzelm@64648
   309
wenzelm@64767
   310
  /* document highlights */
wenzelm@64767
   311
wenzelm@64767
   312
  def document_highlights(id: Protocol.Id, node_pos: Line.Node_Position)
wenzelm@64767
   313
  {
wenzelm@64767
   314
    val result =
wenzelm@64767
   315
      (for ((rendering, offset) <- rendering_offset(node_pos))
wenzelm@64767
   316
        yield {
wenzelm@64830
   317
          val doc = rendering.model.content.doc
wenzelm@64821
   318
          rendering.caret_focus_ranges(Text.Range(offset, offset + 1), doc.text_range)
wenzelm@64767
   319
            .map(r => Protocol.DocumentHighlight.text(doc.range(r)))
wenzelm@64767
   320
        }) getOrElse Nil
wenzelm@64767
   321
    channel.write(Protocol.DocumentHighlights.reply(id, result))
wenzelm@64767
   322
  }
wenzelm@64767
   323
wenzelm@64767
   324
wenzelm@64605
   325
  /* main loop */
wenzelm@64605
   326
wenzelm@64605
   327
  def start()
wenzelm@64605
   328
  {
wenzelm@64717
   329
    log("Server started " + Date.now())
wenzelm@64605
   330
wenzelm@64605
   331
    def handle(json: JSON.T)
wenzelm@64605
   332
    {
wenzelm@64605
   333
      try {
wenzelm@64605
   334
        json match {
wenzelm@64641
   335
          case Protocol.Initialize(id) => init(id)
wenzelm@64641
   336
          case Protocol.Shutdown(id) => shutdown(id)
wenzelm@64645
   337
          case Protocol.Exit(()) => exit()
wenzelm@64801
   338
          case Protocol.DidOpenTextDocument(file, lang, version, text) =>
wenzelm@64801
   339
            update_document(file, text)
wenzelm@64801
   340
          case Protocol.DidChangeTextDocument(file, version, List(Protocol.TextDocumentContent(text))) =>
wenzelm@64801
   341
            update_document(file, text)
wenzelm@64801
   342
          case Protocol.DidCloseTextDocument(file) =>
wenzelm@64801
   343
            close_document(file)
wenzelm@64651
   344
          case Protocol.Hover(id, node_pos) => hover(id, node_pos)
wenzelm@64651
   345
          case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
wenzelm@64767
   346
          case Protocol.DocumentHighlights(id, node_pos) => document_highlights(id, node_pos)
wenzelm@64717
   347
          case _ => log("### IGNORED")
wenzelm@64605
   348
        }
wenzelm@64605
   349
      }
wenzelm@64687
   350
      catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) }
wenzelm@64605
   351
    }
wenzelm@64605
   352
wenzelm@64605
   353
    @tailrec def loop()
wenzelm@64605
   354
    {
wenzelm@64605
   355
      channel.read() match {
wenzelm@64605
   356
        case Some(json) =>
wenzelm@64605
   357
          json match {
wenzelm@64605
   358
            case bulk: List[_] => bulk.foreach(handle(_))
wenzelm@64605
   359
            case _ => handle(json)
wenzelm@64605
   360
          }
wenzelm@64605
   361
          loop()
wenzelm@64717
   362
        case None => log("### TERMINATE")
wenzelm@64605
   363
      }
wenzelm@64605
   364
    }
wenzelm@64605
   365
    loop()
wenzelm@64605
   366
  }
wenzelm@64605
   367
}