Skip to content

Commit

Permalink
Use boxed macro inside jsonrpc_client
Browse files Browse the repository at this point in the history
  • Loading branch information
efoerster committed Jun 1, 2019
1 parent 45b5f6b commit 2f6ad42
Show file tree
Hide file tree
Showing 51 changed files with 38 additions and 97 deletions.
9 changes: 7 additions & 2 deletions futures_boxed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro::{TokenStream, TokenTree};
use quote::quote;
use quote::ToTokens;
use std::iter::FromIterator;
Expand All @@ -15,7 +15,11 @@ pub fn boxed(_attr: TokenStream, item: TokenStream) -> TokenStream {
match parse::<ItemFn>(item.clone()) {
Ok(fn_) => boxed_fn(fn_),
Err(_) => {
let item: TokenStream = TokenStream::from_iter(item.into_iter().skip(1));
let item = TokenStream::from_iter(item.into_iter().filter(|x| match x {
TokenTree::Ident(x) if x.to_string() == "async" => false,
_ => true,
}));

let method: TraitItemMethod = parse(item).unwrap();
boxed_trait_method(method)
}
Expand All @@ -30,6 +34,7 @@ fn boxed_fn(fn_: ItemFn) -> TokenStream {
let tokens = quote! {
#(#attrs)*
#vis #decl {
use futures::future::FutureExt;
let task = async move #block;
task.boxed()
}
Expand Down
3 changes: 0 additions & 3 deletions jsonrpc/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::types::*;
use futures::channel::oneshot;
use futures::future::BoxFuture;
use futures::lock::Mutex;
use futures::prelude::*;
use futures_boxed::boxed;
Expand All @@ -12,8 +11,6 @@ use std::sync::Arc;

pub type Result<T> = std::result::Result<T, Error>;

pub type FutureResult<'a, T> = BoxFuture<'a, Result<T>>;

pub trait ResponseHandler {
#[boxed]
async fn handle(&self, response: Response);
Expand Down
54 changes: 18 additions & 36 deletions jsonrpc_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use std::str::FromStr;
use syn::export::TokenStream2;
use syn::*;

Expand Down Expand Up @@ -52,18 +54,12 @@ impl MethodMeta {
}

#[proc_macro_attribute]
pub fn jsonrpc_method(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
pub fn jsonrpc_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
item
}

#[proc_macro_attribute]
pub fn jsonrpc_server(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
pub fn jsonrpc_server(_attr: TokenStream, item: TokenStream) -> TokenStream {
let impl_: ItemImpl = parse_macro_input!(item);
let generics = &impl_.generics;
let self_ty = &impl_.self_ty;
Expand All @@ -75,7 +71,6 @@ pub fn jsonrpc_server(
impl #generics jsonrpc::RequestHandler for #self_ty {
#[boxed]
async fn handle_request(&self, request: jsonrpc::Request) -> jsonrpc::Response {
use futures::prelude::*;
use jsonrpc::*;

match request.method.as_str() {
Expand All @@ -99,10 +94,8 @@ pub fn jsonrpc_server(
}

#[proc_macro_attribute]
pub fn jsonrpc_client(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
pub fn jsonrpc_client(attr: TokenStream, item: TokenStream) -> TokenStream {
let item = TokenStream::from_str(&item.to_string().replace("async ", "")).unwrap();
let trait_: ItemTrait = parse_macro_input!(item);
let trait_ident = &trait_.ident;
let stubs = generate_client_stubs(&trait_.items);
Expand All @@ -113,7 +106,7 @@ pub fn jsonrpc_client(
#trait_

pub struct #struct_ident<O> {
client: std::sync::Arc<jsonrpc::Client<O>>
client: jsonrpc::Client<O>
}

impl<O> #struct_ident<O>
Expand All @@ -122,7 +115,7 @@ pub fn jsonrpc_client(
{
pub fn new(output: std::sync::Arc<futures::lock::Mutex<O>>) -> Self {
Self {
client: std::sync::Arc::new(jsonrpc::Client::new(output)),
client: jsonrpc::Client::new(output),
}
}
}
Expand All @@ -138,8 +131,9 @@ pub fn jsonrpc_client(
where
O: jsonrpc::Output,
{
fn handle(&self, response: jsonrpc::Response) -> futures::future::BoxFuture<'_, ()> {
self.client.handle(response)
#[boxed]
async fn handle(&self, response: jsonrpc::Response) -> () {
self.client.handle(response).await
}
}
};
Expand Down Expand Up @@ -195,36 +189,24 @@ fn generate_client_stubs(items: &Vec<TraitItem>) -> Vec<TokenStream2> {
let mut stubs = Vec::new();
for item in items {
let method = unwrap!(item, TraitItem::Method(x) => x);
let attrs = &method.attrs;
let sig = &method.sig;
let param = unwrap!(&sig.decl.inputs[1], FnArg::Captured(x) => &x.pat);
let meta = MethodMeta::parse(method.attrs.first().unwrap());
let meta = MethodMeta::parse(attrs.first().unwrap());
let name = &meta.name;

let stub = match meta.kind {
MethodKind::Request => quote!(
#[boxed]
#sig {
use futures::prelude::*;
use jsonrpc::*;

let client = std::sync::Arc::clone(&self.client);
let task = async move {
let result = client.send_request(#name.to_owned(), #param).await?;
serde_json::from_value(result).map_err(|_| Error::deserialize_error())
};

task.boxed()
let result = self.client.send_request(#name.to_owned(), #param).await?;
serde_json::from_value(result).map_err(|_| jsonrpc::Error::deserialize_error())
}
),
MethodKind::Notification => quote!(
#[boxed]
#sig {
use futures::prelude::*;

let client = std::sync::Arc::clone(&self.client);
let task = async move {
self.client.send_notification(#name.to_owned(), #param).await;
};

task.boxed()
self.client.send_notification(#name.to_owned(), #param).await
}
),
};
Expand Down
1 change: 0 additions & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::client::LspClient;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::*;
use serde::{Deserialize, Serialize};
Expand Down
22 changes: 13 additions & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::diagnostics::LatexLintOptions;
use crate::formatting::bibtex::BibtexFormattingOptions;
use futures::future::BoxFuture;
use futures::lock::Mutex;
use futures::prelude::*;
use futures_boxed::boxed;
use jsonrpc::client::{FutureResult, Result};
use jsonrpc::client::Result;
use jsonrpc_derive::{jsonrpc_client, jsonrpc_method};
use lsp_types::*;
use serde::Serialize;
Expand All @@ -14,22 +12,28 @@ use std::collections::HashMap;
#[jsonrpc_client(LatexLspClient)]
pub trait LspClient {
#[jsonrpc_method("workspace/configuration", kind = "request")]
fn configuration(&self, params: ConfigurationParams) -> FutureResult<'_, serde_json::Value>;
#[boxed]
async fn configuration(&self, params: ConfigurationParams) -> Result<serde_json::Value>;

#[jsonrpc_method("window/showMessage", kind = "notification")]
fn show_message(&self, params: ShowMessageParams) -> BoxFuture<'_, ()>;
#[boxed]
async fn show_message(&self, params: ShowMessageParams);

#[jsonrpc_method("client/registerCapability", kind = "request")]
fn register_capability(&self, params: RegistrationParams) -> FutureResult<'_, ()>;
#[boxed]
async fn register_capability(&self, params: RegistrationParams) -> Result<()>;

#[jsonrpc_method("textDocument/publishDiagnostics", kind = "notification")]
fn publish_diagnostics(&self, params: PublishDiagnosticsParams) -> BoxFuture<'_, ()>;
#[boxed]
async fn publish_diagnostics(&self, params: PublishDiagnosticsParams);

#[jsonrpc_method("window/progress", kind = "notification")]
fn progress(&self, params: ProgressParams) -> BoxFuture<'_, ()>;
#[boxed]
async fn progress(&self, params: ProgressParams) -> ();

#[jsonrpc_method("window/logMessage", kind = "notification")]
fn log_message(&self, params: LogMessageParams) -> BoxFuture<'_, ()>;
#[boxed]
async fn log_message(&self, params: LogMessageParams) -> ();
}

#[derive(Debug, PartialEq, Eq, Clone, Default)]
Expand Down
1 change: 0 additions & 1 deletion src/completion/bibtex/entry_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::feature::{FeatureProvider, FeatureRequest};
use crate::syntax::bibtex::BibtexDeclaration;
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};

Expand Down
1 change: 0 additions & 1 deletion src/completion/bibtex/field_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::feature::{FeatureProvider, FeatureRequest};
use crate::syntax::bibtex::BibtexNode;
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};

Expand Down
1 change: 0 additions & 1 deletion src/completion/bibtex/kernel_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::feature::{FeatureProvider, FeatureRequest};
use crate::syntax::bibtex::BibtexNode;
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/argument_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::completion::factory;
use crate::completion::latex::combinators::LatexCombinators;
use crate::data::symbols::DATABASE;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};

Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/begin_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::completion::factory;
use crate::completion::factory::LatexComponentId;
use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/citation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use crate::syntax::latex::CITATION_COMMANDS;
use crate::syntax::SyntaxTree;
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};

Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/color.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::completion::factory;
use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/color_model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::completion::factory;
use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/command_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::completion::factory::LatexComponentId;
use crate::completion::latex::combinators::LatexCombinators;
use crate::data::symbols::DATABASE;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/import.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::completion::factory;
use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::completion::factory;
use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use crate::syntax::latex::LatexCommand;
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::path::{Path, PathBuf};
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/kernel_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::completion::factory::LatexComponentId;
use crate::completion::latex::combinators::LatexCombinators;
use crate::completion::latex::kernel_primitives::KERNEL_COMMANDS;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/kernel_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::completion::factory::LatexComponentId;
use crate::completion::latex::combinators::LatexCombinators;
use crate::completion::latex::kernel_primitives::KERNEL_ENVIRONMENTS;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use crate::syntax::latex::*;
use crate::syntax::SyntaxTree;
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/pgf_library.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::completion::factory;
use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/tikz_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::completion::factory;
use crate::completion::factory::LatexComponentId;
use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/tikz_library.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::completion::factory;
use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/completion/latex/user_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::completion::latex::combinators::LatexCombinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use futures::prelude::*;
use futures_boxed::boxed;
use itertools::Itertools;
use lsp_types::{CompletionItem, CompletionParams};
Expand Down
1 change: 0 additions & 1 deletion src/completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use self::latex::tikz_library::LatexTikzLibraryCompletionProvider;
use self::latex::user_command::LatexUserCommandCompletionProvider;
use self::quality::OrderByQualityCompletionProvider;
use crate::feature::{ConcatProvider, FeatureProvider, FeatureRequest};
use futures::prelude::*;
use futures_boxed::boxed;
use itertools::Itertools;
use lsp_types::{CompletionItem, CompletionParams};
Expand Down
1 change: 0 additions & 1 deletion src/completion/quality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::syntax::latex::*;
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use crate::workspace::Document;
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams, Position};
use std::borrow::Cow;
Expand Down
1 change: 0 additions & 1 deletion src/definition/latex_citation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::syntax::latex::{LatexCitation, LatexToken};
use crate::syntax::text::SyntaxNode;
use crate::syntax::SyntaxTree;
use crate::workspace::Document;
use futures::prelude::*;
use futures_boxed::boxed;
use lsp_types::{Location, TextDocumentPositionParams};

Expand Down
Loading

0 comments on commit 2f6ad42

Please sign in to comment.