use super::{Generated, SourceSpan};
use crate::ir::Origin;
pub(super) struct Doc<'a> {
kind: Kind<'a>,
flat_width: usize,
}
enum Kind<'a> {
Text(String),
Break(&'static str),
HardLine,
Concat(Vec<Doc<'a>>),
Nest(Box<Doc<'a>>),
Group(Box<Doc<'a>>),
Attributed {
origin: &'a Origin,
diagnostic_site: bool,
body: Box<Doc<'a>>,
},
}
pub(super) fn text(value: impl Into<String>) -> Doc<'static> {
let value = value.into();
let flat_width = if value.contains('\n') {
usize::MAX
} else {
value.chars().count()
};
Doc {
kind: Kind::Text(value),
flat_width,
}
}
pub(super) fn line<'a>() -> Doc<'a> {
Doc {
kind: Kind::Break(" "),
flat_width: 1,
}
}
pub(super) fn hard_line() -> Doc<'static> {
Doc {
kind: Kind::HardLine,
flat_width: usize::MAX,
}
}
pub(super) fn concat(parts: Vec<Doc<'_>>) -> Doc<'_> {
let flat_width = parts
.iter()
.fold(0_usize, |width, part| width.saturating_add(part.flat_width));
Doc {
kind: Kind::Concat(parts),
flat_width,
}
}
pub(super) fn nest(body: Doc<'_>) -> Doc<'_> {
Doc {
flat_width: body.flat_width,
kind: Kind::Nest(Box::new(body)),
}
}
pub(super) fn group(body: Doc<'_>) -> Doc<'_> {
Doc {
flat_width: body.flat_width,
kind: Kind::Group(Box::new(body)),
}
}
pub(super) fn attributed<'a>(origin: &'a Origin, diagnostic_site: bool, body: Doc<'a>) -> Doc<'a> {
Doc {
flat_width: body.flat_width,
kind: Kind::Attributed {
origin,
diagnostic_site,
body: Box::new(body),
},
}
}
#[derive(Clone, Copy)]
enum Command<'d, 'a> {
Visit(&'d Doc<'a>, usize, bool),
Close(usize, &'a Origin, bool),
}
fn fits(width: usize, remaining: usize, pending: &[Command<'_, '_>]) -> bool {
if width > remaining {
return false;
}
let mut remaining = remaining - width;
let mut suffix = pending.iter().rev().copied();
let mut children = Vec::new();
while let Some(command) = children.pop().or_else(|| suffix.next()) {
let Command::Visit(doc, indent, flat) = command else {
continue;
};
match &doc.kind {
Kind::Text(value) => {
let width = if doc.flat_width == usize::MAX {
value.split('\n').next().unwrap().chars().count()
} else {
doc.flat_width
};
if width > remaining {
return false;
}
remaining -= width;
if value.contains('\n') {
return true;
}
}
Kind::Break(value) if flat => {
if value.len() > remaining {
return false;
}
remaining -= value.len();
}
Kind::Break(_) | Kind::HardLine => return true,
Kind::Concat(parts) => children.extend(
parts
.iter()
.rev()
.map(|part| Command::Visit(part, indent, flat)),
),
Kind::Nest(body) => children.push(Command::Visit(body, indent + 2, flat)),
Kind::Group(body) | Kind::Attributed { body, .. } => {
if flat && body.flat_width != usize::MAX {
if body.flat_width > remaining {
return false;
}
remaining -= body.flat_width;
} else {
children.push(Command::Visit(body, indent, flat));
}
}
}
}
true
}
pub(super) fn render(doc: &Doc<'_>, width: usize) -> Generated {
let mut generated = Generated::default();
generated
.source
.push_str("# Generated by Rusix. Edit the Rust source.\n");
let mut pending = vec![Command::Visit(doc, 0, false)];
let mut enclosing = Vec::new();
let mut column = 0;
let mut indentation = Some(0);
while let Some(command) = pending.pop() {
let (doc, indent, flat) = match command {
Command::Visit(doc, indent, flat) => (doc, indent, flat),
Command::Close(start, origin, diagnostic_site) => {
enclosing.pop();
generated.spans.push(SourceSpan {
start,
end: generated.source.len(),
origin: origin.clone(),
enclosing: enclosing
.iter()
.map(|origin: &&Origin| (*origin).clone())
.collect(),
diagnostic_site,
});
continue;
}
};
match &doc.kind {
Kind::Text(value) => {
if !value.is_empty() {
flush_indent(&mut generated, &mut column, &mut indentation);
generated.source.push_str(value);
column = if value.contains('\n') {
value.rsplit('\n').next().unwrap().chars().count()
} else {
column + doc.flat_width
};
}
}
Kind::Break(value) if flat => {
generated.source.push_str(value);
column += value.len();
}
Kind::Break(_) | Kind::HardLine => {
generated.source.push('\n');
column = 0;
indentation = Some(indent);
}
Kind::Concat(parts) => pending.extend(
parts
.iter()
.rev()
.map(|part| Command::Visit(part, indent, flat)),
),
Kind::Nest(body) => pending.push(Command::Visit(body, indent + 2, flat)),
Kind::Group(body) => {
let available = width.saturating_sub(indentation.unwrap_or(column));
let flat = flat || fits(body.flat_width, available, &pending);
pending.push(Command::Visit(body, indent, flat));
}
Kind::Attributed {
origin,
diagnostic_site,
body,
} => {
flush_indent(&mut generated, &mut column, &mut indentation);
pending.push(Command::Close(
generated.source.len(),
origin,
*diagnostic_site,
));
enclosing.push(origin);
pending.push(Command::Visit(body, indent, flat));
}
}
}
generated.source.push('\n');
generated
}
fn flush_indent(generated: &mut Generated, column: &mut usize, indentation: &mut Option<usize>) {
if let Some(indent) = indentation.take() {
generated.source.extend(std::iter::repeat_n(' ', indent));
*column = indent;
}
}