Source file indentPrinter.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
272
273
274
275
276
open Compat
open Pos
open Nstream
open Approx_lexer
open Util
type output_elt = Newline | Indent of int | Whitespace of string | Text of string
type 'a output_kind =
| Numeric of (int -> 'a -> 'a)
| Print of (string -> 'a -> 'a)
| Extended of (IndentBlock.t -> output_elt -> 'a -> 'a)
type 'a output = {
debug: bool;
config: IndentConfig.t;
in_lines: int -> bool;
adaptive: bool;
indent_empty: bool;
kind: 'a output_kind;
}
let std_output = {
debug = false;
config = IndentConfig.default;
in_lines = (fun _ -> true);
adaptive = true;
indent_empty = false;
kind = Print (fun s () -> print_endline s);
}
let pr_string output block text usr =
match output.kind with
| Numeric _ -> usr
| Print f -> f text usr
| Extended f -> f block (Text text) usr
let pr_whitespace output block text usr =
match output.kind with
| Numeric _ -> usr
| Print f -> f text usr
| Extended f -> f block (Whitespace text) usr
let pr_nl output block usr =
match output.kind with
| Numeric _ -> usr
| Print pr -> pr "\n" usr
| Extended pr -> pr block Newline usr
type indentKind = Normal
| Empty
| Padded
| Fixed of int
let warn_tabs = ref true
let print_indent output line blank ?(kind=Normal) block usr =
if output.in_lines line then
let indent =
match kind with
| Normal -> IndentBlock.indent block
| Empty ->
if output.indent_empty then IndentBlock.guess_indent line block
else 0
| Padded ->
IndentBlock.indent block + IndentBlock.padding block
| Fixed n -> n
in
match output.kind with
| Numeric pr -> pr indent usr
| Print pr -> pr (String.make indent ' ') usr
| Extended pr -> pr block (Indent indent) usr
else (
if !warn_tabs && String.contains blank '\t' then (
warn_tabs := false;
prerr_endline
"Warning: ocp-indent input contains indentation by tabs, \
partial indent will be unreliable."
);
match output.kind with
| Numeric _ -> usr
| Print pr -> pr blank usr
| Extended pr -> pr block (Whitespace blank) usr
)
let print_token output block tok usr =
let orig_start_column = IndentBlock.original_column block in
let start_column = IndentBlock.offset block in
let rec line pad last ?(item_cont=false) lines usr =
match lines with
| [] -> usr
| text::next_lines ->
let usr = usr |> pr_nl output block in
if not (output.in_lines line) then
usr
|> print_indent output line "" block
|> pr_string output block text
|> print_extra_lines (line+1) pad text next_lines
else if String.trim text = "" && tok.token <> OCAMLDOC_VERB then
usr
|> print_indent output line "" ~kind:Empty block
|> print_extra_lines (line+1) pad text next_lines
else
let orig_line_indent = count_leading_spaces text in
let orig_offset = orig_line_indent - orig_start_column in
let text =
String.sub text orig_line_indent
(String.length text - orig_line_indent)
in
let indent_value, item_cont =
match pad with
| None -> orig_line_indent, false
| Some pad -> match tok.token with
| STRING _ ->
if ends_with_escape last then
if is_prefix "\"" text || is_prefix "\\ " text
then start_column, item_cont
else start_column + pad, item_cont
else orig_line_indent, item_cont
| COMMENT | COMMENTCONT ->
let is_item =
is_prefix "- " text && not (is_prefix "- :" text)
in
let n =
if is_prefix "*" text then 1 else
if not is_item && item_cont then pad + 2
else pad
in
let item_cont = is_item || item_cont && text <> "" in
let n =
if output.config.IndentConfig.i_strict_comments || is_item
then n else max orig_offset n
in
let n = if next_lines = [] && text = "*)" then 0 else n in
start_column + n, item_cont
| QUOTATION opening ->
if is_prefix "{" opening then orig_line_indent, item_cont
else
(start_column +
if next_lines = [] && text = ">>" then 0
else max orig_offset pad),
item_cont
| _ -> start_column + max orig_offset pad, item_cont
in
usr
|> print_indent output line "" ~kind:(Fixed indent_value) block
|> pr_string output block text
|> print_extra_lines (line+1) pad ~item_cont text next_lines
in
let line = Region.start_line tok.region in
let text, next_lines =
if line = Region.end_line tok.region then (Lazy.force tok.substr), []
else match string_split '\n' (Lazy.force tok.substr) with
| [] -> assert false
| hd::tl -> hd,tl
in
let pad =
if next_lines = [] then None
else match tok.token with
| STRING _ ->
(match String.trim text with
| "\"" | "\"\\" -> None
| _ -> Some 1 )
| COMMENT ->
(match String.trim text with
| "(*" when not output.config.IndentConfig.i_strict_comments -> None
| _ -> Some (IndentBlock.padding block))
| COMMENTCONT ->
Some (IndentBlock.padding block)
| OCAMLDOC_VERB -> None
| QUOTATION opening ->
let oplen = String.length opening in
let textlen = String.length text in
if oplen = textlen then None
else
Some
(oplen +
count_leading_spaces
(String.sub text oplen (textlen - oplen - 1)))
| _ -> Some 2
in
usr
|> pr_string output block text
|> print_extra_lines (line+1) pad text next_lines
let rec loop output block stream usr =
match Nstream.next stream with
| None -> usr
| Some (t, stream) ->
let line = Region.start_line t.region in
let last_line = line - t.newlines in
let blank, usr =
let rec indent_between line blanks usr = match blanks with
| [] -> assert false
| bl::[] -> bl, usr |> pr_nl output block
| bl::blanks ->
usr
|> pr_nl output block
|> print_indent output line bl ~kind:Empty block
|> indent_between (line + 1) blanks
in
let blanks = string_split '\n' (Lazy.force t.between) in
match blanks with
| [] -> assert false
| bl::[] -> bl, usr
| bl::blanks ->
usr
|> (if last_line = 0 then print_indent output 1 "" ~kind:Empty block
else fun usr -> usr)
|> pr_whitespace output block bl
|> indent_between (last_line + 1) blanks
in
let block = IndentBlock.update output.config block stream t in
let block =
if output.adaptive && not (output.in_lines line)
then IndentBlock.reverse block
else block
in
if output.debug then IndentBlock.dump block;
let at_line_start = t.newlines > 0 in
let usr =
if at_line_start then
let kind = match t.token with
| COMMENT when is_prefix "(*\n" (Lazy.force t.substr) ->
Fixed (String.length blank)
| OCAMLDOC_VERB -> Padded
| EOF -> Empty
| COMMENTCONT when (Lazy.force t.substr <> "*)") -> Padded
| _ -> Normal
in
usr
|> print_indent output line blank ~kind block
else
usr
|> pr_whitespace output block blank
in
let usr = usr |> print_token output block t in
match t.token with EOF -> usr
| _ -> usr |> loop output block stream
let proceed output stream block usr =
usr |> loop output block stream