Przeglądaj źródła

handle weird trailing whitespace in autofmt

Jonathan Kelley 2 lat temu
rodzic
commit
983789e4c5

+ 18 - 16
packages/autofmt/src/lib.rs

@@ -46,6 +46,8 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
     use triple_accel::{levenshtein_search, Match};
 
     for Match { end, start, k } in levenshtein_search(b"rsx! {", contents.as_bytes()) {
+        let open = end;
+
         if k > 1 {
             continue;
         }
@@ -55,7 +57,7 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
             continue;
         }
 
-        let mut indent_level = {
+        let indent_level = {
             // walk backwards from start until we find a new line
             let mut lines = contents[..start].lines().rev();
             match lines.next() {
@@ -70,34 +72,34 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
             }
         };
 
-        let remaining = &contents[end - 1..];
-
-        let bracket_end = find_bracket_end(remaining).unwrap();
+        let remaining = &contents[open - 1..];
+        let close = find_bracket_end(remaining).unwrap();
+        // Move the last bracket end to the end of this block to avoid nested blocks
+        last_bracket_end = close + open - 1;
 
-        let sub_string = &remaining[1..bracket_end - 1];
+        // Format the substring, doesn't include the outer brackets
+        let substring = &remaining[1..close - 1];
 
-        last_bracket_end = bracket_end + end - 1;
+        // make sure to add back whatever weird whitespace there was at the end
+        let mut remaining_whitespace = substring.chars().rev().take_while(|c| *c == ' ').count();
 
-        let mut new = fmt_block(sub_string, indent_level).unwrap();
+        let mut new = fmt_block(substring, indent_level).unwrap();
 
+        // if the new string is not multiline, don't try to adjust the marker ending
+        // We want to trim off any indentation that there might be
         if new.len() <= 80 && !new.contains('\n') {
             new = format!(" {new} ");
-
-            // if the new string is not multiline, don't try to adjust the marker ending
-            // We want to trim off any indentation that there might be
-            indent_level = 0;
+            remaining_whitespace = 0;
         }
 
-        let end_marker = end + bracket_end - indent_level * 4 - 2;
-
-        if new == contents[end..end_marker] {
+        if new == substring {
             continue;
         }
 
         formatted_blocks.push(FormattedBlock {
             formatted: new,
-            start: end,
-            end: end_marker,
+            start: open,
+            end: last_bracket_end - remaining_whitespace - 1,
         });
     }
 

+ 2 - 1
packages/autofmt/tests/samples.rs

@@ -32,5 +32,6 @@ twoway![
     key,
     multirsx,
     commentshard,
-    emoji
+    emoji,
+    messy_indent
 ];

+ 13 - 0
packages/autofmt/tests/samples/messy_indent.rsx

@@ -0,0 +1,13 @@
+fn SaveClipboard(cx: Scope) -> Element {
+    rsx! {
+        div { class: "relative w-1/2 {align} max-w-md leading-8",
+            h2 { class: "mb-6 text-3xl leading-tight md:text-4xl md:leading-tight lg:text-3xl lg:leading-tight font-heading font-mono font-bold",
+                "{title}"
+            }
+        }
+    };
+
+    cx.render(rsx! {
+        div { "hello world", "hello world", "hello world" }
+})
+}