src/Tools/VSCode/src/server.scala
author wenzelm
Thu, 22 Dec 2016 11:08:58 +0100
changeset 64655 ea34f36ff6a5
parent 64651 ea5620f7b0d8
child 64667 cdb0d559a24b
permissions -rw-r--r--
clarified message;
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
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    30
      var text_length = Length.encoding(default_text_length)
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 =
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    37
        commas(Length.encodings.map(
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))),
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    54
        "T:" -> (arg => Length.encoding(arg)),
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
  /* server state */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    88
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    89
  sealed case class State(
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    90
    session: Option[Session] = None,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    91
    models: Map[String, Document_Model] = Map.empty)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    92
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    93
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    94
class Server(
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    95
  channel: Channel,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    96
  options: Options,
64618
c81bd30839a6 added option -T: text length encoding;
wenzelm
parents: 64605
diff changeset
    97
  text_length: Length = Length.encoding(Server.default_text_length),
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    98
  session_name: String = Server.default_logic,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    99
  session_dirs: List[Path] = Nil,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   100
  modes: List[String] = Nil)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   101
{
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   102
  /* server state */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   103
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   104
  private val state = Synchronized(Server.State())
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   105
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   106
  def session: Session = state.value.session getOrElse error("Session inactive")
64623
83f012ce2567 clarified module name;
wenzelm
parents: 64622
diff changeset
   107
  def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources]
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   108
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   109
  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
   110
    for {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   111
      model <- state.value.models.get(node_pos.name)
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   112
      rendering = model.rendering(options)
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   113
      offset <- model.doc.offset(node_pos.pos, text_length)
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   114
    } yield (rendering, offset)
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   115
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   116
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   117
  /* init and exit */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   118
64641
wenzelm
parents: 64623
diff changeset
   119
  def init(id: Protocol.Id)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   120
  {
64641
wenzelm
parents: 64623
diff changeset
   121
    def reply(err: String)
wenzelm
parents: 64623
diff changeset
   122
    {
wenzelm
parents: 64623
diff changeset
   123
      channel.write(Protocol.Initialize.reply(id, err))
64642
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   124
      if (err == "")
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   125
        channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")")
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   126
      else channel.error_message(err)
64641
wenzelm
parents: 64623
diff changeset
   127
    }
wenzelm
parents: 64623
diff changeset
   128
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   129
    val try_session =
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   130
      try {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   131
        val content = Build.session_content(options, false, session_dirs, session_name)
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   132
        val resources =
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   133
          new VSCode_Resources(content.loaded_theories, content.known_theories, content.syntax)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   134
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   135
        Some(new Session(resources) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   136
          override def output_delay = options.seconds("editor_output_delay")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   137
          override def prune_delay = options.seconds("editor_prune_delay")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   138
          override def syslog_limit = options.int("editor_syslog_limit")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   139
          override def reparse_limit = options.int("editor_reparse_limit")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   140
        })
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   141
      }
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   142
      catch { case ERROR(msg) => reply(msg); None }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   143
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   144
    for (session <- try_session) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   145
      var session_phase: Session.Consumer[Session.Phase] = null
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   146
      session_phase =
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   147
        Session.Consumer(getClass.getName) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   148
          case Session.Ready =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   149
            session.phase_changed -= session_phase
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   150
            session.update_options(options)
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   151
            reply("")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   152
          case Session.Failed =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   153
            session.phase_changed -= session_phase
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   154
            reply("Prover startup failed")
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   155
          case _ =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   156
        }
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   157
      session.phase_changed += session_phase
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   158
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   159
      session.start(receiver =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   160
        Isabelle_Process(options = options, logic = session_name, dirs = session_dirs,
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   161
          modes = modes, receiver = receiver))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   162
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   163
      state.change(_.copy(session = Some(session)))
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   164
    }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   165
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   166
64641
wenzelm
parents: 64623
diff changeset
   167
  def shutdown(id: Protocol.Id)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   168
  {
64641
wenzelm
parents: 64623
diff changeset
   169
    def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err))
wenzelm
parents: 64623
diff changeset
   170
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   171
    state.change(st =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   172
      st.session match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   173
        case None => reply("Prover inactive"); st
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   174
        case Some(session) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   175
          var session_phase: Session.Consumer[Session.Phase] = null
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   176
          session_phase =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   177
            Session.Consumer(getClass.getName) {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   178
              case Session.Inactive =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   179
                session.phase_changed -= session_phase
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   180
                reply("")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   181
              case _ =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   182
            }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   183
          session.phase_changed += session_phase
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   184
          session.stop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   185
          st.copy(session = None)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   186
      })
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   187
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   188
64646
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   189
  def exit() {
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   190
    channel.log("\n")
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   191
    sys.exit(if (state.value.session.isDefined) 1 else 0)
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   192
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   193
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   194
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   195
  /* document management */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   196
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   197
  private val delay_flush =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   198
    Standard_Thread.delay_last(options.seconds("editor_input_delay")) {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   199
      state.change(st =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   200
        {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   201
          val models = st.models
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   202
          val changed = (for { entry <- models.iterator if entry._2.changed } yield entry).toList
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   203
          val edits = for { (_, model) <- changed; edit <- model.node_edits } yield edit
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   204
          val models1 =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   205
            (models /: changed)({ case (m, (uri, model)) => m + (uri -> model.unchanged) })
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   206
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   207
          session.update(Document.Blobs.empty, edits)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   208
          st.copy(models = models1)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   209
        })
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   210
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   211
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   212
  def update_document(uri: String, text: String)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   213
  {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   214
    state.change(st =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   215
      {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   216
        val node_name = resources.node_name(uri)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   217
        val model = Document_Model(session, Line.Document(text), node_name)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   218
        st.copy(models = st.models + (uri -> model))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   219
      })
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   220
    delay_flush.invoke()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   221
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   222
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   223
64622
529bbb8977c7 more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents: 64618
diff changeset
   224
  /* hover */
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   225
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   226
  def hover(id: Protocol.Id, node_pos: Line.Node_Position)
64641
wenzelm
parents: 64623
diff changeset
   227
  {
wenzelm
parents: 64623
diff changeset
   228
    val result =
wenzelm
parents: 64623
diff changeset
   229
      for {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   230
        (rendering, offset) <- rendering_offset(node_pos)
64641
wenzelm
parents: 64623
diff changeset
   231
        info <- rendering.tooltip(Text.Range(offset, offset + 1))
wenzelm
parents: 64623
diff changeset
   232
      } yield {
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   233
        val doc = rendering.model.doc
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   234
        val start = doc.position(info.range.start, text_length)
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   235
        val stop = doc.position(info.range.stop, text_length)
64641
wenzelm
parents: 64623
diff changeset
   236
        val s = Pretty.string_of(info.info, margin = rendering.tooltip_margin)
wenzelm
parents: 64623
diff changeset
   237
        (Line.Range(start, stop), List("```\n" + s + "\n```"))  // FIXME proper content format
wenzelm
parents: 64623
diff changeset
   238
      }
wenzelm
parents: 64623
diff changeset
   239
    channel.write(Protocol.Hover.reply(id, result))
wenzelm
parents: 64623
diff changeset
   240
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   241
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   242
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   243
  /* goto definition */
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   244
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   245
  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
   246
  {
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   247
    val result =
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   248
      (for ((rendering, offset) <- rendering_offset(node_pos))
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   249
        yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   250
    channel.write(Protocol.GotoDefinition.reply(id, result))
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   251
  }
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   252
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   253
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   254
  /* main loop */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   255
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   256
  def start()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   257
  {
64646
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   258
    channel.log("Server started " + Date.now())
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
    def handle(json: JSON.T)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   261
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   262
      try {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   263
        json match {
64641
wenzelm
parents: 64623
diff changeset
   264
          case Protocol.Initialize(id) => init(id)
wenzelm
parents: 64623
diff changeset
   265
          case Protocol.Shutdown(id) => shutdown(id)
64645
0b513620d949 proper pattern match;
wenzelm
parents: 64644
diff changeset
   266
          case Protocol.Exit(()) => exit()
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   267
          case Protocol.DidOpenTextDocument(uri, lang, version, text) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   268
            update_document(uri, text)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   269
          case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   270
            update_document(uri, text)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   271
          case Protocol.DidCloseTextDocument(uri) => channel.log("CLOSE " + uri)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   272
          case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri)
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   273
          case Protocol.Hover(id, node_pos) => hover(id, node_pos)
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   274
          case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   275
          case _ => channel.log("### IGNORED")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   276
        }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   277
      }
64655
ea34f36ff6a5 clarified message;
wenzelm
parents: 64651
diff changeset
   278
      catch { case exn: Throwable => channel.error_message(Exn.message(exn)) }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   279
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   280
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   281
    @tailrec def loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   282
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   283
      channel.read() match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   284
        case Some(json) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   285
          json match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   286
            case bulk: List[_] => bulk.foreach(handle(_))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   287
            case _ => handle(json)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   288
          }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   289
          loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   290
        case None => channel.log("### TERMINATE")
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
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   293
    loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   294
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   295
}