//! Prettier's document IR (`src/document`), stored flat in an arena. //! //! A [`Doc`] is an index into [`Docs`]. Builders mirror Prettier's: the same doc may appear in //! several places, as in Prettier, and keeps its identity there. mod print; mod width; mod width_tables; use bumpalo::Bump; pub use print::{Eol, Options, print}; pub use width::width; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct Doc(u32); #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct GroupId(u32); #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Line { /// `line`: a space when flat. Space, /// `softline`: nothing when flat. Soft, /// `hardlineWithoutBreakParent`. Hard, /// `literallineWithoutBreakParent`: a line break back to the root indent. Literal, } #[derive(Clone, Copy, Debug)] pub enum Align<'a> { Width(usize), Text(&'a str), /// `dedent`: drops the innermost indent. Dedent, /// `dedentToRoot`. Root, /// `markAsRoot`. Mark, } #[derive(Clone, Copy, Debug)] pub(crate) struct List { start: u32, len: u32, } #[derive(Clone, Copy, Debug)] pub(crate) enum Node<'a> { Text(&'a str), Concat(List), Fill(List), Indent(Doc), Align(Align<'a>, Doc), Group { contents: Doc, id: Option, broken: bool, states: Option }, IfBreak { broken: Doc, flat: Doc, id: Option }, IndentIfBreak { contents: Doc, id: GroupId, negate: bool }, LineSuffix(Doc), LineSuffixBoundary, Line(Line), BreakParent, Trim, Label(&'static str, Doc), } /// The arena that owns every doc of one format run. pub struct Docs<'a> { bump: &'a Bump, nodes: Vec>, lists: Vec, groups: u32, } impl<'a> Docs<'a> { pub const EMPTY: Doc = Doc(0); /// A shared `hardlineWithoutBreakParent`. pub(crate) const HARD: Doc = Doc(1); pub fn new(bump: &'a Bump) -> Self { let nodes = vec![Node::Text(""), Node::Line(Line::Hard)]; Self { bump, nodes, lists: Vec::new(), groups: 0 } } pub fn clear(&mut self) { self.nodes.truncate(2); self.lists.clear(); self.groups = 0; } fn push(&mut self, node: Node<'a>) -> Doc { self.nodes.push(node); Doc(self.nodes.len() as u32 - 1) } fn list(&mut self, parts: &[Doc]) -> List { let start = self.lists.len() as u32; self.lists.extend_from_slice(parts); List { start, len: parts.len() as u32 } } pub(crate) fn node(&self, doc: Doc) -> Node<'a> { self.nodes[doc.0 as usize] } pub(crate) fn parts(&self, list: List) -> &[Doc] { &self.lists[list.start as usize..(list.start + list.len) as usize] } pub fn group_id(&mut self) -> GroupId { self.groups += 1; GroupId(self.groups - 1) } pub fn text(&mut self, text: &'a str) -> Doc { if text.is_empty() { return Self::EMPTY; } self.push(Node::Text(text)) } /// A text this arena keeps. pub fn owned(&mut self, text: &str) -> Doc { let text = self.bump.alloc_str(text); self.text(text) } pub fn concat(&mut self, parts: &[Doc]) -> Doc { match parts { [] => Self::EMPTY, [one] => *one, _ => { let list = self.list(parts); self.push(Node::Concat(list)) } } } pub fn fill(&mut self, parts: &[Doc]) -> Doc { let list = self.list(parts); self.push(Node::Fill(list)) } pub fn indent(&mut self, contents: Doc) -> Doc { self.push(Node::Indent(contents)) } pub fn align(&mut self, align: Align<'a>, contents: Doc) -> Doc { self.push(Node::Align(align, contents)) } pub fn group(&mut self, contents: Doc) -> Doc { self.push(Node::Group { contents, id: None, broken: false, states: None }) } pub fn group_with(&mut self, contents: Doc, id: Option, broken: bool) -> Doc { self.push(Node::Group { contents, id, broken, states: None }) } pub fn conditional(&mut self, states: &[Doc]) -> Doc { let list = self.list(states); self.push(Node::Group { contents: states[0], id: None, broken: false, states: Some(list) }) } pub fn if_break(&mut self, broken: Doc, flat: Doc, id: Option) -> Doc { self.push(Node::IfBreak { broken, flat, id }) } pub fn indent_if_break(&mut self, contents: Doc, id: GroupId, negate: bool) -> Doc { self.push(Node::IndentIfBreak { contents, id, negate }) } pub fn line_suffix(&mut self, contents: Doc) -> Doc { self.push(Node::LineSuffix(contents)) } pub fn line_suffix_boundary(&mut self) -> Doc { self.push(Node::LineSuffixBoundary) } pub fn line(&mut self) -> Doc { self.push(Node::Line(Line::Space)) } pub fn softline(&mut self) -> Doc { self.push(Node::Line(Line::Soft)) } pub fn hardline(&mut self) -> Doc { let line = self.push(Node::Line(Line::Hard)); let parent = self.push(Node::BreakParent); self.concat(&[line, parent]) } pub fn literalline(&mut self) -> Doc { let line = self.push(Node::Line(Line::Literal)); let parent = self.push(Node::BreakParent); self.concat(&[line, parent]) } pub fn hardline_without_break_parent(&mut self) -> Doc { self.push(Node::Line(Line::Hard)) } pub fn literalline_without_break_parent(&mut self) -> Doc { self.push(Node::Line(Line::Literal)) } pub fn break_parent(&mut self) -> Doc { self.push(Node::BreakParent) } pub fn trim(&mut self) -> Doc { self.push(Node::Trim) } pub fn label(&mut self, label: &'static str, contents: Doc) -> Doc { self.push(Node::Label(label, contents)) } pub fn join(&mut self, separator: Doc, parts: &[Doc]) -> Doc { let mut joined = Vec::with_capacity(parts.len() * 2); for (index, part) in parts.iter().enumerate() { if index > 0 { joined.push(separator); } joined.push(*part); } self.concat(&joined) } /// `replaceEndOfLine`: each line break of `text` becomes `separator`. pub fn lines(&mut self, text: &'a str, separator: Doc) -> Doc { if !text.contains('\n') { return self.text(text); } let parts: Vec = text.split('\n').map(|line| self.text(line)).collect(); self.join(separator, &parts) } /// `mapDoc(doc, cb)` where `cb` changes texts: `change` returns the new text, or `None` to keep it. pub fn map_text(&mut self, doc: Doc, change: impl Fn(&str) -> Option) -> Doc { let mut mapped = std::collections::HashMap::new(); self.map_with(doc, &change, &mut mapped) } fn map_with( &mut self, doc: Doc, change: &impl Fn(&str) -> Option, mapped: &mut std::collections::HashMap, ) -> Doc { if let Some(&done) = mapped.get(&doc) { return done; } let mut list = |docs: &mut Self, list: List, mapped: &mut std::collections::HashMap| { let parts = docs.parts(list).to_vec(); let parts: Vec = parts.into_iter().map(|part| docs.map_with(part, change, mapped)).collect(); docs.list(&parts) }; let node = match self.node(doc) { Node::Text(text) => match change(text) { Some(text) => { let done = self.owned(&text); mapped.insert(doc, done); return done; } None => Node::Text(text), }, Node::Concat(parts) => Node::Concat(list(self, parts, mapped)), Node::Fill(parts) => Node::Fill(list(self, parts, mapped)), Node::Indent(contents) => Node::Indent(self.map_with(contents, change, mapped)), Node::Align(align, contents) => Node::Align(align, self.map_with(contents, change, mapped)), Node::Group { contents, id, broken, states: Some(states) } => { let states = list(self, states, mapped); Node::Group { contents: self.lists[states.start as usize], id, broken, states: Some(states), } } Node::Group { contents, id, broken, states: None } => { Node::Group { contents: self.map_with(contents, change, mapped), id, broken, states: None } } Node::IfBreak { broken, flat, id } => { let broken = self.map_with(broken, change, mapped); let flat = self.map_with(flat, change, mapped); Node::IfBreak { broken, flat, id } } Node::IndentIfBreak { contents, id, negate } => { Node::IndentIfBreak { contents: self.map_with(contents, change, mapped), id, negate } } Node::LineSuffix(contents) => Node::LineSuffix(self.map_with(contents, change, mapped)), Node::Label(label, contents) => Node::Label(label, self.map_with(contents, change, mapped)), other => other, }; let done = self.push(node); mapped.insert(doc, done); done } /// `willBreak`. pub fn will_break(&self, doc: Doc) -> bool { self.find(doc, &mut |node| match node { Node::Group { broken, .. } => broken, Node::Line(line) => matches!(line, Line::Hard | Line::Literal), Node::BreakParent => true, _ => false, }) } /// `canBreak`. pub fn can_break(&self, doc: Doc) -> bool { self.find(doc, &mut |node| matches!(node, Node::Line(_))) } /// `traverseDoc` without conditional states, stopping at the first doc `found` matches. fn find(&self, doc: Doc, found: &mut impl FnMut(Node<'a>) -> bool) -> bool { let mut stack = vec![doc]; while let Some(doc) = stack.pop() { let node = self.node(doc); if found(node) { return true; } match node { Node::Concat(list) | Node::Fill(list) => stack.extend(self.parts(list).iter().rev()), Node::IfBreak { broken, flat, .. } => stack.extend([flat, broken]), Node::Group { contents, .. } | Node::Indent(contents) | Node::Align(_, contents) | Node::IndentIfBreak { contents, .. } | Node::Label(_, contents) | Node::LineSuffix(contents) => stack.push(contents), _ => {} } } false } /// `propagateBreaks`: a group with a forced break inside breaks too, up to a conditional group. pub(crate) fn propagate(&mut self, doc: Doc) { enum Step { Enter(Doc), Exit(Doc), } let mut visited = vec![false; self.nodes.len()]; let mut groups: Vec = Vec::new(); let mut stack = vec![Step::Enter(doc)]; while let Some(step) = stack.pop() { match step { Step::Exit(doc) => { groups.pop(); if let Node::Group { broken: true, .. } = self.node(doc) { self.break_group(&groups); } } Step::Enter(doc) => { let node = self.node(doc); if matches!(node, Node::Group { .. }) { stack.push(Step::Exit(doc)); groups.push(doc); if std::mem::replace(&mut visited[doc.0 as usize], true) { continue; } } match node { Node::BreakParent => self.break_group(&groups), Node::Concat(list) | Node::Fill(list) => { stack.extend(self.parts(list).iter().rev().map(|&part| Step::Enter(part))) } Node::IfBreak { broken, flat, .. } => { stack.extend([Step::Enter(flat), Step::Enter(broken)]) } Node::Group { states: Some(list), .. } => { stack.extend(self.parts(list).iter().rev().map(|&part| Step::Enter(part))) } Node::Group { contents, .. } | Node::Indent(contents) | Node::Align(_, contents) | Node::IndentIfBreak { contents, .. } | Node::Label(_, contents) | Node::LineSuffix(contents) => stack.push(Step::Enter(contents)), _ => {} } } } } } fn break_group(&mut self, groups: &[Doc]) { let Some(&parent) = groups.last() else { return }; if let Node::Group { broken, states: None, .. } = &mut self.nodes[parent.0 as usize] { *broken = true; } } }