fix: some edge cases around autoformatting (#485)
* wip: remove comment support from autofmt * fix: some edge cases around autoformatting * fix: revert readme * fix: failing test
This commit is contained in:
1 parent
3a5d1a2ff8
commit
3a30b36f68
11 files changed
+536
-141
No files matched your search
+114
-33
@@ -1,14 +1,34 @@
|
||||
use std::fmt::{Result, Write};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fmt::{Result, Write},
|
||||
};
|
||||
|
||||
use dioxus_rsx::BodyNode;
|
||||
use dioxus_rsx::{BodyNode, ElementAttr, ElementAttrNamed};
|
||||
use proc_macro2::{LineColumn, Span};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct Buffer {
|
||||
pub src: Vec<String>,
|
||||
pub cached_formats: HashMap<Location, String>,
|
||||
pub buf: String,
|
||||
pub indent: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
|
||||
pub struct Location {
|
||||
pub line: usize,
|
||||
pub col: usize,
|
||||
}
|
||||
impl Location {
|
||||
pub fn new(start: LineColumn) -> Self {
|
||||
Self {
|
||||
line: start.line,
|
||||
col: start.column,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Buffer {
|
||||
// Create a new line and tab it to the current tab level
|
||||
pub fn tabbed_line(&mut self) -> Result {
|
||||
@@ -41,6 +61,7 @@ impl Buffer {
|
||||
writeln!(self.buf)
|
||||
}
|
||||
|
||||
// Expects to be written directly into place
|
||||
pub fn write_ident(&mut self, node: &BodyNode) -> Result {
|
||||
match node {
|
||||
BodyNode::Element(el) => self.write_element(el),
|
||||
@@ -62,41 +83,12 @@ impl Buffer {
|
||||
pub fn write_body_indented(&mut self, children: &[BodyNode]) -> Result {
|
||||
self.indent += 1;
|
||||
|
||||
let mut comments = Vec::new();
|
||||
// let mut had_comments = false;
|
||||
// let mut comments = Vec::new();
|
||||
|
||||
for child in children {
|
||||
// Exprs handle their own indenting/line breaks
|
||||
if !matches!(child, BodyNode::RawExpr(_)) {
|
||||
// collect all comments upwards
|
||||
let start = child.span().start().line;
|
||||
|
||||
for (id, line) in self.src[..start - 1].iter().enumerate().rev() {
|
||||
if line.trim().starts_with("//") || line.is_empty() {
|
||||
comments.push(id);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if comments.len() == 1 && self.src[comments[0]].is_empty() {
|
||||
comments.pop();
|
||||
}
|
||||
|
||||
let mut last_was_empty = false;
|
||||
for comment_line in comments.drain(..).rev() {
|
||||
let line = &self.src[comment_line];
|
||||
if line.is_empty() {
|
||||
if !last_was_empty {
|
||||
self.new_line()?;
|
||||
}
|
||||
last_was_empty = true;
|
||||
} else {
|
||||
last_was_empty = false;
|
||||
self.tabbed_line()?;
|
||||
write!(self.buf, "{}", self.src[comment_line].trim())?;
|
||||
}
|
||||
}
|
||||
|
||||
self.tabbed_line()?;
|
||||
}
|
||||
|
||||
@@ -106,4 +98,93 @@ impl Buffer {
|
||||
self.indent -= 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn is_short_attrs(&mut self, attributes: &[ElementAttrNamed]) -> usize {
|
||||
attributes
|
||||
.iter()
|
||||
.map(|attr| match &attr.attr {
|
||||
ElementAttr::AttrText { value, name } => {
|
||||
value.value().len() + name.span().line_length() + 3
|
||||
}
|
||||
ElementAttr::AttrExpression { name, value } => {
|
||||
value.span().line_length() + name.span().line_length() + 3
|
||||
}
|
||||
ElementAttr::CustomAttrText { value, name } => {
|
||||
value.value().len() + name.value().len() + 3
|
||||
}
|
||||
ElementAttr::CustomAttrExpression { name, value } => {
|
||||
name.value().len() + value.span().line_length() + 3
|
||||
}
|
||||
ElementAttr::EventTokens { tokens, name } => {
|
||||
let location = Location::new(tokens.span().start());
|
||||
|
||||
let len = if let std::collections::hash_map::Entry::Vacant(e) =
|
||||
self.cached_formats.entry(location)
|
||||
{
|
||||
let formatted = prettyplease::unparse_expr(tokens);
|
||||
let len = if formatted.contains('\n') {
|
||||
10000
|
||||
} else {
|
||||
formatted.len()
|
||||
};
|
||||
e.insert(formatted);
|
||||
len
|
||||
} else {
|
||||
self.cached_formats[&location].len()
|
||||
};
|
||||
|
||||
len + name.span().line_length() + 3
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn retrieve_formatted_expr(&mut self, location: LineColumn) -> Option<String> {
|
||||
self.cached_formats.remove(&Location::new(location))
|
||||
}
|
||||
}
|
||||
|
||||
trait SpanLength {
|
||||
fn line_length(&self) -> usize;
|
||||
}
|
||||
impl SpanLength for Span {
|
||||
fn line_length(&self) -> usize {
|
||||
self.end().line - self.start().line
|
||||
}
|
||||
}
|
||||
|
||||
// collect all comments upwards
|
||||
// let start = child.span().start().line;
|
||||
|
||||
// for (id, line) in self.src[..start - 1].iter().enumerate().rev() {
|
||||
// if line.trim().starts_with("//") {
|
||||
// // if line.trim().starts_with("//") || line.is_empty() {
|
||||
// if line.is_empty() && id == 0 {
|
||||
// break;
|
||||
// }
|
||||
// comments.push(id);
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if comments.len() == 1 && self.src[comments[0]].is_empty() {
|
||||
// comments.pop();
|
||||
// }
|
||||
|
||||
// // let mut last_line_was_empty = false;
|
||||
// had_comments = !comments.is_empty();
|
||||
// for comment_line in comments.drain(..).rev() {
|
||||
// let line = &self.src[comment_line];
|
||||
// if line.is_empty() {
|
||||
// continue;
|
||||
// // if last_line_was_empty {
|
||||
// // continue;
|
||||
// // } else {
|
||||
// // last_line_was_empty = true;
|
||||
// // }
|
||||
// }
|
||||
|
||||
// self.tabbed_line()?;
|
||||
// write!(self.buf, "{}", self.src[comment_line].trim())?;
|
||||
// }
|
||||
Reference in new issue
Block a user