Przeglądaj źródła

fix emoji support in autofmt

Jonathan Kelley 2 lat temu
rodzic
commit
cc8b910cf3

+ 6 - 3
packages/autofmt/src/lib.rs

@@ -71,8 +71,11 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
         };
 
         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<FormattedBlock> {
             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<String> {
 }
 
 pub fn fmt_block(block: &str, indent_level: usize) -> Option<String> {
-    let body = syn::parse_str::<dioxus_rsx::CallBody>(block).ok()?;
+    let body = syn::parse_str::<dioxus_rsx::CallBody>(block).unwrap();
 
     let mut buf = Writer {
         src: block.lines().map(|f| f.to_string()).collect(),

+ 5 - 2
packages/autofmt/src/util.rs

@@ -1,14 +1,17 @@
 pub fn find_bracket_end(contents: &str) -> Option<usize> {
     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);
             }
         }
     }

+ 32 - 31
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
+];

+ 5 - 0
packages/autofmt/tests/samples/emoji.rsx

@@ -0,0 +1,5 @@
+rsx! {
+    div { class: "asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd asdasdasd",
+        section { "🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀" }
+    }
+}