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
//! Tiny implementation of [Wadler-style](http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf)
//! pretty printing algorithm.
//!
//! ## Basic Usage
//!
//! Supposed we're going to print a code snippet of function calls,
//! and we already have data structure defined as:
//!
//! ```
//! struct FunctionCall {
//! name: String,
//! args: Vec<FunctionCall>,
//! }
//! ```
//!
//! We may have a function call that is very very long,
//! so we need to pretty print it for better readability.
//! Our function call may behave like:
//!
//! ```
//! # struct FunctionCall {
//! # name: String,
//! # args: Vec<FunctionCall>,
//! # }
//! let fn_call = FunctionCall {
//! name: "foo".into(),
//! args: vec![
//! FunctionCall { name: "really_long_arg".into(), args: vec![] },
//! FunctionCall { name: "omg_so_many_parameters".into(), args: vec![] },
//! FunctionCall { name: "we_should_refactor_this".into(), args: vec![] },
//! FunctionCall { name: "is_there_seriously_another_one".into(), args: vec![] },
//! ],
//! };
//! ```
//!
//! (This example is copied from [Prettier](https://github.com/prettier/prettier/blob/411ef345a2b8b424d93aed80e28db862f3341c8f/README.md?plain=1#L69) with modifications.)
//!
//! Now we're going to implement about building [`Doc`] from the data structure above.
//! We expect arguments should be placed on a single line as possible.
//! If they're too long to fit, we insert line break with indentation:
//!
//! - When being on a single line, there're no spaces after left paren and before right paren,
//! and there must be a space after each argument comma.
//! - When being splitted into different lines, there must be indentation when printing arguments,
//! and there must be a line break between arguments.
//!
//! So, we can build [`Doc`] like this:
//!
//! ```
//! # struct FunctionCall {
//! # name: String,
//! # args: Vec<FunctionCall>,
//! # }
//! use itertools::Itertools;
//! use tiny_pretty::Doc;
//!
//! fn build_doc(fn_call: &FunctionCall) -> Doc {
//! Doc::text(&fn_call.name)
//! .append(Doc::text("("))
//! .append(
//! Doc::line_or_nil()
//! .concat(Itertools::intersperse(
//! fn_call.args.iter().map(build_doc),
//! Doc::text(",").append(Doc::line_or_space())
//! ))
//! .nest(2)
//! .append(Doc::line_or_nil())
//! .group()
//! )
//! .append(Doc::text(")"))
//! }
//! ```
//!
//! Once we have a [`Doc`], we can pretty print it:
//!
//! ```
//! # struct FunctionCall {
//! # name: String,
//! # args: Vec<FunctionCall>,
//! # }
//! # use itertools::Itertools;
//! # use tiny_pretty::Doc;
//! # let fn_call = FunctionCall {
//! # name: "foo".into(),
//! # args: vec![
//! # FunctionCall { name: "really_long_arg".into(), args: vec![] },
//! # FunctionCall { name: "omg_so_many_parameters".into(), args: vec![] },
//! # FunctionCall { name: "we_should_refactor_this".into(), args: vec![] },
//! # FunctionCall { name: "is_there_seriously_another_one".into(), args: vec![] },
//! # ],
//! # };
//! #
//! # fn build_doc(fn_call: &FunctionCall) -> Doc {
//! # Doc::text(&fn_call.name)
//! # .append(Doc::text("("))
//! # .append(
//! # Doc::line_or_nil()
//! # .append(Doc::list(Itertools::intersperse(
//! # fn_call.args.iter().map(build_doc),
//! # Doc::text(",").append(Doc::line_or_space())
//! # ).collect()))
//! # .nest(2)
//! # .append(Doc::line_or_nil())
//! # .group()
//! # )
//! # .append(Doc::text(")"))
//! # }
//! use tiny_pretty::{print, PrintOptions};
//!
//! assert_eq!(r#"
//! foo(
//! really_long_arg(),
//! omg_so_many_parameters(),
//! we_should_refactor_this(),
//! is_there_seriously_another_one()
//! )"#.trim(), &print(&build_doc(&fn_call), &PrintOptions::default()));
//! ```
//!
//! Besides, if we have a function call which is short enough to fit on single line:
//!
//! ```
//! # struct FunctionCall {
//! # name: String,
//! # args: Vec<FunctionCall>,
//! # }
//! # use itertools::Itertools;
//! # use tiny_pretty::Doc;
//! use tiny_pretty::{print, PrintOptions};
//!
//! let fn_call = FunctionCall {
//! name: "foo".into(),
//! args: vec![
//! FunctionCall { name: "a".into(), args: vec![] },
//! FunctionCall { name: "b".into(), args: vec![] },
//! FunctionCall { name: "c".into(), args: vec![] },
//! FunctionCall { name: "d".into(), args: vec![] },
//! ],
//! };
//!
//! # fn build_doc(fn_call: &FunctionCall) -> Doc {
//! # Doc::text(&fn_call.name)
//! # .append(Doc::text("("))
//! # .append(
//! # Doc::line_or_nil()
//! # .append(Doc::list(Itertools::intersperse(
//! # fn_call.args.iter().map(build_doc),
//! # Doc::text(",").append(Doc::line_or_space())
//! # ).collect()))
//! # .nest(2)
//! # .append(Doc::line_or_nil())
//! # .group()
//! # )
//! # .append(Doc::text(")"))
//! # }
//! #
//! assert_eq!(
//! "foo(a(), b(), c(), d())",
//! &print(&build_doc(&fn_call), &PrintOptions::default()),
//! );
//! ```
//!
//! You can specify advanced printing options, such as controlling line break and
//! indentation kind. See [`PrintOptions`] for details.
//!
//! ## Text Width Measurement
//!
//! By default, text width is measured as "visual width".
//! This strategy makes it satisfy the width limitation visually.
//!
//! But sometimes for some Unicode characters, you may want the column to
//! be close to width limitation as possible, though it will exceed visually.
//! To achieve that, please enable the `unicode-width` feature gate.
pub use Doc;
pub use *;
pub use print;