wip: more cleanup, more tests

This commit is contained in:
Jonathan Kelley committed 2022-06-27 16:24:36 -04:00
1 parent a2a194ca40
commit d70b436157
10 files changed
+560 -423

No files matched your search

+44 -30
View File
@@ -1,39 +1,53 @@
//! pretty printer for rsx!
use std::fmt::{self, Write};
use std::fmt::{self, Result, Write};
pub fn write_raw_expr(
exp: &syn::Expr,
indent: usize,
lines: &[&str],
buf: &mut String,
) -> Result<(), fmt::Error> {
use syn::spanned::Spanned;
let placement = exp.span();
let start = placement.start();
let end = placement.end();
let num_spaces_desired = (indent * 4) as isize;
let first = lines[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;
use crate::Buffer;
for line_id in start.line - 1..end.line {
let line = lines[line_id];
impl Buffer {
pub fn write_raw_expr(&mut self, exp: &syn::Expr, lines: &[&str]) -> Result {
/*
We want to normalize the expr to the appropriate indent level.
*/
// trim the leading whitespace
// in a perfect world, just fire up the rust pretty printer
// pretty_print_rust_code_as_if_it_were_rustfmt()
if offset < 0 {
for _ in 0..-offset {
write!(buf, " ")?;
use syn::spanned::Spanned;
let placement = exp.span();
let start = placement.start();
let end = placement.end();
let num_spaces_desired = (self.indent * 4) as isize;
let first = lines[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 line in &lines[start.line - 1..end.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)?;
}
writeln!(buf, "{}", line)?;
} else {
let offset = offset as usize;
let right = &line[offset..];
writeln!(buf, "{}", right)?;
}
}
Ok(())
Ok(())
}
}
// :(
// fn pretty_print_rust_code_as_if_it_were_rustfmt(code: &str) -> String {
// let formatted = prettyplease::unparse_expr(exp);
// for line in formatted.lines() {
// write!(self.buf, "{}", line)?;
// self.new_line()?;
// }
// }