src/Tools/VSCode/src/server.scala
author wenzelm
Sat, 31 Dec 2016 14:20:50 +0100
changeset 64720 8cc2d7c4ada1
parent 64717 d2b50eb3d9ab
child 64722 6df73de0d3c7
permissions -rw-r--r--
proper log;
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
64710
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
    14
import java.io.{PrintStream, OutputStream, File => JFile}
64605
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
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
    67
      val log = Logger.make(log_file)
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
    68
      val channel = new Channel(System.in, System.out, log)
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
    69
      val server = new Server(channel, options, text_length, logic, dirs, modes, log)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    70
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    71
      // prevent spurious garbage on the main protocol channel
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    72
      val orig_out = System.out
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    73
      try {
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    74
        System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} }))
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    75
        server.start()
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    76
      }
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    77
      finally { System.setOut(orig_out) }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    78
    }
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    79
    catch {
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    80
      case exn: Throwable =>
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
    81
        val channel = new Channel(System.in, System.out, No_Logger)
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    82
        channel.error_message(Exn.message(exn))
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    83
        throw(exn)
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    84
    }
64605
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
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    88
class Server(
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    89
  channel: Channel,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    90
  options: Options,
64682
7e119f32276a clarified modules;
wenzelm
parents: 64679
diff changeset
    91
  text_length: Text.Length = Text.Length.encoding(Server.default_text_length),
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    92
  session_name: String = Server.default_logic,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    93
  session_dirs: List[Path] = Nil,
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
    94
  modes: List[String] = Nil,
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
    95
  log: Logger = No_Logger)
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
  /* server session */
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    98
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
    99
  private val session_ = Synchronized(None: Option[Session])
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   100
  def session: Session = session_.value getOrElse error("Server inactive")
64623
83f012ce2567 clarified module name;
wenzelm
parents: 64622
diff changeset
   101
  def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources]
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   102
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   103
  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
   104
    for {
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   105
      model <- resources.get_model(node_pos.name)
64704
08c2d80428ff re-use options from resources;
wenzelm
parents: 64703
diff changeset
   106
      rendering = model.rendering()
64683
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
   107
      offset <- model.doc.offset(node_pos.pos)
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   108
    } yield (rendering, offset)
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   109
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   110
64710
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   111
  /* document content */
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   112
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   113
  private def update_document(uri: String, text: String)
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   114
  {
64709
5e6566ab78bf more explicit edits -- eliminated Clear;
wenzelm
parents: 64708
diff changeset
   115
    resources.update_model(session, uri, text)
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   116
    delay_input.invoke()
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   117
  }
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   118
64710
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   119
  private def close_document(uri: String)
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   120
  {
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   121
    resources.close_model(uri) match {
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   122
      case Some(model) =>
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   123
        model.register(watcher)
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   124
        sync_external(Set(model.file))
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   125
      case None =>
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   126
    }
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   127
  }
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   128
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   129
  private def sync_external(changed: Set[JFile]): Unit =
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   130
    if (resources.sync_external(changed)) delay_input.invoke()
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   131
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   132
  private val watcher = File_Watcher(sync_external(_))
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   133
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   134
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   135
  /* input from client */
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   136
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   137
  private val delay_input =
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   138
    Standard_Thread.delay_last(options.seconds("vscode_input_delay"))
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   139
    { resources.flush_input(session) }
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   140
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   141
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   142
  /* output to client */
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   143
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   144
  private val commands_changed =
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   145
    Session.Consumer[Session.Commands_Changed](getClass.getName) {
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   146
      case changed if changed.nodes.nonEmpty =>
64708
wenzelm
parents: 64707
diff changeset
   147
        resources.update_output(changed.nodes.toList.map(_.node))
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   148
        delay_output.invoke()
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   149
      case _ =>
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   150
    }
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   151
64691
db2b21a52f20 prefer stable state -- reduce repeated diagnostics;
wenzelm
parents: 64690
diff changeset
   152
  private val delay_output: Standard_Thread.Delay =
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   153
    Standard_Thread.delay_last(options.seconds("vscode_output_delay"))
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   154
    {
64691
db2b21a52f20 prefer stable state -- reduce repeated diagnostics;
wenzelm
parents: 64690
diff changeset
   155
      if (session.current_state().stable_tip_version.isEmpty) delay_output.invoke()
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   156
      else resources.flush_output(channel)
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   157
    }
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   158
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   159
64710
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   160
  /* file watcher */
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   161
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   162
64692
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   163
  /* syslog */
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   164
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   165
  private val all_messages =
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   166
    Session.Consumer[Prover.Message](getClass.getName) {
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   167
      case output: Prover.Output if output.is_syslog =>
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   168
        channel.log_writeln(XML.content(output.message))
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   169
      case _ =>
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   170
    }
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   171
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   172
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   173
  /* init and exit */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   174
64641
wenzelm
parents: 64623
diff changeset
   175
  def init(id: Protocol.Id)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   176
  {
64641
wenzelm
parents: 64623
diff changeset
   177
    def reply(err: String)
wenzelm
parents: 64623
diff changeset
   178
    {
wenzelm
parents: 64623
diff changeset
   179
      channel.write(Protocol.Initialize.reply(id, err))
64642
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   180
      if (err == "")
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   181
        channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")")
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   182
      else channel.error_message(err)
64641
wenzelm
parents: 64623
diff changeset
   183
    }
wenzelm
parents: 64623
diff changeset
   184
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   185
    val try_session =
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   186
      try {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   187
        val content = Build.session_content(options, false, session_dirs, session_name)
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   188
        val resources =
64720
8cc2d7c4ada1 proper log;
wenzelm
parents: 64717
diff changeset
   189
          new VSCode_Resources(options, text_length, content.loaded_theories,
8cc2d7c4ada1 proper log;
wenzelm
parents: 64717
diff changeset
   190
            content.known_theories, content.syntax, log)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   191
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   192
        Some(new Session(resources) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   193
          override def output_delay = options.seconds("editor_output_delay")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   194
          override def prune_delay = options.seconds("editor_prune_delay")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   195
          override def syslog_limit = options.int("editor_syslog_limit")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   196
          override def reparse_limit = options.int("editor_reparse_limit")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   197
        })
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   198
      }
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   199
      catch { case ERROR(msg) => reply(msg); None }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   200
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   201
    for (session <- try_session) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   202
      var session_phase: Session.Consumer[Session.Phase] = null
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   203
      session_phase =
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   204
        Session.Consumer(getClass.getName) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   205
          case Session.Ready =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   206
            session.phase_changed -= session_phase
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   207
            session.update_options(options)
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   208
            reply("")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   209
          case Session.Failed =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   210
            session.phase_changed -= session_phase
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   211
            reply("Prover startup failed")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   212
          case _ =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   213
        }
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   214
      session.phase_changed += session_phase
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   215
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   216
      session.commands_changed += commands_changed
64692
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   217
      session.all_messages += all_messages
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   218
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   219
      session.start(receiver =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   220
        Isabelle_Process(options = options, logic = session_name, dirs = session_dirs,
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   221
          modes = modes, receiver = receiver))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   222
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   223
      session_.change(_ => Some(session))
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   224
    }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   225
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   226
64641
wenzelm
parents: 64623
diff changeset
   227
  def shutdown(id: Protocol.Id)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   228
  {
64641
wenzelm
parents: 64623
diff changeset
   229
    def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err))
wenzelm
parents: 64623
diff changeset
   230
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   231
    session_.change({
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   232
      case Some(session) =>
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   233
        var session_phase: Session.Consumer[Session.Phase] = null
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   234
        session_phase =
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   235
          Session.Consumer(getClass.getName) {
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   236
            case Session.Inactive =>
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   237
              session.phase_changed -= session_phase
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   238
              session.commands_changed -= commands_changed
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   239
              session.all_messages -= all_messages
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   240
              reply("")
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   241
            case _ =>
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   242
          }
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   243
        session.phase_changed += session_phase
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   244
        session.stop()
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   245
        delay_input.revoke()
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   246
        delay_output.revoke()
64710
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   247
        watcher.shutdown()
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   248
        None
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   249
      case None =>
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   250
        reply("Prover inactive")
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   251
        None
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   252
    })
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   253
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   254
64646
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   255
  def exit() {
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
   256
    log("\n")
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   257
    sys.exit(if (session_.value.isDefined) 1 else 0)
64646
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   258
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   259
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   260
64622
529bbb8977c7 more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents: 64618
diff changeset
   261
  /* hover */
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   262
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   263
  def hover(id: Protocol.Id, node_pos: Line.Node_Position)
64641
wenzelm
parents: 64623
diff changeset
   264
  {
wenzelm
parents: 64623
diff changeset
   265
    val result =
wenzelm
parents: 64623
diff changeset
   266
      for {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   267
        (rendering, offset) <- rendering_offset(node_pos)
64641
wenzelm
parents: 64623
diff changeset
   268
        info <- rendering.tooltip(Text.Range(offset, offset + 1))
wenzelm
parents: 64623
diff changeset
   269
      } yield {
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   270
        val doc = rendering.model.doc
64683
c0c09b6dfbe0 clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents: 64682
diff changeset
   271
        val range = doc.range(info.range)
64641
wenzelm
parents: 64623
diff changeset
   272
        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
   273
        (range, List("```\n" + s + "\n```"))  // FIXME proper content format
64641
wenzelm
parents: 64623
diff changeset
   274
      }
wenzelm
parents: 64623
diff changeset
   275
    channel.write(Protocol.Hover.reply(id, result))
wenzelm
parents: 64623
diff changeset
   276
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   277
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   278
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   279
  /* goto definition */
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   280
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   281
  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
   282
  {
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   283
    val result =
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   284
      (for ((rendering, offset) <- rendering_offset(node_pos))
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   285
        yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   286
    channel.write(Protocol.GotoDefinition.reply(id, result))
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   287
  }
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   288
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   289
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   290
  /* main loop */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   291
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   292
  def start()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   293
  {
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
   294
    log("Server started " + Date.now())
64605
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
    def handle(json: JSON.T)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   297
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   298
      try {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   299
        json match {
64641
wenzelm
parents: 64623
diff changeset
   300
          case Protocol.Initialize(id) => init(id)
wenzelm
parents: 64623
diff changeset
   301
          case Protocol.Shutdown(id) => shutdown(id)
64645
0b513620d949 proper pattern match;
wenzelm
parents: 64644
diff changeset
   302
          case Protocol.Exit(()) => exit()
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   303
          case Protocol.DidOpenTextDocument(uri, lang, version, text) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   304
            update_document(uri, text)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   305
          case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   306
            update_document(uri, text)
64710
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   307
          case Protocol.DidCloseTextDocument(uri) =>
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   308
            close_document(uri)
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
   309
          case Protocol.DidSaveTextDocument(uri) => log("SAVE " + uri)
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   310
          case Protocol.Hover(id, node_pos) => hover(id, node_pos)
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   311
          case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
   312
          case _ => log("### IGNORED")
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   313
        }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   314
      }
64687
04806ad1e43a clarified protocol errors;
wenzelm
parents: 64684
diff changeset
   315
      catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   316
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   317
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   318
    @tailrec def loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   319
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   320
      channel.read() match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   321
        case Some(json) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   322
          json match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   323
            case bulk: List[_] => bulk.foreach(handle(_))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   324
            case _ => handle(json)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   325
          }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   326
          loop()
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
   327
        case None => log("### TERMINATE")
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   328
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   329
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   330
    loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   331
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   332
}