Skip to content

Commit

Permalink
Update to syn 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
agluszak authored and ebkalderon committed Aug 10, 2023
1 parent e96d8ca commit 0ce208a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion tower-lsp-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "1", features = ["full"] }
syn = { version = "2", features = ["full"] }
46 changes: 23 additions & 23 deletions tower-lsp-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse_macro_input, AttributeArgs, FnArg, ItemTrait, Lit, Meta, MetaNameValue, NestedMeta,
ReturnType, TraitItem,
};
use syn::{parse_macro_input, FnArg, ItemTrait, LitStr, ReturnType, TraitItem};

/// Macro for generating LSP server implementation from [`lsp-types`](https://docs.rs/lsp-types).
///
Expand All @@ -18,12 +15,9 @@ use syn::{
/// as RPC handlers.
#[proc_macro_attribute]
pub fn rpc(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr_args = parse_macro_input!(attr as AttributeArgs);

match attr_args.as_slice() {
[] => {}
[NestedMeta::Meta(meta)] if meta.path().is_ident("name") => return item,
_ => panic!("unexpected attribute arguments"),
// It will be checked later in `parse_method_calls()`.
if !attr.is_empty() {
return item;
}

let lang_server_trait = parse_macro_input!(item as ItemTrait);
Expand All @@ -50,22 +44,28 @@ fn parse_method_calls(lang_server_trait: &ItemTrait) -> Vec<MethodCall> {

for item in &lang_server_trait.items {
let method = match item {
TraitItem::Method(m) => m,
TraitItem::Fn(m) => m,
_ => continue,
};

let rpc_name = method
.attrs
.iter()
.filter_map(|attr| attr.parse_args::<Meta>().ok())
.filter(|meta| meta.path().is_ident("name"))
.find_map(|meta| match meta {
Meta::NameValue(MetaNameValue {
lit: Lit::Str(lit), ..
}) => Some(lit.value().trim_matches('"').to_owned()),
_ => panic!("expected string literal for `#[rpc(name = ???)]` attribute"),
})
.expect("expected `#[rpc(name = \"foo\")]` attribute");
let mut rpc_name: Option<String> = None;

for attr in &method.attrs {
if attr.meta.path().is_ident("rpc") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("name") {
let s: LitStr = meta.value()?.parse()?;
rpc_name = Some(s.value());
Ok(())
} else {
Err(meta.error("expected `name`"))
}
})
.unwrap();
}
}

let rpc_name = rpc_name.expect("expected `#[rpc(name = \"foo\")]` attribute");

let params = method.sig.inputs.iter().nth(1).and_then(|arg| match arg {
FnArg::Typed(pat) => Some(&*pat.ty),
Expand Down

0 comments on commit 0ce208a

Please sign in to comment.