From 983789e4c55a81d54e083aca5e61d4d38edf5c85 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Thu, 12 Jan 2023 16:55:13 -0800 Subject: [PATCH] handle weird trailing whitespace in autofmt --- packages/autofmt/src/lib.rs | 34 ++++++++++--------- packages/autofmt/tests/samples.rs | 3 +- .../autofmt/tests/samples/messy_indent.rsx | 13 +++++++ 3 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 packages/autofmt/tests/samples/messy_indent.rsx diff --git a/packages/autofmt/src/lib.rs b/packages/autofmt/src/lib.rs index afd3c3c92..184de1003 100644 --- a/packages/autofmt/src/lib.rs +++ b/packages/autofmt/src/lib.rs @@ -46,6 +46,8 @@ pub fn fmt_file(contents: &str) -> Vec { 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 { 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 { } }; - let remaining = &contents[end - 1..]; + 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 bracket_end = find_bracket_end(remaining).unwrap(); + // Format the substring, doesn't include the outer brackets + let substring = &remaining[1..close - 1]; - let sub_string = &remaining[1..bracket_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(); - last_bracket_end = bracket_end + end - 1; - - 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, }); } diff --git a/packages/autofmt/tests/samples.rs b/packages/autofmt/tests/samples.rs index a5752c4d5..d8d53a4a0 100644 --- a/packages/autofmt/tests/samples.rs +++ b/packages/autofmt/tests/samples.rs @@ -32,5 +32,6 @@ twoway![ key, multirsx, commentshard, - emoji + emoji, + messy_indent ]; diff --git a/packages/autofmt/tests/samples/messy_indent.rsx b/packages/autofmt/tests/samples/messy_indent.rsx new file mode 100644 index 000000000..260dd08a6 --- /dev/null +++ b/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" } +}) +}