bon_macros/util/
ide.rs

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
// We must make sure this code never panics. It must be able to handle all possible inputs.
#![deny(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::unreachable,
    clippy::unimplemented,
    clippy::todo,
    clippy::exit
)]

use crate::util::prelude::*;
use proc_macro2::TokenTree;
use syn::parse::{Parse, ParseStream, Parser};
use syn::{token, Token};

pub(crate) fn parse_comma_separated_meta(input: ParseStream<'_>) -> syn::Result<Vec<Meta>> {
    let mut output = vec![];

    while !input.is_empty() {
        let value = input.parse::<Meta>()?;

        output.push(value);

        while !input.is_empty() {
            if input.peek(Token![,]) {
                input.parse::<Token![,]>()?;
                break;
            }

            // Skip the invalid token (comma is expected only).
            input.parse::<TokenTree>()?;
        }
    }

    Ok(output)
}

#[derive(Clone, Debug)]
pub(crate) enum Meta {
    None,
    Path(syn::Path),
    List(MetaList),
    NameValue(MetaNameValue),
}

impl Parse for Meta {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let path = loop {
            let path = input.call(syn::Path::parse_mod_style).ok();

            if let Some(path) = path {
                break path;
            }

            if input.parse::<TokenTree>().is_err() {
                return Ok(Self::None);
            }
        };

        let meta = if input.peek(token::Paren) {
            let content;
            syn::parenthesized!(content in input);

            Self::List(MetaList {
                path,
                tokens: content.parse()?,
            })
        } else if input.peek(token::Bracket) {
            let content;
            syn::bracketed!(content in input);

            Self::List(MetaList {
                path,
                tokens: content.parse()?,
            })
        } else if input.peek(token::Brace) {
            let content;
            syn::braced!(content in input);

            Self::List(MetaList {
                path,
                tokens: content.parse()?,
            })
        } else if input.peek(Token![=]) {
            Self::NameValue(MetaNameValue {
                path,
                eq_token: input.parse()?,
                value: input.parse().ok(),
            })
        } else {
            Self::Path(path)
        };

        Ok(meta)
    }
}

#[derive(Clone, Debug)]
pub(crate) struct MetaList {
    pub(crate) path: syn::Path,
    pub(crate) tokens: TokenStream,
}

#[derive(Clone, Debug)]
pub(crate) struct MetaNameValue {
    pub(crate) path: syn::Path,

    #[allow(dead_code)]
    pub(crate) eq_token: syn::Token![=],

    #[allow(dead_code)]
    pub(crate) value: Option<syn::Expr>,
}

fn paths_from_meta(meta: Vec<Meta>) -> Vec<syn::Path> {
    meta.into_iter()
        .filter_map(|meta| match meta {
            Meta::Path(path) => Some(path),
            Meta::NameValue(meta) => Some(meta.path),
            Meta::List(meta) => Some(meta.path),
            Meta::None => None,
        })
        .collect()
}

/// This is a highly experimental function that attempts to generate code that
/// can be used by IDEs to provide hints and completions for attributes in macros.
///
/// The idea is that we parse a potentially incomplete syntax of the macro invocation
/// and then we generate a set of modules that contain `use` statements with the
/// identifiers passed to the macro.
///
/// By placing these input identifiers in the right places inside of `use` statements
/// we can hint the IDEs to provide completions for the attributes based on what's
/// available in the module the use statement references.
pub(crate) fn generate_completion_triggers(meta: Vec<Meta>) -> TokenStream {
    let bon = meta
        .iter()
        .find_map(|meta| match meta {
            Meta::NameValue(meta) if meta.path.is_ident("crate") => Some(meta.value.as_ref()),
            _ => None,
        })
        .flatten()
        .and_then(|expr| Some(expr.require_path_mod_style().ok()?.clone()))
        .unwrap_or_else(|| syn::parse_quote!(::bon));

    let completions = CompletionsSchema::with_children(
        "builder_top_level",
        vec![
            CompletionsSchema::leaf("builder_type"),
            CompletionsSchema::leaf("start_fn"),
            CompletionsSchema::leaf("finish_fn"),
            CompletionsSchema::leaf("state_mod"),
            CompletionsSchema::leaf("on").set_custom_filter(|meta| {
                if let Some(first) = meta.first() {
                    if let Meta::Path(path) = first {
                        if path.is_ident("into")
                            || path.is_ident("required")
                            || path.is_ident("overwritable")
                        {
                            return;
                        }
                    }

                    meta.remove(0);
                }
            }),
            CompletionsSchema::leaf("derive"),
        ],
    );

    let completion_triggers = completions.generate_completion_triggers(&bon, meta, &[]);

    quote! {
        // The special `rust_analyzer` CFG is enabled only when Rust Analyzer is
        // running its code analysis. This allows us to provide code that is
        // useful only for Rust Analyzer for it to provide hints and completions.
        #[allow(unexpected_cfgs)]
        const _: () = {
            #[cfg(rust_analyzer)]
            {
                #completion_triggers
            }
        };
    }
}

struct CompletionsSchema {
    key: &'static str,
    children: Vec<CompletionsSchema>,
    custom_filter: Option<fn(&mut Vec<Meta>)>,
}

impl CompletionsSchema {
    fn leaf(key: &'static str) -> Self {
        Self {
            key,
            children: vec![],
            custom_filter: None,
        }
    }

    fn with_children(key: &'static str, children: Vec<Self>) -> Self {
        Self {
            key,
            children,
            custom_filter: None,
        }
    }

    fn set_custom_filter(mut self, custom_filter: fn(&mut Vec<Meta>)) -> Self {
        self.custom_filter = Some(custom_filter);
        self
    }

    fn generate_completion_triggers(
        &self,
        bon: &syn::Path,
        mut meta: Vec<Meta>,
        module_prefix: &[&syn::Ident],
    ) -> TokenStream {
        if let Some(custom_filter) = self.custom_filter {
            custom_filter(&mut meta);
        };

        let module_suffix = syn::Ident::new(self.key, Span::call_site());
        let module_name = module_prefix
            .iter()
            .copied()
            .chain([&module_suffix])
            .collect::<Vec<_>>();

        let child_completion_triggers = self
            .children
            .iter()
            .map(|child| {
                let child_metas = meta
                    .iter()
                    .filter_map(|meta| {
                        let meta = match meta {
                            Meta::List(meta) => meta,
                            _ => return None,
                        };

                        if !meta.path.is_ident(&child.key) {
                            return None;
                        }

                        parse_comma_separated_meta.parse2(meta.tokens.clone()).ok()
                    })
                    .concat();

                child.generate_completion_triggers(bon, child_metas, &module_name)
            })
            .collect::<Vec<_>>();

        let paths = paths_from_meta(meta);
        let module_name_snake_case =
            syn::Ident::new(&module_name.iter().join("_"), Span::call_site());

        quote! {
            mod #module_name_snake_case {
                // We separately import everything from the IDE hints module
                // and then put the `paths` in the `use self::{ ... }` statement
                // to avoid Rust Analyzer from providing completions for the
                // `self` keyword in the `use` statement. It works because
                // `use self::self` is not a valid syntax.
                use #bon::__::ide #(::#module_name)* ::*;
                use self::{ #( #paths as _, )* };
            }

            #(#child_completion_triggers)*
        }
    }
}