2018-08-10 07:07:43 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate failure;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_json;
|
|
|
|
extern crate languageserver_types;
|
|
|
|
extern crate drop_bomb;
|
2018-08-10 09:49:45 -05:00
|
|
|
#[macro_use]
|
2018-08-10 07:07:43 -05:00
|
|
|
extern crate crossbeam_channel;
|
2018-08-10 09:49:45 -05:00
|
|
|
extern crate threadpool;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2018-08-10 16:55:32 -05:00
|
|
|
extern crate url_serde;
|
2018-08-10 09:49:45 -05:00
|
|
|
extern crate flexi_logger;
|
2018-08-10 07:07:43 -05:00
|
|
|
extern crate libeditor;
|
|
|
|
extern crate libanalysis;
|
2018-08-11 06:44:12 -05:00
|
|
|
extern crate libsyntax2;
|
2018-08-10 07:07:43 -05:00
|
|
|
|
|
|
|
mod io;
|
|
|
|
mod caps;
|
|
|
|
mod req;
|
|
|
|
mod dispatch;
|
2018-08-10 16:12:31 -05:00
|
|
|
mod util;
|
2018-08-12 13:02:56 -05:00
|
|
|
mod conv;
|
2018-08-12 14:08:14 -05:00
|
|
|
mod main_loop;
|
2018-08-10 07:07:43 -05:00
|
|
|
|
2018-08-10 09:49:45 -05:00
|
|
|
use threadpool::ThreadPool;
|
2018-08-12 14:08:14 -05:00
|
|
|
use crossbeam_channel::bounded;
|
2018-08-10 09:49:45 -05:00
|
|
|
use flexi_logger::Logger;
|
2018-08-12 14:08:14 -05:00
|
|
|
use libanalysis::WorldState;
|
2018-08-10 09:49:45 -05:00
|
|
|
|
|
|
|
use ::{
|
2018-08-12 14:08:14 -05:00
|
|
|
io::{Io, RawMsg, RawResponse, RawNotification}
|
2018-08-10 09:49:45 -05:00
|
|
|
};
|
2018-08-10 07:07:43 -05:00
|
|
|
|
|
|
|
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
2018-08-10 17:04:09 -05:00
|
|
|
Logger::with_env()
|
2018-08-10 09:49:45 -05:00
|
|
|
.log_to_file()
|
|
|
|
.directory("log")
|
|
|
|
.start()?;
|
2018-08-10 14:55:42 -05:00
|
|
|
info!("lifecycle: server started");
|
2018-08-10 09:49:45 -05:00
|
|
|
match ::std::panic::catch_unwind(|| main_inner()) {
|
|
|
|
Ok(res) => {
|
2018-08-10 14:55:42 -05:00
|
|
|
info!("lifecycle: terminating process with {:?}", res);
|
2018-08-10 09:49:45 -05:00
|
|
|
res
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
error!("server panicked");
|
|
|
|
bail!("server panicked")
|
2018-08-10 10:01:59 -05:00
|
|
|
}
|
2018-08-10 09:49:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main_inner() -> Result<()> {
|
2018-08-10 07:07:43 -05:00
|
|
|
let mut io = Io::from_stdio();
|
2018-08-10 09:49:45 -05:00
|
|
|
let res = initialize(&mut io);
|
|
|
|
info!("shutting down IO...");
|
|
|
|
let io_res = io.stop();
|
|
|
|
info!("... IO is down");
|
|
|
|
match (res, io_res) {
|
|
|
|
(Ok(()), Ok(())) => Ok(()),
|
|
|
|
(res, Ok(())) => res,
|
|
|
|
(Ok(()), io_res) => io_res,
|
|
|
|
(res, Err(io_err)) => {
|
|
|
|
error!("shutdown error: {:?}", io_err);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 07:07:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn initialize(io: &mut Io) -> Result<()> {
|
2018-08-12 14:23:44 -05:00
|
|
|
match io.recv()? {
|
|
|
|
RawMsg::Notification(n) =>
|
|
|
|
bail!("expected initialize request, got {:?}", n),
|
|
|
|
RawMsg::Response(res) =>
|
|
|
|
bail!("expected initialize request, got {:?}", res),
|
2018-08-12 13:47:27 -05:00
|
|
|
|
2018-08-12 14:23:44 -05:00
|
|
|
RawMsg::Request(req) => {
|
|
|
|
let mut req = Some(req);
|
|
|
|
dispatch::handle_request::<req::Initialize, _>(&mut req, |_params, resp| {
|
|
|
|
let res = req::InitializeResult { capabilities: caps::SERVER_CAPABILITIES };
|
|
|
|
let resp = resp.into_response(Ok(res))?;
|
|
|
|
io.send(RawMsg::Response(resp));
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
if let Some(req) = req {
|
|
|
|
bail!("expected initialize request, got {:?}", req)
|
|
|
|
}
|
|
|
|
match io.recv()? {
|
|
|
|
RawMsg::Notification(n) => {
|
|
|
|
if n.method != "initialized" {
|
|
|
|
bail!("expected initialized notification");
|
2018-08-10 07:07:43 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 14:23:44 -05:00
|
|
|
_ => bail!("expected initialized notification"),
|
2018-08-10 07:07:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 13:47:27 -05:00
|
|
|
initialized(io)
|
2018-08-10 07:07:43 -05:00
|
|
|
}
|
|
|
|
|
2018-08-12 13:34:17 -05:00
|
|
|
enum Task {
|
|
|
|
Respond(RawResponse),
|
|
|
|
Notify(RawNotification),
|
|
|
|
Die(::failure::Error),
|
|
|
|
}
|
2018-08-10 09:49:45 -05:00
|
|
|
|
2018-08-10 07:07:43 -05:00
|
|
|
fn initialized(io: &mut Io) -> Result<()> {
|
2018-08-10 14:55:42 -05:00
|
|
|
{
|
|
|
|
let mut world = WorldState::new();
|
|
|
|
let mut pool = ThreadPool::new(4);
|
2018-08-12 13:34:17 -05:00
|
|
|
let (sender, receiver) = bounded::<Task>(16);
|
2018-08-10 14:55:42 -05:00
|
|
|
info!("lifecycle: handshake finished, server ready to serve requests");
|
2018-08-12 14:08:14 -05:00
|
|
|
let res = main_loop::main_loop(io, &mut world, &mut pool, sender, receiver.clone());
|
2018-08-10 14:55:42 -05:00
|
|
|
info!("waiting for background jobs to finish...");
|
|
|
|
receiver.for_each(drop);
|
|
|
|
pool.join();
|
|
|
|
info!("...background jobs have finished");
|
|
|
|
res
|
|
|
|
}?;
|
|
|
|
|
|
|
|
match io.recv()? {
|
|
|
|
RawMsg::Notification(n) => {
|
|
|
|
if n.method == "exit" {
|
|
|
|
info!("lifecycle: shutdown complete");
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
bail!("unexpected notification during shutdown: {:?}", n)
|
|
|
|
}
|
|
|
|
m => {
|
|
|
|
bail!("unexpected message during shutdown: {:?}", m)
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 09:49:45 -05:00
|
|
|
}
|