-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathprettier_printer.ml
271 lines (243 loc) · 7.78 KB
/
prettier_printer.ml
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
module DSL = struct
type namedField = {name: string; value: oak}
and oak =
| Application of string * oak
| Record of namedField list
| Ident of string
| Tuple of namedField list
| List of oak list
| String of string
end
(** Transform the Oak types to string *)
module CodePrinter = struct
open DSL
(**
The idea is that we capture events in a context type.
Doing this allows us to reason about the current state of the writer
and whether the next expression fits on the current line or not.
*)
type writerEvents =
| Write of string
| WriteLine
| IndentBy of int
| UnindentBy of int
type writerMode = Standard | TrySingleLine | ConfirmedMultiline
(* Type representing the writer context during code printing
- [indent_size] is the configured indentation size, typically 2
- [max_line_length] is the maximum line length before we break the line
- [current_indent] is the current indentation size
- [current_line_column] is the characters written on the current line
- [line_count] is the number of lines written
- [events] is the write events in reverse order, head event is last written
- [mode] is the current writer mode (Standard or SingleLine)
*)
type context = {
indent_size: int;
max_line_length: int;
current_indent: int;
current_line_column: int;
line_count: int;
events: writerEvents list;
mode: writerMode;
}
type appendEvents = context -> context
let emptyContext =
{
indent_size = 2;
max_line_length = 120;
current_indent = 0;
current_line_column = 0;
line_count = 0;
events = [];
mode = Standard;
}
(** Fold all the events in context into text *)
let dump (ctx : context) =
let buf = Buffer.create 1024 in
let addSpaces n = Buffer.add_string buf (String.make n ' ') in
List.fold_right
(fun event current_indent ->
match event with
| Write str ->
Buffer.add_string buf str;
current_indent
| WriteLine ->
Buffer.add_char buf '\n';
addSpaces current_indent;
current_indent
| IndentBy n -> current_indent + n
| UnindentBy n -> current_indent - n)
ctx.events ctx.current_indent
|> ignore;
Buffer.contents buf
let debug_context (ctx : context) =
let mode =
match ctx.mode with
| Standard -> "Standard"
| TrySingleLine -> "TrySingleLine"
| ConfirmedMultiline -> "ConfirmedMultiline"
in
Format.printf
"Current indent: %d, Current column: %d, # Lines: %d Events: %d, Mode: %s\n"
ctx.current_indent ctx.current_line_column ctx.line_count
(List.length ctx.events) mode;
ctx
let updateMode (newlineWasAdded : bool) (ctx : context) =
match ctx.mode with
| Standard -> ctx
| ConfirmedMultiline -> ctx
| TrySingleLine ->
{
ctx with
mode =
(if newlineWasAdded || ctx.current_line_column > ctx.max_line_length
then ConfirmedMultiline
else TrySingleLine);
}
let id x = x
(** add a write event to the context *)
let write str ctx =
{
ctx with
events = Write str :: ctx.events;
current_line_column = ctx.current_line_column + String.length str;
}
|> updateMode false
(** compose two context transforming functions *)
let ( +> ) f g ctx =
let fCtx = f ctx in
match fCtx.mode with
| ConfirmedMultiline -> fCtx
| _ -> g fCtx
let sepNln ctx =
{
ctx with
events = WriteLine :: ctx.events;
current_line_column = ctx.current_indent;
line_count = ctx.line_count + 1;
}
|> updateMode true
let sepSpace ctx = write " " ctx
let sepComma ctx = write ", " ctx
let sepSemi ctx = write "; " ctx
let sepOpenT ctx = write "(" ctx
let sepCloseT ctx = write ")" ctx
let sepOpenR ctx = write "{" ctx
let sepCloseR ctx = write "}" ctx
let sepOpenL ctx = write "[" ctx
let sepCloseL ctx = write "]" ctx
let sepEq ctx = write " = " ctx
let wrapInParentheses f = sepOpenT +> f +> sepCloseT
let indent ctx =
let nextIdent = ctx.current_indent + ctx.indent_size in
{
ctx with
current_indent = nextIdent;
current_line_column = nextIdent;
events = IndentBy ctx.indent_size :: ctx.events;
}
let unindent ctx =
let nextIdent = ctx.current_indent - ctx.indent_size in
{
ctx with
current_indent = nextIdent;
current_line_column = nextIdent;
events = UnindentBy ctx.indent_size :: ctx.events;
}
let indentAndNln f = indent +> sepNln +> f +> unindent
let col (f : 't -> appendEvents) (intertwine : appendEvents) items ctx =
let rec visit items ctx =
match items with
| [] -> ctx
| [item] -> f item ctx
| item :: rest ->
let ctx' = (f item +> intertwine) ctx in
visit rest ctx'
in
visit items ctx
let expressionFitsOnRestOfLine (f : appendEvents) (fallback : appendEvents)
(ctx : context) =
match ctx.mode with
| ConfirmedMultiline -> ctx
| _ -> (
let shortCtx =
match ctx.mode with
| Standard -> {ctx with mode = TrySingleLine}
| _ -> ctx
in
let resultCtx = f shortCtx in
match resultCtx.mode with
| ConfirmedMultiline -> fallback ctx
| TrySingleLine -> {resultCtx with mode = ctx.mode}
| Standard ->
failwith "Unexpected Standard mode after trying SingleLine mode")
let rec genOak (oak : oak) : appendEvents =
match oak with
| Application (name, argument) -> genApplication name argument
| Record record -> genRecord record
| Ident ident -> genIdent ident
| String str -> write (Format.sprintf "\"%s\"" str)
| Tuple ts -> genTuple ts
| List xs -> genList xs
and genApplication (name : string) (argument : oak) : appendEvents =
let short = write name +> sepOpenT +> genOak argument +> sepCloseT in
let long =
write name +> sepOpenT
+> (match argument with
| List _ | Record _ -> genOak argument
| _ -> indentAndNln (genOak argument) +> sepNln)
+> sepCloseT
in
expressionFitsOnRestOfLine short long
and genRecord (recordFields : namedField list) : appendEvents =
let short =
match recordFields with
| [] -> sepOpenR +> sepCloseR
| fields ->
sepOpenR +> sepSpace
+> col genNamedField sepSemi fields
+> sepSpace +> sepCloseR
in
let long =
sepOpenR
+> indentAndNln (col genNamedField sepNln recordFields)
+> sepNln +> sepCloseR
in
expressionFitsOnRestOfLine short long
and genTuple (oaks : namedField list) : appendEvents =
let short = col genNamedField sepComma oaks in
let long = col genNamedField sepNln oaks in
expressionFitsOnRestOfLine short long
and genIdent (ident : string) : appendEvents = write ident
and genNamedField (field : namedField) : appendEvents =
let genValue =
match field.value with
| Tuple _ -> sepOpenT +> genOak field.value +> sepCloseT
| _ -> genOak field.value
in
let short = write (field.name) +> sepEq +> genValue in
let long =
write (field.name) +> sepEq
+>
match field.value with
| List _ | Record _ -> genOak field.value
| _ -> indentAndNln genValue
in
expressionFitsOnRestOfLine short long
and genList (items : oak list) : appendEvents =
let genItem = function
| Tuple _ as item -> wrapInParentheses (genOak item)
| item -> genOak item
in
let short =
match items with
| [] -> sepOpenL +> sepCloseL
| _ ->
sepOpenL +> sepSpace +> col genItem sepSemi items +> sepSpace
+> sepCloseL
in
let long =
sepOpenL +> indentAndNln (col genItem sepNln items) +> sepNln +> sepCloseL
in
expressionFitsOnRestOfLine short long
end