src/Tools/VSCode/src/server.scala
author wenzelm
Fri, 30 Dec 2016 11:54:11 +0100
changeset 64708 dd7f1a7e03f4
parent 64707 7157685b71e3
child 64709 5e6566ab78bf
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/VSCode/src/server.scala
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     3
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     4
Server for VS Code Language Server Protocol 2.0, see also
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     5
https://github.com/Microsoft/language-server-protocol
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     6
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     7
*/
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     8
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     9
package isabelle.vscode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    10
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    11
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    12
import isabelle._
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    13
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    14
import java.io.{PrintStream, OutputStream}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    15
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    16
import scala.annotation.tailrec
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    17
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    18
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    19
object Server
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    20
{
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    21
  /* Isabelle tool wrapper */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    22
64618
c81bd30839a6 added option -T: text length encoding;
wenzelm
parents: 64605
diff changeset
    23
  private val default_text_length = "UTF-16"
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    24
  private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    25
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    26
  val isabelle_tool = Isabelle_Tool("vscode_server", "VSCode Language Server for PIDE", args =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    27
  {
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    28
    try {
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    29
      var log_file: Option[Path] = None
64682
7e119f32276a clarified modules;
wenzelm
parents: 64679
diff changeset
    30
      var text_length = Text.Length.encoding(default_text_length)
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    31
      var dirs: List[Path] = Nil
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    32
      var logic = default_logic
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    33
      var modes: List[String] = Nil
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    34
      var options = Options.init()
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    35
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    36
      def text_length_choice: String =
64682
7e119f32276a clarified modules;
wenzelm
parents: 64679
diff changeset
    37
        commas(Text.Length.encodings.map(
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    38
          { case (a, _) => if (a == default_text_length) a + " (default)" else a }))
64618
c81bd30839a6 added option -T: text length encoding;
wenzelm
parents: 64605
diff changeset
    39
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    40
      val getopts = Getopts("""
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    41
Usage: isabelle vscode_server [OPTIONS]
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    42
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    43
  Options are:
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    44
    -L FILE      enable logging on FILE
64618
c81bd30839a6 added option -T: text length encoding;
wenzelm
parents: 64605
diff changeset
    45
    -T LENGTH    text length encoding: """ + text_length_choice + """
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    46
    -d DIR       include session directory
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    47
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    48
    -m MODE      add print mode for output
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    49
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    50
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    51
  Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout.
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    52
""",
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    53
        "L:" -> (arg => log_file = Some(Path.explode(arg))),
64682
7e119f32276a clarified modules;
wenzelm
parents: 64679
diff changeset
    54
        "T:" -> (arg => Text.Length.encoding(arg)),
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    55
        "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    56
        "l:" -> (arg => logic = arg),
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    57
        "m:" -> (arg => modes = arg :: modes),
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    58
        "o:" -> (arg => options = options + arg))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    59
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    60
      val more_args = getopts(args)
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    61
      if (more_args.nonEmpty) getopts.usage()
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    62
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    63
      if (!Build.build(options = options, build_heap = true, no_build = true,
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    64
            dirs = dirs, sessions = List(logic)).ok)
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    65
        error("Missing logic image " + quote(logic))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    66
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    67
      val channel = new Channel(System.in, System.out, log_file)
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    68
      val server = new Server(channel, options, text_length, logic, dirs, modes)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    69
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    70
      // prevent spurious garbage on the main protocol channel
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    71
      val orig_out = System.out
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    72
      try {
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    73
        System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} }))
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    74
        server.start()
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    75
      }
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    76
      finally { System.setOut(orig_out) }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    77
    }
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    78
    catch {
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    79
      case exn: Throwable =>
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    80
        val channel = new Channel(System.in, System.out, None)
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    81
        channel.error_message(Exn.message(exn))
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    82
        throw(exn)
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    83
    }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    84
  })
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    85
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    86
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    87
class Server(
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    88
  channel: Channel,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    89
  options: Options,
64682
7e119f32276a clarified modules;
wenzelm
parents: 64679
diff changeset
    90
  text_length: Text.Length = Text.Length.encoding(Server.default_text_length),
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    91
  session_name: String = Server.default_logic,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    92
  session_dirs: List[Path] = Nil,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    93
  modes: List[String] = Nil)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    94
{
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
    95
  /* server session */
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    96
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
    97
  private val session_ = Synchronized(None: Option[Session])
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
    98
  def session: Session = session_.value getOrElse error("Server inactive")
64623
83f012ce2567 clarified module name;
wenzelm
parents: 64622
diff changeset
    99
  def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources]
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   100
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   101
  def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] =
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   102
    for {
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   103
      model <- resources.get_model(node_pos.name)
64704
08c2d80428ff re-use options from resources;
wenzelm
parents: 64703
diff changeset
   104
      rendering = model.rendering()
64683
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
   105
      offset <- model.doc.offset(node_pos.pos)
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   106
    } yield (rendering, offset)
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   107
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   108
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   109
  /* input from client */
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   110
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   111
  private val delay_input =
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   112
    Standard_Thread.delay_last(options.seconds("vscode_input_delay"))
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   113
    { resources.flush_input(session) }
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   114
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   115
  private def update_document(uri: String, text: String)
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   116
  {
64707
7157685b71e3 clarified Document_Model perspective and edits;
wenzelm
parents: 64706
diff changeset
   117
    val model = Document_Model.init(session, resources.node_name(uri), text)
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   118
    resources.update_model(model)
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   119
    delay_input.invoke()
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   120
  }
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   121
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   122
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   123
  /* output to client */
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   124
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   125
  private val commands_changed =
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   126
    Session.Consumer[Session.Commands_Changed](getClass.getName) {
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   127
      case changed if changed.nodes.nonEmpty =>
64708
wenzelm
parents: 64707
diff changeset
   128
        resources.update_output(changed.nodes.toList.map(_.node))
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   129
        delay_output.invoke()
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   130
      case _ =>
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   131
    }
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   132
64691
db2b21a52f20 prefer stable state -- reduce repeated diagnostics;
wenzelm
parents: 64690
diff changeset
   133
  private val delay_output: Standard_Thread.Delay =
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   134
    Standard_Thread.delay_last(options.seconds("vscode_output_delay"))
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   135
    {
64691
db2b21a52f20 prefer stable state -- reduce repeated diagnostics;
wenzelm
parents: 64690
diff changeset
   136
      if (session.current_state().stable_tip_version.isEmpty) delay_output.invoke()
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   137
      else resources.flush_output(channel)
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   138
    }
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   139
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   140
64692
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   141
  /* syslog */
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   142
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   143
  private val all_messages =
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   144
    Session.Consumer[Prover.Message](getClass.getName) {
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   145
      case output: Prover.Output if output.is_syslog =>
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   146
        channel.log_writeln(XML.content(output.message))
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   147
      case _ =>
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   148
    }
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   149
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   150
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   151
  /* init and exit */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   152
64641
wenzelm
parents: 64623
diff changeset
   153
  def init(id: Protocol.Id)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   154
  {
64641
wenzelm
parents: 64623
diff changeset
   155
    def reply(err: String)
wenzelm
parents: 64623
diff changeset
   156
    {
wenzelm
parents: 64623
diff changeset
   157
      channel.write(Protocol.Initialize.reply(id, err))
64642
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   158
      if (err == "")
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   159
        channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")")
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   160
      else channel.error_message(err)
64641
wenzelm
parents: 64623
diff changeset
   161
    }
wenzelm
parents: 64623
diff changeset
   162
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   163
    val try_session =
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   164
      try {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   165
        val content = Build.session_content(options, false, session_dirs, session_name)
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   166
        val resources =
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   167
          new VSCode_Resources(
64706
wenzelm
parents: 64704
diff changeset
   168
            options, text_length, content.loaded_theories, content.known_theories, content.syntax)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   169
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   170
        Some(new Session(resources) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   171
          override def output_delay = options.seconds("editor_output_delay")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   172
          override def prune_delay = options.seconds("editor_prune_delay")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   173
          override def syslog_limit = options.int("editor_syslog_limit")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   174
          override def reparse_limit = options.int("editor_reparse_limit")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   175
        })
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   176
      }
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   177
      catch { case ERROR(msg) => reply(msg); None }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   178
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   179
    for (session <- try_session) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   180
      var session_phase: Session.Consumer[Session.Phase] = null
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   181
      session_phase =
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   182
        Session.Consumer(getClass.getName) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   183
          case Session.Ready =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   184
            session.phase_changed -= session_phase
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   185
            session.update_options(options)
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   186
            reply("")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   187
          case Session.Failed =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   188
            session.phase_changed -= session_phase
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   189
            reply("Prover startup failed")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   190
          case _ =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   191
        }
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   192
      session.phase_changed += session_phase
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   193
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   194
      session.commands_changed += commands_changed
64692
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   195
      session.all_messages += all_messages
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   196
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   197
      session.start(receiver =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   198
        Isabelle_Process(options = options, logic = session_name, dirs = session_dirs,
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   199
          modes = modes, receiver = receiver))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   200
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   201
      session_.change(_ => Some(session))
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   202
    }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   203
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   204
64641
wenzelm
parents: 64623
diff changeset
   205
  def shutdown(id: Protocol.Id)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   206
  {
64641
wenzelm
parents: 64623
diff changeset
   207
    def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err))
wenzelm
parents: 64623
diff changeset
   208
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   209
    session_.change({
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   210
      case Some(session) =>
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   211
        var session_phase: Session.Consumer[Session.Phase] = null
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   212
        session_phase =
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   213
          Session.Consumer(getClass.getName) {
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   214
            case Session.Inactive =>
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   215
              session.phase_changed -= session_phase
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   216
              session.commands_changed -= commands_changed
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   217
              session.all_messages -= all_messages
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   218
              reply("")
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   219
            case _ =>
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   220
          }
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   221
        session.phase_changed += session_phase
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   222
        session.stop()
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   223
        delay_input.revoke()
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   224
        delay_output.revoke()
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   225
        None
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   226
      case None =>
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   227
        reply("Prover inactive")
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   228
        None
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   229
    })
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   230
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   231
64646
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   232
  def exit() {
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   233
    channel.log("\n")
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   234
    sys.exit(if (session_.value.isDefined) 1 else 0)
64646
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   235
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   236
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   237
64622
529bbb8977c7 more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents: 64618
diff changeset
   238
  /* hover */
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   239
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   240
  def hover(id: Protocol.Id, node_pos: Line.Node_Position)
64641
wenzelm
parents: 64623
diff changeset
   241
  {
wenzelm
parents: 64623
diff changeset
   242
    val result =
wenzelm
parents: 64623
diff changeset
   243
      for {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   244
        (rendering, offset) <- rendering_offset(node_pos)
64641
wenzelm
parents: 64623
diff changeset
   245
        info <- rendering.tooltip(Text.Range(offset, offset + 1))
wenzelm
parents: 64623
diff changeset
   246
      } yield {
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   247
        val doc = rendering.model.doc
64683
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
   248
        val range = doc.range(info.range)
64641
wenzelm
parents: 64623
diff changeset
   249
        val s = Pretty.string_of(info.info, margin = rendering.tooltip_margin)
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   250
        (range, List("```\n" + s + "\n```"))  // FIXME proper content format
64641
wenzelm
parents: 64623
diff changeset
   251
      }
wenzelm
parents: 64623
diff changeset
   252
    channel.write(Protocol.Hover.reply(id, result))
wenzelm
parents: 64623
diff changeset
   253
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   254
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   255
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   256
  /* goto definition */
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   257
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   258
  def goto_definition(id: Protocol.Id, node_pos: Line.Node_Position)
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   259
  {
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   260
    val result =
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   261
      (for ((rendering, offset) <- rendering_offset(node_pos))
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   262
        yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   263
    channel.write(Protocol.GotoDefinition.reply(id, result))
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   264
  }
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   265
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   266
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   267
  /* main loop */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   268
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   269
  def start()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   270
  {
64646
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   271
    channel.log("Server started " + Date.now())
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   272
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   273
    def handle(json: JSON.T)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   274
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   275
      try {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   276
        json match {
64641
wenzelm
parents: 64623
diff changeset
   277
          case Protocol.Initialize(id) => init(id)
wenzelm
parents: 64623
diff changeset
   278
          case Protocol.Shutdown(id) => shutdown(id)
64645
0b513620d949 proper pattern match;
wenzelm
parents: 64644
diff changeset
   279
          case Protocol.Exit(()) => exit()
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   280
          case Protocol.DidOpenTextDocument(uri, lang, version, text) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   281
            update_document(uri, text)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   282
          case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   283
            update_document(uri, text)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   284
          case Protocol.DidCloseTextDocument(uri) => channel.log("CLOSE " + uri)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   285
          case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri)
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   286
          case Protocol.Hover(id, node_pos) => hover(id, node_pos)
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   287
          case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   288
          case _ => channel.log("### IGNORED")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   289
        }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   290
      }
64687
04806ad1e43a clarified protocol errors;
wenzelm
parents: 64684
diff changeset
   291
      catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   292
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   293
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   294
    @tailrec def loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   295
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   296
      channel.read() match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   297
        case Some(json) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   298
          json match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   299
            case bulk: List[_] => bulk.foreach(handle(_))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   300
            case _ => handle(json)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   301
          }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   302
          loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   303
        case None => channel.log("### TERMINATE")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   304
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   305
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   306
    loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   307
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   308
}