diff --git a/packages/autofmt/src/block.rs b/packages/autofmt/src/block.rs deleted file mode 100644 index 2dad1b780..000000000 --- a/packages/autofmt/src/block.rs +++ /dev/null @@ -1,64 +0,0 @@ -use crate::{util::*, FormattedBlock}; -use dioxus_rsx::*; -use std::fmt::Write; -use triple_accel::{levenshtein_search, Match}; - -#[test] -fn format_block_basic() { - let block = r#" - let a = 120; - - rsx! { - div { - h1 { "thing" } - h1 { "thing" } - h1 { "thing" } - h1 { "thing" "is whack" "but you're wacker" } - h1 { "thing" div {"special cases?" } } - div { - a: 123, - b: 456, - c: 789, - d: "hello", - } - div { - a: 123, - b: 456, - c: 789, - d: "hello", - p {} - c {} - } - - h3 { class: "asdasd", "asdasd" } - h3 { class: "mx-large bg-gray-900 tall-md-400", div { - "classy" - }} - - - // Some comments explaining my genius - div { - "comment compression" - } - - // Some comments explaining my genius - div { - // Some comments explaining my genius - a: 123, - - // comment compression - b: 456, - - // comments on attributes - c: 789, - } - } - } - "#; - - // div { class: "asdasd", p { "hello!" } } - - // let edits = get_format_blocks(block); - - // println!("{}", edits[0].formatted); -} diff --git a/packages/autofmt/src/buffer.rs b/packages/autofmt/src/buffer.rs index 46f3668be..b6769d8e2 100644 --- a/packages/autofmt/src/buffer.rs +++ b/packages/autofmt/src/buffer.rs @@ -59,6 +59,10 @@ impl Buffer { write!(self.buf, "\"{}\"", text.value()) } + pub fn consume(self) -> Option { + Some(self.buf) + } + // Push out the indent level and write each component, line by line pub fn write_body_indented(&mut self, children: &[BodyNode]) -> Result { self.indent += 1; @@ -100,94 +104,7 @@ impl Buffer { self.write_ident(child)?; } - // let mut children_iter = children.iter().enumerate().peekable(); - - // while let Some((id, child)) = children_iter.next() { - // let mut written_empty = false; - - // for line in lines[self.line..child.span().start().line - 1].iter() { - // if id == 0 && line.trim().is_empty() { - // continue; - // } - - // if !written_empty && line.is_empty() { - // writeln!(self.buf)?; - // written_empty = true; - // continue; - // } - - // if written_empty && line.is_empty() { - // continue; - // } - - // writeln!(self.buf)?; - // // self.write_tabs(indent + 1)?; - // write!(self.buf, "{}", line.trim())?; - // } - - // writeln!(self.buf)?; - // self.indented_tab()?; - // self.write_ident(lines, child)?; - - // self.line = child.span().end().line; - // } - - // for child in children { - // // Exprs handle their own indenting/line breaks - // if !matches!(child, BodyNode::RawExpr(_)) { - // self.tabbed_line()?; - // } - - // self.write_ident(lines, child)?; - // } self.indent -= 1; Ok(()) } } - -// ShortOptimization::PropsOnTop => { -// write!(self.buf, " ")?; -// self.write_attributes(attributes, true, indent)?; - -// if !children.is_empty() && !attributes.is_empty() { -// write!(self.buf, ",")?; -// } - -// if let Some(last_attr) = attributes.last() { -// self.cur_line = last_attr.name_span().end().line + 1; -// } - -// // write the children -// for (id, child) in children.iter().enumerate() { -// let mut written_empty = false; -// for line in self.lines[self.cur_line..child.span().start().line - 1].iter() { -// if id == 0 && line.trim().is_empty() { -// continue; -// } - -// if !written_empty && line.is_empty() { -// writeln!(self.buf)?; -// written_empty = true; -// continue; -// } - -// if written_empty && line.is_empty() { -// continue; -// } - -// writeln!(self.buf)?; -// // self.write_tabs(indent + 1)?; -// write!(self.buf, "{}", line.trim())?; -// } - -// writeln!(self.buf)?; -// self.write_tabs(indent + 1)?; -// self.write_ident(child, indent + 1)?; - -// self.cur_line = child.span().end().line; -// } - -// writeln!(self.buf)?; -// self.write_tabs(indent)?; -// write!(self.buf, "}}")?; -// } diff --git a/packages/autofmt/src/children.rs b/packages/autofmt/src/children.rs deleted file mode 100644 index 3b56590bc..000000000 --- a/packages/autofmt/src/children.rs +++ /dev/null @@ -1,4 +0,0 @@ -// write the body of a component or element -pub fn write_children() { - -} diff --git a/packages/autofmt/src/component.rs b/packages/autofmt/src/component.rs index 81c75c540..5b312b739 100644 --- a/packages/autofmt/src/component.rs +++ b/packages/autofmt/src/component.rs @@ -1,7 +1,7 @@ use crate::Buffer; use dioxus_rsx::*; use quote::ToTokens; -use std::fmt::{self, Result, Write}; +use std::fmt::{Result, Write}; impl Buffer { pub fn write_component( diff --git a/packages/autofmt/src/element.rs b/packages/autofmt/src/element.rs index 247c7dae1..c7cdad03a 100644 --- a/packages/autofmt/src/element.rs +++ b/packages/autofmt/src/element.rs @@ -69,7 +69,7 @@ impl Buffer { } match opt_level { - ShortOptimization::Empty => write!(self.buf, "}}")?, + ShortOptimization::Empty => {} ShortOptimization::Oneliner => { write!(self.buf, " ")?; self.write_attributes(attributes, true)?; @@ -78,12 +78,11 @@ impl Buffer { write!(self.buf, ", ")?; } - // write the children for child in children { self.write_ident(child)?; } - write!(self.buf, " }}")?; + write!(self.buf, " ")?; } ShortOptimization::PropsOnTop => { @@ -94,26 +93,19 @@ impl Buffer { write!(self.buf, ",")?; } - // write the children self.write_body_indented(children)?; - self.tabbed_line()?; - write!(self.buf, "}}")?; } ShortOptimization::NoOpt => { - // write the key - - // write the attributes self.write_attributes(attributes, false)?; - self.write_body_indented(children)?; - self.tabbed_line()?; - write!(self.buf, "}}")?; } } + write!(self.buf, "}}")?; + Ok(()) } @@ -226,19 +218,3 @@ fn is_short_attrs(attrs: &[ElementAttrNamed]) -> bool { let total_attr_len = extract_attr_len(attrs); total_attr_len < 80 } - -fn write_key() { - // if let Some(key) = key.as_ref().map(|f| f.value()) { - // if is_long_attr_list { - // self.new_line()?; - // self.write_tabs( indent + 1)?; - // } else { - // write!(self.buf, " ")?; - // } - // write!(self.buf, "key: \"{key}\"")?; - - // if !attributes.is_empty() { - // write!(self.buf, ",")?; - // } - // } -} diff --git a/packages/autofmt/src/expr.rs b/packages/autofmt/src/expr.rs index 5415f8dd4..41a866ad9 100644 --- a/packages/autofmt/src/expr.rs +++ b/packages/autofmt/src/expr.rs @@ -1,5 +1,5 @@ //! pretty printer for rsx! -use std::fmt::{self, Result, Write}; +use std::fmt::{Result, Write}; use crate::Buffer; diff --git a/packages/autofmt/src/lib.rs b/packages/autofmt/src/lib.rs index f59f74bbd..bae0b0bd1 100644 --- a/packages/autofmt/src/lib.rs +++ b/packages/autofmt/src/lib.rs @@ -1,18 +1,12 @@ -//! pretty printer for rsx code - -pub use crate::buffer::*; +use crate::buffer::*; use crate::util::*; -mod block; mod buffer; -mod children; mod component; mod element; mod expr; mod util; -// pub use block::{fmt_block, get_format_blocks}; - /// A modification to the original file to be applied by an IDE /// /// Right now this re-writes entire rsx! blocks at a time, instead of precise line-by-line changes. @@ -43,7 +37,7 @@ pub fn fmt_file(contents: &str) -> Vec { use triple_accel::{levenshtein_search, Match}; - for Match { end, k, start } in levenshtein_search(b"rsx! {", contents.as_bytes()) { + for Match { end, start, .. } in levenshtein_search(b"rsx! {", contents.as_bytes()) { // ensure the marker is not nested if start < last_bracket_end { continue; @@ -72,14 +66,14 @@ pub fn fmt_file(contents: &str) -> Vec { } pub fn fmt_block(block: &str) -> Option { - let mut buf = Buffer::default(); - buf.src = block.lines().map(|f| f.to_string()).collect(); // unnecessary clone, but eh, most files are small + let mut buf = Buffer { + src: block.lines().map(|f| f.to_string()).collect(), + ..Buffer::default() + }; - let lines = block.split('\n').collect::>(); - - for node in &syn::parse_str::(block).ok()?.roots { + for node in syn::parse_str::(block).ok()?.roots { buf.write_ident(&node).ok()?; } - Some(buf.buf) + buf.consume() } diff --git a/packages/autofmt/src/util.rs b/packages/autofmt/src/util.rs index 96db23697..9c84f98e1 100644 --- a/packages/autofmt/src/util.rs +++ b/packages/autofmt/src/util.rs @@ -1,35 +1,32 @@ use dioxus_rsx::*; -use std::fmt::Write; +// todo: use recursive or complete sizeing pub fn extract_attr_len(attributes: &[ElementAttrNamed]) -> usize { attributes .iter() .map(|attr| match &attr.attr { - ElementAttr::AttrText { name, value } => value.value().len(), - ElementAttr::AttrExpression { name, value } => 10, - ElementAttr::CustomAttrText { name, value } => value.value().len(), - ElementAttr::CustomAttrExpression { name, value } => 10, - ElementAttr::EventTokens { name, tokens } => 1000000, + ElementAttr::AttrText { value, .. } => value.value().len(), + ElementAttr::AttrExpression { .. } => 10, + ElementAttr::CustomAttrText { value, .. } => value.value().len(), + ElementAttr::CustomAttrExpression { .. } => 10, + ElementAttr::EventTokens { .. } => 1000000, }) .sum() } pub fn find_bracket_end(contents: &str) -> Option { let mut depth = 0; - let mut i = 0; - for c in contents.chars() { + for (i, c) in contents.chars().enumerate() { if c == '{' { depth += 1; } else if c == '}' { depth -= 1; - } - if depth == 0 { - return Some(i); + if depth == 0 { + return Some(i); + } } - - i += 1; } None