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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use std::{
fmt,
ops::{Add, AddAssign, Deref},
};
use crate::{text::Text, BuildDoc, Doc, DocAllocator, Pretty};
/// The `DocBuilder` type allows for convenient appending of documents even for arena allocated
/// documents by storing the arena inline.
pub struct DocBuilder<'a, D>(pub &'a D, pub BuildDoc<'a, D::Doc>)
where
D: ?Sized + DocAllocator<'a>;
impl<'a, D> DocBuilder<'a, D>
where
D: ?Sized + DocAllocator<'a>,
{
pub(crate) fn from_utf8_text(allocator: &'a D, text: Text<'a>) -> Self {
let s = &*text;
let doc = if s.is_ascii() {
Doc::Text(text)
} else {
let display_width = unicode_width::UnicodeWidthStr::width(s);
Doc::TextWithLen(display_width, allocator.alloc(Doc::Text(text)))
};
Self(allocator, doc.into())
}
pub fn into_doc(self) -> D::Doc {
match self.1 {
BuildDoc::DocPtr(d) => d,
BuildDoc::Doc(d) => self.0.alloc(d),
}
}
pub fn into_ref(self) -> Self {
match self.1 {
BuildDoc::DocPtr(_) => self,
BuildDoc::Doc(d) => Self(self.0, BuildDoc::DocPtr(self.0.alloc(d))),
}
}
pub(crate) fn into_plain_doc(self) -> Doc<'a, D::Doc> {
match self.1 {
BuildDoc::DocPtr(_) => unreachable!(),
BuildDoc::Doc(d) => d,
}
}
/// Append the given document after this document.
#[inline]
pub fn append<E>(self, that: E) -> Self
where
E: Pretty<'a, D>,
{
let Self(allocator, _) = self;
let that = that.pretty(allocator);
match (&*self, &*that) {
(Doc::Nil, _) => that,
(_, Doc::Nil) => self,
_ => Self(
allocator,
Doc::Append(
allocator.alloc_cow(self.into()),
allocator.alloc_cow(that.into()),
)
.into(),
),
}
}
/// Repeats `self` `n` times, appending each repetition.
///
/// ```
/// use prettyless::{Arena, DocAllocator};
///
/// let arena = Arena::new();
/// let doc = arena.text("[]");
///
/// assert_eq!(doc.clone().repeat(0).print(100).to_string(), "");
/// assert_eq!(arena.len(), 0); // +0
///
/// assert_eq!(doc.clone().repeat(1).print(100).to_string(), "[]");
/// assert_eq!(arena.len(), 0); // +0
///
/// assert_eq!(doc.clone().repeat(2).print(100).to_string(), "[][]");
/// assert_eq!(arena.len(), 1); // +1
///
/// assert_eq!(doc.clone().repeat(5).print(100).to_string(), "[][][][][]");
/// assert_eq!(arena.len(), 5); // +4
/// ```
pub fn repeat(self, n: usize) -> Self
where
D::Doc: Clone,
{
if n == 1 {
return self;
}
let Self(allocator, this) = self;
if n == 0 {
return allocator.nil();
}
let this = allocator.alloc_cow(this);
let mut doc = Doc::Append(this.clone(), this.clone());
for _ in 2..n {
doc = Doc::Append(allocator.alloc_cow(doc.into()), this.clone());
}
Self(allocator, doc.into())
}
/// Make `self` a line suffix.
///
/// ```
/// use prettyless::{Arena, DocAllocator};
///
/// let arena = Arena::new();
/// let doc = arena.text(" // comment").as_line_suffix() + arena.text("x");
///
/// assert_eq!(doc.print(80).to_string(), "x // comment");
/// ```
#[inline]
pub fn as_line_suffix(self) -> Self {
match *self.1 {
Doc::Nil | Doc::LineSuffix(_) => self,
_ => {
let Self(allocator, this) = self;
Self(allocator, Doc::LineSuffix(allocator.alloc_cow(this)).into())
}
}
}
/// Acts as `self` when laid out on multiple lines and acts as `that` when laid out on a single line.
///
/// ```
/// use prettyless::{Arena, DocAllocator};
///
/// let arena = Arena::new();
/// let body = arena.line().append("x");
/// let doc = arena.text("let")
/// .append(arena.line())
/// .append("x")
/// .group()
/// .append(
/// body.clone()
/// .flat_alt(
/// arena.line()
/// .append("in")
/// .append(body)
/// )
/// )
/// .group();
///
/// assert_eq!(doc.print(100).to_string(), "let x in x");
/// assert_eq!(doc.print(8).to_string(), "let x\nx");
/// ```
#[inline]
pub fn flat_alt<E>(self, that: E) -> Self
where
E: Pretty<'a, D>,
{
let Self(allocator, this) = self;
let that = that.pretty(allocator);
Self(
allocator,
Doc::BreakOrFlat(allocator.alloc_cow(this), allocator.alloc_cow(that.into())).into(),
)
}
/// Equivalent to `nil.flat_alt(self)`
/// ```
/// use prettyless::{Arena, DocAllocator};
///
/// let arena = Arena::new();
/// let doc = arena.text("flat-only").when_group_flat().group();
///
/// assert_eq!(doc.print(0).to_string(), "");
/// assert_eq!(doc.print(10).to_string(), "flat-only");
/// ```
#[inline]
pub fn when_group_flat(self) -> Self {
let Self(allocator, this) = self;
allocator.if_group_flat(this)
}
/// Equivalent to `self.flat_alt(nil)`
/// ```
/// use prettyless::{Arena, DocAllocator};
///
/// let arena = Arena::new();
/// let doc = arena.text("a")
/// .append(arena.text(",").when_group_break())
/// .enclose(arena.line_(), arena.line_())
/// .parens()
/// .group();
///
/// assert_eq!(doc.print(1).to_string(), "(\na,\n)");
/// assert_eq!(doc.print(10).to_string(), "(a)");
/// ```
#[inline]
pub fn when_group_break(self) -> Self {
let Self(allocator, this) = self;
allocator.if_group_break(this)
}
/// Flatten inner docs. Hard line will turn to failure.
#[inline]
pub fn flatten(self) -> Self {
let Self(allocator, this) = self;
match *this {
Doc::Nil | Doc::Text(_) | Doc::TextWithLen(_, _) | Doc::Flatten(_) => {
Self(allocator, this)
}
Doc::HardLine => self.0.fail(),
_ => DocBuilder(allocator, Doc::Flatten(allocator.alloc_cow(this)).into()),
}
}
/// Mark this document as a group.
///
/// Groups are layed out on a single line if possible. Within a group, all basic documents with
/// several possible layouts are assigned the same layout, that is, they are all layed out
/// horizontally and combined into a one single line, or they are each layed out on their own
/// line.
#[inline]
pub fn group(self) -> Self {
match *self.1 {
Doc::Nil | Doc::Text(_) | Doc::TextWithLen(_, _) | Doc::Group(_) => self,
_ => {
let Self(allocator, this) = self;
Self(allocator, Doc::Group(allocator.alloc_cow(this)).into())
}
}
}
/// Increase the indentation level of this document.
#[inline]
pub fn nest(self, offset: isize) -> Self {
if offset == 0 {
return self;
}
if let Doc::Nil = &*self.1 {
return self;
}
let Self(allocator, this) = self;
Self(
allocator,
Doc::Nest(offset, allocator.alloc_cow(this)).into(),
)
}
/// Increases the indentation level of this document by the given number of spaces.
///
/// This is equivalent to calling `nest(offset as isize)`.
#[inline]
pub fn indent(self, offset: usize) -> Self {
self.nest(offset as isize)
}
/// Decreases the indentation level of this document by the given number of spaces.
///
/// This is equivalent to calling `nest(-(offset as isize))`.
#[inline]
pub fn dedent(self, offset: usize) -> Self {
self.nest(-(offset as isize))
}
/// Dedents to the root level, which is always 0.
///
/// ```
/// use prettyless::{Arena, DocAllocator};
///
/// let arena = Arena::new();
/// let doc = (
/// arena.text("a")
/// + (arena.text("b") + arena.hardline() + arena.text("c")).dedent_to_root()
/// + arena.hardline()
/// + arena.text("e")
/// ).indent(4);
/// assert_eq!(doc.print(10).to_string(), "ab\nc\n e");
/// ```
#[inline]
pub fn dedent_to_root(self) -> Self {
match *self.1 {
Doc::Nil | Doc::DedentToRoot(_) => self,
_ => {
let Self(allocator, this) = self;
Self(
allocator,
Doc::DedentToRoot(allocator.alloc_cow(this)).into(),
)
}
}
}
/// Lays out `self` so with the nesting level set to the current column
///
/// ```rust
/// use prettyless::{docs, DocAllocator};
///
/// let arena = &prettyless::Arena::new();
/// let doc = docs![
/// arena,
/// "lorem",
/// " ",
/// arena.intersperse(["ipsum", "dolor"].iter().cloned(), arena.line_()).align(),
/// arena.hardline(),
/// "next",
/// ];
/// assert_eq!(
/// doc.print(80).to_string(),
/// "
/// lorem ipsum
/// dolor
/// next".trim_start()
/// );
/// ```
#[inline]
pub fn align(self) -> Self {
match *self.1 {
Doc::Nil | Doc::Align(_) => self,
_ => {
let Self(allocator, this) = self;
Self(allocator, Doc::Align(allocator.alloc_cow(this)).into())
}
}
}
/// ```
/// use prettyless::{Arena, DocAllocator};
///
/// let arena = Arena::new();
/// let doc = (arena.text("short") + arena.hardline() + arena.text("long long long"))
/// .union(arena.text("short") + arena.hardline() + arena.text("short"));
/// assert_eq!(doc.print(10).to_string(), "short\nshort");
/// ```
#[inline]
pub fn union<E>(self, other: E) -> Self
where
E: Into<BuildDoc<'a, D::Doc>>,
{
let Self(allocator, this) = self;
let other = other.into();
let doc = Doc::Union(allocator.alloc_cow(this), allocator.alloc_cow(other));
Self(allocator, doc.into())
}
/// Like `union`, but it only ensures fitting on the first line.
///
/// ```
/// use prettyless::{Arena, DocAllocator};
///
/// let arena = Arena::new();
/// let doc = (arena.text("short") + arena.hardline() + arena.text("long long long"))
/// .partial_union(arena.text("short") + arena.hardline() + arena.text("short"));
/// assert_eq!(doc.print(10).to_string(), "short\nlong long long");
/// ```
#[inline]
pub fn partial_union<E>(self, other: E) -> Self
where
E: Into<BuildDoc<'a, D::Doc>>,
{
let Self(allocator, this) = self;
let other = other.into();
let doc = Doc::PartialUnion(allocator.alloc_cow(this), allocator.alloc_cow(other));
Self(allocator, doc.into())
}
/// Lays out `self` and provides the column width of it available to `f`
///
/// NOTE: The doc pointer type, `D` may need to be cloned. Consider using cheaply cloneable ptr
/// like `RefDoc` or `RcDoc`
#[cfg(feature = "contextual")]
#[inline]
pub fn measure_width(self, f: impl Fn(isize) -> D::Doc + 'a) -> Self
where
BuildDoc<'a, D::Doc>: Clone,
{
let Self(allocator, this) = self;
let f = allocator.alloc_width_fn(f);
allocator.on_column(move |start| {
let f = f.clone();
Self(allocator, this.clone())
.append(allocator.on_column(move |end| f(end as isize - start as isize)))
.into_doc()
})
}
/// Puts `self` between `before` and `after`
#[inline]
pub fn enclose<E, F>(self, before: E, after: F) -> Self
where
E: Pretty<'a, D>,
F: Pretty<'a, D>,
{
let Self(allocator, _) = self;
Self(allocator, before.pretty(allocator).1)
.append(self)
.append(after)
}
pub fn single_quotes(self) -> Self {
self.enclose("'", "'")
}
pub fn double_quotes(self) -> Self {
self.enclose("\"", "\"")
}
pub fn parens(self) -> Self {
self.enclose("(", ")")
}
pub fn angles(self) -> Self {
self.enclose("<", ">")
}
pub fn braces(self) -> Self {
self.enclose("{", "}")
}
pub fn brackets(self) -> Self {
self.enclose("[", "]")
}
}
impl<'a, D> Deref for DocBuilder<'a, D>
where
D: ?Sized + DocAllocator<'a>,
{
type Target = Doc<'a, D::Doc>;
fn deref(&self) -> &Self::Target {
match &self.1 {
BuildDoc::DocPtr(d) => d,
BuildDoc::Doc(d) => d,
}
}
}
impl<'a, D> fmt::Debug for DocBuilder<'a, D>
where
D: ?Sized + DocAllocator<'a>,
D::Doc: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.1.fmt(f)
}
}
impl<'a, D> Clone for DocBuilder<'a, D>
where
D: DocAllocator<'a> + 'a,
D::Doc: Clone,
{
fn clone(&self) -> Self {
Self(self.0, self.1.clone())
}
}
impl<'a, D> From<DocBuilder<'a, D>> for BuildDoc<'a, D::Doc>
where
D: ?Sized + DocAllocator<'a>,
{
fn from(val: DocBuilder<'a, D>) -> Self {
val.1
}
}
impl<'a, D, P> Add<P> for DocBuilder<'a, D>
where
D: ?Sized + DocAllocator<'a>,
P: Pretty<'a, D>,
{
type Output = Self;
fn add(self, other: P) -> Self::Output {
self.append(other)
}
}
impl<'a, D, P> AddAssign<P> for DocBuilder<'a, D>
where
D: ?Sized + DocAllocator<'a>,
P: Pretty<'a, D>,
{
fn add_assign(&mut self, other: P) {
*self = Self(self.0, std::mem::take(&mut self.1)).append(other)
}
}