aboutsummaryrefslogtreecommitdiff
path: root/mingling_macros/src/attr/completion.rs
blob: 71acaaec9dbe8a9136f8cd70b63f42922a265400 (plain) (blame)
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
// Doc Not Optimize
use crate::res_injection::{ResourceInjection, generate_immut_resource_bindings};
use proc_macro::TokenStream;
use quote::quote;
use syn::spanned::Spanned;
use syn::{FnArg, Ident, ItemFn, Pat, PatType, Type, TypePath, parse_macro_input};

#[cfg(feature = "comp")]
#[allow(clippy::too_many_lines)]
pub(crate) fn completion_attr(attr: TokenStream, item: TokenStream) -> TokenStream {
    use crate::get_global_set;

    let previous_type_path: TypePath = if attr.is_empty() {
        return syn::Error::new(
            proc_macro2::Span::call_site(),
            "completion attribute requires a previous type argument, e.g. #[completion(HelloEntry)]",
        )
        .to_compile_error()
        .into();
    } else {
        parse_macro_input!(attr as TypePath)
    };
    let previous_type_ident = &previous_type_path.path.segments.last().unwrap().ident;

    let input_fn = parse_macro_input!(item as ItemFn);

    if input_fn.sig.asyncness.is_some() {
        return syn::Error::new(input_fn.sig.span(), "Completion function cannot be async")
            .to_compile_error()
            .into();
    }

    let sig = &input_fn.sig;
    let inputs = &sig.inputs;
    let output = &sig.output;

    // Parameter classification:
    // - Owned (non-reference) parameters are **Shell sources**: each is derived
    //   from `&ShellContext` via `From<&ShellContext>` (identity `From` covers
    //   `ShellContext` itself).
    // - `&T` / `&mut T` parameters are resource injections (unchanged).
    // - `&ShellContext` is rejected with a helpful message: use the owned
    //   `ShellContext` (or any other `From<&ShellContext>` type) instead.
    let mut derived_stmts: Vec<proc_macro2::TokenStream> = Vec::new();
    let mut call_args: Vec<proc_macro2::TokenStream> = Vec::new();
    let mut resources = Vec::new();

    for (idx, arg) in inputs.iter().enumerate() {
        match arg {
            FnArg::Typed(PatType { pat, ty, .. }) => {
                if let Type::Reference(ref_type) = &**ty {
                    // `&ShellContext` is no longer allowed: it clashes with the
                    // resource-injection semantics of references.
                    if is_shell_context_path(&ref_type.elem) {
                        return syn::Error::new(
                            ty.span(),
                            "`&ShellContext` is not supported; use the owned `ShellContext` \
                             (or any other type implementing `From<&ShellContext>`) as a value \
                             parameter",
                        )
                        .to_compile_error()
                        .into();
                    }

                    // Reference: resource injection (requires a named binding).
                    let var_name = match &**pat {
                        Pat::Ident(pat_ident) => pat_ident.ident.clone(),
                        _ => {
                            return syn::Error::new(
                                pat.span(),
                                "Resource injection parameter must be a simple identifier",
                            )
                            .to_compile_error()
                            .into();
                        }
                    };

                    // Reference: resource injection.
                    let (inner_type, is_mut) = match &*ref_type.elem {
                        Type::Path(type_path) => {
                            let is_mut = ref_type.mutability.is_some();
                            (type_path.clone(), is_mut)
                        }
                        _ => {
                            return syn::Error::new(
                                ty.span(),
                                "Reference resource type must be a type path",
                            )
                            .to_compile_error()
                            .into();
                        }
                    };
                    resources.push(ResourceInjection {
                        var_name: var_name.clone(),
                        full_type: (**ty).clone(),
                        inner_type,
                        is_ref: true,
                        is_mut,
                    });
                    call_args.push(quote! { #var_name });
                } else {
                    // Owned value: derive from `&ShellContext`. The parameter
                    // name is irrelevant (anonymous `_` is fine) since the
                    // derived binding is generated by this macro.
                    let derived_ident = Ident::new(&format!("__ctx_derived_{idx}"), pat.span());
                    derived_stmts.push(quote! {
                        let #derived_ident: #ty =
                            <#ty as ::std::convert::From<&::mingling::ShellContext>>::from(ctx);
                    });
                    call_args.push(quote! { #derived_ident });
                }
            }
            FnArg::Receiver(_) => {
                return syn::Error::new(
                    arg.span(),
                    "Completion function cannot have self parameter",
                )
                .to_compile_error()
                .into();
            }
        }
    }

    let fn_body = &input_fn.block;

    let mut fn_attrs = input_fn.attrs.clone();
    fn_attrs.retain(|attr| !attr.path().is_ident("completion"));

    let vis = &input_fn.vis;
    let fn_name = &sig.ident;

    let internal_name = format!(
        "__internal_completion_{}",
        just_fmt::snake_case!(fn_name.to_string())
    );
    let struct_name = Ident::new(&internal_name, fn_name.span());

    let program_type = crate::default_program_path();
    let mut_resources: Vec<_> = resources.iter().filter(|r| r.is_mut).collect();

    let immut_resource_stmts = generate_immut_resource_bindings(resources.iter(), &program_type);

    let fn_call = quote! { #fn_name(#(#call_args),*) };

    let inner_call = if mut_resources.is_empty() {
        fn_call
    } else {
        let mut wrapped = fn_call;
        for res in mut_resources.iter().rev() {
            let var_name = &res.var_name;
            let inner_type = &res.inner_type;
            wrapped = quote! {
                ::mingling::this::<#program_type>().modify_res(|#var_name: &mut #inner_type| {
                    #wrapped
                })
            };
        }
        wrapped
    };

    let comp_body = quote! {
        #(#derived_stmts)*
        #(#immut_resource_stmts)*
        #inner_call
    };

    // A `()` return (or no return type) means "no suggestions": map it to an
    // empty `Suggest` instead of requiring `Into<Suggest>`.
    let returns_unit = match &sig.output {
        syn::ReturnType::Default => true,
        syn::ReturnType::Type(_, ty) => {
            matches!(ty.as_ref(), syn::Type::Tuple(t) if t.elems.is_empty())
        }
    };
    let return_stmt = if returns_unit {
        quote! {
            { #comp_body };
            ::mingling::Suggest::new()
        }
    } else {
        quote! {
            let __completion_result = { #comp_body };
            ::std::convert::Into::into(__completion_result)
        }
    };

    let expanded: proc_macro2::TokenStream = quote! {
        #(#fn_attrs)*
        #[doc(hidden)]
        #[allow(non_camel_case_types)]
        #vis struct #struct_name;

        impl ::mingling::Completion for #struct_name {
            type Previous = #previous_type_path;

            fn comp(ctx: &::mingling::ShellContext) -> ::mingling::Suggest {
                #return_stmt
            }
        }

        // Keep the original function for internal use
        #(#fn_attrs)*
        #vis fn #fn_name(#inputs) #output {
            #fn_body
        }
    };

    let completion_entry = quote! {
        Self::#previous_type_ident => <#struct_name as ::mingling::Completion>::comp(ctx),
    };

    let completion_str = completion_entry.to_string();
    let variant_name = previous_type_ident.to_string();
    let span = previous_type_path.span();

    let mut completions = get_global_set(&crate::COMPLETIONS).lock().unwrap();
    if let Err(err) = crate::check_duplicate_variant(
        &completions,
        &completion_str,
        &variant_name,
        "completion",
        span,
    ) {
        return err.into();
    }
    completions.insert(completion_str);
    drop(completions);

    expanded.into()
}

/// Returns `true` when the type is a path whose last segment is `ShellContext`
/// (e.g. `ShellContext` or `mingling::ShellContext`).
fn is_shell_context_path(ty: &Type) -> bool {
    if let Type::Path(type_path) = ty {
        type_path
            .path
            .segments
            .last()
            .is_some_and(|seg| seg.ident == "ShellContext")
    } else {
        false
    }
}