dioxus-autofmt 0.7.5

Autofomatter for Dioxus RSX
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
use dioxus_rsx::CallBody;
use syn::{parse::Parser, visit_mut::VisitMut, Expr, File, Item, MacroDelimiter};

use crate::{IndentOptions, Writer};

impl Writer<'_> {
    pub fn unparse_expr(&mut self, expr: &Expr) -> String {
        unparse_expr(expr, self.raw_src, &self.out.indent)
    }

    pub fn unparse_pat(&self, pat: &syn::Pat) -> String {
        unparse_pat(pat)
    }
}

// we use weird unicode alternatives to avoid conflicts with the actual rsx! macro
const MARKER: &str = "𝕣𝕤𝕩";
const MARKER_REPLACE: &str = "𝕣𝕤𝕩! {}";

pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String {
    struct ReplaceMacros<'a> {
        src: &'a str,
        formatted_stack: Vec<String>,
        cfg: &'a IndentOptions,
    }

    impl VisitMut for ReplaceMacros<'_> {
        fn visit_macro_mut(&mut self, i: &mut syn::Macro) {
            // replace the macro with a block that roughly matches the macro
            if let Some("rsx" | "render") = i
                .path
                .segments
                .last()
                .map(|i| i.ident.to_string())
                .as_deref()
            {
                // format the macro in place
                // we'll use information about the macro to replace it with another formatted block
                // once we've written out the unparsed expr from prettyplease, we can replace
                // this dummy block with the actual formatted block
                let body = CallBody::parse_strict.parse2(i.tokens.clone()).unwrap();
                let multiline = !Writer::is_short_rsx_call(&body.body.roots);
                let mut formatted = {
                    let mut writer = Writer::new(self.src, self.cfg.clone());
                    _ = writer.write_body_nodes(&body.body.roots).ok();
                    writer.consume()
                }
                .unwrap();

                i.path = syn::parse_str(MARKER).unwrap();
                i.tokens = Default::default();

                // make sure to transform the delimiter to a brace so the marker can be found
                // an alternative approach would be to use multiple different markers that are not
                // sensitive to the delimiter.
                i.delimiter = MacroDelimiter::Brace(Default::default());

                // Push out the indent level of the formatted block if it's multiline
                if multiline || formatted.contains('\n') {
                    formatted = formatted
                        .lines()
                        .map(|line| {
                            // Don't add indentation to blank lines (avoid trailing whitespace)
                            if line.is_empty() {
                                String::new()
                            } else {
                                format!("{}{line}", self.cfg.indent_str())
                            }
                        })
                        .collect::<Vec<_>>()
                        .join("\n");
                }

                // Save this formatted block for later, when we apply it to the original expr
                self.formatted_stack.push(formatted)
            }

            syn::visit_mut::visit_macro_mut(self, i);
        }
    }

    // Visit the expr and replace the macros with formatted blocks
    let mut replacer = ReplaceMacros {
        src,
        cfg,
        formatted_stack: vec![],
    };

    // builds the expression stack
    let mut modified_expr = expr.clone();
    replacer.visit_expr_mut(&mut modified_expr);

    // now unparsed with the modified expression
    let mut unparsed = unparse_inner(&modified_expr);

    // now we can replace the macros with the formatted blocks
    for fmted in replacer.formatted_stack.drain(..) {
        let is_multiline = fmted.ends_with('}') || fmted.contains('\n');
        let is_empty = fmted.trim().is_empty();

        let mut out_fmt = String::from("rsx! {");
        if is_multiline {
            out_fmt.push('\n');
        } else if !is_empty {
            out_fmt.push(' ');
        }

        let mut whitespace = 0;

        for line in unparsed.lines() {
            if line.contains(MARKER) {
                whitespace = line.matches(cfg.indent_str()).count();
                break;
            }
        }

        let mut lines = fmted.lines().enumerate().peekable();

        while let Some((_idx, fmt_line)) = lines.next() {
            // Push the indentation (but not for blank lines - avoid trailing whitespace)
            if is_multiline && !fmt_line.is_empty() {
                out_fmt.push_str(&cfg.indent_str().repeat(whitespace));
            }

            // Calculate delta between indentations - the block indentation is too much
            out_fmt.push_str(fmt_line);

            // Push a newline if there's another line
            if lines.peek().is_some() {
                out_fmt.push('\n');
            }
        }

        if is_multiline {
            out_fmt.push('\n');
            out_fmt.push_str(&cfg.indent_str().repeat(whitespace));
        } else if !is_empty {
            out_fmt.push(' ');
        }

        // Replace the dioxus_autofmt_block__________ token with the formatted block
        out_fmt.push('}');

        unparsed = unparsed.replacen(MARKER_REPLACE, &out_fmt, 1);
        continue;
    }

    // stylistic choice to trim whitespace around the expr
    if unparsed.starts_with("{ ") && unparsed.ends_with(" }") {
        let mut out_fmt = String::new();
        out_fmt.push('{');
        out_fmt.push_str(&unparsed[2..unparsed.len() - 2]);
        out_fmt.push('}');
        out_fmt
    } else {
        unparsed
    }
}

/// Unparse an expression back into a string
///
/// This creates a new temporary file, parses the expression into it, and then formats the file.
/// This is a bit of a hack, but dtonlay doesn't want to support this very simple usecase, forcing us to clone the expr
pub fn unparse_inner(expr: &Expr) -> String {
    let file = wrapped(expr);
    let wrapped = prettyplease::unparse(&file);
    unwrapped(wrapped)
}

/// Unparse a pattern into a properly formatted string using prettyplease.
///
/// Wraps the pattern in `fn main() { for PAT in () {} }`, formats with prettyplease,
/// then extracts just the pattern text between "for " and " in".
pub fn unparse_pat(pat: &syn::Pat) -> String {
    let file = File {
        shebang: None,
        attrs: vec![],
        items: vec![Item::Verbatim(quote::quote! {
            fn main() {
                for #pat in () {}
            }
        })],
    };
    let formatted = prettyplease::unparse(&file);
    let after_for = formatted
        .find("for ")
        .map(|i| i + 4)
        .expect("prettyplease output missing 'for' keyword in pattern wrapper");
    let before_in = formatted[after_for..]
        .find(" in ")
        .map(|i| i + after_for)
        .expect("prettyplease output missing 'in' keyword in pattern wrapper");
    formatted[after_for..before_in].trim().to_string()
}

// Split off the fn main and then cut the tabs off the front
fn unwrapped(raw: String) -> String {
    let mut o = raw
        .strip_prefix("fn main() {\n")
        .unwrap()
        .strip_suffix("}\n")
        .unwrap()
        .lines()
        .map(|line| line.strip_prefix("    ").unwrap_or_default()) // todo: set this to tab level
        .collect::<Vec<_>>()
        .join("\n");

    // remove the semicolon
    if o.ends_with(';') {
        o.pop();
    }

    o
}

fn wrapped(expr: &Expr) -> File {
    File {
        shebang: None,
        attrs: vec![],
        items: vec![
            //
            Item::Verbatim(quote::quote! {
                fn main() {
                    #expr;
                }
            }),
        ],
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use proc_macro2::TokenStream;

    fn fmt_block_from_expr(raw: &str, tokens: TokenStream, cfg: IndentOptions) -> Option<String> {
        let body = CallBody::parse_strict.parse2(tokens).unwrap();
        let mut writer = Writer::new(raw, cfg);
        writer.write_body_nodes(&body.body.roots).ok()?;
        writer.consume()
    }

    #[test]
    fn unparses_raw() {
        let expr = syn::parse_str("1 + 1").expect("Failed to parse");
        let unparsed = prettyplease::unparse(&wrapped(&expr));
        assert_eq!(unparsed, "fn main() {\n    1 + 1;\n}\n");
    }

    #[test]
    fn weird_ifcase() {
        let contents = r##"
        fn main() {
            move |_| timer.with_mut(|t| if t.started_at.is_none() { Some(Instant::now()) } else { None })
        }
    "##;

        let expr: File = syn::parse_file(contents).unwrap();
        let out = prettyplease::unparse(&expr);
        println!("{}", out);
    }

    #[test]
    fn multiline_madness() {
        let contents = r##"
        {
        {children.is_some().then(|| rsx! {
            span {
                class: "inline-block ml-auto hover:bg-gray-500",
                onclick: move |evt| {
                    evt.cancel_bubble();
                },
                icons::icon_5 {}
                {rsx! {
                    icons::icon_6 {}
                }}
            }
        })}
        {children.is_some().then(|| rsx! {
            span {
                class: "inline-block ml-auto hover:bg-gray-500",
                onclick: move |evt| {
                    evt.cancel_bubble();
                },
                icons::icon_10 {}
            }
        })}

        }

        "##;

        let expr: Expr = syn::parse_str(contents).unwrap();
        let out = unparse_expr(&expr, contents, &IndentOptions::default());
        println!("{}", out);
    }

    #[test]
    fn write_body_no_indent() {
        let src = r##"
            span {
                class: "inline-block ml-auto hover:bg-gray-500",
                onclick: move |evt| {
                    evt.cancel_bubble();
                },
                icons::icon_10 {}
                icons::icon_10 {}
                icons::icon_10 {}
                icons::icon_10 {}
                div { "hi" }
                div { div {} }
                div { div {} div {} div {} }
                {children}
                {
                    some_big_long()
                        .some_big_long()
                        .some_big_long()
                        .some_big_long()
                        .some_big_long()
                        .some_big_long()
                }
                div { class: "px-4", {is_current.then(|| rsx! { {children} })} }
                Thing {
                    field: rsx! {
                        div { "hi" }
                        Component {
                            onrender: rsx! {
                                div { "hi" }
                                Component {
                                    onclick: move |_| {
                                        another_macro! {
                                            div { class: "max-w-lg lg:max-w-2xl mx-auto mb-16 text-center",
                                                "gomg"
                                                "hi!!"
                                                "womh"
                                            }
                                        };
                                        rsx! {
                                            div { class: "max-w-lg lg:max-w-2xl mx-auto mb-16 text-center",
                                                "gomg"
                                                "hi!!"
                                                "womh"
                                            }
                                        };
                                        println!("hi")
                                    },
                                    onrender: move |_| {
                                        let _ = 12;
                                        let r = rsx! {
                                            div { "hi" }
                                        };
                                        rsx! {
                                            div { "hi" }
                                        }
                                    }
                                }
                                {
                                    rsx! {
                                        BarChart {
                                            id: "bar-plot".to_string(),
                                            x: value,
                                            y: label
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        "##;

        let tokens: TokenStream = syn::parse_str(src).unwrap();
        let out = fmt_block_from_expr(src, tokens, IndentOptions::default()).unwrap();
        println!("{}", out);
    }

    #[test]
    fn write_component_body() {
        let src = r##"
    div { class: "px-4", {is_current.then(|| rsx! { {children} })} }
    "##;

        let tokens: TokenStream = syn::parse_str(src).unwrap();
        let out = fmt_block_from_expr(src, tokens, IndentOptions::default()).unwrap();
        println!("{}", out);
    }

    #[test]
    fn weird_macro() {
        let contents = r##"
        fn main() {
            move |_| {
                drop_macro_semi! {
                    "something_very_long_something_very_long_something_very_long_something_very_long"
                };
                let _ = drop_macro_semi! {
                    "something_very_long_something_very_long_something_very_long_something_very_long"
                };
                drop_macro_semi! {
                    "something_very_long_something_very_long_something_very_long_something_very_long"
                };
            };
        }
    "##;

        let expr: File = syn::parse_file(contents).unwrap();
        let out = prettyplease::unparse(&expr);
        println!("{}", out);
    }

    #[test]
    fn comments_on_nodes() {
        let src = r##"// hiasdasds
    div {
        attr: "value", // comment
        div {}
        "hi" // hello!
        "hi" // hello!
        "hi" // hello!
        // hi!
    }
    "##;

        let tokens: TokenStream = syn::parse_str(src).unwrap();
        let out = fmt_block_from_expr(src, tokens, IndentOptions::default()).unwrap();
        println!("{}", out);
    }
}