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 15:30:11 -05:00
|
|
|
extern crate url;
|
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;
|
|
|
|
|
|
|
|
mod io;
|
|
|
|
mod caps;
|
|
|
|
mod req;
|
|
|
|
mod dispatch;
|
2018-08-10 13:13:39 -05:00
|
|
|
mod handlers;
|
2018-08-10 16:12:31 -05:00
|
|
|
mod util;
|
2018-08-10 07:07:43 -05:00
|
|
|
|
2018-08-10 09:49:45 -05:00
|
|
|
use threadpool::ThreadPool;
|
|
|
|
use crossbeam_channel::{bounded, Sender, Receiver};
|
|
|
|
use flexi_logger::Logger;
|
2018-08-10 16:12:31 -05:00
|
|
|
use url::Url;
|
2018-08-10 15:45:35 -05:00
|
|
|
use libanalysis::{WorldState, World};
|
2018-08-10 09:49:45 -05:00
|
|
|
|
|
|
|
use ::{
|
2018-08-10 15:45:35 -05:00
|
|
|
io::{Io, RawMsg, RawRequest},
|
2018-08-10 16:55:32 -05:00
|
|
|
handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics, publish_decorations},
|
2018-08-10 16:12:31 -05:00
|
|
|
util::{FilePath, FnBox}
|
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 13:13:39 -05:00
|
|
|
Logger::with_env_or_str("m=trace, libanalysis=trace")
|
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<()> {
|
|
|
|
loop {
|
|
|
|
match io.recv()? {
|
|
|
|
RawMsg::Request(req) => {
|
2018-08-10 13:13:39 -05:00
|
|
|
if let Some((_params, resp)) = dispatch::expect_request::<req::Initialize>(io, req)? {
|
2018-08-10 10:01:59 -05:00
|
|
|
resp.result(io, req::InitializeResult {
|
2018-08-10 07:07:43 -05:00
|
|
|
capabilities: caps::SERVER_CAPABILITIES
|
|
|
|
})?;
|
|
|
|
match io.recv()? {
|
|
|
|
RawMsg::Notification(n) => {
|
|
|
|
if n.method != "initialized" {
|
|
|
|
bail!("expected initialized notification");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bail!("expected initialized notification");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return initialized(io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RawMsg::Notification(n) => {
|
|
|
|
bail!("expected initialize request, got {:?}", n)
|
|
|
|
}
|
|
|
|
RawMsg::Response(res) => {
|
|
|
|
bail!("expected initialize request, got {:?}", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 09:49:45 -05:00
|
|
|
type Thunk = Box<for<'a> FnBox<&'a mut Io, Result<()>>>;
|
|
|
|
|
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);
|
|
|
|
let (sender, receiver) = bounded::<Thunk>(16);
|
|
|
|
info!("lifecycle: handshake finished, server ready to serve requests");
|
|
|
|
let res = main_loop(io, &mut world, &mut pool, sender, receiver.clone());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn main_loop(
|
|
|
|
io: &mut Io,
|
|
|
|
world: &mut WorldState,
|
|
|
|
pool: &mut ThreadPool,
|
|
|
|
sender: Sender<Thunk>,
|
|
|
|
receiver: Receiver<Thunk>,
|
|
|
|
) -> Result<()> {
|
|
|
|
info!("server initialized, serving requests");
|
2018-08-10 07:07:43 -05:00
|
|
|
loop {
|
2018-08-10 09:49:45 -05:00
|
|
|
enum Event {
|
|
|
|
Msg(RawMsg),
|
|
|
|
Thunk(Thunk),
|
|
|
|
ReceiverDead,
|
|
|
|
}
|
|
|
|
|
|
|
|
let event = select! {
|
|
|
|
recv(io.receiver(), msg) => match msg {
|
|
|
|
Some(msg) => Event::Msg(msg),
|
|
|
|
None => Event::ReceiverDead,
|
|
|
|
},
|
|
|
|
recv(receiver, thunk) => Event::Thunk(thunk.unwrap()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let msg = match event {
|
|
|
|
Event::ReceiverDead => {
|
|
|
|
io.cleanup_receiver()?;
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
Event::Thunk(thunk) => {
|
|
|
|
thunk.call_box(io)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Event::Msg(msg) => msg,
|
|
|
|
};
|
|
|
|
|
|
|
|
match msg {
|
2018-08-10 07:07:43 -05:00
|
|
|
RawMsg::Request(req) => {
|
2018-08-10 14:23:17 -05:00
|
|
|
let mut req = Some(req);
|
2018-08-10 15:45:35 -05:00
|
|
|
handle_request_on_threadpool::<req::SyntaxTree>(
|
|
|
|
&mut req, pool, world, &sender, handle_syntax_tree
|
|
|
|
)?;
|
|
|
|
handle_request_on_threadpool::<req::ExtendSelection>(
|
|
|
|
&mut req, pool, world, &sender, handle_extend_selection
|
|
|
|
)?;
|
2018-08-10 14:55:42 -05:00
|
|
|
let mut shutdown = false;
|
2018-08-10 14:23:17 -05:00
|
|
|
dispatch::handle_request::<req::Shutdown, _>(&mut req, |(), resp| {
|
2018-08-10 10:01:59 -05:00
|
|
|
resp.result(io, ())?;
|
2018-08-10 14:55:42 -05:00
|
|
|
shutdown = true;
|
2018-08-10 14:23:17 -05:00
|
|
|
Ok(())
|
|
|
|
})?;
|
2018-08-10 14:55:42 -05:00
|
|
|
if shutdown {
|
|
|
|
info!("lifecycle: initiating shutdown");
|
|
|
|
drop(sender);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-08-10 14:23:17 -05:00
|
|
|
if let Some(req) = req {
|
|
|
|
error!("unknown method: {:?}", req);
|
|
|
|
dispatch::unknown_method(io, req)?;
|
2018-08-10 07:07:43 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 13:13:39 -05:00
|
|
|
RawMsg::Notification(not) => {
|
|
|
|
let mut not = Some(not);
|
2018-08-10 14:23:17 -05:00
|
|
|
dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| {
|
2018-08-10 13:13:39 -05:00
|
|
|
let path = params.text_document.file_path()?;
|
|
|
|
world.change_overlay(path, Some(params.text_document.text));
|
2018-08-10 16:55:32 -05:00
|
|
|
update_file_notifications_on_threadpool(
|
2018-08-10 16:12:31 -05:00
|
|
|
pool, world.snapshot(), sender.clone(), params.text_document.uri,
|
|
|
|
);
|
2018-08-10 13:13:39 -05:00
|
|
|
Ok(())
|
|
|
|
})?;
|
2018-08-10 14:23:17 -05:00
|
|
|
dispatch::handle_notification::<req::DidChangeTextDocument, _>(&mut not, |mut params| {
|
2018-08-10 13:13:39 -05:00
|
|
|
let path = params.text_document.file_path()?;
|
|
|
|
let text = params.content_changes.pop()
|
|
|
|
.ok_or_else(|| format_err!("empty changes"))?
|
|
|
|
.text;
|
|
|
|
world.change_overlay(path, Some(text));
|
2018-08-10 16:55:32 -05:00
|
|
|
update_file_notifications_on_threadpool(
|
2018-08-10 16:12:31 -05:00
|
|
|
pool, world.snapshot(), sender.clone(), params.text_document.uri,
|
|
|
|
);
|
2018-08-10 13:13:39 -05:00
|
|
|
Ok(())
|
|
|
|
})?;
|
2018-08-10 14:23:17 -05:00
|
|
|
dispatch::handle_notification::<req::DidCloseTextDocument, _>(&mut not, |params| {
|
2018-08-10 13:13:39 -05:00
|
|
|
let path = params.text_document.file_path()?;
|
|
|
|
world.change_overlay(path, None);
|
2018-08-10 15:30:11 -05:00
|
|
|
dispatch::send_notification::<req::PublishDiagnostics>(io, req::PublishDiagnosticsParams {
|
|
|
|
uri: params.text_document.uri,
|
|
|
|
diagnostics: Vec::new(),
|
|
|
|
})?;
|
2018-08-10 13:13:39 -05:00
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if let Some(not) = not {
|
|
|
|
error!("unhandled notification: {:?}", not)
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 07:07:43 -05:00
|
|
|
msg => {
|
|
|
|
eprintln!("msg = {:?}", msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:56:19 -05:00
|
|
|
fn handle_request_on_threadpool<R: req::ClientRequest>(
|
2018-08-10 15:45:35 -05:00
|
|
|
req: &mut Option<RawRequest>,
|
|
|
|
pool: &ThreadPool,
|
|
|
|
world: &WorldState,
|
|
|
|
sender: &Sender<Thunk>,
|
|
|
|
f: fn(World, R::Params) -> Result<R::Result>,
|
|
|
|
) -> Result<()>
|
|
|
|
{
|
|
|
|
dispatch::handle_request::<R, _>(req, |params, resp| {
|
|
|
|
let world = world.snapshot();
|
|
|
|
let sender = sender.clone();
|
|
|
|
pool.execute(move || {
|
|
|
|
let res = f(world, params);
|
|
|
|
sender.send(Box::new(|io: &mut Io| resp.response(io, res)))
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:55:32 -05:00
|
|
|
fn update_file_notifications_on_threadpool(
|
2018-08-10 16:12:31 -05:00
|
|
|
pool: &ThreadPool,
|
|
|
|
world: World,
|
|
|
|
sender: Sender<Thunk>,
|
|
|
|
uri: Url,
|
|
|
|
) {
|
|
|
|
pool.execute(move || {
|
2018-08-10 16:55:32 -05:00
|
|
|
match publish_diagnostics(world.clone(), uri.clone()) {
|
2018-08-10 16:12:31 -05:00
|
|
|
Err(e) => {
|
|
|
|
error!("failed to compute diagnostics: {:?}", e)
|
|
|
|
}
|
|
|
|
Ok(params) => {
|
|
|
|
sender.send(Box::new(|io: &mut Io| {
|
|
|
|
dispatch::send_notification::<req::PublishDiagnostics>(io, params)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 16:55:32 -05:00
|
|
|
match publish_decorations(world, uri) {
|
|
|
|
Err(e) => {
|
|
|
|
error!("failed to compute decortions: {:?}", e)
|
|
|
|
}
|
|
|
|
Ok(params) => {
|
|
|
|
sender.send(Box::new(|io: &mut Io| {
|
|
|
|
dispatch::send_notification::<req::PublishDecorations>(io, params)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 16:12:31 -05:00
|
|
|
});
|
2018-08-10 13:13:39 -05:00
|
|
|
}
|