clean up macro crate a little bit

This commit is contained in:
Jonathan Kelley 2023-01-13 16:52:37 -08:00
parent ac6035cb2a
commit ea032eeb8e
2 changed files with 9 additions and 119 deletions

View file

@ -97,13 +97,18 @@ pub fn collect_from_expr<'a>(expr: &'a Expr, macros: &mut Vec<CollectedMacro<'a>
Expr::Async(b) => collect_from_block(&b.block, macros), Expr::Async(b) => collect_from_block(&b.block, macros),
Expr::Block(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) => { Expr::Call(c) => {
collect_from_expr(&c.func, macros); collect_from_expr(&c.func, macros);
for expr in c.args.iter() { for expr in c.args.iter() {
collect_from_expr(expr, macros); collect_from_expr(expr, macros);
} }
} }
Expr::Closure(c) => collect_from_expr(&c.body, macros),
Expr::ForLoop(b) => { Expr::ForLoop(b) => {
collect_from_expr(&b.expr, macros); collect_from_expr(&b.expr, macros);
collect_from_block(&b.body, macros); collect_from_block(&b.body, macros);
@ -121,17 +126,12 @@ pub fn collect_from_expr<'a>(expr: &'a Expr, macros: &mut Vec<CollectedMacro<'a>
} }
} }
Expr::Let(l) => collect_from_expr(&l.expr, macros),
Expr::Return(r) => { Expr::Return(r) => {
if let Some(expr) = &r.expr { if let Some(expr) = &r.expr {
collect_from_expr(expr, macros); collect_from_expr(expr, macros);
} }
} }
Expr::Loop(l) => {
collect_from_block(&l.body, macros);
}
Expr::Match(l) => { Expr::Match(l) => {
collect_from_expr(&l.expr, macros); collect_from_expr(&l.expr, macros);
for arm in l.arms.iter() { for arm in l.arms.iter() {
@ -143,10 +143,6 @@ pub fn collect_from_expr<'a>(expr: &'a Expr, macros: &mut Vec<CollectedMacro<'a>
} }
} }
Expr::Unsafe(u) => {
collect_from_block(&u.block, macros);
}
Expr::While(w) => { Expr::While(w) => {
collect_from_expr(&w.cond, macros); collect_from_expr(&w.cond, macros);
collect_from_block(&w.body, macros); collect_from_block(&w.body, macros);
@ -198,15 +194,8 @@ pub fn byte_offset(input: &str, location: LineColumn) -> usize {
#[test] #[test]
fn parses_file_and_collects_rsx_macros() { fn parses_file_and_collects_rsx_macros() {
let contents = include_str!("../tests/samples/long.rsx"); let contents = include_str!("../tests/samples/long.rsx");
let parsed = syn::parse_file(contents).unwrap(); let parsed = syn::parse_file(contents).unwrap();
let mut macros = vec![]; let mut macros = vec![];
collect_from_file(&parsed, &mut macros); collect_from_file(&parsed, &mut macros);
assert_eq!(macros.len(), 3);
println!("Collected {} macros", macros.len());
for item in macros {
println!("Found macro: {:#?}", item.path.segments[0].ident.span());
}
} }

View file

@ -1,10 +1,8 @@
use crate::writer::*;
use collect_macros::byte_offset; use collect_macros::byte_offset;
use dioxus_rsx::CallBody; use dioxus_rsx::CallBody;
use proc_macro2::LineColumn; use proc_macro2::LineColumn;
use syn::ExprMacro; use syn::{ExprMacro, MacroDelimiter};
use syn::MacroDelimiter;
use crate::writer::*;
mod buffer; mod buffer;
mod collect_macros; mod collect_macros;
@ -221,100 +219,3 @@ pub fn apply_formats(input: &str, blocks: Vec<FormattedBlock>) -> String {
out 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::<ExprMacro>(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::<Vec<_>>();
// dbg!(lines);
// let close = lines
// .iter()
// .skip(1)
// .take(brace.span.end().line - 1)
// .map(|f| f.len())
// .sum::<usize>()
// + brace.span.end().column
// + brace.span.end().line
// - 1;
// // let body = syn::parse::<CallBody>(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,
// });
// }