wip: eliminate warnings
This commit is contained in:
1 parent
a825cdcf58
commit
1e4af430a8
8 files changed
+28
-212
No files matched your search
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -59,6 +59,10 @@ impl Buffer {
|
|||||||
write!(self.buf, "\"{}\"", text.value())
|
write!(self.buf, "\"{}\"", text.value())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn consume(self) -> Option<String> {
|
||||||
|
Some(self.buf)
|
||||||
|
}
|
||||||
|
|
||||||
// Push out the indent level and write each component, line by line
|
// Push out the indent level and write each component, line by line
|
||||||
pub fn write_body_indented(&mut self, children: &[BodyNode]) -> Result {
|
pub fn write_body_indented(&mut self, children: &[BodyNode]) -> Result {
|
||||||
self.indent += 1;
|
self.indent += 1;
|
||||||
@@ -100,94 +104,7 @@ impl Buffer {
|
|||||||
self.write_ident(child)?;
|
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;
|
self.indent -= 1;
|
||||||
Ok(())
|
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, "}}")?;
|
|
||||||
// }
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
// write the body of a component or element
|
|
||||||
pub fn write_children() {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::Buffer;
|
use crate::Buffer;
|
||||||
use dioxus_rsx::*;
|
use dioxus_rsx::*;
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use std::fmt::{self, Result, Write};
|
use std::fmt::{Result, Write};
|
||||||
|
|
||||||
impl Buffer {
|
impl Buffer {
|
||||||
pub fn write_component(
|
pub fn write_component(
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ impl Buffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
match opt_level {
|
match opt_level {
|
||||||
ShortOptimization::Empty => write!(self.buf, "}}")?,
|
ShortOptimization::Empty => {}
|
||||||
ShortOptimization::Oneliner => {
|
ShortOptimization::Oneliner => {
|
||||||
write!(self.buf, " ")?;
|
write!(self.buf, " ")?;
|
||||||
self.write_attributes(attributes, true)?;
|
self.write_attributes(attributes, true)?;
|
||||||
@@ -78,12 +78,11 @@ impl Buffer {
|
|||||||
write!(self.buf, ", ")?;
|
write!(self.buf, ", ")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the children
|
|
||||||
for child in children {
|
for child in children {
|
||||||
self.write_ident(child)?;
|
self.write_ident(child)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(self.buf, " }}")?;
|
write!(self.buf, " ")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
ShortOptimization::PropsOnTop => {
|
ShortOptimization::PropsOnTop => {
|
||||||
@@ -94,26 +93,19 @@ impl Buffer {
|
|||||||
write!(self.buf, ",")?;
|
write!(self.buf, ",")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the children
|
|
||||||
self.write_body_indented(children)?;
|
self.write_body_indented(children)?;
|
||||||
|
|
||||||
self.tabbed_line()?;
|
self.tabbed_line()?;
|
||||||
write!(self.buf, "}}")?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ShortOptimization::NoOpt => {
|
ShortOptimization::NoOpt => {
|
||||||
// write the key
|
|
||||||
|
|
||||||
// write the attributes
|
|
||||||
self.write_attributes(attributes, false)?;
|
self.write_attributes(attributes, false)?;
|
||||||
|
|
||||||
self.write_body_indented(children)?;
|
self.write_body_indented(children)?;
|
||||||
|
|
||||||
self.tabbed_line()?;
|
self.tabbed_line()?;
|
||||||
write!(self.buf, "}}")?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
write!(self.buf, "}}")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,19 +218,3 @@ fn is_short_attrs(attrs: &[ElementAttrNamed]) -> bool {
|
|||||||
let total_attr_len = extract_attr_len(attrs);
|
let total_attr_len = extract_attr_len(attrs);
|
||||||
total_attr_len < 80
|
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, ",")?;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
//! pretty printer for rsx!
|
//! pretty printer for rsx!
|
||||||
use std::fmt::{self, Result, Write};
|
use std::fmt::{Result, Write};
|
||||||
|
|
||||||
use crate::Buffer;
|
use crate::Buffer;
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,12 @@
|
|||||||
//! pretty printer for rsx code
|
use crate::buffer::*;
|
||||||
|
|
||||||
pub use crate::buffer::*;
|
|
||||||
use crate::util::*;
|
use crate::util::*;
|
||||||
|
|
||||||
mod block;
|
|
||||||
mod buffer;
|
mod buffer;
|
||||||
mod children;
|
|
||||||
mod component;
|
mod component;
|
||||||
mod element;
|
mod element;
|
||||||
mod expr;
|
mod expr;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
// pub use block::{fmt_block, get_format_blocks};
|
|
||||||
|
|
||||||
/// A modification to the original file to be applied by an IDE
|
/// 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.
|
/// 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<FormattedBlock> {
|
|||||||
|
|
||||||
use triple_accel::{levenshtein_search, Match};
|
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
|
// ensure the marker is not nested
|
||||||
if start < last_bracket_end {
|
if start < last_bracket_end {
|
||||||
continue;
|
continue;
|
||||||
@@ -72,14 +66,14 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn fmt_block(block: &str) -> Option<String> {
|
pub fn fmt_block(block: &str) -> Option<String> {
|
||||||
let mut buf = Buffer::default();
|
let mut buf = Buffer {
|
||||||
buf.src = block.lines().map(|f| f.to_string()).collect(); // unnecessary clone, but eh, most files are small
|
src: block.lines().map(|f| f.to_string()).collect(),
|
||||||
|
..Buffer::default()
|
||||||
|
};
|
||||||
|
|
||||||
let lines = block.split('\n').collect::<Vec<_>>();
|
for node in syn::parse_str::<dioxus_rsx::CallBody>(block).ok()?.roots {
|
||||||
|
|
||||||
for node in &syn::parse_str::<dioxus_rsx::CallBody>(block).ok()?.roots {
|
|
||||||
buf.write_ident(&node).ok()?;
|
buf.write_ident(&node).ok()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(buf.buf)
|
buf.consume()
|
||||||
}
|
}
|
||||||
@@ -1,35 +1,32 @@
|
|||||||
use dioxus_rsx::*;
|
use dioxus_rsx::*;
|
||||||
use std::fmt::Write;
|
|
||||||
|
|
||||||
|
// todo: use recursive or complete sizeing
|
||||||
pub fn extract_attr_len(attributes: &[ElementAttrNamed]) -> usize {
|
pub fn extract_attr_len(attributes: &[ElementAttrNamed]) -> usize {
|
||||||
attributes
|
attributes
|
||||||
.iter()
|
.iter()
|
||||||
.map(|attr| match &attr.attr {
|
.map(|attr| match &attr.attr {
|
||||||
ElementAttr::AttrText { name, value } => value.value().len(),
|
ElementAttr::AttrText { value, .. } => value.value().len(),
|
||||||
ElementAttr::AttrExpression { name, value } => 10,
|
ElementAttr::AttrExpression { .. } => 10,
|
||||||
ElementAttr::CustomAttrText { name, value } => value.value().len(),
|
ElementAttr::CustomAttrText { value, .. } => value.value().len(),
|
||||||
ElementAttr::CustomAttrExpression { name, value } => 10,
|
ElementAttr::CustomAttrExpression { .. } => 10,
|
||||||
ElementAttr::EventTokens { name, tokens } => 1000000,
|
ElementAttr::EventTokens { .. } => 1000000,
|
||||||
})
|
})
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_bracket_end(contents: &str) -> Option<usize> {
|
pub fn find_bracket_end(contents: &str) -> Option<usize> {
|
||||||
let mut depth = 0;
|
let mut depth = 0;
|
||||||
let mut i = 0;
|
|
||||||
|
|
||||||
for c in contents.chars() {
|
for (i, c) in contents.chars().enumerate() {
|
||||||
if c == '{' {
|
if c == '{' {
|
||||||
depth += 1;
|
depth += 1;
|
||||||
} else if c == '}' {
|
} else if c == '}' {
|
||||||
depth -= 1;
|
depth -= 1;
|
||||||
}
|
|
||||||
|
|
||||||
if depth == 0 {
|
if depth == 0 {
|
||||||
return Some(i);
|
return Some(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
i += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
|||||||
Reference in new issue
Block a user