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 13:13:39 -05:00
|
|
|
mod handlers;
|
2018-08-10 16:12:31 -05:00
|
|
|
mod util;
|
2018-08-12 13:02:56 -05:00
|
|
|
mod conv;
|
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-11 06:44:12 -05:00
|
|
|
use languageserver_types::Url;
|
2018-08-10 15:45:35 -05:00
|
|
|
use libanalysis::{WorldState, World};
|
2018-08-10 09:49:45 -05:00
|
|
|
|
|
|
|
use ::{
|
2018-08-12 13:34:17 -05:00
|
|
|
io::{Io, RawMsg, RawRequest, RawResponse, RawNotification},
|
2018-08-11 06:44:12 -05:00
|
|
|
handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics, publish_decorations,
|
2018-08-12 13:02:56 -05:00
|
|
|
handle_document_symbol, handle_code_action},
|
2018-08-12 13:34:17 -05:00
|
|
|
util::FilePath,
|
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<()> {
|
|
|
|
loop {
|
|
|
|
match io.recv()? {
|
2018-08-12 13:47:27 -05:00
|
|
|
RawMsg::Notification(n) =>
|
|
|
|
bail!("expected initialize request, got {:?}", n),
|
|
|
|
RawMsg::Response(res) =>
|
|
|
|
bail!("expected initialize request, got {:?}", res),
|
|
|
|
|
2018-08-10 07:07:43 -05:00
|
|
|
RawMsg::Request(req) => {
|
2018-08-12 13:45:03 -05:00
|
|
|
let mut req = Some(req);
|
|
|
|
dispatch::handle_request::<req::Initialize, _>(&mut req, |_params, resp| {
|
2018-08-12 13:34:17 -05:00
|
|
|
let res = req::InitializeResult { capabilities: caps::SERVER_CAPABILITIES };
|
|
|
|
let resp = resp.into_response(Ok(res))?;
|
|
|
|
io.send(RawMsg::Response(resp));
|
2018-08-12 13:45:03 -05:00
|
|
|
Ok(())
|
|
|
|
})?;
|
2018-08-12 13:47:27 -05:00
|
|
|
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 13:47:27 -05:00
|
|
|
_ => bail!("expected initialized notification"),
|
2018-08-10 07:07:43 -05:00
|
|
|
}
|
2018-08-12 13:47:27 -05:00
|
|
|
break;
|
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");
|
|
|
|
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,
|
2018-08-12 13:34:17 -05:00
|
|
|
sender: Sender<Task>,
|
|
|
|
receiver: Receiver<Task>,
|
2018-08-10 09:49:45 -05:00
|
|
|
) -> 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),
|
2018-08-12 13:34:17 -05:00
|
|
|
Task(Task),
|
2018-08-10 09:49:45 -05:00
|
|
|
ReceiverDead,
|
|
|
|
}
|
|
|
|
|
|
|
|
let event = select! {
|
|
|
|
recv(io.receiver(), msg) => match msg {
|
|
|
|
Some(msg) => Event::Msg(msg),
|
|
|
|
None => Event::ReceiverDead,
|
|
|
|
},
|
2018-08-12 13:34:17 -05:00
|
|
|
recv(receiver, task) => Event::Task(task.unwrap()),
|
2018-08-10 09:49:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let msg = match event {
|
|
|
|
Event::ReceiverDead => {
|
|
|
|
io.cleanup_receiver()?;
|
|
|
|
unreachable!();
|
|
|
|
}
|
2018-08-12 13:34:17 -05:00
|
|
|
Event::Task(task) => {
|
|
|
|
match task {
|
|
|
|
Task::Respond(response) =>
|
|
|
|
io.send(RawMsg::Response(response)),
|
|
|
|
Task::Notify(n) =>
|
|
|
|
io.send(RawMsg::Notification(n)),
|
|
|
|
Task::Die(error) =>
|
|
|
|
return Err(error),
|
|
|
|
}
|
2018-08-10 09:49:45 -05:00
|
|
|
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>(
|
2018-08-12 13:34:17 -05:00
|
|
|
&mut req, pool, world, &sender, handle_syntax_tree,
|
2018-08-10 15:45:35 -05:00
|
|
|
)?;
|
|
|
|
handle_request_on_threadpool::<req::ExtendSelection>(
|
2018-08-12 13:34:17 -05:00
|
|
|
&mut req, pool, world, &sender, handle_extend_selection,
|
2018-08-10 15:45:35 -05:00
|
|
|
)?;
|
2018-08-11 06:44:12 -05:00
|
|
|
handle_request_on_threadpool::<req::DocumentSymbolRequest>(
|
2018-08-12 13:34:17 -05:00
|
|
|
&mut req, pool, world, &sender, handle_document_symbol,
|
2018-08-11 06:44:12 -05:00
|
|
|
)?;
|
2018-08-12 13:02:56 -05:00
|
|
|
handle_request_on_threadpool::<req::CodeActionRequest>(
|
2018-08-12 13:34:17 -05:00
|
|
|
&mut req, pool, world, &sender, handle_code_action,
|
2018-08-12 13:02:56 -05:00
|
|
|
)?;
|
|
|
|
|
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-12 13:34:17 -05:00
|
|
|
let resp = resp.into_response(Ok(()))?;
|
|
|
|
io.send(RawMsg::Response(resp));
|
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-12 13:34:17 -05:00
|
|
|
let not = req::PublishDiagnosticsParams {
|
2018-08-10 15:30:11 -05:00
|
|
|
uri: params.text_document.uri,
|
|
|
|
diagnostics: Vec::new(),
|
2018-08-12 13:34:17 -05:00
|
|
|
};
|
|
|
|
let not = dispatch::send_notification::<req::PublishDiagnostics>(not);
|
|
|
|
io.send(RawMsg::Notification(not));
|
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,
|
2018-08-12 13:34:17 -05:00
|
|
|
sender: &Sender<Task>,
|
2018-08-10 15:45:35 -05:00
|
|
|
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);
|
2018-08-12 13:34:17 -05:00
|
|
|
let task = match resp.into_response(res) {
|
|
|
|
Ok(resp) => Task::Respond(resp),
|
|
|
|
Err(e) => Task::Die(e),
|
|
|
|
};
|
|
|
|
sender.send(task);
|
2018-08-10 15:45:35 -05:00
|
|
|
});
|
|
|
|
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,
|
2018-08-12 13:34:17 -05:00
|
|
|
sender: Sender<Task>,
|
2018-08-10 16:12:31 -05:00
|
|
|
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) => {
|
2018-08-12 13:34:17 -05:00
|
|
|
let not = dispatch::send_notification::<req::PublishDiagnostics>(params);
|
|
|
|
sender.send(Task::Notify(not));
|
2018-08-10 16:12:31 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 16:55:32 -05:00
|
|
|
match publish_decorations(world, uri) {
|
|
|
|
Err(e) => {
|
2018-08-12 13:45:03 -05:00
|
|
|
error!("failed to compute decorations: {:?}", e)
|
2018-08-10 16:55:32 -05:00
|
|
|
}
|
|
|
|
Ok(params) => {
|
2018-08-12 13:34:17 -05:00
|
|
|
let not = dispatch::send_notification::<req::PublishDecorations>(params);
|
|
|
|
sender.send(Task::Notify(not))
|
2018-08-10 16:55:32 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-10 16:12:31 -05:00
|
|
|
});
|
2018-08-10 13:13:39 -05:00
|
|
|
}
|