|
@@ -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,
|
|
|
});
|
|
|
}
|
|
|
|