dprint-core 0.75.2

Core library for dprint.
Documentation
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
use std::cell::UnsafeCell;
use std::mem;

use super::Arena;
use super::condition_resolvers;
use super::printer::Printer;
use super::thread_state;

/// A list of print items allocated in an [`Arena`].
pub struct PrintItems<'a> {
  arena: &'a Arena,
  pub(super) first_node: Option<PrintItemPath<'a>>,
  last_node: Option<PrintItemPath<'a>>,
}

impl<'a> PrintItems<'a> {
  pub fn new(arena: &'a Arena) -> Self {
    Self {
      arena,
      first_node: None,
      last_node: None,
    }
  }

  /// Gets the arena the print items are allocated in.
  #[inline]
  pub fn arena(&self) -> &'a Arena {
    self.arena
  }

  pub fn into_rc_path(self) -> Option<PrintItemPath<'a>> {
    self.first_node
  }

  pub fn push_item(&mut self, item: PrintItem<'a>) {
    self.push_item_internal(item);
  }

  #[inline]
  fn push_item_internal(&mut self, item: PrintItem<'a>) {
    let node = self.arena.alloc_print_node_cell(PrintNodeCell::new(item));
    if let Some(first_node) = &self.first_node {
      let new_last_node = node.get_last_next().unwrap_or(node);
      self.last_node.as_ref().unwrap_or(first_node).set_next(Some(node));
      self.last_node = Some(new_last_node);
    } else {
      self.last_node = node.get_last_next();
      self.first_node = Some(node);
    }
  }

  pub fn extend(&mut self, items: PrintItems<'a>) {
    if let Some(first_node) = items.first_node {
      if let Some(current_first_node) = &self.first_node {
        self.last_node.as_ref().unwrap_or(current_first_node).set_next(Some(first_node));

        if items.last_node.is_some() {
          self.last_node = items.last_node;
        } else if items.first_node.is_some() {
          self.last_node = items.first_node;
        }
      } else {
        self.first_node = items.first_node;
        self.last_node = items.last_node;
      }
    }
  }

  /// Pushes a string that will have its width computed at runtime.
  ///
  /// The string isn't copied, so this can be used for text that lives at least
  /// as long as the arena, such as the text of the file being formatted.
  ///
  /// Instead of using this, prefer to use `push_sc` where possible
  /// with the `sc!` macro from the dprint-core-macros crate.
  pub fn push_str_runtime_width_computed(&mut self, item: &'a str) {
    let string_container = self.arena.alloc_string_container(item);
    self.push_item_internal(PrintItem::String(string_container));
  }

  pub fn push_force_current_line_indentation(&mut self) {
    const STR_EMPTY: StringContainer = StringContainer { text: "", char_count: 0 };
    self.push_item_internal(PrintItem::String(&STR_EMPTY))
  }

  pub fn push_space(&mut self) {
    const STR_SPACE: StringContainer = StringContainer { text: " ", char_count: 1 };
    self.push_item_internal(PrintItem::String(&STR_SPACE))
  }

  /// Pushes a string that's had its width computed at compile time.
  pub fn push_sc(&mut self, item: &'a StringContainer<'a>) {
    self.push_item_internal(PrintItem::String(item))
  }

  /// Pushes a string, copying it into the arena.
  pub fn push_string(&mut self, item: String) {
    self.push_str(&item);
  }

  /// Pushes a borrowed string, copying it into the arena, and computes its width at
  /// runtime.
  ///
  /// Prefer `push_sc` with the `sc!` macro for text known at compile time, and
  /// `push_str_runtime_width_computed` for text that lives as long as the arena, since
  /// neither copies. Use this for other text the caller doesn't own: it avoids building
  /// a `String` only for it to be copied and thrown away.
  pub fn push_str(&mut self, item: &str) {
    let string_container = self.arena.alloc_str(item);
    self.push_item_internal(PrintItem::String(string_container));
  }

  pub fn push_condition(&mut self, condition: Condition<'a>) {
    let condition = self.arena.alloc_condition(condition);
    self.push_item_internal(PrintItem::Condition(condition));
  }

  pub fn push_info(&mut self, info: impl Into<Info>) {
    self.push_item_internal(PrintItem::Info(info.into()));
  }

  pub fn push_line_and_column(&mut self, line_and_col: LineAndColumn) {
    self.push_info(line_and_col.line);
    self.push_info(line_and_col.column);
  }

  pub fn push_anchor(&mut self, anchor: impl Into<Anchor>) {
    self.push_item_internal(PrintItem::Anchor(anchor.into()));
  }

  pub fn push_reevaluation(&mut self, condition_reevaluation: ConditionReevaluation) {
    self.push_item_internal(PrintItem::ConditionReevaluation(condition_reevaluation));
  }

  pub fn push_signal(&mut self, signal: Signal) {
    self.push_item_internal(PrintItem::Signal(signal));
  }

  pub fn push_path(&mut self, path: PrintItemPath<'a>) {
    self.push_item_internal(PrintItem::RcPath(path))
  }

  pub fn push_optional_path(&mut self, path: Option<PrintItemPath<'a>>) {
    if let Some(path) = path {
      self.push_path(path);
    }
  }

  pub fn is_empty(&self) -> bool {
    self.first_node.is_none()
  }

  // todo: clean this up
  #[cfg(debug_assertions)]
  pub fn get_as_text(&self) -> String {
    return if let Some(first_node) = &self.first_node {
      get_items_as_text(first_node, String::from(""))
    } else {
      String::new()
    };

    fn get_items_as_text(items: PrintItemPath, indent_text: String) -> String {
      let mut text = String::new();
      for item in PrintItemsIterator::new(items) {
        match item {
          PrintItem::Signal(signal) => text.push_str(&get_line(format!("Signal::{:?}", signal), &indent_text)),
          PrintItem::Condition(condition) => {
            text.push_str(&get_line(format!("Condition: {}", condition.name), &indent_text));
            if let Some(true_path) = &condition.true_path {
              text.push_str(&get_line(String::from("  true:"), &indent_text));
              text.push_str(&get_items_as_text(true_path, format!("{}    ", indent_text)));
            }
            if let Some(false_path) = &condition.false_path {
              text.push_str(&get_line(String::from("  false:"), &indent_text));
              text.push_str(&get_items_as_text(false_path, format!("{}    ", indent_text)));
            }
          }
          PrintItem::String(str_text) => text.push_str(&get_line(format!("`{}`", str_text.text), &indent_text)),
          PrintItem::RcPath(path) => text.push_str(&get_items_as_text(path, indent_text.clone())),
          PrintItem::Anchor(Anchor::LineNumber(line_number_anchor)) => {
            text.push_str(&get_line(format!("Line number anchor: {}", line_number_anchor.name()), &indent_text))
          }
          PrintItem::Info(info) => {
            let (desc, name) = match info {
              Info::LineNumber(info) => ("Line number", info.name()),
              Info::ColumnNumber(info) => ("Column number", info.name()),
              Info::IsStartOfLine(info) => ("Is start of line", info.name()),
              Info::IndentLevel(info) => ("Indent level", info.name()),
              Info::LineStartColumnNumber(info) => ("Line start column number", info.name()),
              Info::LineStartIndentLevel(info) => ("Line start indent level", info.name()),
            };
            text.push_str(&get_line(format!("{}: {}", desc, name), &indent_text))
          }
          PrintItem::ConditionReevaluation(reevaluation) => text.push_str(&get_line(format!("Condition reevaluation: {}", reevaluation.name()), &indent_text)),
        }
      }

      return text;

      fn get_line(text: String, indent_text: &str) -> String {
        format!("{}{}\n", indent_text, text)
      }
    }
  }

  pub fn iter(&self) -> PrintItemsIterator<'a> {
    PrintItemsIterator { node: self.first_node }
  }
}

pub struct PrintItemsIterator<'a> {
  node: Option<PrintItemPath<'a>>,
}

impl<'a> PrintItemsIterator<'a> {
  pub fn new(path: PrintItemPath<'a>) -> Self {
    Self { node: Some(path) }
  }
}

impl<'a> Iterator for PrintItemsIterator<'a> {
  type Item = PrintItem<'a>;

  fn next(&mut self) -> Option<PrintItem<'a>> {
    let node = self.node.take();

    match node {
      Some(node) => {
        self.node = node.get_next();
        Some(node.get_item())
      }
      None => None,
    }
  }
}

/// Converts a value into print items allocated in an arena.
pub trait IntoPrintItems<'a> {
  fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a>;
}

impl<'a> IntoPrintItems<'a> for PrintItems<'a> {
  fn into_print_items(self, _arena: &'a Arena) -> PrintItems<'a> {
    self
  }
}

impl<'a> IntoPrintItems<'a> for &'a str {
  fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
    let mut items = PrintItems::new(arena);
    items.push_str_runtime_width_computed(self);
    items
  }
}

impl<'a> IntoPrintItems<'a> for &'a StringContainer<'a> {
  fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
    let mut items = PrintItems::new(arena);
    items.push_sc(self);
    items
  }
}

impl<'a> IntoPrintItems<'a> for String {
  fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
    let mut items = PrintItems::new(arena);
    items.push_string(self);
    items
  }
}

impl<'a> IntoPrintItems<'a> for Condition<'a> {
  fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
    let mut items = PrintItems::new(arena);
    items.push_condition(self);
    items
  }
}

impl<'a> IntoPrintItems<'a> for Signal {
  fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
    let mut items = PrintItems::new(arena);
    items.push_signal(self);
    items
  }
}

impl<'a> IntoPrintItems<'a> for PrintItemPath<'a> {
  fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
    let mut items = PrintItems::new(arena);
    items.push_path(self);
    items
  }
}

impl<'a, T: IntoPrintItems<'a>> IntoPrintItems<'a> for Option<T> {
  fn into_print_items(self, arena: &'a Arena) -> PrintItems<'a> {
    match self {
      Some(value) => value.into_print_items(arena),
      None => PrintItems::new(arena),
    }
  }
}

/** Tracing */

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Trace {
  /// The relative time of the trace from the start of printing in nanoseconds.
  pub nanos: u128,
  pub print_node_id: u32,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub writer_node_id: Option<u32>,
  /// Why printing the node made the printer go back to an earlier point, if it did.
  ///
  /// The printer continues from the print node of the next trace.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub jump: Option<TraceJump>,
}

/// Why the printer went back to an earlier point.
#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(tag = "kind", content = "content", rename_all = "camelCase")]
pub enum TraceJump {
  /// The line was too long, so the printer went back to the last
  /// possible newline to write a newline there.
  NewLine,
  /// An info was printed after a condition used it before it was resolved.
  Info(TraceInfoJump),
  /// A condition was printed after another condition used its value
  /// before it was resolved.
  Condition(TraceConditionJump),
  /// A condition was re-evaluated and its value changed.
  ConditionReevaluation(TraceConditionJump),
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TraceInfoJump {
  pub info: TraceInfo,
  /// The value the info resolved to.
  pub value: TraceInfoValue,
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(untagged)]
pub enum TraceInfoValue {
  Number(u32),
  Bool(bool),
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TraceConditionJump {
  pub condition_id: u32,
  pub name: String,
  /// The value the condition resolved to.
  pub value: Option<bool>,
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TraceWriterNode {
  pub writer_node_id: u32,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub previous_node_id: Option<u32>,
  pub text: String,
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TracePrintNode {
  pub print_node_id: u32,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub next_print_node_id: Option<u32>,
  pub print_item: TracePrintItem,
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(tag = "kind", content = "content", rename_all = "camelCase")]
pub enum TracePrintItem {
  String(String),
  Condition(TraceCondition),
  Info(TraceInfo),
  Signal(Signal),
  /// Identifier to the print node.
  RcPath(u32),
  Anchor(TraceLineNumberAnchor),
  ConditionReevaluation(TraceConditionReevaluation),
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(tag = "kind", content = "content", rename_all = "camelCase")]
pub enum TraceInfo {
  LineNumber(TraceInfoInner),
  ColumnNumber(TraceInfoInner),
  IsStartOfLine(TraceInfoInner),
  IndentLevel(TraceInfoInner),
  LineStartColumnNumber(TraceInfoInner),
  LineStartIndentLevel(TraceInfoInner),
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TraceInfoInner {
  pub info_id: u32,
  pub name: String,
}

#[cfg(feature = "tracing")]
impl TraceInfoInner {
  pub fn new(info_id: u32, name: &str) -> Self {
    Self {
      info_id,
      name: name.to_string(),
    }
  }
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TraceLineNumberAnchor {
  pub anchor_id: u32,
  pub name: String,
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TraceConditionReevaluation {
  pub condition_id: u32,
  pub name: String,
}

#[cfg(feature = "tracing")]
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TraceCondition {
  pub condition_id: u32,
  pub name: String,
  pub is_stored: bool,
  pub store_save_point: bool,
  #[serde(skip_serializing_if = "Option::is_none")]
  /// Identifier to the true path print node.
  pub true_path: Option<u32>,
  #[serde(skip_serializing_if = "Option::is_none")]
  /// Identifier to the false path print node.
  pub false_path: Option<u32>,
}

pub struct PrintNode<'a> {
  pub(super) next: Option<PrintItemPath<'a>>,
  pub(super) item: PrintItem<'a>,
  #[cfg(feature = "tracing")]
  pub print_node_id: u32,
}

impl<'a> PrintNode<'a> {
  fn new(item: PrintItem<'a>) -> PrintNode<'a> {
    PrintNode {
      item,
      next: None,
      #[cfg(feature = "tracing")]
      print_node_id: thread_state::next_print_node_id(),
    }
  }

  fn set_next(&mut self, new_next: Option<PrintItemPath<'a>>) {
    let past_next = mem::replace(&mut self.next, new_next);

    if let Some(past_next) = past_next
      && let Some(new_next) = new_next
    {
      new_next.get_last_next().unwrap_or(new_next).set_next(Some(past_next));
    }
  }
}

/// A fast implementation of RefCell<PrintNode> that avoids runtime checks on borrows.
pub struct PrintNodeCell<'a> {
  value: UnsafeCell<PrintNode<'a>>,
}

impl<'a> PrintNodeCell<'a> {
  pub(super) fn new(item: PrintItem<'a>) -> PrintNodeCell<'a> {
    PrintNodeCell {
      value: UnsafeCell::new(PrintNode::new(item)),
    }
  }

  #[inline]
  pub(super) fn get_item(&self) -> PrintItem<'a> {
    unsafe { (*self.value.get()).item.clone() }
  }

  #[inline]
  pub(super) fn get_next(&self) -> Option<PrintItemPath<'a>> {
    unsafe { (*self.value.get()).next }
  }

  #[inline]
  pub(super) fn set_next(&self, new_next: Option<PrintItemPath<'a>>) {
    unsafe {
      (*self.value.get()).set_next(new_next);
    }
  }

  #[inline]
  pub(super) fn get_last_next(&self) -> Option<PrintItemPath<'a>> {
    let mut current = self.get_next();
    loop {
      if let Some(last) = &current
        && let Some(next) = last.get_next()
      {
        current.replace(next);
        continue;
      }
      break;
    }

    current
  }

  #[cfg(feature = "tracing")]
  pub(super) fn get_node_id(&self) -> u32 {
    unsafe { (*self.get_node()).print_node_id }
  }

  /// Gets the node unsafely. Be careful when using this and ensure no mutation is
  /// happening during the borrow.
  #[inline]
  pub(super) unsafe fn get_node(&self) -> *mut PrintNode<'a> {
    self.value.get()
  }

  #[inline]
  pub fn take_next(self) -> Option<PrintItemPath<'a>> {
    self.value.into_inner().next.take()
  }
}

pub type PrintItemPath<'a> = &'a PrintNodeCell<'a>;

/* Print item and kinds */

/// The different items the printer could encounter.
#[derive(Clone)]
pub enum PrintItem<'a> {
  String(&'a StringContainer<'a>),
  Condition(&'a Condition<'a>),
  Signal(Signal),
  RcPath(PrintItemPath<'a>),
  Anchor(Anchor),
  Info(Info),
  ConditionReevaluation(ConditionReevaluation),
}

#[derive(Clone, PartialEq, Eq, Copy, Debug, serde::Serialize)]
pub enum Signal {
  /// Signal that a new line should occur based on the printer settings.
  NewLine,
  /// Signal that a tab should occur based on the printer settings.
  Tab,
  /// Signal that the current location could be a newline when
  /// exceeding the line width.
  PossibleNewLine,
  /// Signal that the current location should be a space, but
  /// could be a newline if exceeding the line width.
  SpaceOrNewLine,
  /// Expect the next character to be a newline. If it's not, force a newline.
  ExpectNewLine,
  /// Queue a start indent to be set after the next written item.
  QueueStartIndent,
  /// Signal the start of a section that should be indented.
  StartIndent,
  /// Signal the end of a section that should be indented.
  FinishIndent,
  /// Signal the start of a group of print items that have a lower precedence
  /// for being broken up with a newline for exceeding the line width.
  StartNewLineGroup,
  /// Signal the end of a newline group.
  FinishNewLineGroup,
  /// Signal that a single indent should occur based on the printer settings.
  SingleIndent,
  /// Signal to the printer that it should stop using indentation.
  StartIgnoringIndent,
  /// Signal to the printer that it should start using indentation again.
  FinishIgnoringIndent,
  /// Signal the start of a section whose indentation starts from no indentation
  /// instead of the current indentation, which can then be indented with `StartIndent`
  /// (ex. to indent relative to text written as is while ignoring the indentation).
  ///
  /// The indent levels in the section (ex. of an `IndentLevel` info) count from the
  /// start of the section, so they shouldn't be compared with the indent levels from
  /// outside of it. The column numbers are the actual columns. A `QueueStartIndent`
  /// from before the section is applied after it.
  StartResettingIndent,
  /// Signal the end of a section that reset the indentation, which restores the
  /// indentation from before. The indents and aligns started in the section must be
  /// finished first.
  FinishResettingIndent,
  /// Signal the start of a section that's aligned by the number of columns of spaces
  /// after the indentation (ex. to line up with text that isn't at an indent level).
  /// The alignment always goes after the indentation, so with tabs it's tabs then spaces.
  ///
  /// The alignment isn't part of the indent levels (ex. of an `IndentLevel` info),
  /// but it's included in the column numbers.
  StartAlign(u8),
  /// Signal the end of the most recent section that aligns.
  FinishAlign,
  /// Signal to the printer that it shouldn't print any new lines.
  StartForceNoNewLines,
  /// Signal to the printer that it should finish not printing any new lines.
  FinishForceNoNewLines,
  /// Signal that a space should occur if not trailing.
  SpaceIfNotTrailing,
}

#[derive(Clone)]
pub enum Anchor {
  LineNumber(LineNumberAnchor),
}

impl From<LineNumberAnchor> for Anchor {
  fn from(anchor: LineNumberAnchor) -> Self {
    Anchor::LineNumber(anchor)
  }
}

/// Handles updating the position of a future resolved line
/// number if the anchor changes.
#[derive(Clone)]
pub struct LineNumberAnchor {
  id: u32,
  line_number: LineNumber,
}

impl LineNumberAnchor {
  pub fn new(line_number: LineNumber) -> Self {
    Self {
      id: thread_state::next_line_number_anchor_id(),
      line_number,
    }
  }

  #[inline]
  pub fn unique_id(&self) -> u32 {
    self.id
  }

  #[inline]
  pub fn line_number_id(&self) -> u32 {
    self.line_number.id
  }

  #[inline]
  pub fn name(&self) -> &'static str {
    self.line_number.name()
  }
}

#[derive(Clone, PartialEq, Eq, Copy, Debug)]
pub enum Info {
  LineNumber(LineNumber),
  ColumnNumber(ColumnNumber),
  IsStartOfLine(IsStartOfLine),
  IndentLevel(IndentLevel),
  LineStartColumnNumber(LineStartColumnNumber),
  LineStartIndentLevel(LineStartIndentLevel),
}

impl From<LineNumber> for Info {
  fn from(info: LineNumber) -> Self {
    Info::LineNumber(info)
  }
}

impl From<ColumnNumber> for Info {
  fn from(info: ColumnNumber) -> Self {
    Info::ColumnNumber(info)
  }
}

impl From<IsStartOfLine> for Info {
  fn from(info: IsStartOfLine) -> Self {
    Info::IsStartOfLine(info)
  }
}

impl From<IndentLevel> for Info {
  fn from(info: IndentLevel) -> Self {
    Info::IndentLevel(info)
  }
}

impl From<LineStartColumnNumber> for Info {
  fn from(info: LineStartColumnNumber) -> Self {
    Info::LineStartColumnNumber(info)
  }
}

impl From<LineStartIndentLevel> for Info {
  fn from(info: LineStartIndentLevel) -> Self {
    Info::LineStartIndentLevel(info)
  }
}

/// Helper IR that holds line and column number IR.
#[derive(Clone, PartialEq, Eq, Copy, Debug)]
pub struct LineAndColumn {
  pub line: LineNumber,
  pub column: ColumnNumber,
}

impl LineAndColumn {
  pub fn new(name: &'static str) -> Self {
    Self {
      line: LineNumber::new(name),
      column: ColumnNumber::new(name),
    }
  }
}

#[derive(Clone, PartialEq, Eq, Copy, Debug)]
pub struct LineNumber {
  id: u32,
  /// Name for debugging purposes.
  #[cfg(debug_assertions)]
  name: &'static str,
}

impl LineNumber {
  pub fn new(_name: &'static str) -> Self {
    Self {
      id: thread_state::next_line_number_id(),
      #[cfg(debug_assertions)]
      name: _name,
    }
  }

  #[inline]
  pub fn unique_id(&self) -> u32 {
    self.id
  }

  #[inline]
  pub fn name(&self) -> &'static str {
    #[cfg(debug_assertions)]
    return self.name;
    #[cfg(not(debug_assertions))]
    return "line_number";
  }
}

#[derive(Clone, PartialEq, Eq, Copy, Debug)]
pub struct ColumnNumber {
  id: u32,
  /// Name for debugging purposes.
  #[cfg(debug_assertions)]
  name: &'static str,
}

impl ColumnNumber {
  pub fn new(_name: &'static str) -> Self {
    Self {
      id: thread_state::next_column_number_id(),
      #[cfg(debug_assertions)]
      name: _name,
    }
  }

  #[inline]
  pub fn unique_id(&self) -> u32 {
    self.id
  }

  #[inline]
  pub fn name(&self) -> &'static str {
    #[cfg(debug_assertions)]
    return self.name;
    #[cfg(not(debug_assertions))]
    return "column_number";
  }
}

#[derive(Clone, PartialEq, Eq, Copy, Debug)]
pub struct IsStartOfLine {
  id: u32,
  /// Name for debugging purposes.
  #[cfg(debug_assertions)]
  name: &'static str,
}

impl IsStartOfLine {
  pub fn new(_name: &'static str) -> Self {
    Self {
      id: thread_state::next_is_start_of_line_id(),
      #[cfg(debug_assertions)]
      name: _name,
    }
  }

  #[inline]
  pub fn unique_id(&self) -> u32 {
    self.id
  }

  #[inline]
  pub fn name(&self) -> &'static str {
    #[cfg(debug_assertions)]
    return self.name;
    #[cfg(not(debug_assertions))]
    return "is_start_of_line";
  }
}

#[derive(Clone, PartialEq, Eq, Copy, Debug)]
pub struct LineStartColumnNumber {
  id: u32,
  /// Name for debugging purposes.
  #[cfg(debug_assertions)]
  name: &'static str,
}

impl LineStartColumnNumber {
  pub fn new(_name: &'static str) -> Self {
    Self {
      id: thread_state::next_line_start_column_number_id(),
      #[cfg(debug_assertions)]
      name: _name,
    }
  }

  #[inline]
  pub fn unique_id(&self) -> u32 {
    self.id
  }

  #[inline]
  pub fn name(&self) -> &'static str {
    #[cfg(debug_assertions)]
    return self.name;
    #[cfg(not(debug_assertions))]
    return "line_start_column_number";
  }
}

#[derive(Clone, PartialEq, Eq, Copy, Debug)]
pub struct IndentLevel {
  id: u32,
  /// Name for debugging purposes.
  #[cfg(debug_assertions)]
  name: &'static str,
}

impl IndentLevel {
  pub fn new(_name: &'static str) -> Self {
    Self {
      id: thread_state::next_indent_level_id(),
      #[cfg(debug_assertions)]
      name: _name,
    }
  }

  #[inline]
  pub fn unique_id(&self) -> u32 {
    self.id
  }

  #[inline]
  pub fn name(&self) -> &'static str {
    #[cfg(debug_assertions)]
    return self.name;
    #[cfg(not(debug_assertions))]
    return "indent_level";
  }
}

#[derive(Clone, PartialEq, Eq, Copy, Debug)]
pub struct LineStartIndentLevel {
  id: u32,
  /// Name for debugging purposes.
  #[cfg(debug_assertions)]
  name: &'static str,
}

impl LineStartIndentLevel {
  pub fn new(_name: &'static str) -> Self {
    Self {
      id: thread_state::next_line_start_indent_level_id(),
      #[cfg(debug_assertions)]
      name: _name,
    }
  }

  #[inline]
  pub fn unique_id(&self) -> u32 {
    self.id
  }

  #[inline]
  pub fn name(&self) -> &'static str {
    #[cfg(debug_assertions)]
    return self.name;
    #[cfg(not(debug_assertions))]
    return "line_start_indent_level";
  }
}

/// Used to re-evaluate a condition.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ConditionReevaluation {
  pub(crate) condition_reevaluation_id: u32,
  pub(crate) condition_id: u32,
  /// Name for debugging purposes.
  #[cfg(debug_assertions)]
  name: &'static str,
}

impl ConditionReevaluation {
  pub(crate) fn new(_name: &'static str, condition_id: u32) -> Self {
    ConditionReevaluation {
      condition_reevaluation_id: thread_state::next_condition_reevaluation_id(),
      condition_id,
      #[cfg(debug_assertions)]
      name: _name,
    }
  }

  pub fn name(&self) -> &'static str {
    #[cfg(debug_assertions)]
    return self.name;
    #[cfg(not(debug_assertions))]
    return "condition_reevaluation";
  }
}

/// Conditionally print items based on a condition.
///
/// These conditions are extremely flexible and can even be resolved based on
/// information found later on in the file.
#[derive(Clone)]
pub struct Condition<'a> {
  /// Unique identifier.
  id: u32,
  /// Name for debugging purposes.
  #[cfg(debug_assertions)]
  name: &'static str,
  /// If a reference has been created for the condition via `create_reference()`. If so, the printer
  /// will store the condition and it will be retrievable via a condition resolver.
  pub(super) is_stored: bool,
  pub(super) store_save_point: bool,
  /// If what's printed when the condition isn't resolved yet is the same as when it's false.
  pub(super) is_unknown_same_as_false: bool,
  /// The condition to resolve.
  pub(super) condition: ConditionResolver<'a>,
  /// The items to print when the condition is true.
  pub(super) true_path: Option<PrintItemPath<'a>>,
  /// The items to print when the condition is false or undefined (not yet resolved).
  pub(super) false_path: Option<PrintItemPath<'a>>,
}

impl<'a> Condition<'a> {
  pub fn new(name: &'static str, properties: ConditionProperties<'a>) -> Self {
    Self::new_internal(name, properties)
  }

  pub fn new_true() -> Self {
    Self::new_internal(
      "trueCondition",
      ConditionProperties {
        condition: condition_resolvers::true_resolver(),
        true_path: None,
        false_path: None,
      },
    )
  }

  pub fn new_false() -> Self {
    Self::new_internal(
      "falseCondition",
      ConditionProperties {
        condition: condition_resolvers::false_resolver(),
        true_path: None,
        false_path: None,
      },
    )
  }

  fn new_internal(_name: &'static str, properties: ConditionProperties<'a>) -> Self {
    Self {
      id: thread_state::next_condition_id(),
      is_stored: false,
      store_save_point: false,
      is_unknown_same_as_false: false,
      #[cfg(debug_assertions)]
      name: _name,
      condition: properties.condition,
      true_path: properties.true_path.and_then(|x| x.first_node),
      false_path: properties.false_path.and_then(|x| x.first_node),
    }
  }

  #[inline]
  pub fn unique_id(&self) -> u32 {
    self.id
  }

  #[inline]
  pub fn name(&self) -> &'static str {
    #[cfg(debug_assertions)]
    return self.name;
    #[cfg(not(debug_assertions))]
    return "condition";
  }

  #[inline]
  pub fn true_path(&self) -> &Option<PrintItemPath<'a>> {
    &self.true_path
  }

  #[inline]
  pub fn false_path(&self) -> &Option<PrintItemPath<'a>> {
    &self.false_path
  }

  #[inline]
  pub(super) fn resolve(&self, context: &mut ConditionResolverContext) -> Option<bool> {
    (self.condition)(context)
  }

  pub fn create_reference(&mut self) -> ConditionReference {
    self.is_stored = true;
    ConditionReference::new(self.name(), self.id)
  }

  pub fn create_reevaluation(&mut self) -> ConditionReevaluation {
    self.store_save_point = true;
    self.is_stored = true;
    ConditionReevaluation::new(self.name(), self.id)
  }

  /// Says that what's printed when the condition isn't resolved yet is the same as when it's
  /// false, including by the conditions using its value. When it looked ahead and turns out to be
  /// false, the printer then doesn't go back to print the same thing again.
  ///
  /// This is only needed when the condition has a reference, since the printer already doesn't
  /// go back to a condition without one when it takes the same path as before.
  pub fn set_unknown_same_as_false(&mut self) {
    self.is_unknown_same_as_false = true;
  }
}

#[derive(Clone, PartialEq, Eq, Copy, Debug)]
pub struct ConditionReference {
  #[cfg(debug_assertions)]
  pub(super) name: &'static str,
  pub(super) id: u32,
}

impl ConditionReference {
  pub(super) fn new(_name: &'static str, id: u32) -> ConditionReference {
    ConditionReference {
      #[cfg(debug_assertions)]
      name: _name,
      id,
    }
  }

  #[inline]
  pub(super) fn name(&self) -> &'static str {
    #[cfg(debug_assertions)]
    return self.name;
    #[cfg(not(debug_assertions))]
    return "conditionRef";
  }

  /// Creates a condition resolver that checks the value of the condition this references.
  pub fn create_resolver<'a>(&self, arena: &'a Arena) -> ConditionResolver<'a> {
    let captured_self = *self;
    arena.alloc_resolver(move |condition_context| condition_context.resolved_condition(&captured_self))
  }
}

/// Properties for the condition.
pub struct ConditionProperties<'a> {
  /// The condition to resolve.
  pub condition: ConditionResolver<'a>,
  /// The items to print when the condition is true.
  pub true_path: Option<PrintItems<'a>>,
  /// The items to print when the condition is false or undefined (not yet resolved).
  pub false_path: Option<PrintItems<'a>>,
}

/// Function used to resolve a condition, which is allocated in an arena with
/// [`Arena::alloc_resolver`] (or is static, like the ones in `condition_resolvers`).
pub type ConditionResolver<'a> = &'a dyn Fn(&mut ConditionResolverContext) -> Option<bool>;

/// Context used when resolving a condition.
pub struct ConditionResolverContext<'a, 'b> {
  printer: &'a mut Printer<'b>,
  /// Gets the writer info at the condition's location.
  pub writer_info: WriterInfo,
}

impl<'a, 'b> ConditionResolverContext<'a, 'b> {
  pub(super) fn new(printer: &'a mut Printer<'b>, writer_info: WriterInfo) -> Self {
    ConditionResolverContext { printer, writer_info }
  }

  /// Gets if a condition was true, false, or returns None when not yet resolved.
  /// A condition reference can be retrieved by calling the `create_reference()` on a condition.
  pub fn resolved_condition(&mut self, condition_reference: &ConditionReference) -> Option<bool> {
    self.printer.resolved_condition(condition_reference)
  }

  /// Gets a resolved line and column.
  pub fn resolved_line_and_column(&mut self, line_and_column: LineAndColumn) -> Option<(u32, u32)> {
    let line = self.printer.resolved_line_number(line_and_column.line)?;
    let column = self.printer.resolved_column_number(line_and_column.column)?;
    Some((line, column))
  }

  /// Gets the line number or returns None when not yet resolved.
  pub fn resolved_line_number(&mut self, line_number: LineNumber) -> Option<u32> {
    self.printer.resolved_line_number(line_number)
  }

  /// Gets the column number or returns None when not yet resolved.
  pub fn resolved_column_number(&mut self, column_number: ColumnNumber) -> Option<u32> {
    self.printer.resolved_column_number(column_number)
  }

  /// Gets if the info is at the start of the line or returns None when not yet resolved.
  pub fn resolved_is_start_of_line(&mut self, is_start_of_line: IsStartOfLine) -> Option<bool> {
    self.printer.resolved_is_start_of_line(is_start_of_line)
  }

  /// Gets if the indent level at this info or returns None when not yet resolved.
  pub fn resolved_indent_level(&mut self, indent_level: IndentLevel) -> Option<u8> {
    self.printer.resolved_indent_level(indent_level)
  }

  /// Gets the column number at the start of the line this info appears or returns None when not yet resolved.
  pub fn resolved_line_start_column_number(&mut self, line_start_column_number: LineStartColumnNumber) -> Option<u32> {
    self.printer.resolved_line_start_column_number(line_start_column_number)
  }

  /// Gets the indent level at the start of the line this info appears or returns None when not yet resolved.
  pub fn resolved_line_start_indent_level(&mut self, line_start_indent_level: LineStartIndentLevel) -> Option<u8> {
    self.printer.resolved_line_start_indent_level(line_start_indent_level)
  }

  /// Clears the line and column from being stored.
  pub fn clear_line_and_column(&mut self, lc: LineAndColumn) {
    self.clear_info(lc.line);
    self.clear_info(lc.column);
  }

  /// Clears the info from being stored.
  pub fn clear_info(&mut self, info: impl Into<Info>) {
    self.printer.clear_info(info.into())
  }

  /// Moves an already resolved line number by `delta` lines.
  ///
  /// Use this instead of clearing when the text the line number refers to has only moved up or down
  /// the page, so that it stays correct relative to everything around it.
  pub fn shift_line_number(&mut self, info: LineNumber, delta: isize) {
    self.printer.shift_line_number(info, delta)
  }

  /// Gets the maximum width of a line.
  pub fn max_width(&self) -> u32 {
    self.printer.max_width()
  }

  /// Runs `f` without the printer going back to this condition once the infos and conditions
  /// it gets that aren't resolved yet are resolved.
  ///
  /// This is for a condition that's re-evaluated after them (see `Condition::create_reevaluation`),
  /// which only goes back when its value changes, so it can assume a value until then.
  pub fn without_looking_ahead<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
    let was_not_looking_ahead = self.printer.replace_is_not_looking_ahead(true);
    let result = f(self);
    self.printer.replace_is_not_looking_ahead(was_not_looking_ahead);
    result
  }

  /// Gets the line number when it's resolved, without looking ahead to it when it isn't.
  pub fn peek_line_number(&self, line_number: LineNumber) -> Option<u32> {
    self.printer.peek_line_number(line_number)
  }

  /// Sets the line number to a value that was worked out before (ex. when the text it
  /// refers to is printed at the same position again).
  pub fn set_line_number(&mut self, line_number: LineNumber, value: u32) {
    self.printer.set_line_number(line_number, value)
  }

  /// Gets if the printer is currently forcing no newlines.
  pub fn is_forcing_no_newlines(&self) -> bool {
    self.printer.is_forcing_no_newlines()
  }
}

/// A container that holds the string's value and character count.
#[derive(Clone)]
pub struct StringContainer<'a> {
  /// The string value.
  pub text: &'a str,
  /// The cached character count.
  /// It is much faster to cache this than to recompute it all the time.
  pub(super) char_count: u32,
}

impl<'a> StringContainer<'a> {
  /// Creates a new string container.
  pub fn new(text: &'a str) -> Self {
    let char_count = unicode_width::UnicodeWidthStr::width(text) as u32;
    Self { text, char_count }
  }

  /// This is used by the sc! proc macro and should not be used otherwise
  /// because the character count is pre-computed.
  pub const fn proc_macro_new_with_char_count(text: &'a str, char_count: u32) -> Self {
    Self { text, char_count }
  }
}

/// Information about a certain location being printed.
#[derive(Clone, Debug)]
pub struct WriterInfo {
  pub line_number: u32,
  pub column_number: u32,
  pub indent_level: u8,
  pub line_start_indent_level: u8,
  /// The columns of spaces after the indentation that the line started with.
  pub line_start_align_columns: u32,
  pub indent_width: u8,
  pub expect_newline_next: bool,
}

impl WriterInfo {
  /// Gets if the current column number equals the line start column number
  /// or if a newline is expected next.
  pub fn is_start_of_line(&self) -> bool {
    self.expect_newline_next || self.is_column_number_at_line_start()
  }

  /// Gets if the start of the line is indented.
  pub fn is_start_of_line_indented(&self) -> bool {
    self.line_start_indent_level > self.indent_level
  }

  /// Gets if the current column number is at the line start column number.
  pub fn is_column_number_at_line_start(&self) -> bool {
    self.column_number == self.line_start_column_number()
  }

  pub fn line_start_column_number(&self) -> u32 {
    (self.line_start_indent_level as u32) * (self.indent_width as u32) + self.line_start_align_columns
  }

  /// Gets the line and column number.
  pub fn line_and_column(&self) -> (u32, u32) {
    (self.line_number, self.column_number)
  }
}