feat: improve autoformatting algorithm

- tons of new tests for various cases to be consistent
This commit is contained in:
Jonathan Kelley committed 2022-04-26 18:45:08 -04:00
1 parent 36ba49fe27
commit 804d9af1c7
3 files changed
+321 -44

No files matched your search

+198 -12
View File
@@ -75,6 +75,103 @@ fn formats_component() {
print!("{formatted}");
}
#[test]
fn formats_element() {
let block = r#"
div {
a: "1234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910",
a: "123",
a: "123",
a: "123",
a: "123",
a: "123",
a: "123",
a: "123",
a: "123",
}
"#;
let formatted = fmt_block(block).unwrap();
print!("{formatted}");
}
#[test]
fn formats_element_short() {
let block = r#"
div {
a: "123",
a: "123",
a: "123",
a: "123",
a: "123",
a: "123",
a: "123",
a: "123",
a: "123",
}
"#;
let formatted = fmt_block(block).unwrap();
print!("{formatted}");
}
#[test]
fn formats_element_nested() {
let block = r#"
h3 {
class: "mb-2 text-xl font-bold",
"Invite Member"
}
"#;
let formatted = fmt_block(block).unwrap();
print!("{formatted}");
}
#[test]
fn formats_element_nested_no_trailing_tabs() {
let block = r#"
img { class: "mb-6 mx-auto h-24", src: "artemis-assets/images/friends.png", alt: "", }
"#;
let formatted = fmt_block(block).unwrap();
print!("{formatted}");
}
#[test]
fn formats_element_with_correct_indent() {
let block = r###"
div {
a { class: "py-2 px-3 bg-indigo-500 hover:bg-indigo-600 rounded text-xs text-white", href: "#",
"Send invitation"
}
}
"###;
let formatted = fmt_block(block).unwrap();
print!("{formatted}");
}
#[test]
fn small_elements_and_text_are_small() {
let block = r###"
a { class: " text-white",
"Send invitation"
}
"###;
let formatted = fmt_block(block).unwrap();
print!("{formatted}");
}
#[test]
fn formats_component_man_props() {
let block = r#"
@@ -94,6 +191,18 @@ fn formats_component_man_props() {
print!("{formatted}");
}
#[test]
fn formats_component_tiny() {
let block = r#"
Component { a: 123
}
"#;
let formatted = fmt_block(block).unwrap();
print!("{formatted}");
}
#[test]
fn formats_document() {
let block = r#"
@@ -114,6 +223,24 @@ rsx!{
print!("{formatted:?}");
}
#[test]
fn component_path_mod_style() {
let block = r#"
rsx!{
my::thing::Component {
adsasd: "asd", // this is a comment
onclick: move |_| {
let blah = 120;
},
}
}
"#;
let formatted = get_format_blocks(block);
print!("{formatted:?}");
}
#[test]
fn formats_valid_rust_src() {
let src = r#"
@@ -159,20 +286,79 @@ fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape)
let block = formatted.into_iter().next().unwrap();
// println!("{}", &src[block.start..block.end]);
// println!("{}", &src[block.start..block.end - 1]);
src.replace_range(
block.start - 1..block.end + 1,
&format!("{{ {} }}", &block.formatted),
);
}
#[test]
fn formats_multiple_blocks() {
let mut src = r#"
#[inline_props]
fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
const ICON_SIZE: u32 = 36;
rsx! {
div {
h1 {"thing"}
}
}
rsx! {
div {
Ball {
a: rsx!{
"asdasd"
}
}
}
}
}
#[inline_props]
fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
const ICON_SIZE: u32 = 36;
rsx! {
div {
h1 {"thing"}
}
}
rsx! {
div {
Ball {
a: rsx!{
"asdasd"
}
}
}
}
}
"#
.to_string();
let formatted = get_format_blocks(&src);
dbg!(&formatted);
let block = formatted.into_iter().next().unwrap();
src.replace_range(
block.start - 1..block.end + 1,
&format!("{{ {} }}", &block.formatted),
// &format!("{{ {} }}", &block.formatted),
&format!("{{ {} }}", &block.formatted),
);
println!("{src}");
println!("\tthing");
// for block in formatted {
// println!("{}", block.formatted);
// }
}