-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrinter.cs
704 lines (635 loc) · 22.4 KB
/
Printer.cs
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
// https://github.com/needle-mirror/com.unity.entities/blob/master/Unity.Entities/SourceGenerators/Source~/AspectGenerator/Printer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace CustomLogicSourceGen
{
public interface IPrintable
{
void Print(Printer printer);
}
public struct Printer
{
public StringBuilder Builder;
private int CurrentIndentIndex;
public string CurrentIndent => sm_Tabs[CurrentIndentIndex];
public int IndentDepth => CurrentIndentIndex;
// Array of indent string for each depth level starting with "" and up to 31 tabs.
private static readonly string[] sm_Tabs = Enumerable.Range(start: 0, count: 32).Select(i => string.Join("", Enumerable.Repeat(element: "\t", count: i))).ToArray();
public static Printer Default => new Printer(0);
public static Printer DefaultLarge => new Printer(0, 1024*16);
/// <summary>
/// Get a copy of this printer with the same setting but new output.
/// </summary>
/// <param name="printer"></param>
/// <returns></returns>
public static Printer NewLike(Printer printer) => new Printer(printer.CurrentIndentIndex);
public Printer(int indentCount)
{
System.Diagnostics.Debug.Assert(indentCount < sm_Tabs.Length);
Builder = new StringBuilder();
CurrentIndentIndex = indentCount;
}
public Printer(int indentCount, int capacity)
{
System.Diagnostics.Debug.Assert(indentCount < sm_Tabs.Length);
Builder = new StringBuilder(capacity);
CurrentIndentIndex = indentCount;
}
public Printer(StringBuilder builder, int indentCount)
{
System.Diagnostics.Debug.Assert(indentCount < sm_Tabs.Length);
Builder = builder;
CurrentIndentIndex = indentCount;
}
/// <summary>
/// Allows to continue inline printing using a different printer
/// </summary>
/// <param name="printer"></param>
/// <returns></returns>
public Printer PrintWith(Printer printer) => printer;
/// <summary>
/// Allows to continue inline printing using the same printer from an function call
/// </summary>
/// <param name="printer"></param>
/// <returns></returns>
public Printer PrintWith(Func<Printer, Printer> func) => func(this);
/// <summary>
/// Creates a copy of this printer but with a relative indentCount
/// </summary>
/// <returns></returns>
public Printer WithRelativeIndent(int indentCount) => new Printer(Builder, CurrentIndentIndex + indentCount);
public Printer RelativeIndent(int indentCount)
{
CurrentIndentIndex += indentCount;
return this;
}
/// <summary>
/// Creates a copy of this printer but with a deeper indent
/// </summary>
/// <returns></returns>
public Printer WithIncreasedIndent() => new Printer(Builder, CurrentIndentIndex + 1);
public Printer IncreasedIndent()
{
++CurrentIndentIndex;
return this;
}
/// <summary>
/// Creates a copy of this printer but with a shallower indent
/// </summary>
/// <returns></returns>
public Printer WithDecreasedIndent() => new Printer(Builder, CurrentIndentIndex - 1);
public Printer DecreasedIndent()
{
--CurrentIndentIndex;
return this;
}
/// <summary>
/// The current output of the printer.
/// </summary>
public string Result => Builder.ToString();
/// <summary>
/// Clear the output of the printer and reset indent
/// </summary>
/// <returns></returns>
public Printer Clear()
{
Builder.Clear();
CurrentIndentIndex = 0;
return this;
}
/// <summary>
/// Clear the output and set the current indent
/// </summary>
/// <param name="indentCount"></param>
/// <returns></returns>
public Printer ClearAndIndent(int indentCount)
{
System.Diagnostics.Debug.Assert(indentCount < sm_Tabs.Length);
Builder.Clear();
CurrentIndentIndex = indentCount;
return this;
}
#region printing
/// <summary>
/// Print a string
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public Printer Print(string text)
{
Builder.Append(text);
return this;
}
/// <summary>
/// Print indent
/// </summary>
/// <returns></returns>
public Printer PrintBeginLine()
=> Print(CurrentIndent);
/// <summary>
/// Print indent and a string
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public Printer PrintBeginLine(string text)
=> Print(CurrentIndent).Print(text);
/// <summary>
/// Print end-line
/// </summary>
/// <returns></returns>
public Printer PrintEndLine()
{
Builder.AppendLine();
return this;
}
/// <summary>
/// Print a string and an end-line
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public Printer PrintEndLine(string text)
{
Builder.AppendLine(text);
return this;
}
/// <summary>
/// Print indent, a string and an end-line
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public Printer PrintLine(string text)
=> PrintBeginLine().PrintEndLine(text);
/// <summary>
/// Print a string if condition is truw
/// </summary>
/// <param name="condition"></param>
/// <param name="text"></param>
/// <returns></returns>
public Printer PrintIf(bool condition, string text)
{
if(condition)
Print(text);
return this;
}
/// <summary>
/// Print indent, a string and an end-line if a condition is true
/// </summary>
/// <param name="condition"></param>
/// <param name="text"></param>
/// <param name="elseText"></param>
/// <returns></returns>
public Printer PrintLineIf(bool condition, string text, string elseText = null)
{
if (condition)
PrintLine(text);
else if (elseText != null)
PrintLine(elseText);
return this;
}
#endregion
#region List
/// <summary>
/// Print item in a list with a separator
/// </summary>
public struct ListPrinter
{
public Printer Printer;
public string Separator;
public bool IsStarted;
public ListPrinter(Printer printer, string separator)
{
Printer = printer;
Separator = separator;
IsStarted = false;
}
/// <summary>
/// Get the next item printer
/// </summary>
/// <returns></returns>
public Printer NextItemPrinter()
{
if (IsStarted)
Printer.Print(Separator);
else
IsStarted = true;
return Printer;
}
/// <summary>
/// Print all the element in an IEnumerable as list items
/// </summary>
/// <param name="elements"></param>
/// <returns></returns>
public ListPrinter PrintAll(IEnumerable<string> elements)
{
foreach(var e in elements)
NextItemPrinter().Print(e);
return this;
}
/// <summary>
/// Get a multiline list version of this list
/// </summary>
public MultilineListPrinter AsMultiline => new MultilineListPrinter(this);
/// <summary>
/// Get a multiline list where each item is indented one level deeper
/// </summary>
public MultilineListPrinter AsMultilineIndented
{
get
{
var printer = AsMultiline;
printer.ListPrinter.Printer = printer.ListPrinter.Printer.WithIncreasedIndent();
return printer;
}
}
}
/// <summary>
/// Print item in a multi-line list such as:
/// "item0, item1, ..., itemN"
/// </summary>
public struct MultilineListPrinter
{
public ListPrinter ListPrinter;
public MultilineListPrinter(ListPrinter listPrinter)
{
ListPrinter = listPrinter;
}
/// <summary>
/// Get the next item printer
/// </summary>
/// <returns></returns>
public Printer NextItemPrinter()
{
if (ListPrinter.IsStarted)
{
ListPrinter.Printer.PrintEndLine(ListPrinter.Separator);
ListPrinter.Printer.PrintBeginLine();
}
else
ListPrinter.IsStarted = true;
return ListPrinter.Printer;
}
}
/// <summary>
/// Create a list printer from this printer
/// </summary>
/// <param name="separator"></param>
/// <returns></returns>
public ListPrinter AsListPrinter(string separator) => new ListPrinter(this, separator);
#endregion
#region Scope
/// <summary>
/// print the scope open string and return a new printer with deeper indent
/// The returned printer must be terminated with CloseScope.
/// </summary>
/// <param name="scopeOpen"></param>
/// <returns>a copy of this printer with a deeper indent</returns>
public Printer ScopePrinter(string scopeOpen = "{")
{
PrintEndLine(scopeOpen);
return WithIncreasedIndent();
}
/// <summary>
/// Close a scope printer and print a string.
/// </summary>
/// <param name="scopedPrinter">the scope printer to close</param>
/// <param name="scopeClose"></param>
/// <returns>a copy of the closed scope printer with a shallower indent</returns>
public Printer CloseScope(Printer scopedPrinter, string scopeClose = "{")
{
PrintBeginLine(scopeClose);
return scopedPrinter.WithDecreasedIndent();
}
/// <summary>
/// print the scope open string and increase indent
/// </summary>
/// <param name="scopeOpen"></param>
/// <param name="printEnd"></param>
/// <returns>this</returns>
public Printer OpenScope(string scopeOpen = "{", bool printEnd = false)
{
if (printEnd)
PrintEndLine();
PrintLine(scopeOpen);
IncreasedIndent();
return this;
}
/// <summary>
/// Decrease indent and print the scope close string.
/// </summary>
/// <param name="scopeClose"></param>
/// <returns>this</returns>
public Printer CloseScope(string scopeClose = "}")
{
DecreasedIndent();
PrintLine(scopeClose);
return this;
}
#endregion
/// <summary>
/// Print a IPrintable to a string
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="printable"></param>
/// <returns></returns>
public static string PrintToString<T>(T printable)
where T : struct, IPrintable
{
var printer = Printer.Default;
printable.Print(printer);
return printer.Builder.ToString();
}
/// <summary>
/// Access debug printing interface
/// </summary>
public DebugPrinter Debug => new DebugPrinter(this);
}
/// <summary>
/// Printer used for debug purposes only.
/// Some boxing may happen
/// </summary>
public struct DebugPrinter
{
public Printer BasePrinter;
public static implicit operator Printer(DebugPrinter dp) => dp.BasePrinter;
public DebugPrinter(Printer basePrinter)
{
BasePrinter = basePrinter;
}
public DebugPrinter Print(string text)
{
BasePrinter.Print(text);
return this;
}
public DebugPrinter Print<T>(T printable)
where T : struct, IPrintable
{
printable.Print(BasePrinter);
return this;
}
/// <summary>
/// Print indent, a string and an end-line
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public DebugPrinter PrintLine(string text)
{
BasePrinter.PrintLine(text);
return this;
}
/// <summary>
/// Print indent and a string
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public DebugPrinter PrintBeginLine(string text)
{
BasePrinter.PrintBeginLine(text);
return this;
}
/// <summary>
/// Print indent
/// </summary>
/// <returns></returns>
public DebugPrinter PrintBeginLine()
{
BasePrinter.PrintBeginLine();
return this;
}
/// <summary>
/// Print a string and an end-line
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public DebugPrinter PrintEndLine(string text)
{
BasePrinter.PrintEndLine(text);
return this;
}
/// <summary>
/// Print end-line
/// </summary>
/// <returns></returns>
public DebugPrinter PrintEndLine()
{
BasePrinter.PrintEndLine();
return this;
}
/// <summary>
/// Print a printable or "<null>" when null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="printable"></param>
/// <returns></returns>
public DebugPrinter PrintNullable<T>(T printable)
where T : class, IPrintable
{
if (printable == null)
Print("<null>");
else
printable.Print(BasePrinter);
return this;
}
/// <summary>
/// Print:
/// kv.Key = kv.Value
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="kv"></param>
/// <returns></returns>
public DebugPrinter Print<TKey, TValue>(KeyValuePair<TKey, TValue> kv)
where TValue : struct, IPrintable
{
Print(kv.Key.ToString());
Print(" = ");
Print(kv.Value);
return this;
}
/// <summary>
/// Print: $"{key} = {value}"
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public DebugPrinter PrintKeyValue(string key, string value)
{
Print(key);
Print(" = ");
Print(value);
return this;
}
/// <summary>
/// Print $"{key} = {value}" where value is a printable
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public DebugPrinter PrintKeyValue<TValue>(string key, TValue value)
where TValue : struct, IPrintable
{
BasePrinter.Print(key);
BasePrinter.Print(" = ");
Print(value);
return this;
}
/// <summary>
/// Print $"{key} = {value}" where value is a printable or "<null>" when null
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public DebugPrinter PrintKeyValueNullable<TValue>(string key, TValue value)
where TValue : class, IPrintable
{
BasePrinter.Print(key);
BasePrinter.Print(" = ");
PrintNullable(value);
return this;
}
/// <summary>
/// Print:
/// key = { TValue0, TValue1, ..., TValueN }
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="openScope"></param>
/// <param name="closeScope"></param>
/// <returns></returns>
public DebugPrinter PrintKeyList<TValue>(string key, IEnumerable<TValue> value, string openScope = "{", string closeScope = "}")
where TValue : struct, IPrintable
{
if (value == null)
{
PrintKeyValue(key, "<null>");
return this;
}
Print(key);
var scope = BasePrinter.Print(" = IEnumerable<>").ScopePrinter(openScope);
var list = scope.PrintBeginLine().AsListPrinter(", ").AsMultiline;
foreach (var v in value)
new DebugPrinter(list.NextItemPrinter()).Print(v);
scope.PrintEndLine();
BasePrinter.CloseScope(scope, closeScope);
Print("}");
return this;
}
/// <summary>
/// Print:
/// key = {
/// TKey0 = TValue0,
/// TKey1 = TValue1,
/// ... = ...,
/// TKeyN = TValueN
/// }
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="openScope"></param>
/// <param name="closeScope"></param>
/// <returns></returns>
public DebugPrinter PrintKeyList<TKey, TValue>(string key, IEnumerable<KeyValuePair<TKey, TValue>> value, string openScope = "{", string closeScope = "}")
where TValue : struct, IPrintable
{
Print(key);
var scope = BasePrinter.Print(" = ").ScopePrinter(openScope);
var list = scope.PrintBeginLine().AsListPrinter(", ").AsMultiline;
foreach (var v in value)
new DebugPrinter(list.NextItemPrinter()).Print(v);
scope.PrintEndLine();
BasePrinter.CloseScope(scope, closeScope);
return this;
}
/// <summary>
/// Print a multiline list of item with a given separator
/// ex:
/// item0,
/// item1,
/// ...,
/// itemN,
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="separator"></param>
/// <param name="items"></param>
/// <returns></returns>
public DebugPrinter PrintListMultiline<T>(string separator, IEnumerable<T> items)
where T : struct, IPrintable
{
using var itor = items.GetEnumerator();
if (itor.MoveNext())
itor.Current.Print(BasePrinter);
while (itor.MoveNext())
{
PrintEndLine(separator);
PrintBeginLine();
itor.Current.Print(BasePrinter);
}
return this;
}
}
/// <summary>
/// Printer that replicate the full scope path declaration (namespace, class and struct declarations) to a given SyntaxNode
/// </summary>
public struct SyntaxNodeScopePrinter
{
private SyntaxNode m_LeafNode;
public Printer Printer;
public SyntaxNodeScopePrinter(Printer printer, SyntaxNode node)
{
Printer = printer;
m_LeafNode = node;
}
public SyntaxNodeScopePrinter PrintScope(SyntaxNode node)
{
if (node.Parent != null)
PrintScope(node.Parent);
switch (node)
{
case NamespaceDeclarationSyntax ns:
Printer.PrintBeginLine();
foreach (var m in ns.Modifiers)
Printer.Print(m.ToString()).Print(" ");
Printer = Printer.Print("namespace ").PrintEndLine(ns.Name.ToString()).PrintLine("{").WithIncreasedIndent();
break;
case ClassDeclarationSyntax cl:
Printer.PrintBeginLine();
foreach (var m in cl.Modifiers)
Printer.Print(m.ToString()).Print(" ");
Printer = Printer.Print("class ").PrintEndLine(cl.Identifier.Text).PrintLine("{").WithIncreasedIndent();
break;
case StructDeclarationSyntax st:
Printer.PrintBeginLine();
foreach (var m in st.Modifiers)
Printer.Print(m.ToString()).Print(" ");
Printer = Printer.Print("struct ").PrintEndLine(st.Identifier.Text).PrintLine("{").WithIncreasedIndent();
break;
}
return this;
}
public SyntaxNodeScopePrinter PrintOpen() => PrintScope(m_LeafNode);
public SyntaxNodeScopePrinter PrintClose()
{
var parent = m_LeafNode;
while (parent != null)
{
switch (parent)
{
case NamespaceDeclarationSyntax _:
case ClassDeclarationSyntax _:
case StructDeclarationSyntax _:
Printer = Printer.WithDecreasedIndent().PrintLine("}");
break;
}
parent = parent.Parent;
}
return this;
}
}
}