chore: fixes to autoformat to prevent double rendering of expr line

This commit is contained in:
Jonathan Kelley committed 2022-09-28 16:47:10 -07:00
1 parent 28fba42e7e
commit a37458b9fa
16 files changed
+203 -47

No files matched your search

+19 -9
View File
@@ -124,16 +124,26 @@ impl Buffer {
}
pub fn write_body_no_indent(&mut self, children: &[BodyNode]) -> Result {
for child in children {
// Exprs handle their own indenting/line breaks
if !matches!(child, BodyNode::RawExpr(_)) {
if self.current_span_is_primary(child.span()) {
self.write_comments(child.span())?;
}
self.tabbed_line()?;
}
let last_child = children.len();
self.write_ident(child)?;
for (idx, child) in children.iter().enumerate() {
match child {
// check if the expr is a short
BodyNode::RawExpr(_) => {
self.tabbed_line()?;
self.write_ident(child)?;
if idx != last_child - 1 {
write!(self.buf, ",")?;
}
}
_ => {
if self.current_span_is_primary(child.span()) {
self.write_comments(child.span())?;
}
self.tabbed_line()?;
self.write_ident(child)?;
}
}
}
Ok(())
+46 -16
View File
@@ -2,6 +2,7 @@ use crate::Buffer;
use dioxus_rsx::*;
use proc_macro2::Span;
use std::{fmt::Result, fmt::Write};
use syn::{spanned::Spanned, Expr};
#[derive(Debug)]
enum ShortOptimization {
@@ -87,8 +88,11 @@ impl Buffer {
write!(self.buf, ", ")?;
}
for child in children {
for (id, child) in children.iter().enumerate() {
self.write_ident(child)?;
if id != children.len() - 1 && children.len() > 1 {
write!(self.buf, ", ")?;
}
}
write!(self.buf, " ")?;
@@ -283,28 +287,54 @@ impl Buffer {
.map(|child_len| child_len + attr_len)
}
}
[BodyNode::RawExpr(ref _expr)] => {
[BodyNode::RawExpr(ref expr)] => {
// TODO: let rawexprs to be inlined
// let span = syn::spanned::Spanned::span(&text);
// let (start, end) = (span.start(), span.end());
// if start.line == end.line {
// Some(end.column - start.column)
// } else {
// None
// }
None
get_expr_length(expr)
}
[BodyNode::Element(ref el)] => {
let attr_len = self.is_short_attrs(&el.attributes);
if attr_len > 80 {
None
} else {
self.is_short_children(&el.children)
.map(|child_len| child_len + attr_len)
if el.children.is_empty() && attr_len < 80 {
return Some(el.name.to_string().len());
}
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);
}
}
}
None
}
// todo, allow non-elements to be on the same line
items => {
let mut total_count = 0;
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) {
Some(len) => total_count += len,
None => return None,
},
}
}
Some(total_count)
}
_ => None,
}
}
}
fn get_expr_length(expr: &Expr) -> Option<usize> {
let span = expr.span();
let (start, end) = (span.start(), span.end());
if start.line == end.line {
Some(end.column - start.column)
} else {
None
}
}
+61 -5
View File
@@ -16,16 +16,46 @@ impl Buffer {
let placement = exp.span();
let start = placement.start();
let end = placement.end();
let num_spaces_desired = (self.indent * 4) as isize;
// let num_spaces_desired = (self.indent * 4) as isize;
let first = &self.src[start.line - 1];
let num_spaces_real = first.chars().take_while(|c| c.is_whitespace()).count() as isize;
// if the expr is on one line, just write it directly
if start.line == end.line {
write!(
self.buf,
"{}",
&self.src[start.line - 1][start.column - 1..end.column].trim()
)?;
return Ok(());
}
let offset = num_spaces_real - num_spaces_desired;
// 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
for line in &self.src[start.line - 1..end.line] {
let first_line = &self.src[start.line - 1];
write!(
self.buf,
"{}",
&first_line[start.column - 1..first_line.len()].trim()
)?;
let first_prefix = &self.src[start.line - 1][..start.column];
let offset = match first_prefix.trim() {
"" => 0,
_ => first_prefix
.chars()
.rev()
.take_while(|c| c.is_whitespace())
.count() as isize,
};
for (id, line) in self.src[start.line..end.line].iter().enumerate() {
writeln!(self.buf)?;
// trim the leading whitespace
let line = match id {
x if x == (end.line - start.line) - 1 => &line[..end.column],
_ => line,
};
if offset < 0 {
for _ in 0..-offset {
write!(self.buf, " ")?;
@@ -39,6 +69,32 @@ impl Buffer {
}
}
// let first = &self.src[start.line - 1];
// let num_spaces_real = first.chars().take_while(|c| c.is_whitespace()).count() as isize;
// let offset = num_spaces_real - num_spaces_desired;
// for (row, line) in self.src[start.line - 1..end.line].iter().enumerate() {
// let line = match row {
// 0 => &line[start.column - 1..],
// a if a == (end.line - start.line) => &line[..end.column - 1],
// _ => line,
// };
// writeln!(self.buf)?;
// // trim the leading whitespace
// if offset < 0 {
// for _ in 0..-offset {
// write!(self.buf, " ")?;
// }
// write!(self.buf, "{}", line)?;
// } else {
// let offset = offset as usize;
// let right = &line[offset..];
// write!(self.buf, "{}", right)?;
// }
// }
Ok(())
}
}
+1 -1
View File
@@ -124,7 +124,7 @@ pub fn apply_format(input: &str, block: FormattedBlock) -> String {
let (left, _) = input.split_at(start);
let (_, right) = input.split_at(end);
dbg!(&block.formatted);
// dbg!(&block.formatted);
format!("{}{}{}", left, block.formatted, right)
}