oxc/crates/oxc_prettier/src/format/statement.rs
2023-11-18 16:00:52 +08:00

41 lines
1,011 B
Rust

use oxc_allocator::Vec;
use crate::{doc::Doc, hardline, Prettier};
use oxc_span::GetSpan;
use super::Format;
pub(super) fn print_statement_sequence<'a, F: Format<'a> + GetSpan>(
p: &mut Prettier<'a>,
stmts: &Vec<'a, F>,
remove_last_statement_hardline: bool,
) -> Vec<'a, Doc<'a>> {
let mut parts = p.vec();
for (i, stmt) in stmts.iter().enumerate() {
let mut docs = stmt.format(p);
if remove_last_statement_hardline && i == stmts.len() - 1 {
match docs {
Doc::Array(ref mut docs) | Doc::Group(ref mut docs) => {
if matches!(docs.last(), Some(Doc::Hardline)) {
docs.pop();
}
}
_ => {}
}
}
parts.push(docs);
if i < stmts.len() - 1 {
parts.push(hardline!());
if p.is_next_line_empty(stmt.span().end) {
parts.push(hardline!());
}
}
}
parts
}