src/Tools/VSCode/src/server.scala
author wenzelm
Wed Dec 28 20:17:01 2016 +0100 (2016-12-28)
changeset 64692 ccf017e2f2b4
parent 64691 db2b21a52f20
child 64702 d95b9117cb5b
permissions -rw-r--r--
support for syslog messages;
clarified shutdown;
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@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@64605
    35
wenzelm@64644
    36
      def text_length_choice: String =
wenzelm@64682
    37
        commas(Text.Length.encodings.map(
wenzelm@64644
    38
          { case (a, _) => if (a == default_text_length) a + " (default)" else a }))
wenzelm@64618
    39
wenzelm@64644
    40
      val getopts = Getopts("""
wenzelm@64605
    41
Usage: isabelle vscode_server [OPTIONS]
wenzelm@64605
    42
wenzelm@64605
    43
  Options are:
wenzelm@64605
    44
    -L FILE      enable logging on FILE
wenzelm@64618
    45
    -T LENGTH    text length encoding: """ + text_length_choice + """
wenzelm@64605
    46
    -d DIR       include session directory
wenzelm@64605
    47
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
wenzelm@64605
    48
    -m MODE      add print mode for output
wenzelm@64605
    49
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@64605
    50
wenzelm@64605
    51
  Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout.
wenzelm@64605
    52
""",
wenzelm@64644
    53
        "L:" -> (arg => log_file = Some(Path.explode(arg))),
wenzelm@64682
    54
        "T:" -> (arg => Text.Length.encoding(arg)),
wenzelm@64644
    55
        "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
wenzelm@64644
    56
        "l:" -> (arg => logic = arg),
wenzelm@64644
    57
        "m:" -> (arg => modes = arg :: modes),
wenzelm@64644
    58
        "o:" -> (arg => options = options + arg))
wenzelm@64605
    59
wenzelm@64644
    60
      val more_args = getopts(args)
wenzelm@64644
    61
      if (more_args.nonEmpty) getopts.usage()
wenzelm@64644
    62
wenzelm@64644
    63
      if (!Build.build(options = options, build_heap = true, no_build = true,
wenzelm@64644
    64
            dirs = dirs, sessions = List(logic)).ok)
wenzelm@64644
    65
        error("Missing logic image " + quote(logic))
wenzelm@64605
    66
wenzelm@64644
    67
      val channel = new Channel(System.in, System.out, log_file)
wenzelm@64644
    68
      val server = new Server(channel, options, text_length, logic, dirs, modes)
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@64644
    80
        val channel = new Channel(System.in, System.out, None)
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
  /* server state */
wenzelm@64605
    88
wenzelm@64605
    89
  sealed case class State(
wenzelm@64605
    90
    session: Option[Session] = None,
wenzelm@64679
    91
    models: Map[String, Document_Model] = Map.empty,
wenzelm@64690
    92
    pending_input: Set[Document.Node.Name] = Set.empty,
wenzelm@64679
    93
    pending_output: Set[Document.Node.Name] = Set.empty)
wenzelm@64605
    94
}
wenzelm@64605
    95
wenzelm@64605
    96
class Server(
wenzelm@64605
    97
  channel: Channel,
wenzelm@64605
    98
  options: Options,
wenzelm@64682
    99
  text_length: Text.Length = Text.Length.encoding(Server.default_text_length),
wenzelm@64605
   100
  session_name: String = Server.default_logic,
wenzelm@64605
   101
  session_dirs: List[Path] = Nil,
wenzelm@64605
   102
  modes: List[String] = Nil)
wenzelm@64605
   103
{
wenzelm@64605
   104
  /* server state */
wenzelm@64605
   105
wenzelm@64605
   106
  private val state = Synchronized(Server.State())
wenzelm@64605
   107
wenzelm@64605
   108
  def session: Session = state.value.session getOrElse error("Session inactive")
wenzelm@64623
   109
  def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources]
wenzelm@64605
   110
wenzelm@64651
   111
  def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] =
wenzelm@64649
   112
    for {
wenzelm@64651
   113
      model <- state.value.models.get(node_pos.name)
wenzelm@64679
   114
      rendering = model.rendering(options)
wenzelm@64683
   115
      offset <- model.doc.offset(node_pos.pos)
wenzelm@64649
   116
    } yield (rendering, offset)
wenzelm@64649
   117
wenzelm@64605
   118
wenzelm@64679
   119
  /* input from client */
wenzelm@64679
   120
wenzelm@64679
   121
  private val delay_input =
wenzelm@64684
   122
    Standard_Thread.delay_last(options.seconds("vscode_input_delay")) {
wenzelm@64679
   123
      state.change(st =>
wenzelm@64679
   124
        {
wenzelm@64690
   125
          val changed =
wenzelm@64690
   126
            (for {
wenzelm@64690
   127
              node_name <- st.pending_input.iterator
wenzelm@64690
   128
              model <- st.models.get(node_name.node)
wenzelm@64690
   129
              if model.changed } yield model).toList
wenzelm@64690
   130
          session.update(Document.Blobs.empty,
wenzelm@64690
   131
            for { model <- changed; edit <- model.node_edits(resources) } yield edit)
wenzelm@64679
   132
          st.copy(
wenzelm@64690
   133
            models = (st.models /: changed) { case (ms, m) => ms + (m.uri -> m.unchanged) },
wenzelm@64690
   134
            pending_input = Set.empty)
wenzelm@64679
   135
        })
wenzelm@64679
   136
    }
wenzelm@64679
   137
wenzelm@64679
   138
  def update_document(uri: String, text: String)
wenzelm@64679
   139
  {
wenzelm@64679
   140
    state.change(st =>
wenzelm@64679
   141
      {
wenzelm@64679
   142
        val node_name = resources.node_name(uri)
wenzelm@64683
   143
        val model = Document_Model(session, Line.Document(text, text_length), node_name)
wenzelm@64690
   144
        st.copy(
wenzelm@64690
   145
          models = st.models + (uri -> model),
wenzelm@64690
   146
          pending_input = st.pending_input + node_name)
wenzelm@64679
   147
      })
wenzelm@64679
   148
    delay_input.invoke()
wenzelm@64679
   149
  }
wenzelm@64679
   150
wenzelm@64679
   151
wenzelm@64679
   152
  /* output to client */
wenzelm@64679
   153
wenzelm@64679
   154
  private val commands_changed =
wenzelm@64679
   155
    Session.Consumer[Session.Commands_Changed](getClass.getName) {
wenzelm@64679
   156
      case changed =>
wenzelm@64679
   157
        state.change(st => st.copy(pending_output = st.pending_output ++ changed.nodes))
wenzelm@64679
   158
        delay_output.invoke()
wenzelm@64679
   159
    }
wenzelm@64679
   160
wenzelm@64691
   161
  private val delay_output: Standard_Thread.Delay =
wenzelm@64684
   162
    Standard_Thread.delay_last(options.seconds("vscode_output_delay")) {
wenzelm@64691
   163
      if (session.current_state().stable_tip_version.isEmpty) delay_output.invoke()
wenzelm@64691
   164
      else {
wenzelm@64691
   165
        state.change(st =>
wenzelm@64691
   166
          {
wenzelm@64691
   167
            val changed_iterator =
wenzelm@64691
   168
              for {
wenzelm@64691
   169
                node_name <- st.pending_output.iterator
wenzelm@64691
   170
                model <- st.models.get(node_name.node)
wenzelm@64691
   171
                rendering = model.rendering(options)
wenzelm@64691
   172
                (diagnostics, model1) <- model.publish_diagnostics(rendering)
wenzelm@64691
   173
              } yield {
wenzelm@64691
   174
                channel.diagnostics(model1.uri, rendering.diagnostics_output(diagnostics))
wenzelm@64691
   175
                model1
wenzelm@64691
   176
              }
wenzelm@64691
   177
            st.copy(
wenzelm@64691
   178
              models = (st.models /: changed_iterator) { case (ms, m) => ms + (m.uri -> m) },
wenzelm@64691
   179
              pending_output = Set.empty)
wenzelm@64691
   180
          }
wenzelm@64691
   181
        )
wenzelm@64691
   182
      }
wenzelm@64679
   183
    }
wenzelm@64679
   184
wenzelm@64679
   185
wenzelm@64692
   186
  /* syslog */
wenzelm@64692
   187
wenzelm@64692
   188
  private val all_messages =
wenzelm@64692
   189
    Session.Consumer[Prover.Message](getClass.getName) {
wenzelm@64692
   190
      case output: Prover.Output if output.is_syslog =>
wenzelm@64692
   191
        channel.log_writeln(XML.content(output.message))
wenzelm@64692
   192
      case _ =>
wenzelm@64692
   193
    }
wenzelm@64692
   194
wenzelm@64692
   195
wenzelm@64605
   196
  /* init and exit */
wenzelm@64605
   197
wenzelm@64641
   198
  def init(id: Protocol.Id)
wenzelm@64605
   199
  {
wenzelm@64641
   200
    def reply(err: String)
wenzelm@64641
   201
    {
wenzelm@64641
   202
      channel.write(Protocol.Initialize.reply(id, err))
wenzelm@64642
   203
      if (err == "")
wenzelm@64642
   204
        channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")")
wenzelm@64642
   205
      else channel.error_message(err)
wenzelm@64641
   206
    }
wenzelm@64641
   207
wenzelm@64643
   208
    val try_session =
wenzelm@64643
   209
      try {
wenzelm@64643
   210
        val content = Build.session_content(options, false, session_dirs, session_name)
wenzelm@64643
   211
        val resources =
wenzelm@64643
   212
          new VSCode_Resources(content.loaded_theories, content.known_theories, content.syntax)
wenzelm@64605
   213
wenzelm@64643
   214
        Some(new Session(resources) {
wenzelm@64643
   215
          override def output_delay = options.seconds("editor_output_delay")
wenzelm@64643
   216
          override def prune_delay = options.seconds("editor_prune_delay")
wenzelm@64643
   217
          override def syslog_limit = options.int("editor_syslog_limit")
wenzelm@64643
   218
          override def reparse_limit = options.int("editor_reparse_limit")
wenzelm@64643
   219
        })
wenzelm@64605
   220
      }
wenzelm@64643
   221
      catch { case ERROR(msg) => reply(msg); None }
wenzelm@64605
   222
wenzelm@64643
   223
    for (session <- try_session) {
wenzelm@64643
   224
      var session_phase: Session.Consumer[Session.Phase] = null
wenzelm@64643
   225
      session_phase =
wenzelm@64643
   226
        Session.Consumer(getClass.getName) {
wenzelm@64643
   227
          case Session.Ready =>
wenzelm@64643
   228
            session.phase_changed -= session_phase
wenzelm@64643
   229
            session.update_options(options)
wenzelm@64643
   230
            reply("")
wenzelm@64643
   231
          case Session.Failed =>
wenzelm@64643
   232
            session.phase_changed -= session_phase
wenzelm@64643
   233
            reply("Prover startup failed")
wenzelm@64643
   234
          case _ =>
wenzelm@64643
   235
        }
wenzelm@64643
   236
      session.phase_changed += session_phase
wenzelm@64605
   237
wenzelm@64679
   238
      session.commands_changed += commands_changed
wenzelm@64692
   239
      session.all_messages += all_messages
wenzelm@64679
   240
wenzelm@64643
   241
      session.start(receiver =>
wenzelm@64643
   242
        Isabelle_Process(options = options, logic = session_name, dirs = session_dirs,
wenzelm@64643
   243
          modes = modes, receiver = receiver))
wenzelm@64605
   244
wenzelm@64643
   245
      state.change(_.copy(session = Some(session)))
wenzelm@64643
   246
    }
wenzelm@64605
   247
  }
wenzelm@64605
   248
wenzelm@64641
   249
  def shutdown(id: Protocol.Id)
wenzelm@64605
   250
  {
wenzelm@64641
   251
    def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err))
wenzelm@64641
   252
wenzelm@64605
   253
    state.change(st =>
wenzelm@64605
   254
      st.session match {
wenzelm@64605
   255
        case None => reply("Prover inactive"); st
wenzelm@64605
   256
        case Some(session) =>
wenzelm@64605
   257
          var session_phase: Session.Consumer[Session.Phase] = null
wenzelm@64605
   258
          session_phase =
wenzelm@64605
   259
            Session.Consumer(getClass.getName) {
wenzelm@64605
   260
              case Session.Inactive =>
wenzelm@64605
   261
                session.phase_changed -= session_phase
wenzelm@64692
   262
                session.commands_changed -= commands_changed
wenzelm@64692
   263
                session.all_messages -= all_messages
wenzelm@64605
   264
                reply("")
wenzelm@64605
   265
              case _ =>
wenzelm@64605
   266
            }
wenzelm@64605
   267
          session.phase_changed += session_phase
wenzelm@64605
   268
          session.stop()
wenzelm@64679
   269
          delay_input.revoke()
wenzelm@64679
   270
          delay_output.revoke()
wenzelm@64605
   271
          st.copy(session = None)
wenzelm@64605
   272
      })
wenzelm@64605
   273
  }
wenzelm@64605
   274
wenzelm@64646
   275
  def exit() {
wenzelm@64646
   276
    channel.log("\n")
wenzelm@64646
   277
    sys.exit(if (state.value.session.isDefined) 1 else 0)
wenzelm@64646
   278
  }
wenzelm@64605
   279
wenzelm@64605
   280
wenzelm@64622
   281
  /* hover */
wenzelm@64605
   282
wenzelm@64651
   283
  def hover(id: Protocol.Id, node_pos: Line.Node_Position)
wenzelm@64641
   284
  {
wenzelm@64641
   285
    val result =
wenzelm@64641
   286
      for {
wenzelm@64651
   287
        (rendering, offset) <- rendering_offset(node_pos)
wenzelm@64641
   288
        info <- rendering.tooltip(Text.Range(offset, offset + 1))
wenzelm@64641
   289
      } yield {
wenzelm@64649
   290
        val doc = rendering.model.doc
wenzelm@64683
   291
        val range = doc.range(info.range)
wenzelm@64641
   292
        val s = Pretty.string_of(info.info, margin = rendering.tooltip_margin)
wenzelm@64679
   293
        (range, List("```\n" + s + "\n```"))  // FIXME proper content format
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@64605
   310
  /* main loop */
wenzelm@64605
   311
wenzelm@64605
   312
  def start()
wenzelm@64605
   313
  {
wenzelm@64646
   314
    channel.log("Server started " + Date.now())
wenzelm@64605
   315
wenzelm@64605
   316
    def handle(json: JSON.T)
wenzelm@64605
   317
    {
wenzelm@64605
   318
      try {
wenzelm@64605
   319
        json match {
wenzelm@64641
   320
          case Protocol.Initialize(id) => init(id)
wenzelm@64641
   321
          case Protocol.Shutdown(id) => shutdown(id)
wenzelm@64645
   322
          case Protocol.Exit(()) => exit()
wenzelm@64605
   323
          case Protocol.DidOpenTextDocument(uri, lang, version, text) =>
wenzelm@64605
   324
            update_document(uri, text)
wenzelm@64605
   325
          case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
wenzelm@64605
   326
            update_document(uri, text)
wenzelm@64605
   327
          case Protocol.DidCloseTextDocument(uri) => channel.log("CLOSE " + uri)
wenzelm@64605
   328
          case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri)
wenzelm@64651
   329
          case Protocol.Hover(id, node_pos) => hover(id, node_pos)
wenzelm@64651
   330
          case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
wenzelm@64605
   331
          case _ => channel.log("### IGNORED")
wenzelm@64605
   332
        }
wenzelm@64605
   333
      }
wenzelm@64687
   334
      catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) }
wenzelm@64605
   335
    }
wenzelm@64605
   336
wenzelm@64605
   337
    @tailrec def loop()
wenzelm@64605
   338
    {
wenzelm@64605
   339
      channel.read() match {
wenzelm@64605
   340
        case Some(json) =>
wenzelm@64605
   341
          json match {
wenzelm@64605
   342
            case bulk: List[_] => bulk.foreach(handle(_))
wenzelm@64605
   343
            case _ => handle(json)
wenzelm@64605
   344
          }
wenzelm@64605
   345
          loop()
wenzelm@64605
   346
        case None => channel.log("### TERMINATE")
wenzelm@64605
   347
      }
wenzelm@64605
   348
    }
wenzelm@64605
   349
    loop()
wenzelm@64605
   350
  }
wenzelm@64605
   351
}