diff --git a/packages/autofmt/src/lib.rs b/packages/autofmt/src/lib.rs index 67f087854..afd3c3c92 100644 --- a/packages/autofmt/src/lib.rs +++ b/packages/autofmt/src/lib.rs @@ -71,8 +71,11 @@ pub fn fmt_file(contents: &str) -> Vec { }; let remaining = &contents[end - 1..]; + let bracket_end = find_bracket_end(remaining).unwrap(); - let sub_string = &contents[end..bracket_end + end - 1]; + + let sub_string = &remaining[1..bracket_end - 1]; + last_bracket_end = bracket_end + end - 1; let mut new = fmt_block(sub_string, indent_level).unwrap(); @@ -85,7 +88,7 @@ pub fn fmt_file(contents: &str) -> Vec { indent_level = 0; } - let end_marker = end + bracket_end - indent_level * 4 - 1; + let end_marker = end + bracket_end - indent_level * 4 - 2; if new == contents[end..end_marker] { continue; @@ -118,7 +121,7 @@ pub fn write_block_out(body: CallBody) -> Option { } pub fn fmt_block(block: &str, indent_level: usize) -> Option { - let body = syn::parse_str::(block).ok()?; + let body = syn::parse_str::(block).unwrap(); let mut buf = Writer { src: block.lines().map(|f| f.to_string()).collect(), diff --git a/packages/autofmt/src/util.rs b/packages/autofmt/src/util.rs index 7fd4c325c..7ffd36a9a 100644 --- a/packages/autofmt/src/util.rs +++ b/packages/autofmt/src/util.rs @@ -1,14 +1,17 @@ pub fn find_bracket_end(contents: &str) -> Option { let mut depth = 0; - for (i, c) in contents.chars().enumerate() { + let mut len = 0; + + for c in contents.chars() { + len += c.len_utf8(); if c == '{' { depth += 1; } else if c == '}' { depth -= 1; if depth == 0 { - return Some(i); + return Some(len); } } } diff --git a/packages/autofmt/tests/samples.rs b/packages/autofmt/tests/samples.rs index 19588fda0..a5752c4d5 100644 --- a/packages/autofmt/tests/samples.rs +++ b/packages/autofmt/tests/samples.rs @@ -1,35 +1,36 @@ macro_rules! twoway { - ($val:literal => $name:ident) => { - #[test] - fn $name() { - let src = include_str!(concat!("./samples/", $val, ".rsx")); - let formatted = dioxus_autofmt::fmt_file(src); - let out = dioxus_autofmt::apply_formats(src, formatted); - pretty_assertions::assert_eq!(&src, &out); - } + ( + $( + + // doc attrs + $( #[doc = $doc:expr] )* + $name:ident + ),* + ) => { + $( + $( #[doc = $doc] )* + #[test] + fn $name() { + let src = include_str!(concat!("./samples/", stringify!($name), ".rsx")); + let formatted = dioxus_autofmt::fmt_file(src); + let out = dioxus_autofmt::apply_formats(src, formatted); + pretty_assertions::assert_eq!(&src, &out); + } + )* }; } -twoway! ("simple" => simple); - -twoway! ("comments" => comments); - -twoway! ("attributes" => attributes); - -twoway! ("manual_props" => manual_props); - -twoway! ("complex" => complex); - -twoway! ("tiny" => tiny); - -twoway! ("tinynoopt" => tinynoopt); - -twoway! ("long" => long); - -twoway! ("key" => key); - -// Disabled because we can't handle comments on exprs yet -twoway! ("multirsx" => multirsx); - -// Disabled because we can't handle comments on exprs yet -twoway! ("commentshard" => commentshard); +twoway![ + simple, + comments, + attributes, + manual_props, + complex, + tiny, + tinynoopt, + long, + key, + multirsx, + commentshard, + emoji +]; diff --git a/packages/autofmt/tests/samples/emoji.rsx b/packages/autofmt/tests/samples/emoji.rsx new file mode 100644 index 000000000..066491583 --- /dev/null +++ b/packages/autofmt/tests/samples/emoji.rsx @@ -0,0 +1,5 @@ +rsx! { + div { class: "asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd", + section { "🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀" } + } +}