sync-lsp-derive 0.1.0

Macros for the sync-lsp crate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#![doc = include_str!("../README.md")]

use proc_macro::{TokenStream, Span};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::{Paren, Brace, Colon, Bracket, Semi, Eq};
use quote::{quote, ToTokens};
use syn::{
    parse_macro_input, DeriveInput, Data, Fields, Ident, Meta, Expr, Lit, ExprLit,
    ExprAssign, ExprPath, Generics, Variant, Result, Error, PathSegment,
    FieldsUnnamed, Visibility, Pat, PatStruct, Path, PatTupleStruct, PatIdent,
    FieldPat, Member, Field, Type, TypeArray, LitInt, FieldMutability, PatSlice,
    ItemImpl, ImplItemType, TypeNever, TypePath, ImplItem
};

/// This macro provides default implementations for all required types in `TypeProvider`.
/// 
/// # Example
/// ```
/// use sync_lsp::{TypeProvider, type_provider};
/// 
/// struct MyServerState;
/// 
/// #[type_provider]
/// impl TypeProvider for MyServerState {
///     type ShowMessageRequestData = u32;
///     // All other types will be set to `Option<()>`
/// }
/// ```
#[proc_macro_attribute]
pub fn type_provider(args: TokenStream, input: TokenStream) -> TokenStream {
    if !args.is_empty() {
        return Error::new(
            Span::call_site().into(),
            "The type_provider attribute does not take any arguments"
        ).to_compile_error().into()
    }

    let mut input = parse_macro_input!(input as ItemImpl);
    let mut types = Vec::new();

    let unit = Type::Verbatim(quote! {
        std::option::Option::<()>
    });

    let default = ImplItemType {
        attrs: Vec::new(),
        vis: Visibility::Inherited,
        defaultness: None,
        type_token: Default::default(),
        ident: Ident::new("Default", Span::call_site().into()),
        eq_token: Eq::default(),
        ty: Type::Never(TypeNever {
            bang_token: Default::default()
        }),
        semi_token: Semi::default(),
        generics: Generics::default()
    };

    types.push(ImplItem::Type(ImplItemType {
        ident: Ident::new("Command", Span::call_site().into()),
        ty: Type::Path(TypePath {
            qself: None,
            path: {
                let mut unit = Path {
                    leading_colon: Some(Default::default()),
                    segments: Punctuated::new()
                };

                unit.segments.push(PathSegment::from(Ident::new("sync_lsp", Span::call_site().into())));
                unit.segments.push(PathSegment::from(Ident::new("workspace", Span::call_site().into())));
                unit.segments.push(PathSegment::from(Ident::new("execute_command", Span::call_site().into())));
                unit.segments.push(PathSegment::from(Ident::new("UnitCommand", Span::call_site().into())));
                unit
            }
        }),
        ..default.clone()
    }));

    types.push(ImplItem::Type(ImplItemType {
        ident: Ident::new("CodeLensData", Span::call_site().into()),
        ty: unit.clone(),
        ..default.clone()
    }));

    types.push(ImplItem::Type(ImplItemType {
        ident: Ident::new("CompletionData", Span::call_site().into()),
        ty: unit.clone(),
        ..default.clone()
    }));

    types.push(ImplItem::Type(ImplItemType {
        ident: Ident::new("Configuration", Span::call_site().into()),
        ty: unit.clone(),
        ..default.clone()
    }));

    types.push(ImplItem::Type(ImplItemType {
        ident: Ident::new("InitializeOptions", Span::call_site().into()),
        ty: unit.clone(),
        ..default.clone()
    }));

    types.push(ImplItem::Type(ImplItemType {
        ident: Ident::new("ShowMessageRequestData", Span::call_site().into()),
        ty: unit.clone(),
        ..default.clone()
    }));

    types.push(ImplItem::Type(ImplItemType {
        ident: Ident::new("ApplyEditData", Span::call_site().into()),
        ty: unit.clone(),
        ..default.clone()
    }));

    for item in input.items.iter() {
        let ImplItem::Type(item) = item else { continue };
        types.retain(|r#type| {
            let ImplItem::Type(r#type) = r#type else { return false };
            r#type.ident != item.ident
        });
    }

    input.items.extend_from_slice(&types);

    input.into_token_stream().into()
}

/// This macro implements the [`Command`] trait for a given type.
/// the `#[command(title = "...")]` attribute can be used to define the title of the command
/// on enum variants or structs.
/// 
/// # Example
/// ```
/// use sync_lsp::workspace::execute_command::Command;
/// 
/// #[derive(Clone, Command)]
/// #[command(title = "My command without variants or arguments")]
/// struct MyCommand;
/// ```
/// ```
/// use sync_lsp::workspace::execute_command::Command;
/// 
/// #[derive(Clone, Command)]
/// enum MyCommand {
///     #[command(title = "My first command")]
///     MyCommand,
///     #[command(title = "My command with arguments")]
///     MyCommandWithArguments(u32),
/// }
/// ```
#[proc_macro_derive(Command, attributes(command))]
pub fn command(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    match input.data {
        Data::Struct(data) => {
            let variant = Variant {
                attrs: input.attrs,
                ident: Ident::new("Target", Span::call_site().into()),
                fields: data.fields,
                discriminant: None
            };
            impl_enum(input.ident, input.generics, vec![variant], None)
        },
        Data::Enum(data) => {
            for attr in input.attrs.iter() {
                let Meta::List(list) = &attr.meta else { continue };
                if list.path.is_ident("command") {
                    return Error::new(
                        attr.span(), 
                        "The command attribute is not supported in this position"
                    ).to_compile_error().into()
                }
            }

            let segment = PathSegment::from(Ident::new("Target", Span::call_site().into()));
            impl_enum(input.ident, input.generics, data.variants.into_iter().collect(), Some(segment))
        },
        Data::Union(..) => panic!("Command macro cannot be implemented on unions"),
    }
        .unwrap_or_else(|err| err.to_compile_error().into())
        .into()
}

fn impl_enum(ident: Ident, generics: Generics, variants: Vec<Variant>, segment: Option<PathSegment>) -> Result<TokenStream> {
    let arguments = Ident::new("Arguments", Span::call_site().into());
    let module = Ident::new(&format!("command_{}", ident.to_string().to_lowercase()), ident.span());
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    let where_clause_extension = where_clause.map(|where_clause| &where_clause.predicates);

    let commands = get_commands(variants.iter());
    let argument_variants = get_argument_variants(variants.iter());
    let variant_patterns = get_variant_patterns(segment, variants.iter(), false);
    let argument_patterns = get_variant_patterns(Some(PathSegment::from(arguments.clone())), argument_variants.iter(), true);
    let titles = variant_titles(variants.iter())?;

    Ok(quote!(
        #[allow(non_camel_case_types)]
        mod #module {
            use super::#ident as Target;
            use sync_lsp::workspace::execute_command::Command;
            use serde::{Serialize, Deserialize};
            use serde::de::{Deserializer, DeserializeOwned};
            use serde::ser::Serializer;

            impl #impl_generics Command for Target #ty_generics where Self: Clone, #arguments #ty_generics: Serialize + DeserializeOwned + Clone, #where_clause_extension {
                fn commands() -> Vec<String> {
                    let mut commands = Vec::new();
                    #(commands.push(#commands.to_string());)*
                    commands
                }

                fn serialize<__S__: Serializer>(&self, serializer: __S__) -> Result<__S__::Ok, __S__::Error> {
                    Title {
                        title: match &self {
                            #(#variant_patterns => #titles.to_string(),)*
                        },
                        arguments: <#arguments #ty_generics as From<_>>::from(self.clone())
                    }.serialize(serializer)
                }

                fn deserialize<'__de__, __D__: Deserializer<'__de__>>(deserializer: __D__) -> Result<Self, __D__::Error> {
                    <#arguments #ty_generics as Deserialize>::deserialize(deserializer).map(Into::into)
                }
            }

            impl #impl_generics From<Target #ty_generics> for #arguments #ty_generics #where_clause {
                fn from(command: Target #ty_generics) -> Self {
                    match command {
                        #(#variant_patterns => #argument_patterns,)*
                    }
                }
            }

            impl #impl_generics Into<Target #ty_generics> for #arguments #ty_generics #where_clause {
                fn into(self) -> Target #ty_generics {
                    match self {
                        #(#argument_patterns => #variant_patterns,)*
                    }
                }
            }

            #[derive(Serialize)]
            struct Title<T> {
                title: String,
                #[serde(flatten)]
                arguments: T
            }

            #[derive(Serialize, Deserialize, Clone)]
            #[serde(tag = "command", content = "arguments")]
            enum #arguments #generics {
                #(#argument_variants,)*
            }
        }
    ).into())
}

fn get_variant_patterns<'a>(segment: Option<PathSegment>, variants: impl Iterator<Item = &'a Variant>, cast_unit: bool) -> Vec<Pat> {
    let mut patterns = Vec::new();
    
    for variant in variants {

        let path = if let Some(segment) = segment.clone() {
            let mut path = Path::from(segment);
            path.segments.push(PathSegment::from(variant.ident.clone()));
            path
        } else {
            Path::from(PathSegment::from(variant.ident.clone()))
        };


        match variant.fields {
            Fields::Named(ref named) => {
                let mut fields = Punctuated::new();

                for (index, field) in named.named.iter().enumerate() {
                    fields.push(FieldPat {
                        attrs: Vec::new(),
                        member: Member::Named(field.ident.clone().unwrap()),
                        colon_token: Some(Colon::default()),
                        pat: Box::new(Pat::Ident(PatIdent {
                            attrs: Vec::new(),
                            by_ref: None,
                            mutability: None,
                            ident: Ident::new(&format!("m{index}"), Span::call_site().into()),
                            subpat: None
                        }))
                    })
                }

                patterns.push(Pat::Struct(PatStruct {
                    attrs: Vec::new(),
                    qself: None,
                    path,
                    brace_token: Brace::default(),
                    fields,
                    rest: None
                }))
            },
            Fields::Unnamed(ref unnamed) => {
                let mut elems = Punctuated::new();

                for index in 0..unnamed.unnamed.len() {
                    elems.push(Pat::Ident(PatIdent {
                        attrs: Vec::new(),
                        by_ref: None,
                        mutability: None,
                        ident: Ident::new(&format!("m{index}"), Span::call_site().into()),
                        subpat: None
                    }));
                }
                
                if elems.len() == 1 && cast_unit {
                    let mut puncutated = Punctuated::new();
                    puncutated.push(elems.pop().unwrap().into_value());
                    elems.push(Pat::Slice(PatSlice {
                        attrs: Vec::new(),
                        bracket_token: Bracket::default(),
                        elems: puncutated
                    }))
                }

                patterns.push(Pat::TupleStruct(PatTupleStruct {
                    attrs: Vec::new(),
                    qself: None,
                    path,
                    paren_token: Paren::default(),
                    elems
                }))
            },
            Fields::Unit => {
                patterns.push(Pat::Struct(PatStruct {
                    attrs: Vec::new(),
                    qself: None,
                    path,
                    brace_token: Brace::default(),
                    fields: Punctuated::new(),
                    rest: None
                }))
            }
        }
    }

    patterns
}

fn get_argument_variants<'a>(variants: impl Iterator<Item = &'a Variant>) -> Vec<Variant> {
    variants.map(|variant| {
        let mut fields = variant.fields.clone();

        if let Fields::Named(named) = fields {
            fields = Fields::Unnamed(FieldsUnnamed {
                paren_token: Paren::default(),
                unnamed: named.named
            });
        }

        //Deserialisation will fail if the client omits arguments for a command with no arguments
        if let Fields::Unit = fields {
            fields = Fields::Unnamed(FieldsUnnamed {
                paren_token: Paren::default(),
                unnamed: Punctuated::new()
            });
        }

        //Cast commands with a single argument to an array
        if fields.len() == 1 {
            if let Fields::Unnamed(unnamed) = &mut fields {
                let first = unnamed.unnamed.pop().unwrap().into_value();
                unnamed.unnamed.push(Field {
                    attrs: Vec::new(),
                    vis: Visibility::Inherited,
                    mutability: FieldMutability::None,
                    ident: None,
                    colon_token: None,
                    ty: Type::Array(TypeArray {
                        bracket_token: Bracket::default(),
                        elem: Box::new(first.ty),
                        semi_token: Semi::default(),
                        len: Expr::Lit(ExprLit {
                            attrs: Vec::new(),
                            lit: Lit::Int(LitInt::new("1", Span::call_site().into()))
                        })
                    })
                })
            }
        }

        for field in fields.iter_mut() {
            field.attrs = Vec::new();
            field.ident = None;
            field.vis = Visibility::Inherited;
        }

        Variant {
            attrs: Vec::new(),
            ident: variant.ident.clone(),
            fields,
            discriminant: None
        }
    }).collect()
}

fn get_commands<'a>(variants: impl Iterator<Item = &'a Variant>) -> Vec<String> {
    variants.map(|variant| variant.ident.to_string()).collect()
}

fn variant_titles<'a>(variants: impl Iterator<Item = &'a Variant>) -> Result<Vec<String>> {
    let mut titles = Vec::new();
    
    for variant in variants {
        let mut title = variant.ident.to_string();

        for field in variant.fields.iter() {
            for attr in field.attrs.iter() {
                let Meta::List(list) = &attr.meta else { continue };
                if list.path.is_ident("command") {
                    return Err(Error::new(
                        attr.span(), 
                        "The command attribute is not supported on fields"
                    ))
                }
            }
        }

        for attribute in variant.attrs.iter() {
            let span = attribute.span();
            let Meta::List(list) = &attribute.meta else { continue };
            if !list.path.is_ident("command") { continue };
            let pair: ExprAssign = list.parse_args()?;

            let Expr::Path(ExprPath { path, .. }) = *pair.left else {
                return Err(Error::new(
                    span, 
                    "Expected a path for the left side of the command attribute")
                );
            };

            if !path.is_ident("title") {
                return Err(Error::new(
                    span, 
                    "The command attribute only supports the title field")
                );
            };

            let Expr::Lit(ExprLit { lit: Lit::Str(literal), .. }) = *pair.right else {
                return Err(Error::new(
                    span, 
                    "Expected a string literal for the right side of the command attribute")
                );
            };

            title = literal.value();
        }

        titles.push(title);
    }

    Ok(titles)
}