2018-08-12 14:08:14 -05:00
|
|
|
mod handlers;
|
|
|
|
|
2018-08-13 05:46:05 -05:00
|
|
|
use std::{
|
|
|
|
collections::{HashSet, HashMap},
|
|
|
|
};
|
2018-08-12 16:09:30 -05:00
|
|
|
|
2018-08-12 14:08:14 -05:00
|
|
|
use threadpool::ThreadPool;
|
|
|
|
use crossbeam_channel::{Sender, Receiver};
|
|
|
|
use languageserver_types::Url;
|
2018-08-15 09:24:20 -05:00
|
|
|
use libanalysis::{World, WorldState, FileId};
|
2018-08-12 18:38:34 -05:00
|
|
|
use serde_json::to_value;
|
2018-08-12 16:09:30 -05:00
|
|
|
|
2018-08-12 14:08:14 -05:00
|
|
|
use {
|
|
|
|
req, dispatch,
|
2018-08-15 09:24:20 -05:00
|
|
|
Task, Result, PathMap,
|
2018-08-12 16:09:30 -05:00
|
|
|
io::{Io, RawMsg, RawRequest, RawNotification},
|
2018-08-13 05:46:05 -05:00
|
|
|
vfs::{FileEvent, FileEventKind},
|
2018-08-15 09:24:20 -05:00
|
|
|
conv::TryConvWith,
|
2018-08-12 14:08:14 -05:00
|
|
|
main_loop::handlers::{
|
|
|
|
handle_syntax_tree,
|
|
|
|
handle_extend_selection,
|
|
|
|
publish_diagnostics,
|
|
|
|
publish_decorations,
|
|
|
|
handle_document_symbol,
|
|
|
|
handle_code_action,
|
2018-08-12 18:38:34 -05:00
|
|
|
handle_execute_command,
|
2018-08-13 07:35:53 -05:00
|
|
|
handle_workspace_symbol,
|
2018-08-13 08:35:17 -05:00
|
|
|
handle_goto_definition,
|
2018-08-15 16:23:22 -05:00
|
|
|
handle_find_matching_brace,
|
2018-08-12 14:08:14 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub(super) fn main_loop(
|
|
|
|
io: &mut Io,
|
|
|
|
world: &mut WorldState,
|
|
|
|
pool: &mut ThreadPool,
|
2018-08-13 05:46:05 -05:00
|
|
|
task_sender: Sender<Task>,
|
|
|
|
task_receiver: Receiver<Task>,
|
|
|
|
fs_events_receiver: Receiver<Vec<FileEvent>>,
|
2018-08-12 14:08:14 -05:00
|
|
|
) -> Result<()> {
|
|
|
|
info!("server initialized, serving requests");
|
2018-08-12 16:09:30 -05:00
|
|
|
let mut next_request_id = 0;
|
|
|
|
let mut pending_requests: HashSet<u64> = HashSet::new();
|
2018-08-15 09:24:20 -05:00
|
|
|
let mut path_map = PathMap::new();
|
|
|
|
let mut mem_map: HashMap<FileId, Option<String>> = HashMap::new();
|
2018-08-13 05:46:05 -05:00
|
|
|
let mut fs_events_receiver = Some(&fs_events_receiver);
|
2018-08-12 14:08:14 -05:00
|
|
|
loop {
|
|
|
|
enum Event {
|
|
|
|
Msg(RawMsg),
|
|
|
|
Task(Task),
|
2018-08-13 05:46:05 -05:00
|
|
|
Fs(Vec<FileEvent>),
|
2018-08-12 14:08:14 -05:00
|
|
|
ReceiverDead,
|
2018-08-13 05:46:05 -05:00
|
|
|
FsWatcherDead,
|
2018-08-12 14:08:14 -05:00
|
|
|
}
|
|
|
|
let event = select! {
|
|
|
|
recv(io.receiver(), msg) => match msg {
|
|
|
|
Some(msg) => Event::Msg(msg),
|
|
|
|
None => Event::ReceiverDead,
|
|
|
|
},
|
2018-08-13 05:46:05 -05:00
|
|
|
recv(task_receiver, task) => Event::Task(task.unwrap()),
|
|
|
|
recv(fs_events_receiver, events) => match events {
|
|
|
|
Some(events) => Event::Fs(events),
|
|
|
|
None => Event::FsWatcherDead,
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
|
|
|
Event::ReceiverDead => {
|
|
|
|
io.cleanup_receiver()?;
|
|
|
|
unreachable!();
|
|
|
|
}
|
2018-08-13 05:46:05 -05:00
|
|
|
Event::FsWatcherDead => {
|
|
|
|
fs_events_receiver = None;
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
Event::Task(task) => {
|
|
|
|
match task {
|
2018-08-12 16:09:30 -05:00
|
|
|
Task::Request(mut request) => {
|
|
|
|
request.id = next_request_id;
|
|
|
|
pending_requests.insert(next_request_id);
|
|
|
|
next_request_id += 1;
|
|
|
|
io.send(RawMsg::Request(request));
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
Task::Respond(response) =>
|
|
|
|
io.send(RawMsg::Response(response)),
|
|
|
|
Task::Notify(n) =>
|
|
|
|
io.send(RawMsg::Notification(n)),
|
|
|
|
Task::Die(error) =>
|
|
|
|
return Err(error),
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-13 05:46:05 -05:00
|
|
|
Event::Fs(events) => {
|
|
|
|
trace!("fs change, {} events", events.len());
|
|
|
|
let changes = events.into_iter()
|
|
|
|
.map(|event| {
|
|
|
|
let text = match event.kind {
|
|
|
|
FileEventKind::Add(text) => Some(text),
|
|
|
|
FileEventKind::Remove => None,
|
|
|
|
};
|
|
|
|
(event.path, text)
|
|
|
|
})
|
2018-08-15 09:24:20 -05:00
|
|
|
.map(|(path, text)| {
|
|
|
|
(path_map.get_or_insert(path), text)
|
|
|
|
})
|
|
|
|
.filter_map(|(id, text)| {
|
|
|
|
if mem_map.contains_key(&id) {
|
|
|
|
mem_map.insert(id, text);
|
2018-08-13 05:46:05 -05:00
|
|
|
None
|
|
|
|
} else {
|
2018-08-15 09:24:20 -05:00
|
|
|
Some((id, text))
|
2018-08-13 05:46:05 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
world.change_files(changes);
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
Event::Msg(msg) => {
|
2018-08-12 16:09:30 -05:00
|
|
|
match msg {
|
|
|
|
RawMsg::Request(req) => {
|
2018-08-15 09:24:20 -05:00
|
|
|
if !on_request(io, world, &path_map, pool, &task_sender, req)? {
|
2018-08-12 16:09:30 -05:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RawMsg::Notification(not) => {
|
2018-08-15 09:24:20 -05:00
|
|
|
on_notification(io, world, &mut path_map, pool, &task_sender, not, &mut mem_map)?
|
2018-08-12 16:09:30 -05:00
|
|
|
}
|
|
|
|
RawMsg::Response(resp) => {
|
2018-08-12 18:38:34 -05:00
|
|
|
if !pending_requests.remove(&resp.id) {
|
|
|
|
error!("unexpected response: {:?}", resp)
|
|
|
|
}
|
2018-08-12 16:09:30 -05:00
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:09:30 -05:00
|
|
|
fn on_request(
|
2018-08-12 14:08:14 -05:00
|
|
|
io: &mut Io,
|
2018-08-12 16:09:30 -05:00
|
|
|
world: &WorldState,
|
2018-08-15 09:24:20 -05:00
|
|
|
path_map: &PathMap,
|
2018-08-12 16:09:30 -05:00
|
|
|
pool: &ThreadPool,
|
2018-08-12 14:08:14 -05:00
|
|
|
sender: &Sender<Task>,
|
2018-08-12 16:09:30 -05:00
|
|
|
req: RawRequest,
|
2018-08-12 14:08:14 -05:00
|
|
|
) -> Result<bool> {
|
2018-08-12 16:09:30 -05:00
|
|
|
let mut req = Some(req);
|
|
|
|
handle_request_on_threadpool::<req::SyntaxTree>(
|
2018-08-15 09:24:20 -05:00
|
|
|
&mut req, pool, path_map, world, sender, handle_syntax_tree,
|
2018-08-12 16:09:30 -05:00
|
|
|
)?;
|
|
|
|
handle_request_on_threadpool::<req::ExtendSelection>(
|
2018-08-15 09:24:20 -05:00
|
|
|
&mut req, pool, path_map, world, sender, handle_extend_selection,
|
2018-08-12 16:09:30 -05:00
|
|
|
)?;
|
2018-08-15 16:23:22 -05:00
|
|
|
handle_request_on_threadpool::<req::FindMatchingBrace>(
|
|
|
|
&mut req, pool, path_map, world, sender, handle_find_matching_brace,
|
|
|
|
)?;
|
2018-08-12 16:09:30 -05:00
|
|
|
handle_request_on_threadpool::<req::DocumentSymbolRequest>(
|
2018-08-15 09:24:20 -05:00
|
|
|
&mut req, pool, path_map, world, sender, handle_document_symbol,
|
2018-08-12 16:09:30 -05:00
|
|
|
)?;
|
|
|
|
handle_request_on_threadpool::<req::CodeActionRequest>(
|
2018-08-15 09:24:20 -05:00
|
|
|
&mut req, pool, path_map, world, sender, handle_code_action,
|
2018-08-12 16:09:30 -05:00
|
|
|
)?;
|
2018-08-13 07:35:53 -05:00
|
|
|
handle_request_on_threadpool::<req::WorkspaceSymbol>(
|
2018-08-15 09:24:20 -05:00
|
|
|
&mut req, pool, path_map, world, sender, handle_workspace_symbol,
|
2018-08-13 07:35:53 -05:00
|
|
|
)?;
|
2018-08-13 08:35:17 -05:00
|
|
|
handle_request_on_threadpool::<req::GotoDefinition>(
|
2018-08-15 09:24:20 -05:00
|
|
|
&mut req, pool, path_map, world, sender, handle_goto_definition,
|
2018-08-13 08:35:17 -05:00
|
|
|
)?;
|
2018-08-12 18:38:34 -05:00
|
|
|
dispatch::handle_request::<req::ExecuteCommand, _>(&mut req, |params, resp| {
|
|
|
|
io.send(RawMsg::Response(resp.into_response(Ok(None))?));
|
|
|
|
|
2018-08-16 16:18:14 -05:00
|
|
|
let world = world.snapshot({
|
|
|
|
let pm = path_map.clone();
|
|
|
|
move |id, path| pm.resolve(id, path)
|
|
|
|
});
|
2018-08-15 09:24:20 -05:00
|
|
|
let path_map = path_map.clone();
|
2018-08-12 18:38:34 -05:00
|
|
|
let sender = sender.clone();
|
|
|
|
pool.execute(move || {
|
2018-08-16 05:46:31 -05:00
|
|
|
let (edit, cursor) = match handle_execute_command(world, path_map, params) {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(e) => return sender.send(Task::Die(e)),
|
2018-08-12 18:38:34 -05:00
|
|
|
};
|
2018-08-16 05:46:31 -05:00
|
|
|
match to_value(edit) {
|
|
|
|
Err(e) => return sender.send(Task::Die(e.into())),
|
|
|
|
Ok(params) => {
|
|
|
|
let request = RawRequest {
|
|
|
|
id: 0,
|
|
|
|
method: <req::ApplyWorkspaceEdit as req::ClientRequest>::METHOD.to_string(),
|
|
|
|
params,
|
|
|
|
};
|
|
|
|
sender.send(Task::Request(request))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(cursor) = cursor {
|
|
|
|
let request = RawRequest {
|
|
|
|
id: 0,
|
|
|
|
method: <req::MoveCursor as req::ClientRequest>::METHOD.to_string(),
|
|
|
|
params: to_value(cursor).unwrap(),
|
|
|
|
};
|
|
|
|
sender.send(Task::Request(request))
|
|
|
|
}
|
2018-08-12 18:38:34 -05:00
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
})?;
|
2018-08-12 14:08:14 -05:00
|
|
|
|
2018-08-12 16:09:30 -05:00
|
|
|
let mut shutdown = false;
|
|
|
|
dispatch::handle_request::<req::Shutdown, _>(&mut req, |(), resp| {
|
|
|
|
let resp = resp.into_response(Ok(()))?;
|
|
|
|
io.send(RawMsg::Response(resp));
|
|
|
|
shutdown = true;
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
if shutdown {
|
|
|
|
info!("lifecycle: initiating shutdown");
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
if let Some(req) = req {
|
|
|
|
error!("unknown method: {:?}", req);
|
|
|
|
io.send(RawMsg::Response(dispatch::unknown_method(req.id)?));
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:09:30 -05:00
|
|
|
fn on_notification(
|
|
|
|
io: &mut Io,
|
|
|
|
world: &mut WorldState,
|
2018-08-15 09:24:20 -05:00
|
|
|
path_map: &mut PathMap,
|
2018-08-12 16:09:30 -05:00
|
|
|
pool: &ThreadPool,
|
|
|
|
sender: &Sender<Task>,
|
|
|
|
not: RawNotification,
|
2018-08-15 09:24:20 -05:00
|
|
|
mem_map: &mut HashMap<FileId, Option<String>>,
|
2018-08-12 16:09:30 -05:00
|
|
|
) -> Result<()> {
|
|
|
|
let mut not = Some(not);
|
|
|
|
dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| {
|
2018-08-15 09:24:20 -05:00
|
|
|
let uri = params.text_document.uri;
|
|
|
|
let path = uri.to_file_path()
|
|
|
|
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
|
|
|
let file_id = path_map.get_or_insert(path);
|
|
|
|
mem_map.insert(file_id, None);
|
|
|
|
world.change_file(file_id, Some(params.text_document.text));
|
2018-08-12 16:09:30 -05:00
|
|
|
update_file_notifications_on_threadpool(
|
2018-08-16 16:18:14 -05:00
|
|
|
pool,
|
|
|
|
world.snapshot({
|
|
|
|
let pm = path_map.clone();
|
|
|
|
move |id, path| pm.resolve(id, path)
|
|
|
|
}),
|
|
|
|
path_map.clone(),
|
|
|
|
sender.clone(),
|
|
|
|
uri,
|
2018-08-12 16:09:30 -05:00
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
dispatch::handle_notification::<req::DidChangeTextDocument, _>(&mut not, |mut params| {
|
2018-08-15 09:24:20 -05:00
|
|
|
let file_id = params.text_document.try_conv_with(path_map)?;
|
2018-08-12 16:09:30 -05:00
|
|
|
let text = params.content_changes.pop()
|
|
|
|
.ok_or_else(|| format_err!("empty changes"))?
|
|
|
|
.text;
|
2018-08-15 09:24:20 -05:00
|
|
|
world.change_file(file_id, Some(text));
|
2018-08-12 16:09:30 -05:00
|
|
|
update_file_notifications_on_threadpool(
|
2018-08-16 16:18:14 -05:00
|
|
|
pool,
|
|
|
|
world.snapshot({
|
|
|
|
let pm = path_map.clone();
|
|
|
|
move |id, path| pm.resolve(id, path)
|
|
|
|
}),
|
|
|
|
path_map.clone(),
|
|
|
|
sender.clone(),
|
|
|
|
params.text_document.uri,
|
2018-08-12 16:09:30 -05:00
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
dispatch::handle_notification::<req::DidCloseTextDocument, _>(&mut not, |params| {
|
2018-08-15 09:24:20 -05:00
|
|
|
let file_id = params.text_document.try_conv_with(path_map)?;
|
|
|
|
let text = match mem_map.remove(&file_id) {
|
2018-08-13 05:46:05 -05:00
|
|
|
Some(text) => text,
|
|
|
|
None => bail!("unmatched close notification"),
|
|
|
|
};
|
2018-08-15 09:24:20 -05:00
|
|
|
world.change_file(file_id, text);
|
2018-08-12 16:09:30 -05:00
|
|
|
let not = req::PublishDiagnosticsParams {
|
|
|
|
uri: params.text_document.uri,
|
|
|
|
diagnostics: Vec::new(),
|
|
|
|
};
|
|
|
|
let not = dispatch::send_notification::<req::PublishDiagnostics>(not);
|
|
|
|
io.send(RawMsg::Notification(not));
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if let Some(not) = not {
|
|
|
|
error!("unhandled notification: {:?}", not);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:08:14 -05:00
|
|
|
fn handle_request_on_threadpool<R: req::ClientRequest>(
|
|
|
|
req: &mut Option<RawRequest>,
|
|
|
|
pool: &ThreadPool,
|
2018-08-15 09:24:20 -05:00
|
|
|
path_map: &PathMap,
|
2018-08-12 14:08:14 -05:00
|
|
|
world: &WorldState,
|
|
|
|
sender: &Sender<Task>,
|
2018-08-15 09:24:20 -05:00
|
|
|
f: fn(World, PathMap, R::Params) -> Result<R::Result>,
|
2018-08-12 14:08:14 -05:00
|
|
|
) -> Result<()>
|
|
|
|
{
|
|
|
|
dispatch::handle_request::<R, _>(req, |params, resp| {
|
2018-08-16 16:18:14 -05:00
|
|
|
let world = world.snapshot({
|
|
|
|
let pm = path_map.clone();
|
|
|
|
move |id, path| pm.resolve(id, path)
|
|
|
|
});
|
2018-08-15 09:24:20 -05:00
|
|
|
let path_map = path_map.clone();
|
2018-08-12 14:08:14 -05:00
|
|
|
let sender = sender.clone();
|
|
|
|
pool.execute(move || {
|
2018-08-15 09:24:20 -05:00
|
|
|
let res = f(world, path_map, params);
|
2018-08-12 14:08:14 -05:00
|
|
|
let task = match resp.into_response(res) {
|
|
|
|
Ok(resp) => Task::Respond(resp),
|
|
|
|
Err(e) => Task::Die(e),
|
|
|
|
};
|
|
|
|
sender.send(task);
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_file_notifications_on_threadpool(
|
|
|
|
pool: &ThreadPool,
|
|
|
|
world: World,
|
2018-08-15 09:24:20 -05:00
|
|
|
path_map: PathMap,
|
2018-08-12 14:08:14 -05:00
|
|
|
sender: Sender<Task>,
|
|
|
|
uri: Url,
|
|
|
|
) {
|
|
|
|
pool.execute(move || {
|
2018-08-15 09:24:20 -05:00
|
|
|
match publish_diagnostics(world.clone(), path_map.clone(), uri.clone()) {
|
2018-08-12 14:08:14 -05:00
|
|
|
Err(e) => {
|
|
|
|
error!("failed to compute diagnostics: {:?}", e)
|
|
|
|
}
|
|
|
|
Ok(params) => {
|
|
|
|
let not = dispatch::send_notification::<req::PublishDiagnostics>(params);
|
|
|
|
sender.send(Task::Notify(not));
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 09:24:20 -05:00
|
|
|
match publish_decorations(world, path_map.clone(), uri) {
|
2018-08-12 14:08:14 -05:00
|
|
|
Err(e) => {
|
|
|
|
error!("failed to compute decorations: {:?}", e)
|
|
|
|
}
|
|
|
|
Ok(params) => {
|
|
|
|
let not = dispatch::send_notification::<req::PublishDecorations>(params);
|
|
|
|
sender.send(Task::Notify(not))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|