From ea032eeb8e5a4ecd754221a03765028f1ff33a17 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Fri, 13 Jan 2023 16:52:37 -0800 Subject: [PATCH] clean up macro crate a little bit --- packages/autofmt/src/collect_macros.rs | 25 ++---- packages/autofmt/src/lib.rs | 103 +------------------------ 2 files changed, 9 insertions(+), 119 deletions(-) diff --git a/packages/autofmt/src/collect_macros.rs b/packages/autofmt/src/collect_macros.rs index 13a2ea8ee..b2e99b127 100644 --- a/packages/autofmt/src/collect_macros.rs +++ b/packages/autofmt/src/collect_macros.rs @@ -97,13 +97,18 @@ pub fn collect_from_expr<'a>(expr: &'a Expr, macros: &mut Vec Expr::Async(b) => collect_from_block(&b.block, macros), Expr::Block(b) => collect_from_block(&b.block, macros), + Expr::Closure(c) => collect_from_expr(&c.body, macros), + Expr::Let(l) => collect_from_expr(&l.expr, macros), + Expr::Unsafe(u) => collect_from_block(&u.block, macros), + Expr::Loop(l) => collect_from_block(&l.body, macros), + Expr::Call(c) => { collect_from_expr(&c.func, macros); for expr in c.args.iter() { collect_from_expr(expr, macros); } } - Expr::Closure(c) => collect_from_expr(&c.body, macros), + Expr::ForLoop(b) => { collect_from_expr(&b.expr, macros); collect_from_block(&b.body, macros); @@ -121,17 +126,12 @@ pub fn collect_from_expr<'a>(expr: &'a Expr, macros: &mut Vec } } - Expr::Let(l) => collect_from_expr(&l.expr, macros), - Expr::Return(r) => { if let Some(expr) = &r.expr { collect_from_expr(expr, macros); } } - Expr::Loop(l) => { - collect_from_block(&l.body, macros); - } Expr::Match(l) => { collect_from_expr(&l.expr, macros); for arm in l.arms.iter() { @@ -143,10 +143,6 @@ pub fn collect_from_expr<'a>(expr: &'a Expr, macros: &mut Vec } } - Expr::Unsafe(u) => { - collect_from_block(&u.block, macros); - } - Expr::While(w) => { collect_from_expr(&w.cond, macros); collect_from_block(&w.body, macros); @@ -198,15 +194,8 @@ pub fn byte_offset(input: &str, location: LineColumn) -> usize { #[test] fn parses_file_and_collects_rsx_macros() { let contents = include_str!("../tests/samples/long.rsx"); - let parsed = syn::parse_file(contents).unwrap(); - let mut macros = vec![]; collect_from_file(&parsed, &mut macros); - - println!("Collected {} macros", macros.len()); - - for item in macros { - println!("Found macro: {:#?}", item.path.segments[0].ident.span()); - } + assert_eq!(macros.len(), 3); } diff --git a/packages/autofmt/src/lib.rs b/packages/autofmt/src/lib.rs index ae76b67c6..ba5b4cb20 100644 --- a/packages/autofmt/src/lib.rs +++ b/packages/autofmt/src/lib.rs @@ -1,10 +1,8 @@ +use crate::writer::*; use collect_macros::byte_offset; use dioxus_rsx::CallBody; use proc_macro2::LineColumn; -use syn::ExprMacro; -use syn::MacroDelimiter; - -use crate::writer::*; +use syn::{ExprMacro, MacroDelimiter}; mod buffer; mod collect_macros; @@ -221,100 +219,3 @@ pub fn apply_formats(input: &str, blocks: Vec) -> String { out } - -#[test] -fn get_some_blocks() { - let contents = include_str!("../tests/samples/long.rsx"); - - let out = fmt_file(contents); - - dbg!(out); -} - -// for Match { end, start, k } in levenshtein_search(b"rsx! {", contents.as_bytes()) { -// let open = end; - -// if k > 1 { -// continue; -// } - -// // ensure the marker is not nested -// if start < last_bracket_end { -// continue; -// } - -// let indent_level = { -// // walk backwards from start until we find a new line -// let mut lines = contents[..start].lines().rev(); -// match lines.next() { -// Some(line) => { -// if line.starts_with("//") || line.starts_with("///") { -// continue; -// } - -// line.chars().take_while(|c| *c == ' ').count() / 4 -// } -// None => 0, -// } -// }; - -// let remaining = &contents[open - 1..]; - -// let rest_with_macro = &contents[start..]; - -// dbg!(rest_with_macro); - -// let body = syn::parse_str::(rest_with_macro).unwrap(); - -// let MacroDelimiter::Brace(brace) = body.mac.delimiter else { panic!() }; -// // dbg!(brace.span.end()); -// let lines = &contents[start..] -// .lines() -// .map(|f| f.to_string()) -// .collect::>(); - -// dbg!(lines); - -// let close = lines -// .iter() -// .skip(1) -// .take(brace.span.end().line - 1) -// .map(|f| f.len()) -// .sum::() -// + brace.span.end().column -// + brace.span.end().line -// - 1; - -// // let body = syn::parse::(stream.into()).unwrap(); - -// // let close = find_bracket_end(remaining).unwrap(); - -// dbg!(close); -// // Move the last bracket end to the end of this block to avoid nested blocks -// last_bracket_end = close + open - 1; - -// // Format the substring, doesn't include the outer brackets -// let substring = &remaining[1..close - 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(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} "); -// remaining_whitespace = 0; -// } - -// if new == substring { -// continue; -// } - -// formatted_blocks.push(FormattedBlock { -// formatted: new, -// start: open, -// end: last_bracket_end - remaining_whitespace - 1, -// }); -// }