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
use bumpalo::Bump;
use super::collections::{GraphNode, GraphNodeIterator};
use super::print_items::WriterInfo;
use super::StringContainer;
use super::WriteItem;
pub struct WriterState<'a> {
current_line_column: u32,
current_line_number: u32,
last_line_indent_level: u8,
indent_level: u8,
expect_newline_next: bool,
indent_queue_count: u8,
last_was_not_trailing_space: bool,
ignore_indent_count: u8,
items: Option<&'a GraphNode<'a, WriteItem<'a>>>,
}
impl<'a> WriterState<'a> {
pub fn get_writer_info(&self, indent_width: u8) -> WriterInfo {
WriterInfo {
line_number: self.current_line_number,
column_number: self.current_line_column,
indent_level: self.indent_level,
line_start_indent_level: self.last_line_indent_level,
line_start_column_number: get_line_start_column_number(&self, indent_width),
}
}
}
impl<'a> Clone for WriterState<'a> {
fn clone(&self) -> WriterState<'a> {
WriterState {
current_line_column: self.current_line_column,
current_line_number: self.current_line_number,
last_line_indent_level: self.last_line_indent_level,
indent_level: self.indent_level,
expect_newline_next: self.expect_newline_next,
indent_queue_count: self.indent_queue_count,
last_was_not_trailing_space: self.last_was_not_trailing_space,
ignore_indent_count: self.ignore_indent_count,
items: self.items.clone(),
}
}
}
pub struct WriterOptions {
pub indent_width: u8,
#[cfg(feature = "tracing")]
pub enable_tracing: bool,
}
pub struct Writer<'a> {
bump: &'a Bump,
state: WriterState<'a>,
indent_width: u8,
#[cfg(feature = "tracing")]
nodes: Option<Vec<&'a GraphNode<'a, WriteItem<'a>>>>,
}
impl<'a> Writer<'a> {
pub fn new(bump: &'a Bump, options: WriterOptions) -> Writer<'a> {
Writer {
bump,
indent_width: options.indent_width,
state: WriterState {
current_line_column: 0,
current_line_number: 0,
last_line_indent_level: 0,
indent_level: 0,
expect_newline_next: false,
indent_queue_count: 0,
last_was_not_trailing_space: false,
ignore_indent_count: 0,
items: None,
},
#[cfg(feature = "tracing")]
nodes: if options.enable_tracing { Some(Vec::new()) } else { None },
}
}
pub fn get_state(&self) -> WriterState<'a> {
self.state.clone()
}
pub fn set_state(&mut self, state: WriterState<'a>) {
self.state = state;
}
pub fn start_indent(&mut self) {
self.set_indent_level(self.state.indent_level + 1);
}
pub fn finish_indent(&mut self) {
if self.state.indent_queue_count > 0 {
self.state.indent_queue_count -= 1;
} else {
if self.state.indent_level == 0 {
panic!("For some reason finish_indent was called without a corresponding start_indent.");
}
self.set_indent_level(self.state.indent_level - 1);
}
}
fn set_indent_level(&mut self, new_level: u8) {
self.state.indent_level = new_level;
if self.state.current_line_column == 0 {
self.state.last_line_indent_level = new_level;
}
}
pub fn start_ignoring_indent(&mut self) {
self.state.ignore_indent_count += 1;
}
pub fn finish_ignoring_indent(&mut self) {
self.state.ignore_indent_count -= 1;
}
pub fn mark_expect_new_line(&mut self) {
self.state.expect_newline_next = true;
}
pub fn space_if_not_trailing(&mut self) {
if !self.state.expect_newline_next {
self.space();
self.state.last_was_not_trailing_space = true;
}
}
pub fn queue_indent(&mut self) {
self.state.indent_queue_count += 1;
}
#[inline]
pub fn get_line_start_indent_level(&self) -> u8 {
self.state.last_line_indent_level
}
#[inline]
pub fn get_indentation_level(&self) -> u8 {
self.state.indent_level
}
#[inline]
pub fn get_indent_width(&self) -> u8 {
self.indent_width
}
#[cfg(debug_assertions)]
pub fn get_ignore_indent_count(&self) -> u8 {
self.state.ignore_indent_count
}
#[inline]
pub fn get_line_start_column_number(&self) -> u32 {
get_line_start_column_number(&self.state, self.indent_width)
}
#[inline]
pub fn get_line_column(&self) -> u32 {
if self.state.current_line_column == 0 {
(self.indent_width as u32) * (self.state.indent_level as u32)
} else {
self.state.current_line_column
}
}
#[inline]
pub fn get_line_number(&self) -> u32 {
self.state.current_line_number
}
pub fn new_line(&mut self) {
if self.state.last_was_not_trailing_space {
self.pop_item();
self.state.last_was_not_trailing_space = false;
}
self.state.current_line_column = 0;
self.state.current_line_number += 1;
self.state.last_line_indent_level = self.state.indent_level;
self.state.expect_newline_next = false;
self.push_item(WriteItem::NewLine);
}
pub fn single_indent(&mut self) {
self.handle_first_column();
self.state.current_line_column += self.indent_width as u32;
self.push_item(WriteItem::Indent(1));
}
pub fn tab(&mut self) {
self.handle_first_column();
self.state.current_line_column += self.indent_width as u32;
self.push_item(WriteItem::Tab);
}
fn space(&mut self) {
self.handle_first_column();
self.state.current_line_column += 1;
self.push_item(WriteItem::Space);
}
pub fn write(&mut self, text: &'a StringContainer) {
self.handle_first_column();
self.state.current_line_column += text.char_count;
self.push_item(WriteItem::String(text));
}
fn handle_first_column(&mut self) {
if self.state.expect_newline_next {
self.new_line();
}
self.state.last_was_not_trailing_space = false;
if self.state.current_line_column == 0 && self.state.indent_level > 0 && self.state.ignore_indent_count == 0 {
self.state.last_line_indent_level = self.state.indent_level;
self.state.current_line_column = self.state.indent_level as u32 * self.indent_width as u32;
if self.state.indent_level > 0 {
self.push_item(WriteItem::Indent(self.state.indent_level));
}
}
}
fn push_item(&mut self, item: WriteItem<'a>) {
let previous = std::mem::replace(&mut self.state.items, None);
let graph_node = self.bump.alloc(GraphNode::new(item, previous));
self.state.items = Some(graph_node);
#[cfg(feature = "tracing")]
if let Some(nodes) = self.nodes.as_mut() {
nodes.push(graph_node);
}
if self.state.indent_queue_count > 0 {
let indent_count = self.state.indent_queue_count;
self.state.indent_queue_count = 0;
self.state.indent_level += indent_count;
}
}
fn pop_item(&mut self) {
if let Some(previous) = &self.state.items {
self.state.items = previous.borrow_previous().clone();
}
}
pub fn get_items(self) -> impl Iterator<Item = &'a WriteItem<'a>> {
match self.state.items {
Some(items) => items.into_iter().collect::<Vec<_>>().into_iter().rev(),
None => GraphNodeIterator::empty().collect::<Vec<_>>().into_iter().rev(),
}
}
#[cfg(feature = "tracing")]
pub fn get_current_node_id(&self) -> Option<usize> {
self.state.items.as_ref().map(|node| node.graph_node_id)
}
#[cfg(feature = "tracing")]
pub fn get_nodes(self) -> Vec<&'a GraphNode<'a, WriteItem<'a>>> {
self.nodes.expect("Should have set enable_tracing to true.")
}
#[cfg(debug_assertions)]
#[allow(dead_code)]
pub fn to_string_for_debugging(&self) -> String {
let write_items = self.get_items_cloned();
super::print_write_items(
write_items.iter(),
super::WriteItemsPrinterOptions {
use_tabs: false,
new_line_text: "\n",
indent_width: self.indent_width,
},
)
}
#[cfg(debug_assertions)]
fn get_items_cloned(&self) -> Vec<WriteItem> {
let mut items = Vec::new();
let mut current_item = self.state.items;
while let Some(item) = current_item {
items.insert(0, item.borrow_item().clone());
current_item = item.borrow_previous().clone();
}
items
}
}
#[inline]
fn get_line_start_column_number(writer_state: &WriterState, indent_width: u8) -> u32 {
(writer_state.last_line_indent_level as u32) * (indent_width as u32)
}
#[cfg(test)]
mod test {
use super::super::{print_write_items, utils::with_bump_allocator_mut, StringContainer, WriteItemsPrinterOptions};
use super::*;
#[test]
fn write_singleword_writes() {
with_bump_allocator_mut(|bump| {
let mut writer = create_writer(&bump);
write_text(&mut writer, "test", &bump);
assert_writer_equal(writer, "test");
bump.reset();
});
}
#[test]
fn write_multiple_lines_writes() {
with_bump_allocator_mut(|bump| {
let mut writer = create_writer(&bump);
write_text(&mut writer, "1", &bump);
writer.new_line();
write_text(&mut writer, "2", &bump);
assert_writer_equal(writer, "1\n2");
bump.reset();
});
}
#[test]
fn write_indented_writes() {
with_bump_allocator_mut(|bump| {
let mut writer = create_writer(&bump);
write_text(&mut writer, "1", &bump);
writer.new_line();
writer.start_indent();
write_text(&mut writer, "2", &bump);
writer.finish_indent();
writer.new_line();
write_text(&mut writer, "3", &bump);
assert_writer_equal(writer, "1\n 2\n3");
bump.reset();
});
}
#[test]
fn write_singleindent_writes() {
with_bump_allocator_mut(|bump| {
let mut writer = create_writer(&bump);
writer.single_indent();
write_text(&mut writer, "t", &bump);
assert_writer_equal(writer, " t");
bump.reset();
});
}
#[test]
fn markexpectnewline_writesnewline() {
with_bump_allocator_mut(|bump| {
let mut writer = create_writer(&bump);
write_text(&mut writer, "1", &bump);
writer.mark_expect_new_line();
write_text(&mut writer, "2", &bump);
assert_writer_equal(writer, "1\n2");
bump.reset();
});
}
fn assert_writer_equal(writer: Writer, text: &str) {
let result = print_write_items(
writer.get_items(),
WriteItemsPrinterOptions {
indent_width: 2,
use_tabs: false,
new_line_text: "\n",
},
);
assert_eq!(result, String::from(text));
}
fn write_text(writer: &mut Writer, text: &'static str, bump: &Bump) {
let string_container = {
let result = bump.alloc(StringContainer::new(String::from(text)));
unsafe { std::mem::transmute::<&StringContainer, &'static StringContainer>(result) }
};
writer.write(string_container);
}
fn create_writer<'a>(bump: &'a Bump) -> Writer<'a> {
Writer::new(
bump,
WriterOptions {
indent_width: 2,
#[cfg(feature = "tracing")]
enable_tracing: false,
},
)
}
}