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-13 05:46:05 -05:00
|
|
|
extern crate walkdir;
|
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-13 05:46:05 -05:00
|
|
|
mod vfs;
|
|
|
|
|
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-12 18:38:34 -05:00
|
|
|
use flexi_logger::{Logger, Duplicate};
|
2018-08-12 14:08:14 -05:00
|
|
|
use libanalysis::WorldState;
|
2018-08-10 09:49:45 -05:00
|
|
|
|
|
|
|
use ::{
|
2018-08-12 16:09:30 -05:00
|
|
|
io::{Io, RawMsg, RawResponse, RawRequest, 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-12 18:38:34 -05:00
|
|
|
.duplicate_to_stderr(Duplicate::All)
|
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| {
|
2018-08-12 18:38:34 -05:00
|
|
|
let res = req::InitializeResult { capabilities: caps::server_capabilities() };
|
2018-08-12 14:23:44 -05: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 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),
|
2018-08-12 16:09:30 -05:00
|
|
|
Request(RawRequest),
|
2018-08-12 13:34:17 -05:00
|
|
|
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-13 05:46:05 -05:00
|
|
|
let (task_sender, task_receiver) = bounded::<Task>(16);
|
|
|
|
let (fs_events_receiver, watcher) = vfs::watch(vec![
|
2018-08-13 07:35:53 -05:00
|
|
|
::std::env::current_dir()?,
|
2018-08-13 05:46:05 -05:00
|
|
|
]);
|
2018-08-10 14:55:42 -05:00
|
|
|
info!("lifecycle: handshake finished, server ready to serve requests");
|
2018-08-13 05:46:05 -05:00
|
|
|
let res = main_loop::main_loop(
|
|
|
|
io,
|
|
|
|
&mut world,
|
|
|
|
&mut pool,
|
|
|
|
task_sender,
|
|
|
|
task_receiver.clone(),
|
|
|
|
fs_events_receiver,
|
|
|
|
);
|
|
|
|
|
2018-08-10 14:55:42 -05:00
|
|
|
info!("waiting for background jobs to finish...");
|
2018-08-13 05:46:05 -05:00
|
|
|
task_receiver.for_each(drop);
|
2018-08-10 14:55:42 -05:00
|
|
|
pool.join();
|
|
|
|
info!("...background jobs have finished");
|
2018-08-13 05:46:05 -05:00
|
|
|
|
|
|
|
info!("waiting for file watcher to finish...");
|
|
|
|
watcher.stop()?;
|
|
|
|
info!("...file watcher has finished");
|
|
|
|
|
2018-08-10 14:55:42 -05: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 09:49:45 -05:00
|
|
|
}
|