src/Tools/VSCode/src/server.scala
author wenzelm
Tue Dec 20 17:09:40 2016 +0100 (2016-12-20)
changeset 64618 c81bd30839a6
parent 64605 9c1173a7e4cb
child 64622 529bbb8977c7
permissions -rw-r--r--
added option -T: text length encoding;
     1 /*  Title:      Tools/VSCode/src/server.scala
     2     Author:     Makarius
     3 
     4 Server for VS Code Language Server Protocol 2.0, see also
     5 https://github.com/Microsoft/language-server-protocol
     6 https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
     7 */
     8 
     9 package isabelle.vscode
    10 
    11 
    12 import isabelle._
    13 
    14 import java.io.{PrintStream, OutputStream}
    15 
    16 import scala.annotation.tailrec
    17 
    18 
    19 object Server
    20 {
    21   /* Isabelle tool wrapper */
    22 
    23   private val default_text_length = "UTF-16"
    24   private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC")
    25 
    26   val isabelle_tool = Isabelle_Tool("vscode_server", "VSCode Language Server for PIDE", args =>
    27   {
    28     var log_file: Option[Path] = None
    29     var text_length = Length.encoding(default_text_length)
    30     var dirs: List[Path] = Nil
    31     var logic = default_logic
    32     var modes: List[String] = Nil
    33     var options = Options.init()
    34 
    35     def text_length_choice: String =
    36       commas(Length.encodings.map(
    37         { case (a, _) => if (a == default_text_length) a + " (default)" else a }))
    38 
    39     val getopts = Getopts("""
    40 Usage: isabelle vscode_server [OPTIONS]
    41 
    42   Options are:
    43     -L FILE      enable logging on FILE
    44     -T LENGTH    text length encoding: """ + text_length_choice + """
    45     -d DIR       include session directory
    46     -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """)
    47     -m MODE      add print mode for output
    48     -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
    49 
    50   Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout.
    51 """,
    52       "L:" -> (arg => log_file = Some(Path.explode(arg))),
    53       "T:" -> (arg => Length.encoding(arg)),
    54       "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
    55       "l:" -> (arg => logic = arg),
    56       "m:" -> (arg => modes = arg :: modes),
    57       "o:" -> (arg => options = options + arg))
    58 
    59     val more_args = getopts(args)
    60     if (more_args.nonEmpty) getopts.usage()
    61 
    62     if (!Build.build(options = options, build_heap = true, no_build = true,
    63           dirs = dirs, sessions = List(logic)).ok)
    64       error("Missing logic image " + quote(logic))
    65 
    66     val channel = new Channel(System.in, System.out, log_file)
    67     val server = new Server(channel, options, text_length, logic, dirs, modes)
    68 
    69     // prevent spurious garbage on the main protocol channel
    70     val orig_out = System.out
    71     try {
    72       System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} }))
    73       server.start()
    74     }
    75     finally { System.setOut(orig_out) }
    76   })
    77 
    78 
    79   /* server state */
    80 
    81   sealed case class State(
    82     session: Option[Session] = None,
    83     models: Map[String, Document_Model] = Map.empty)
    84 }
    85 
    86 class Server(
    87   channel: Channel,
    88   options: Options,
    89   text_length: Length = Length.encoding(Server.default_text_length),
    90   session_name: String = Server.default_logic,
    91   session_dirs: List[Path] = Nil,
    92   modes: List[String] = Nil)
    93 {
    94   /* server state */
    95 
    96   private val state = Synchronized(Server.State())
    97 
    98   def session: Session = state.value.session getOrElse error("Session inactive")
    99   def resources: URI_Resources = session.resources.asInstanceOf[URI_Resources]
   100 
   101 
   102   /* init and exit */
   103 
   104   def initialize(reply: String => Unit)
   105   {
   106     val content = Build.session_content(options, false, session_dirs, session_name)
   107     val resources =
   108       new URI_Resources(content.loaded_theories, content.known_theories, content.syntax)
   109 
   110     val session =
   111       new Session(resources) {
   112         override def output_delay = options.seconds("editor_output_delay")
   113         override def prune_delay = options.seconds("editor_prune_delay")
   114         override def syslog_limit = options.int("editor_syslog_limit")
   115         override def reparse_limit = options.int("editor_reparse_limit")
   116       }
   117 
   118     var session_phase: Session.Consumer[Session.Phase] = null
   119     session_phase =
   120       Session.Consumer(getClass.getName) {
   121         case Session.Ready =>
   122           session.phase_changed -= session_phase
   123           session.update_options(options)
   124           reply("")
   125         case Session.Failed =>
   126           session.phase_changed -= session_phase
   127           reply("Prover startup failed")
   128         case _ =>
   129       }
   130     session.phase_changed += session_phase
   131 
   132     session.start(receiver =>
   133       Isabelle_Process(options = options, logic = session_name, dirs = session_dirs,
   134         modes = modes, receiver = receiver))
   135 
   136     state.change(_.copy(session = Some(session)))
   137   }
   138 
   139   def shutdown(reply: String => Unit)
   140   {
   141     state.change(st =>
   142       st.session match {
   143         case None => reply("Prover inactive"); st
   144         case Some(session) =>
   145           var session_phase: Session.Consumer[Session.Phase] = null
   146           session_phase =
   147             Session.Consumer(getClass.getName) {
   148               case Session.Inactive =>
   149                 session.phase_changed -= session_phase
   150                 reply("")
   151               case _ =>
   152             }
   153           session.phase_changed += session_phase
   154           session.stop()
   155           st.copy(session = None)
   156       })
   157   }
   158 
   159   def exit() { sys.exit(if (state.value.session.isDefined) 1 else 0) }
   160 
   161 
   162   /* document management */
   163 
   164   private val delay_flush =
   165     Standard_Thread.delay_last(options.seconds("editor_input_delay")) {
   166       state.change(st =>
   167         {
   168           val models = st.models
   169           val changed = (for { entry <- models.iterator if entry._2.changed } yield entry).toList
   170           val edits = for { (_, model) <- changed; edit <- model.node_edits } yield edit
   171           val models1 =
   172             (models /: changed)({ case (m, (uri, model)) => m + (uri -> model.unchanged) })
   173 
   174           session.update(Document.Blobs.empty, edits)
   175           st.copy(models = models1)
   176         })
   177     }
   178 
   179   def update_document(uri: String, text: String)
   180   {
   181     state.change(st =>
   182       {
   183         val node_name = resources.node_name(uri)
   184         val model = Document_Model(session, Line.Document(text), node_name)
   185         st.copy(models = st.models + (uri -> model))
   186       })
   187     delay_flush.invoke()
   188   }
   189 
   190 
   191   /* tooltips -- see $ISABELLE_HOME/src/Tools/jEdit/rendering.scala */
   192 
   193   def hover(uri: String, pos: Line.Position): Option[(Line.Range, List[String])] =
   194     for {
   195       model <- state.value.models.get(uri)
   196       snapshot = model.snapshot()
   197       offset <- model.doc.offset(pos, text_length)
   198       info <- tooltip(snapshot, Text.Range(offset, offset + 1))
   199     } yield {
   200       val start = model.doc.position(info.range.start, text_length)
   201       val stop = model.doc.position(info.range.stop, text_length)
   202       val s = Pretty.string_of(info.info, margin = tooltip_margin.toDouble)
   203       (Line.Range(start, stop), List("```\n" + s + "\n```"))
   204     }
   205 
   206   private val tooltip_descriptions =
   207     Map(
   208       Markup.CITATION -> "citation",
   209       Markup.TOKEN_RANGE -> "inner syntax token",
   210       Markup.FREE -> "free variable",
   211       Markup.SKOLEM -> "skolem variable",
   212       Markup.BOUND -> "bound variable",
   213       Markup.VAR -> "schematic variable",
   214       Markup.TFREE -> "free type variable",
   215       Markup.TVAR -> "schematic type variable")
   216 
   217   private val tooltip_elements =
   218     Markup.Elements(Markup.LANGUAGE, Markup.EXPRESSION, Markup.TIMING, Markup.ENTITY,
   219       Markup.SORTING, Markup.TYPING, Markup.CLASS_PARAMETER, Markup.ML_TYPING,
   220       Markup.ML_BREAKPOINT, Markup.PATH, Markup.DOC, Markup.URL, Markup.MARKDOWN_PARAGRAPH,
   221       Markup.Markdown_List.name) ++ Markup.Elements(tooltip_descriptions.keySet)
   222 
   223   private def pretty_typing(kind: String, body: XML.Body): XML.Tree =
   224     Pretty.block(XML.Text(kind) :: Pretty.brk(1) :: body)
   225 
   226   private def timing_threshold: Double = options.real("jedit_timing_threshold")
   227 
   228   def tooltip(snapshot: Document.Snapshot, range: Text.Range): Option[Text.Info[XML.Body]] =
   229   {
   230     def add(prev: Text.Info[(Timing, Vector[(Boolean, XML.Tree)])],
   231       r0: Text.Range, p: (Boolean, XML.Tree)): Text.Info[(Timing, Vector[(Boolean, XML.Tree)])] =
   232     {
   233       val r = snapshot.convert(r0)
   234       val (t, info) = prev.info
   235       if (prev.range == r)
   236         Text.Info(r, (t, info :+ p))
   237       else Text.Info(r, (t, Vector(p)))
   238     }
   239 
   240     val results =
   241       snapshot.cumulate[Text.Info[(Timing, Vector[(Boolean, XML.Tree)])]](
   242         range, Text.Info(range, (Timing.zero, Vector.empty)), tooltip_elements, _ =>
   243         {
   244           case (Text.Info(r, (t1, info)), Text.Info(_, XML.Elem(Markup.Timing(t2), _))) =>
   245             Some(Text.Info(r, (t1 + t2, info)))
   246 
   247           case (prev, Text.Info(r, XML.Elem(Markup.Entity(kind, name), _)))
   248           if kind != "" &&
   249             kind != Markup.ML_DEF &&
   250             kind != Markup.ML_OPEN &&
   251             kind != Markup.ML_STRUCTURE =>
   252             val kind1 = Word.implode(Word.explode('_', kind))
   253             val txt1 =
   254               if (name == "") kind1
   255               else if (kind1 == "") quote(name)
   256               else kind1 + " " + quote(name)
   257             val t = prev.info._1
   258             val txt2 =
   259               if (kind == Markup.COMMAND && t.elapsed.seconds >= timing_threshold)
   260                 "\n" + t.message
   261               else ""
   262             Some(add(prev, r, (true, XML.Text(txt1 + txt2))))
   263 
   264           case (prev, Text.Info(r, XML.Elem(Markup.Doc(name), _))) =>
   265             val text = "doc " + quote(name)
   266             Some(add(prev, r, (true, XML.Text(text))))
   267 
   268           case (prev, Text.Info(r, XML.Elem(Markup.Url(name), _))) =>
   269             Some(add(prev, r, (true, XML.Text("URL " + quote(name)))))
   270 
   271           case (prev, Text.Info(r, XML.Elem(Markup(name, _), body)))
   272           if name == Markup.SORTING || name == Markup.TYPING =>
   273             Some(add(prev, r, (true, pretty_typing("::", body))))
   274 
   275           case (prev, Text.Info(r, XML.Elem(Markup(Markup.CLASS_PARAMETER, _), body))) =>
   276             Some(add(prev, r, (true, Pretty.block(0, body))))
   277 
   278           case (prev, Text.Info(r, XML.Elem(Markup(Markup.ML_TYPING, _), body))) =>
   279             Some(add(prev, r, (false, pretty_typing("ML:", body))))
   280 
   281           case (prev, Text.Info(r, XML.Elem(Markup.Language(language, _, _, _), _))) =>
   282             val lang = Word.implode(Word.explode('_', language))
   283             Some(add(prev, r, (true, XML.Text("language: " + lang))))
   284 
   285           case (prev, Text.Info(r, XML.Elem(Markup.Expression(kind), _))) =>
   286             val descr = if (kind == "") "expression" else "expression: " + kind
   287             Some(add(prev, r, (true, XML.Text(descr))))
   288 
   289           case (prev, Text.Info(r, XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), _))) =>
   290             Some(add(prev, r, (true, XML.Text("Markdown: paragraph"))))
   291           case (prev, Text.Info(r, XML.Elem(Markup.Markdown_List(kind), _))) =>
   292             Some(add(prev, r, (true, XML.Text("Markdown: " + kind))))
   293 
   294           case (prev, Text.Info(r, XML.Elem(Markup(name, _), _))) =>
   295             tooltip_descriptions.get(name).
   296               map(descr => add(prev, r, (true, XML.Text(descr))))
   297         }).map(_.info)
   298 
   299     results.map(_.info).flatMap(res => res._2.toList) match {
   300       case Nil => None
   301       case tips =>
   302         val r = Text.Range(results.head.range.start, results.last.range.stop)
   303         val all_tips = (tips.filter(_._1) ++ tips.filter(!_._1).lastOption.toList).map(_._2)
   304         Some(Text.Info(r, Library.separate(Pretty.fbrk, all_tips)))
   305     }
   306   }
   307 
   308   def tooltip_margin: Int = options.int("jedit_tooltip_margin")
   309 
   310 
   311   /* main loop */
   312 
   313   def start()
   314   {
   315     channel.log("\nServer started " + Date.now())
   316 
   317     def handle(json: JSON.T)
   318     {
   319       try {
   320         json match {
   321           case Protocol.Initialize(id) =>
   322             def initialize_reply(err: String)
   323             {
   324               channel.write(Protocol.Initialize.reply(id, err))
   325               if (err == "") {
   326                 channel.write(Protocol.DisplayMessage(Protocol.MessageType.Info,
   327                   "Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")"))
   328               }
   329               else channel.write(Protocol.DisplayMessage(Protocol.MessageType.Error, err))
   330             }
   331             initialize(initialize_reply _)
   332           case Protocol.Shutdown(id) =>
   333             shutdown((error: String) => channel.write(Protocol.Shutdown.reply(id, error)))
   334           case Protocol.Exit =>
   335             exit()
   336           case Protocol.DidOpenTextDocument(uri, lang, version, text) =>
   337             update_document(uri, text)
   338           case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
   339             update_document(uri, text)
   340           case Protocol.DidCloseTextDocument(uri) => channel.log("CLOSE " + uri)
   341           case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri)
   342           case Protocol.Hover(id, uri, pos) =>
   343             channel.write(Protocol.Hover.reply(id, hover(uri, pos)))
   344           case _ => channel.log("### IGNORED")
   345         }
   346       }
   347       catch { case exn: Throwable => channel.log("*** ERROR: " + Exn.message(exn)) }
   348     }
   349 
   350     @tailrec def loop()
   351     {
   352       channel.read() match {
   353         case Some(json) =>
   354           json match {
   355             case bulk: List[_] => bulk.foreach(handle(_))
   356             case _ => handle(json)
   357           }
   358           loop()
   359         case None => channel.log("### TERMINATE")
   360       }
   361     }
   362     loop()
   363   }
   364 }