mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 05:08:52 +00:00
dedupe
This commit is contained in:
parent
bf42a75f1e
commit
52de08330f
3 changed files with 29 additions and 27 deletions
|
@ -9,20 +9,17 @@ use drop_bomb::DropBomb;
|
|||
|
||||
use ::{
|
||||
Result,
|
||||
req::{Request, Notification},
|
||||
req::{ClientRequest, Notification},
|
||||
io::{Io, RawMsg, RawResponse, RawRequest, RawNotification},
|
||||
};
|
||||
|
||||
pub struct Responder<R: Request> {
|
||||
pub struct Responder<R: ClientRequest> {
|
||||
id: u64,
|
||||
bomb: DropBomb,
|
||||
ph: PhantomData<R>,
|
||||
}
|
||||
|
||||
impl<R: Request> Responder<R>
|
||||
where
|
||||
R::Params: DeserializeOwned,
|
||||
R::Result: Serialize,
|
||||
impl<R: ClientRequest> Responder<R>
|
||||
{
|
||||
pub fn response(self, io: &mut Io, resp: Result<R::Result>) -> Result<()> {
|
||||
match resp {
|
||||
|
@ -52,11 +49,8 @@ impl<R: Request> Responder<R>
|
|||
}
|
||||
|
||||
|
||||
fn parse_request_as<R>(raw: RawRequest) -> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>>
|
||||
where
|
||||
R: Request,
|
||||
R::Params: DeserializeOwned,
|
||||
R::Result: Serialize,
|
||||
fn parse_request_as<R: ClientRequest>(raw: RawRequest)
|
||||
-> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>>
|
||||
{
|
||||
if raw.method != R::METHOD {
|
||||
return Ok(Err(raw));
|
||||
|
@ -73,9 +67,7 @@ fn parse_request_as<R>(raw: RawRequest) -> Result<::std::result::Result<(R::Para
|
|||
|
||||
pub fn handle_request<R, F>(req: &mut Option<RawRequest>, f: F) -> Result<()>
|
||||
where
|
||||
R: Request,
|
||||
R::Params: DeserializeOwned,
|
||||
R::Result: Serialize,
|
||||
R: ClientRequest,
|
||||
F: FnOnce(R::Params, Responder<R>) -> Result<()>
|
||||
{
|
||||
match req.take() {
|
||||
|
@ -90,11 +82,8 @@ pub fn handle_request<R, F>(req: &mut Option<RawRequest>, f: F) -> Result<()>
|
|||
}
|
||||
}
|
||||
|
||||
pub fn expect_request<R>(io: &mut Io, raw: RawRequest) -> Result<Option<(R::Params, Responder<R>)>>
|
||||
where
|
||||
R: Request,
|
||||
R::Params: DeserializeOwned,
|
||||
R::Result: Serialize,
|
||||
pub fn expect_request<R: ClientRequest>(io: &mut Io, raw: RawRequest)
|
||||
-> Result<Option<(R::Params, Responder<R>)>>
|
||||
{
|
||||
let ret = match parse_request_as::<R>(raw)? {
|
||||
Ok(x) => Some(x),
|
||||
|
|
|
@ -28,12 +28,10 @@ use threadpool::ThreadPool;
|
|||
use crossbeam_channel::{bounded, Sender, Receiver};
|
||||
use flexi_logger::Logger;
|
||||
use languageserver_types::{TextDocumentItem, VersionedTextDocumentIdentifier, TextDocumentIdentifier};
|
||||
use serde::{ser::Serialize, de::DeserializeOwned};
|
||||
use libanalysis::{WorldState, World};
|
||||
|
||||
use ::{
|
||||
io::{Io, RawMsg, RawRequest},
|
||||
req::Request,
|
||||
handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics},
|
||||
};
|
||||
|
||||
|
@ -261,17 +259,13 @@ fn main_loop(
|
|||
}
|
||||
}
|
||||
|
||||
fn handle_request_on_threadpool<R>(
|
||||
fn handle_request_on_threadpool<R: req::ClientRequest>(
|
||||
req: &mut Option<RawRequest>,
|
||||
pool: &ThreadPool,
|
||||
world: &WorldState,
|
||||
sender: &Sender<Thunk>,
|
||||
f: fn(World, R::Params) -> Result<R::Result>,
|
||||
) -> Result<()>
|
||||
where
|
||||
R: Request + Send + 'static,
|
||||
R::Params: DeserializeOwned + Send + 'static,
|
||||
R::Result: Serialize + Send + 'static,
|
||||
{
|
||||
dispatch::handle_request::<R, _>(req, |params, resp| {
|
||||
let world = world.snapshot();
|
||||
|
|
|
@ -1,10 +1,29 @@
|
|||
use serde::{ser::Serialize, de::DeserializeOwned};
|
||||
use languageserver_types::{TextDocumentIdentifier, Range};
|
||||
|
||||
pub use languageserver_types::{
|
||||
request::*, notification::*,
|
||||
InitializeResult, PublishDiagnosticsParams
|
||||
InitializeResult, PublishDiagnosticsParams,
|
||||
};
|
||||
|
||||
|
||||
pub trait ClientRequest: Send + 'static {
|
||||
type Params: DeserializeOwned + Send + 'static;
|
||||
type Result: Serialize + Send + 'static;
|
||||
const METHOD: &'static str;
|
||||
}
|
||||
|
||||
impl<T> ClientRequest for T
|
||||
where T: Request + Send + '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 {}
|
||||
|
||||
impl Request for SyntaxTree {
|
||||
|
|
Loading…
Reference in a new issue