mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
simplify
This commit is contained in:
parent
8f1ce82753
commit
4268fbeaa1
4 changed files with 38 additions and 54 deletions
|
@ -25,11 +25,9 @@ pub use {
|
||||||
stdio::{stdio_transport, Threads},
|
stdio::{stdio_transport, Threads},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type LspServer = fn(&mut Receiver<RawMessage>, &mut Sender<RawMessage>) -> Result<()>;
|
|
||||||
|
|
||||||
pub fn run_server(
|
pub fn run_server(
|
||||||
caps: ServerCapabilities,
|
caps: ServerCapabilities,
|
||||||
server: LspServer,
|
server: impl FnOnce(&mut Receiver<RawMessage>, &mut Sender<RawMessage>) -> Result<()>,
|
||||||
mut receiver: Receiver<RawMessage>,
|
mut receiver: Receiver<RawMessage>,
|
||||||
mut sender: Sender<RawMessage>,
|
mut sender: Sender<RawMessage>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
|
|
@ -55,7 +55,13 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
fn main_inner() -> Result<()> {
|
fn main_inner() -> Result<()> {
|
||||||
let (receiver, sender, threads) = stdio_transport();
|
let (receiver, sender, threads) = stdio_transport();
|
||||||
run_server(caps::server_capabilities(), main_loop::main_loop, receiver, sender)?;
|
let root = ::std::env::current_dir()?;
|
||||||
|
run_server(
|
||||||
|
caps::server_capabilities(),
|
||||||
|
|r, s| main_loop::main_loop(root, r, s),
|
||||||
|
receiver,
|
||||||
|
sender,
|
||||||
|
)?;
|
||||||
info!("shutting down IO...");
|
info!("shutting down IO...");
|
||||||
threads.join()?;
|
threads.join()?;
|
||||||
info!("... IO is down");
|
info!("... IO is down");
|
||||||
|
|
|
@ -2,6 +2,7 @@ mod handlers;
|
||||||
mod subscriptions;
|
mod subscriptions;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
path::PathBuf,
|
||||||
collections::{HashMap},
|
collections::{HashMap},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -26,14 +27,13 @@ enum Task {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn main_loop(
|
pub(super) fn main_loop(
|
||||||
receriver: &mut Receiver<RawMessage>,
|
root: PathBuf,
|
||||||
sender: &mut Sender<RawMessage>,
|
msg_receriver: &mut Receiver<RawMessage>,
|
||||||
|
msg_sender: &mut Sender<RawMessage>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let pool = ThreadPool::new(4);
|
let pool = ThreadPool::new(4);
|
||||||
let (task_sender, task_receiver) = bounded::<Task>(16);
|
let (task_sender, task_receiver) = bounded::<Task>(16);
|
||||||
let (fs_events_receiver, watcher) = vfs::watch(vec![
|
let (fs_events_receiver, watcher) = vfs::watch(vec![root]);
|
||||||
::std::env::current_dir()?,
|
|
||||||
]);
|
|
||||||
|
|
||||||
info!("server initialized, serving requests");
|
info!("server initialized, serving requests");
|
||||||
let mut state = ServerWorldState::new();
|
let mut state = ServerWorldState::new();
|
||||||
|
@ -42,8 +42,8 @@ pub(super) fn main_loop(
|
||||||
let mut subs = Subscriptions::new();
|
let mut subs = Subscriptions::new();
|
||||||
main_loop_inner(
|
main_loop_inner(
|
||||||
&pool,
|
&pool,
|
||||||
receriver,
|
msg_receriver,
|
||||||
sender,
|
msg_sender,
|
||||||
task_receiver.clone(),
|
task_receiver.clone(),
|
||||||
task_sender,
|
task_sender,
|
||||||
fs_events_receiver,
|
fs_events_receiver,
|
||||||
|
@ -53,7 +53,7 @@ pub(super) fn main_loop(
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
info!("waiting for background jobs to finish...");
|
info!("waiting for background jobs to finish...");
|
||||||
task_receiver.for_each(drop);
|
task_receiver.for_each(|task| on_task(task, msg_sender, &mut pending_requests));
|
||||||
pool.join();
|
pool.join();
|
||||||
info!("...background jobs have finished");
|
info!("...background jobs have finished");
|
||||||
|
|
||||||
|
@ -95,22 +95,8 @@ fn main_loop_inner(
|
||||||
};
|
};
|
||||||
let mut state_changed = false;
|
let mut state_changed = false;
|
||||||
match event {
|
match event {
|
||||||
Event::FsWatcherDead => {
|
Event::FsWatcherDead => fs_receiver = None,
|
||||||
fs_receiver = None;
|
Event::Task(task) => on_task(task, msg_sender, pending_requests),
|
||||||
}
|
|
||||||
Event::Task(task) => {
|
|
||||||
match task {
|
|
||||||
Task::Respond(response) => {
|
|
||||||
if let Some(handle) = pending_requests.remove(&response.id) {
|
|
||||||
assert!(handle.has_completed());
|
|
||||||
}
|
|
||||||
msg_sender.send(RawMessage::Response(response))
|
|
||||||
}
|
|
||||||
Task::Notify(n) =>
|
|
||||||
msg_sender.send(RawMessage::Notification(n)),
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Event::Fs(events) => {
|
Event::Fs(events) => {
|
||||||
trace!("fs change, {} events", events.len());
|
trace!("fs change, {} events", events.len());
|
||||||
state.apply_fs_changes(events);
|
state.apply_fs_changes(events);
|
||||||
|
@ -158,6 +144,23 @@ fn main_loop_inner(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn on_task(
|
||||||
|
task: Task,
|
||||||
|
msg_sender: &mut Sender<RawMessage>,
|
||||||
|
pending_requests: &mut HashMap<u64, JobHandle>,
|
||||||
|
) {
|
||||||
|
match task {
|
||||||
|
Task::Respond(response) => {
|
||||||
|
if let Some(handle) = pending_requests.remove(&response.id) {
|
||||||
|
assert!(handle.has_completed());
|
||||||
|
}
|
||||||
|
msg_sender.send(RawMessage::Response(response))
|
||||||
|
}
|
||||||
|
Task::Notify(n) =>
|
||||||
|
msg_sender.send(RawMessage::Notification(n)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn on_request(
|
fn on_request(
|
||||||
world: &mut ServerWorldState,
|
world: &mut ServerWorldState,
|
||||||
pending_requests: &mut HashMap<u64, JobHandle>,
|
pending_requests: &mut HashMap<u64, JobHandle>,
|
||||||
|
@ -280,15 +283,12 @@ impl<'a> PoolDispatcher<'a> {
|
||||||
None => return Ok(self),
|
None => return Ok(self),
|
||||||
Some(req) => req,
|
Some(req) => req,
|
||||||
};
|
};
|
||||||
let world = self.world;
|
|
||||||
let sender = self.sender;
|
|
||||||
let pool = self.pool;
|
|
||||||
match req.cast::<R>() {
|
match req.cast::<R>() {
|
||||||
Ok((id, params)) => {
|
Ok((id, params)) => {
|
||||||
let (handle, token) = JobHandle::new();
|
let (handle, token) = JobHandle::new();
|
||||||
let world = world.snapshot();
|
let world = self.world.snapshot();
|
||||||
let sender = sender.clone();
|
let sender = self.sender.clone();
|
||||||
pool.execute(move || {
|
self.pool.execute(move || {
|
||||||
let resp = match f(world, params, token) {
|
let resp = match f(world, params, token) {
|
||||||
Ok(resp) => RawResponse::ok(id, resp),
|
Ok(resp) => RawResponse::ok(id, resp),
|
||||||
Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()),
|
Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()),
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use serde::{ser::Serialize, de::DeserializeOwned};
|
|
||||||
use languageserver_types::{TextDocumentIdentifier, Range, Url, Position, Location};
|
use languageserver_types::{TextDocumentIdentifier, Range, Url, Position, Location};
|
||||||
use url_serde;
|
use url_serde;
|
||||||
|
|
||||||
|
@ -18,24 +16,6 @@ pub use languageserver_types::{
|
||||||
TextDocumentEdit,
|
TextDocumentEdit,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
pub trait ClientRequest: 'static {
|
|
||||||
type Params: DeserializeOwned + Send + 'static;
|
|
||||||
type Result: Serialize + Send + 'static;
|
|
||||||
const METHOD: &'static str;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> ClientRequest for T
|
|
||||||
where T: Request + 'static,
|
|
||||||
T::Params: DeserializeOwned + Send + 'static,
|
|
||||||
T::Result: Serialize + Send + 'static,
|
|
||||||
{
|
|
||||||
type Params = <T as Request>::Params;
|
|
||||||
type Result = <T as Request>::Result;
|
|
||||||
const METHOD: &'static str = <T as Request>::METHOD;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub enum SyntaxTree {}
|
pub enum SyntaxTree {}
|
||||||
|
|
||||||
impl Request for SyntaxTree {
|
impl Request for SyntaxTree {
|
||||||
|
|
Loading…
Reference in a new issue