wip: add whitespace parsing to rsx
This commit is contained in:
1 parent
5313fc0cc6
commit
181a51a63b
14 files changed
+278
-24
No files matched your search
@@ -18,3 +18,71 @@ Sorted roughly in order of what's possible
|
||||
|
||||
|
||||
# Technique
|
||||
|
||||
|
||||
div {
|
||||
div {}
|
||||
div {}
|
||||
}
|
||||
|
||||
|
||||
div
|
||||
|
||||
possible line break
|
||||
div
|
||||
div
|
||||
|
||||
|
||||
|
||||
string of possible items within a nesting
|
||||
div {
|
||||
attr_pair
|
||||
expr
|
||||
text
|
||||
comment
|
||||
}
|
||||
a nesting is either a component or an element
|
||||
|
||||
idea:
|
||||
collect all items into a queue
|
||||
q
|
||||
```rust
|
||||
section {
|
||||
div {
|
||||
h1 { p { "asdasd" } }
|
||||
h1 { p { "asdasd" } }
|
||||
}
|
||||
}
|
||||
|
||||
section {}
|
||||
```
|
||||
|
||||
|
||||
// space
|
||||
// space
|
||||
// space
|
||||
|
||||
|
||||
3 - section
|
||||
3 - section div
|
||||
3 - section div h1
|
||||
3 - section div h1 p
|
||||
3 - section div h1 p text
|
||||
3 - section
|
||||
3 - section div
|
||||
3 - section div h1
|
||||
3 - section div h1 p
|
||||
3 - section div h1 p text
|
||||
|
||||
block
|
||||
|
||||
- when we hit the end of a trail, we can make a decision what needs to be hard breaked
|
||||
- most nestings cannot be merged into a single one, so at some point we need to write the line break
|
||||
- this is the scan section. we scan forward until it's obvious where to place a hard break
|
||||
- when a line is finished, we can print it out by unloading our queued items
|
||||
- never double nested
|
||||
|
||||
|
||||
Terms
|
||||
- break is a whitespace than can flex, dependent on the situation
|
||||
- ‹›
|
||||
@@ -67,8 +67,8 @@ impl Buffer {
|
||||
match node {
|
||||
BodyNode::Element(el) => self.write_element(el),
|
||||
BodyNode::Component(component) => self.write_component(component),
|
||||
BodyNode::Text(text) => self.write_text(text),
|
||||
BodyNode::RawExpr(exp) => self.write_raw_expr(exp),
|
||||
BodyNode::Text(text) => self.write_text(&text.text),
|
||||
BodyNode::RawExpr(exp) => self.write_raw_expr(&exp.expr),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ impl Buffer {
|
||||
for (idx, child) in children.iter().enumerate() {
|
||||
match child {
|
||||
// check if the expr is a short
|
||||
BodyNode::RawExpr(_) => {
|
||||
BodyNode::RawExpr { .. } => {
|
||||
self.tabbed_line()?;
|
||||
self.write_ident(child)?;
|
||||
if idx != last_child - 1 {
|
||||
|
||||
@@ -276,7 +276,7 @@ impl Buffer {
|
||||
}
|
||||
|
||||
match children {
|
||||
[BodyNode::Text(ref text)] => Some(text.value().len()),
|
||||
[BodyNode::Text(ref text)] => Some(text.text.value().len()),
|
||||
[BodyNode::Component(ref comp)] => {
|
||||
let attr_len = self.field_len(&comp.fields, &comp.manual_props);
|
||||
|
||||
@@ -290,7 +290,7 @@ impl Buffer {
|
||||
}
|
||||
[BodyNode::RawExpr(ref expr)] => {
|
||||
// TODO: let rawexprs to be inlined
|
||||
get_expr_length(expr)
|
||||
get_expr_length(&expr.expr)
|
||||
}
|
||||
[BodyNode::Element(ref el)] => {
|
||||
let attr_len = self.is_short_attrs(&el.attributes);
|
||||
@@ -301,8 +301,10 @@ impl Buffer {
|
||||
|
||||
if el.children.len() == 1 {
|
||||
if let BodyNode::Text(ref text) = el.children[0] {
|
||||
if text.value().len() + el.name.to_string().len() + attr_len < 80 {
|
||||
return Some(text.value().len() + el.name.to_string().len() + attr_len);
|
||||
if text.text.value().len() + el.name.to_string().len() + attr_len < 80 {
|
||||
return Some(
|
||||
text.text.value().len() + el.name.to_string().len() + attr_len,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -316,8 +318,8 @@ impl Buffer {
|
||||
for item in items {
|
||||
match item {
|
||||
BodyNode::Component(_) | BodyNode::Element(_) => return None,
|
||||
BodyNode::Text(text) => total_count += text.value().len(),
|
||||
BodyNode::RawExpr(expr) => match get_expr_length(expr) {
|
||||
BodyNode::Text(text) => total_count += text.text.value().len(),
|
||||
BodyNode::RawExpr(expr) => match get_expr_length(&expr.expr) {
|
||||
Some(len) => total_count += len,
|
||||
None => return None,
|
||||
},
|
||||
|
||||
@@ -18,6 +18,24 @@ impl Buffer {
|
||||
let end = placement.end();
|
||||
// let num_spaces_desired = (self.indent * 4) as isize;
|
||||
|
||||
// print comments
|
||||
// let mut queued_comments = vec![];
|
||||
// let mut offset = 2;
|
||||
// loop {
|
||||
// let line = &self.src[start.line - offset];
|
||||
// if line.trim_start().starts_with("//") {
|
||||
// queued_comments.push(line);
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
|
||||
// offset += 1;
|
||||
// }
|
||||
// let had_comments = !queued_comments.is_empty();
|
||||
// for comment in queued_comments.into_iter().rev() {
|
||||
// writeln!(self.buf, "{}", comment)?;
|
||||
// }
|
||||
|
||||
// if the expr is on one line, just write it directly
|
||||
if start.line == end.line {
|
||||
write!(
|
||||
@@ -30,7 +48,6 @@ impl Buffer {
|
||||
|
||||
// If the expr is multiline, we want to collect all of its lines together and write them out properly
|
||||
// This involves unshifting the first line if it's aligned
|
||||
|
||||
let first_line = &self.src[start.line - 1];
|
||||
write!(
|
||||
self.buf,
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use dioxus_rsx::{BodyNode, CallBody};
|
||||
|
||||
const LINE_WIDTH: usize = 80;
|
||||
|
||||
pub struct Printer {
|
||||
buf: String,
|
||||
// queue: Vec<Item>,
|
||||
}
|
||||
|
||||
pub enum Break {
|
||||
// Space flexes to line if need be
|
||||
Space,
|
||||
|
||||
// Line always forces a new line
|
||||
// Comments are an example of this
|
||||
Line,
|
||||
}
|
||||
|
||||
// enum Item {
|
||||
// BreakBegin,
|
||||
// BreakEnd,
|
||||
// Text(Cow<'static, str>),
|
||||
// }
|
||||
|
||||
impl Printer {
|
||||
fn doit(&mut self, body: CallBody) {
|
||||
for node in body.roots {}
|
||||
}
|
||||
fn node(&mut self, node: BodyNode) {}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let src = r#"div {}"#;
|
||||
let contents: CallBody = syn::parse_str(src).unwrap();
|
||||
}
|
||||
@@ -27,3 +27,6 @@ twoway! ("tinynoopt" => tinynoopt);
|
||||
twoway! ("long" => long);
|
||||
|
||||
twoway! ("key" => key);
|
||||
|
||||
// Disabled because we can't handle comments on exprs yet
|
||||
twoway! ("multirsx" => multirsx);
|
||||
@@ -0,0 +1,30 @@
|
||||
rsx! {
|
||||
|
||||
// hi
|
||||
div {}
|
||||
|
||||
// hi
|
||||
div { abcd, ball, s }
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
div { abcd, ball, s }
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
div {
|
||||
//
|
||||
abcd,
|
||||
|
||||
//
|
||||
ball,
|
||||
|
||||
//
|
||||
s,
|
||||
|
||||
//
|
||||
"asdasd"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user