src/Tools/VSCode/src/server.scala
changeset 64717 d2b50eb3d9ab
parent 64710 72ca4e5f976e
child 64720 8cc2d7c4ada1
equal deleted inserted replaced
64716:473793d52d97 64717:d2b50eb3d9ab
    62 
    62 
    63       if (!Build.build(options = options, build_heap = true, no_build = true,
    63       if (!Build.build(options = options, build_heap = true, no_build = true,
    64             dirs = dirs, sessions = List(logic)).ok)
    64             dirs = dirs, sessions = List(logic)).ok)
    65         error("Missing logic image " + quote(logic))
    65         error("Missing logic image " + quote(logic))
    66 
    66 
    67       val channel = new Channel(System.in, System.out, log_file)
    67       val log = Logger.make(log_file)
    68       val server = new Server(channel, options, text_length, logic, dirs, modes)
    68       val channel = new Channel(System.in, System.out, log)
       
    69       val server = new Server(channel, options, text_length, logic, dirs, modes, log)
    69 
    70 
    70       // prevent spurious garbage on the main protocol channel
    71       // prevent spurious garbage on the main protocol channel
    71       val orig_out = System.out
    72       val orig_out = System.out
    72       try {
    73       try {
    73         System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} }))
    74         System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} }))
    75       }
    76       }
    76       finally { System.setOut(orig_out) }
    77       finally { System.setOut(orig_out) }
    77     }
    78     }
    78     catch {
    79     catch {
    79       case exn: Throwable =>
    80       case exn: Throwable =>
    80         val channel = new Channel(System.in, System.out, None)
    81         val channel = new Channel(System.in, System.out, No_Logger)
    81         channel.error_message(Exn.message(exn))
    82         channel.error_message(Exn.message(exn))
    82         throw(exn)
    83         throw(exn)
    83     }
    84     }
    84   })
    85   })
    85 }
    86 }
    88   channel: Channel,
    89   channel: Channel,
    89   options: Options,
    90   options: Options,
    90   text_length: Text.Length = Text.Length.encoding(Server.default_text_length),
    91   text_length: Text.Length = Text.Length.encoding(Server.default_text_length),
    91   session_name: String = Server.default_logic,
    92   session_name: String = Server.default_logic,
    92   session_dirs: List[Path] = Nil,
    93   session_dirs: List[Path] = Nil,
    93   modes: List[String] = Nil)
    94   modes: List[String] = Nil,
       
    95   log: Logger = No_Logger)
    94 {
    96 {
    95   /* server session */
    97   /* server session */
    96 
    98 
    97   private val session_ = Synchronized(None: Option[Session])
    99   private val session_ = Synchronized(None: Option[Session])
    98   def session: Session = session_.value getOrElse error("Server inactive")
   100   def session: Session = session_.value getOrElse error("Server inactive")
   249         None
   251         None
   250     })
   252     })
   251   }
   253   }
   252 
   254 
   253   def exit() {
   255   def exit() {
   254     channel.log("\n")
   256     log("\n")
   255     sys.exit(if (session_.value.isDefined) 1 else 0)
   257     sys.exit(if (session_.value.isDefined) 1 else 0)
   256   }
   258   }
   257 
   259 
   258 
   260 
   259   /* hover */
   261   /* hover */
   287 
   289 
   288   /* main loop */
   290   /* main loop */
   289 
   291 
   290   def start()
   292   def start()
   291   {
   293   {
   292     channel.log("Server started " + Date.now())
   294     log("Server started " + Date.now())
   293 
   295 
   294     def handle(json: JSON.T)
   296     def handle(json: JSON.T)
   295     {
   297     {
   296       try {
   298       try {
   297         json match {
   299         json match {
   302             update_document(uri, text)
   304             update_document(uri, text)
   303           case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
   305           case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) =>
   304             update_document(uri, text)
   306             update_document(uri, text)
   305           case Protocol.DidCloseTextDocument(uri) =>
   307           case Protocol.DidCloseTextDocument(uri) =>
   306             close_document(uri)
   308             close_document(uri)
   307           case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri)
   309           case Protocol.DidSaveTextDocument(uri) => log("SAVE " + uri)
   308           case Protocol.Hover(id, node_pos) => hover(id, node_pos)
   310           case Protocol.Hover(id, node_pos) => hover(id, node_pos)
   309           case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
   311           case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
   310           case _ => channel.log("### IGNORED")
   312           case _ => log("### IGNORED")
   311         }
   313         }
   312       }
   314       }
   313       catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) }
   315       catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) }
   314     }
   316     }
   315 
   317 
   320           json match {
   322           json match {
   321             case bulk: List[_] => bulk.foreach(handle(_))
   323             case bulk: List[_] => bulk.foreach(handle(_))
   322             case _ => handle(json)
   324             case _ => handle(json)
   323           }
   325           }
   324           loop()
   326           loop()
   325         case None => channel.log("### TERMINATE")
   327         case None => log("### TERMINATE")
   326       }
   328       }
   327     }
   329     }
   328     loop()
   330     loop()
   329   }
   331   }
   330 }
   332 }