mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +00:00
Refctor
This commit is contained in:
parent
535bd7ccf7
commit
25aebb5225
2 changed files with 120 additions and 81 deletions
|
@ -31,7 +31,7 @@ use flexi_logger::Logger;
|
||||||
use libanalysis::WorldState;
|
use libanalysis::WorldState;
|
||||||
|
|
||||||
use ::{
|
use ::{
|
||||||
io::{Io, RawMsg, RawResponse, RawNotification}
|
io::{Io, RawMsg, RawResponse, RawRequest, RawNotification}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
|
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
|
||||||
|
@ -104,6 +104,7 @@ fn initialize(io: &mut Io) -> Result<()> {
|
||||||
|
|
||||||
enum Task {
|
enum Task {
|
||||||
Respond(RawResponse),
|
Respond(RawResponse),
|
||||||
|
Request(RawRequest),
|
||||||
Notify(RawNotification),
|
Notify(RawNotification),
|
||||||
Die(::failure::Error),
|
Die(::failure::Error),
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
mod handlers;
|
mod handlers;
|
||||||
|
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use threadpool::ThreadPool;
|
use threadpool::ThreadPool;
|
||||||
use crossbeam_channel::{Sender, Receiver};
|
use crossbeam_channel::{Sender, Receiver};
|
||||||
use languageserver_types::Url;
|
use languageserver_types::Url;
|
||||||
use libanalysis::{World, WorldState};
|
use libanalysis::{World, WorldState};
|
||||||
|
|
||||||
use {
|
use {
|
||||||
req, dispatch,
|
req, dispatch,
|
||||||
Task, Result,
|
Task, Result,
|
||||||
io::{Io, RawMsg, RawRequest},
|
io::{Io, RawMsg, RawRequest, RawNotification},
|
||||||
util::FilePath,
|
util::FilePath,
|
||||||
main_loop::handlers::{
|
main_loop::handlers::{
|
||||||
handle_syntax_tree,
|
handle_syntax_tree,
|
||||||
|
@ -27,6 +30,8 @@ pub(super) fn main_loop(
|
||||||
receiver: Receiver<Task>,
|
receiver: Receiver<Task>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
info!("server initialized, serving requests");
|
info!("server initialized, serving requests");
|
||||||
|
let mut next_request_id = 0;
|
||||||
|
let mut pending_requests: HashSet<u64> = HashSet::new();
|
||||||
loop {
|
loop {
|
||||||
enum Event {
|
enum Event {
|
||||||
Msg(RawMsg),
|
Msg(RawMsg),
|
||||||
|
@ -48,6 +53,12 @@ pub(super) fn main_loop(
|
||||||
}
|
}
|
||||||
Event::Task(task) => {
|
Event::Task(task) => {
|
||||||
match task {
|
match task {
|
||||||
|
Task::Request(mut request) => {
|
||||||
|
request.id = next_request_id;
|
||||||
|
pending_requests.insert(next_request_id);
|
||||||
|
next_request_id += 1;
|
||||||
|
io.send(RawMsg::Request(request));
|
||||||
|
}
|
||||||
Task::Respond(response) =>
|
Task::Respond(response) =>
|
||||||
io.send(RawMsg::Response(response)),
|
io.send(RawMsg::Response(response)),
|
||||||
Task::Notify(n) =>
|
Task::Notify(n) =>
|
||||||
|
@ -58,23 +69,31 @@ pub(super) fn main_loop(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Event::Msg(msg) => {
|
Event::Msg(msg) => {
|
||||||
if !on_msg(io, world, pool, &sender, msg)? {
|
match msg {
|
||||||
|
RawMsg::Request(req) => {
|
||||||
|
if !on_request(io, world, pool, &sender, req)? {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
RawMsg::Notification(not) => {
|
||||||
|
on_notification(io, world, pool, &sender, not)?
|
||||||
|
}
|
||||||
|
RawMsg::Response(resp) => {
|
||||||
|
error!("unexpected response: {:?}", resp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_msg(
|
fn on_request(
|
||||||
io: &mut Io,
|
io: &mut Io,
|
||||||
world: &mut WorldState,
|
world: &WorldState,
|
||||||
pool: &mut ThreadPool,
|
pool: &ThreadPool,
|
||||||
sender: &Sender<Task>,
|
sender: &Sender<Task>,
|
||||||
msg: RawMsg,
|
req: RawRequest,
|
||||||
) -> Result<bool> {
|
) -> Result<bool> {
|
||||||
match msg {
|
|
||||||
RawMsg::Request(req) => {
|
|
||||||
let mut req = Some(req);
|
let mut req = Some(req);
|
||||||
handle_request_on_threadpool::<req::SyntaxTree>(
|
handle_request_on_threadpool::<req::SyntaxTree>(
|
||||||
&mut req, pool, world, sender, handle_syntax_tree,
|
&mut req, pool, world, sender, handle_syntax_tree,
|
||||||
|
@ -88,6 +107,22 @@ fn on_msg(
|
||||||
handle_request_on_threadpool::<req::CodeActionRequest>(
|
handle_request_on_threadpool::<req::CodeActionRequest>(
|
||||||
&mut req, pool, world, sender, handle_code_action,
|
&mut req, pool, world, sender, handle_code_action,
|
||||||
)?;
|
)?;
|
||||||
|
// dispatch::handle_request::<req::ExecuteCommand, _>(&mut req, |(), resp| {
|
||||||
|
// let world = world.snapshot();
|
||||||
|
// let sender = sender.clone();
|
||||||
|
// pool.execute(move || {
|
||||||
|
// let task = match handle_execute_command(world, params) {
|
||||||
|
// Ok(req) => Task::Request(req),
|
||||||
|
// Err(e) => Task::Die(e),
|
||||||
|
// };
|
||||||
|
// sender.send(task)
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// let resp = resp.into_response(Ok(()))?;
|
||||||
|
// io.send(RawMsg::Response(resp));
|
||||||
|
// shutdown = true;
|
||||||
|
// Ok(())
|
||||||
|
// })?;
|
||||||
|
|
||||||
let mut shutdown = false;
|
let mut shutdown = false;
|
||||||
dispatch::handle_request::<req::Shutdown, _>(&mut req, |(), resp| {
|
dispatch::handle_request::<req::Shutdown, _>(&mut req, |(), resp| {
|
||||||
|
@ -104,8 +139,16 @@ fn on_msg(
|
||||||
error!("unknown method: {:?}", req);
|
error!("unknown method: {:?}", req);
|
||||||
io.send(RawMsg::Response(dispatch::unknown_method(req.id)?));
|
io.send(RawMsg::Response(dispatch::unknown_method(req.id)?));
|
||||||
}
|
}
|
||||||
}
|
Ok(true)
|
||||||
RawMsg::Notification(not) => {
|
}
|
||||||
|
|
||||||
|
fn on_notification(
|
||||||
|
io: &mut Io,
|
||||||
|
world: &mut WorldState,
|
||||||
|
pool: &ThreadPool,
|
||||||
|
sender: &Sender<Task>,
|
||||||
|
not: RawNotification,
|
||||||
|
) -> Result<()> {
|
||||||
let mut not = Some(not);
|
let mut not = Some(not);
|
||||||
dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| {
|
dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| {
|
||||||
let path = params.text_document.file_path()?;
|
let path = params.text_document.file_path()?;
|
||||||
|
@ -139,14 +182,9 @@ fn on_msg(
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if let Some(not) = not {
|
if let Some(not) = not {
|
||||||
error!("unhandled notification: {:?}", not)
|
error!("unhandled notification: {:?}", not);
|
||||||
}
|
}
|
||||||
}
|
Ok(())
|
||||||
msg => {
|
|
||||||
eprintln!("msg = {:?}", msg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_request_on_threadpool<R: req::ClientRequest>(
|
fn handle_request_on_threadpool<R: req::ClientRequest>(
|
||||||
|
|
Loading…
Reference in a new issue