1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use crate::{BufferWrite, PrettyTree};
use alloc::{rc::Rc, vec, vec::Vec};
use color_ansi::AnsiStyle;
use core::fmt::{Debug, Display, Formatter};
#[cfg(feature = "std")]
pub mod write_io;
pub mod write_fmt;
/// Trait representing the operations necessary to render a document
pub trait Render {
/// The type of the output
type Error;
/// Write a string to the output
fn write_str(&mut self, s: &str) -> Result<usize, Self::Error>;
/// Write a character to the output
fn write_str_all(&mut self, mut s: &str) -> Result<(), Self::Error> {
while !s.is_empty() {
let count = self.write_str(s)?;
s = &s[count..];
}
Ok(())
}
/// Write a character to the output
fn fail_doc(&self) -> Self::Error;
}
/// The given text, which must not contain line breaks.
#[derive(Debug)]
pub struct PrettyFormatter<'a> {
tree: &'a PrettyTree,
width: usize,
}
impl<'a> Display for PrettyFormatter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
self.tree.render_fmt(self.width, f)
}
}
impl PrettyTree {
/// Returns a value which implements `std::fmt::Display`
///
/// ```
/// use pretty::{BoxDoc, Doc};
/// let doc =
/// BoxDoc::<()>::group(BoxDoc::text("hello").append(Doc::line()).append(Doc::text("world")));
/// assert_eq!(format!("{}", doc.pretty(80)), "hello world");
/// ```
#[inline]
pub fn pretty(&self, width: usize) -> PrettyFormatter {
PrettyFormatter { tree: self, width }
}
}
/// Trait representing the operations necessary to write an annotated document.
pub trait RenderAnnotated: Render {
/// Push an annotation onto the stack
fn push_annotation(&mut self, annotation: Rc<AnsiStyle>) -> Result<(), Self::Error>;
/// Pop an annotation from the stack
fn pop_annotation(&mut self) -> Result<(), Self::Error>;
}
#[derive(Debug)]
enum Annotation<A> {
Push(Rc<A>),
Pop,
}
macro_rules! make_spaces {
() => { "" };
($s: tt $($t: tt)*) => { concat!(" ", make_spaces!($($t)*)) };
}
pub(crate) const SPACES: &str = make_spaces!(,,,,,,,,,,);
fn append_docs2(ldoc: Rc<PrettyTree>, rdoc: Rc<PrettyTree>, mut consumer: impl FnMut(Rc<PrettyTree>)) -> Rc<PrettyTree> {
let d = append_docs(rdoc, &mut consumer);
consumer(d);
append_docs(ldoc, &mut consumer)
}
fn append_docs(mut doc: Rc<PrettyTree>, consumer: &mut impl FnMut(Rc<PrettyTree>)) -> Rc<PrettyTree> {
loop {
// Since appended documents often appear in sequence on the left side we
// gain a slight performance increase by batching these pushes (avoiding
// to push and directly pop `Append` documents)
match doc.as_ref() {
PrettyTree::Append { lhs, rhs } => {
let d = append_docs(rhs.clone(), consumer);
consumer(d);
doc = lhs.clone();
}
_ => return doc,
}
}
}
pub fn best<W>(doc: Rc<PrettyTree>, width: usize, out: &mut W) -> Result<(), W::Error>
where
W: RenderAnnotated,
W: ?Sized,
{
Best {
pos: 0,
back_cmds: vec![RenderCommand { indent: 0, mode: Mode::Break, node: doc }],
front_cmds: vec![],
annotation_levels: vec![],
width,
}
.best(0, out)?;
Ok(())
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
enum Mode {
Break,
Flat,
}
struct RenderCommand {
indent: usize,
mode: Mode,
node: Rc<PrettyTree>,
}
fn write_newline<W>(ind: usize, out: &mut W) -> Result<(), W::Error>
where
W: ?Sized + Render,
{
out.write_str_all("\n")?;
write_spaces(ind, out)
}
fn write_spaces<W>(spaces: usize, out: &mut W) -> Result<(), W::Error>
where
W: ?Sized + Render,
{
let mut inserted = 0;
while inserted < spaces {
let insert = core::cmp::min(SPACES.len(), spaces - inserted);
inserted += out.write_str(&SPACES[..insert])?;
}
Ok(())
}
struct Best {
pos: usize,
back_cmds: Vec<RenderCommand>,
front_cmds: Vec<Rc<PrettyTree>>,
annotation_levels: Vec<usize>,
width: usize,
}
impl Best {
fn fitting(&mut self, next: Rc<PrettyTree>, mut pos: usize, ind: usize) -> bool {
let mut bidx = self.back_cmds.len();
self.front_cmds.clear(); // clear from previous calls from best
self.front_cmds.push(next);
let mut mode = Mode::Flat;
loop {
let mut doc = match self.front_cmds.pop() {
None => {
if bidx == 0 {
// All commands have been processed
return true;
}
else {
bidx -= 1;
mode = Mode::Break;
self.back_cmds[bidx].node.clone()
}
}
Some(cmd) => cmd,
};
loop {
match doc.as_ref() {
PrettyTree::Nil => {}
PrettyTree::Append { lhs, rhs } => {
doc = append_docs2(lhs.clone(), rhs.clone(), |send| self.front_cmds.push(send));
continue;
}
// Newlines inside the group makes it not fit, but those outside lets it
// fit on the current line
PrettyTree::Hardline => return mode == Mode::Break,
PrettyTree::RenderLength { length: len, body: _ } => {
pos += len;
if pos > self.width {
return false;
}
}
PrettyTree::StaticText(str) => {
pos += str.len();
if pos > self.width {
return false;
}
}
PrettyTree::Text(ref str) => {
pos += str.len();
if pos > self.width {
return false;
}
}
PrettyTree::MaybeInline { block: flat, inline } => {
doc = match mode {
Mode::Break => flat.clone(),
Mode::Flat => inline.clone(),
};
continue;
}
PrettyTree::Column { invoke: function } => {
doc = Rc::new(function(pos));
continue;
}
PrettyTree::Nesting { invoke: function } => {
doc = Rc::new(function(ind));
continue;
}
PrettyTree::Nest { space: _, doc: next }
| PrettyTree::Group { items: next }
| PrettyTree::Annotated { style: _, body: next }
| PrettyTree::Union { lhs: _, rhs: next } => {
doc = next.clone();
continue;
}
PrettyTree::Fail => return false,
}
break;
}
}
}
fn best<W>(&mut self, top: usize, out: &mut W) -> Result<bool, W::Error>
where
W: RenderAnnotated,
W: ?Sized,
{
let mut fits = true;
while top < self.back_cmds.len() {
let mut cmd = self.back_cmds.pop().unwrap();
loop {
let RenderCommand { indent: ind, mode, node } = cmd;
match node.as_ref() {
PrettyTree::Nil => {}
PrettyTree::Append { lhs, rhs } => {
cmd.node = append_docs2(lhs.clone(), rhs.clone(), |send| {
self.back_cmds.push(RenderCommand { indent: ind, mode, node: send })
});
continue;
}
PrettyTree::MaybeInline { block, inline } => {
cmd.node = match mode {
Mode::Break => block.clone(),
Mode::Flat => inline.clone(),
};
continue;
}
PrettyTree::Group { items } => {
match mode {
Mode::Break if self.fitting(items.clone(), self.pos, ind) => {
cmd.mode = Mode::Flat;
}
_ => {}
}
cmd.node = items.clone();
continue;
}
PrettyTree::Nest { space, doc } => {
// Once https://doc.rust-lang.org/std/primitive.usize.html#method.saturating_add_signed is stable
// this can be replaced
let new_ind = if *space >= 0 {
ind.saturating_add(*space as usize)
}
else {
ind.saturating_sub(space.unsigned_abs())
};
cmd = RenderCommand { indent: new_ind, mode, node: doc.clone() };
continue;
}
PrettyTree::Hardline => {
// The next document may have different indentation so we should use it if we can
match self.back_cmds.pop() {
Some(next) => {
write_newline(next.indent, out)?;
self.pos = next.indent;
cmd = next;
continue;
}
None => {
write_newline(ind, out)?;
self.pos = ind;
}
}
}
PrettyTree::RenderLength { length: len, body: doc } => match doc.as_ref() {
PrettyTree::Text(s) => {
out.write_str_all(s)?;
self.pos += len;
fits &= self.pos <= self.width;
}
PrettyTree::StaticText(s) => {
out.write_str_all(s)?;
self.pos += len;
fits &= self.pos <= self.width;
}
_ => unreachable!(),
},
PrettyTree::Text(ref s) => {
out.write_str_all(s)?;
self.pos += s.len();
fits &= self.pos <= self.width;
}
PrettyTree::StaticText(s) => {
out.write_str_all(s)?;
self.pos += s.len();
fits &= self.pos <= self.width;
}
PrettyTree::Annotated { style: color, body: doc } => {
out.push_annotation(color.clone())?;
self.annotation_levels.push(self.back_cmds.len());
cmd.node = doc.clone();
continue;
}
PrettyTree::Union { lhs: left, rhs: right } => {
let pos = self.pos;
let annotation_levels = self.annotation_levels.len();
let bcmds = self.back_cmds.len();
self.back_cmds.push(RenderCommand { indent: ind, mode, node: left.clone() });
let mut buffer = BufferWrite::new(0);
match self.best(bcmds, &mut buffer) {
Ok(true) => buffer.render(out)?,
Ok(false) | Err(_) => {
self.pos = pos;
self.back_cmds.truncate(bcmds);
self.annotation_levels.truncate(annotation_levels);
cmd.node = right.clone();
continue;
}
}
}
PrettyTree::Column { invoke: column } => {
cmd.node = Rc::new(column(self.pos));
continue;
}
PrettyTree::Nesting { invoke: nesting } => {
cmd.node = Rc::new(nesting(self.pos));
continue;
}
PrettyTree::Fail => return Err(out.fail_doc()),
}
break;
}
while self.annotation_levels.last() == Some(&self.back_cmds.len()) {
self.annotation_levels.pop();
out.pop_annotation()?;
}
}
Ok(fits)
}
}