use std::fmt::Write as _;
#[derive(Debug, Default)]
pub struct CodeWriter {
out: String,
depth: usize,
unit: &'static str,
}
impl CodeWriter {
pub fn new() -> Self {
Self::with_indent(" ")
}
pub fn with_indent(unit: &'static str) -> Self {
Self {
out: String::new(),
depth: 0,
unit,
}
}
pub fn line(&mut self, text: impl AsRef<str>) {
let text = text.as_ref();
if text.is_empty() {
self.out.push('\n');
return;
}
for _ in 0..self.depth {
self.out.push_str(self.unit);
}
self.out.push_str(text.trim_end());
self.out.push('\n');
}
pub fn write(&mut self, args: std::fmt::Arguments<'_>) {
let mut text = String::new();
let _ = text.write_fmt(args);
self.line(text);
}
pub fn blank(&mut self) {
if self.out.is_empty() || self.out.ends_with("\n\n") {
return;
}
self.out.push('\n');
}
pub fn comment(&mut self, prefix: &str, text: &str) {
for line in wrap(text, 76_usize.saturating_sub(self.depth * self.unit.len())) {
if line.is_empty() {
self.line(prefix.trim_end());
} else {
self.line(format!("{prefix}{line}"));
}
}
}
pub fn indented(&mut self, body: impl FnOnce(&mut Self)) {
self.depth += 1;
body(self);
self.depth -= 1;
}
pub fn block(&mut self, open: impl AsRef<str>, close: &str, body: impl FnOnce(&mut Self)) {
self.line(open);
self.indented(body);
self.line(close);
}
pub fn is_empty(&self) -> bool {
self.out.is_empty()
}
pub fn finish(self) -> String {
let trimmed = self.out.trim_end();
if trimmed.is_empty() {
String::new()
} else {
format!("{trimmed}\n")
}
}
}
fn wrap(text: &str, width: usize) -> Vec<String> {
let mut out = Vec::new();
for paragraph in text.split('\n') {
if paragraph.trim().is_empty() {
out.push(String::new());
continue;
}
let mut line = String::new();
for word in paragraph.split_whitespace() {
if line.is_empty() {
line.push_str(word);
} else if line.len() + 1 + word.len() <= width {
line.push(' ');
line.push_str(word);
} else {
out.push(std::mem::take(&mut line));
line.push_str(word);
}
}
if !line.is_empty() {
out.push(line);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lines_are_indented_by_how_deep_they_are() {
let mut w = CodeWriter::new();
w.line("outer");
w.indented(|w| {
w.line("inner");
w.indented(|w| w.line("deeper"));
});
assert_eq!(w.finish(), "outer\n inner\n deeper\n");
}
#[test]
fn a_block_opens_indents_and_closes() {
let mut w = CodeWriter::new();
w.block("interface Shape {", "}", |w| w.line("id: string;"));
assert_eq!(w.finish(), "interface Shape {\n id: string;\n}\n");
}
#[test]
fn a_blank_line_is_blank_rather_than_indentation() {
let mut w = CodeWriter::new();
w.indented(|w| {
w.line("a");
w.line("");
w.line("b");
});
assert_eq!(w.finish(), " a\n\n b\n");
}
#[test]
fn nothing_ends_with_whitespace_that_a_diff_would_notice() {
let mut w = CodeWriter::new();
w.line("code ");
w.blank();
w.blank();
let out = w.finish();
assert_eq!(out, "code\n");
}
#[test]
fn blank_lines_do_not_pile_up() {
let mut w = CodeWriter::new();
w.line("a");
w.blank();
w.blank();
w.blank();
w.line("b");
assert_eq!(w.finish(), "a\n\nb\n");
}
#[test]
fn prose_is_wrapped_and_prefixed() {
let mut w = CodeWriter::new();
w.comment(
"// ",
"A shape that is round. This sentence is long enough to need breaking \
across more than a single line of output.",
);
let out = w.finish();
assert!(out.lines().all(|l| l.starts_with("// ")), "{out}");
assert!(out.lines().all(|l| l.len() <= 80), "{out}");
assert!(out.lines().count() > 1, "it should have wrapped: {out}");
}
#[test]
fn a_word_too_long_to_wrap_is_left_alone() {
let mut w = CodeWriter::new();
w.comment("# ", &"x".repeat(120));
let out = w.finish();
assert_eq!(out.lines().count(), 1, "hyphenating a name would be worse");
}
#[test]
fn an_empty_file_stays_empty() {
assert_eq!(CodeWriter::new().finish(), "");
assert!(CodeWriter::new().is_empty());
}
}