mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-23 12:43:08 +00:00
handle weird trailing whitespace in autofmt
This commit is contained in:
parent
92be7f990b
commit
983789e4c5
3 changed files with 33 additions and 17 deletions
|
@ -46,6 +46,8 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
|
||||||
use triple_accel::{levenshtein_search, Match};
|
use triple_accel::{levenshtein_search, Match};
|
||||||
|
|
||||||
for Match { end, start, k } in levenshtein_search(b"rsx! {", contents.as_bytes()) {
|
for Match { end, start, k } in levenshtein_search(b"rsx! {", contents.as_bytes()) {
|
||||||
|
let open = end;
|
||||||
|
|
||||||
if k > 1 {
|
if k > 1 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -55,7 +57,7 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut indent_level = {
|
let indent_level = {
|
||||||
// walk backwards from start until we find a new line
|
// walk backwards from start until we find a new line
|
||||||
let mut lines = contents[..start].lines().rev();
|
let mut lines = contents[..start].lines().rev();
|
||||||
match lines.next() {
|
match lines.next() {
|
||||||
|
@ -70,34 +72,34 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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(substring, indent_level).unwrap();
|
||||||
|
|
||||||
let mut new = fmt_block(sub_string, indent_level).unwrap();
|
|
||||||
|
|
||||||
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
|
// 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
|
// We want to trim off any indentation that there might be
|
||||||
indent_level = 0;
|
if new.len() <= 80 && !new.contains('\n') {
|
||||||
|
new = format!(" {new} ");
|
||||||
|
remaining_whitespace = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let end_marker = end + bracket_end - indent_level * 4 - 2;
|
if new == substring {
|
||||||
|
|
||||||
if new == contents[end..end_marker] {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
formatted_blocks.push(FormattedBlock {
|
formatted_blocks.push(FormattedBlock {
|
||||||
formatted: new,
|
formatted: new,
|
||||||
start: end,
|
start: open,
|
||||||
end: end_marker,
|
end: last_bracket_end - remaining_whitespace - 1,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,5 +32,6 @@ twoway![
|
||||||
key,
|
key,
|
||||||
multirsx,
|
multirsx,
|
||||||
commentshard,
|
commentshard,
|
||||||
emoji
|
emoji,
|
||||||
|
messy_indent
|
||||||
];
|
];
|
||||||
|
|
13
packages/autofmt/tests/samples/messy_indent.rsx
Normal file
13
packages/autofmt/tests/samples/messy_indent.rsx
Normal file
|
@ -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" }
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in a new issue