rust/crates/server/src/main.rs

158 lines
4.2 KiB
Rust
Raw Normal View History

2018-08-10 14:07:43 +02: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 16:49:45 +02:00
#[macro_use]
2018-08-10 14:07:43 +02:00
extern crate crossbeam_channel;
2018-08-10 16:49:45 +02:00
extern crate threadpool;
#[macro_use]
extern crate log;
2018-08-10 23:55:32 +02:00
extern crate url_serde;
2018-08-10 16:49:45 +02:00
extern crate flexi_logger;
2018-08-13 12:46:05 +02:00
extern crate walkdir;
2018-08-10 14:07:43 +02:00
extern crate libeditor;
extern crate libanalysis;
2018-08-11 13:44:12 +02:00
extern crate libsyntax2;
2018-08-10 14:07:43 +02:00
mod io;
mod caps;
mod req;
mod dispatch;
2018-08-10 23:12:31 +02:00
mod util;
2018-08-12 20:02:56 +02:00
mod conv;
2018-08-12 21:08:14 +02:00
mod main_loop;
2018-08-13 12:46:05 +02:00
mod vfs;
2018-08-10 16:49:45 +02:00
use threadpool::ThreadPool;
2018-08-12 21:08:14 +02:00
use crossbeam_channel::bounded;
2018-08-13 01:38:34 +02:00
use flexi_logger::{Logger, Duplicate};
2018-08-12 21:08:14 +02:00
use libanalysis::WorldState;
2018-08-10 16:49:45 +02:00
use ::{
2018-08-12 23:09:30 +02:00
io::{Io, RawMsg, RawResponse, RawRequest, RawNotification}
2018-08-10 16:49:45 +02:00
};
2018-08-10 14:07:43 +02:00
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
fn main() -> Result<()> {
2018-08-11 00:04:09 +02:00
Logger::with_env()
2018-08-13 01:38:34 +02:00
.duplicate_to_stderr(Duplicate::All)
2018-08-10 16:49:45 +02:00
.log_to_file()
.directory("log")
.start()?;
2018-08-10 21:55:42 +02:00
info!("lifecycle: server started");
2018-08-10 16:49:45 +02:00
match ::std::panic::catch_unwind(|| main_inner()) {
Ok(res) => {
2018-08-10 21:55:42 +02:00
info!("lifecycle: terminating process with {:?}", res);
2018-08-10 16:49:45 +02:00
res
}
Err(_) => {
error!("server panicked");
bail!("server panicked")
2018-08-10 17:01:59 +02:00
}
2018-08-10 16:49:45 +02:00
}
}
fn main_inner() -> Result<()> {
2018-08-10 14:07:43 +02:00
let mut io = Io::from_stdio();
2018-08-10 16:49:45 +02: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 14:07:43 +02:00
}
fn initialize(io: &mut Io) -> Result<()> {
2018-08-12 21:23:44 +02: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 20:47:27 +02:00
2018-08-12 21:23:44 +02:00
RawMsg::Request(req) => {
let mut req = Some(req);
dispatch::handle_request::<req::Initialize, _>(&mut req, |_params, resp| {
2018-08-13 01:38:34 +02:00
let res = req::InitializeResult { capabilities: caps::server_capabilities() };
2018-08-12 21:23:44 +02:00
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 14:07:43 +02:00
}
}
2018-08-12 21:23:44 +02:00
_ => bail!("expected initialized notification"),
2018-08-10 14:07:43 +02:00
}
}
}
2018-08-12 20:47:27 +02:00
initialized(io)
2018-08-10 14:07:43 +02:00
}
2018-08-12 20:34:17 +02:00
enum Task {
Respond(RawResponse),
2018-08-12 23:09:30 +02:00
Request(RawRequest),
2018-08-12 20:34:17 +02:00
Notify(RawNotification),
Die(::failure::Error),
}
2018-08-10 16:49:45 +02:00
2018-08-10 14:07:43 +02:00
fn initialized(io: &mut Io) -> Result<()> {
2018-08-10 21:55:42 +02:00
{
let mut world = WorldState::new();
let mut pool = ThreadPool::new(4);
2018-08-13 12:46:05 +02:00
let (task_sender, task_receiver) = bounded::<Task>(16);
let (fs_events_receiver, watcher) = vfs::watch(vec![
2018-08-13 14:35:53 +02:00
::std::env::current_dir()?,
2018-08-13 12:46:05 +02:00
]);
2018-08-10 21:55:42 +02:00
info!("lifecycle: handshake finished, server ready to serve requests");
2018-08-13 12:46:05 +02:00
let res = main_loop::main_loop(
io,
&mut world,
&mut pool,
task_sender,
task_receiver.clone(),
fs_events_receiver,
);
2018-08-10 21:55:42 +02:00
info!("waiting for background jobs to finish...");
2018-08-13 12:46:05 +02:00
task_receiver.for_each(drop);
2018-08-10 21:55:42 +02:00
pool.join();
info!("...background jobs have finished");
2018-08-13 12:46:05 +02:00
info!("waiting for file watcher to finish...");
watcher.stop()?;
info!("...file watcher has finished");
2018-08-10 21:55:42 +02:00
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 16:49:45 +02:00
}