-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnostics.dart
3705 lines (3406 loc) · 127 KB
/
diagnostics.dart
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
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:math' as math;
import 'package:meta/meta.dart';
import 'assertions.dart';
import 'constants.dart';
import 'debug.dart';
import 'object.dart';
// Examples can assume:
// int rows, columns;
// String _name;
// bool inherit;
/// The various priority levels used to filter which diagnostics are shown and
/// omitted.
///
/// Trees of Flutter diagnostics can be very large so filtering the diagnostics
/// shown matters. Typically filtering to only show diagnostics with at least
/// level [debug] is appropriate.
enum DiagnosticLevel {
/// Diagnostics that should not be shown.
///
/// If a user chooses to display [hidden] diagnostics, they should not expect
/// the diagnostics to be formatted consistently with other diagnostics and
/// they should expect them to sometimes be misleading. For example,
/// [FlagProperty] and [ObjectFlagProperty] have uglier formatting when the
/// property `value` does does not match a value with a custom flag
/// description. An example of a misleading diagnostic is a diagnostic for
/// a property that has no effect because some other property of the object is
/// set in a way that causes the hidden property to have no effect.
hidden,
/// A diagnostic that is likely to be low value but where the diagnostic
/// display is just as high quality as a diagnostic with a higher level.
///
/// Use this level for diagnostic properties that match their default value
/// and other cases where showing a diagnostic would not add much value such
/// as an [IterableProperty] where the value is empty.
fine,
/// Diagnostics that should only be shown when performing fine grained
/// debugging of an object.
///
/// Unlike a [fine] diagnostic, these diagnostics provide important
/// information about the object that is likely to be needed to debug. Used by
/// properties that are important but where the property value is too verbose
/// (e.g. 300+ characters long) to show with a higher diagnostic level.
debug,
/// Interesting diagnostics that should be typically shown.
info,
/// Very important diagnostics that indicate problematic property values.
///
/// For example, use if you would write the property description
/// message in ALL CAPS.
warning,
/// Diagnostics that provide a hint about best practices.
///
/// For example, a diagnostic describing best practices for fixing an error.
hint,
/// Diagnostics that summarize other diagnostics present.
///
/// For example, use this level for a short one or two line summary
/// describing other diagnostics present.
summary,
/// Diagnostics that indicate errors or unexpected conditions.
///
/// For example, use for property values where computing the value throws an
/// exception.
error,
/// Special level indicating that no diagnostics should be shown.
///
/// Do not specify this level for diagnostics. This level is only used to
/// filter which diagnostics are shown.
off,
}
/// Styles for displaying a node in a [DiagnosticsNode] tree.
///
/// See also:
///
/// * [DiagnosticsNode.toStringDeep], which dumps text art trees for these
/// styles.
enum DiagnosticsTreeStyle {
/// A style that does not display the tree, for release mode.
none,
/// Sparse style for displaying trees.
///
/// See also:
///
/// * [RenderObject], which uses this style.
sparse,
/// Connects a node to its parent with a dashed line.
///
/// See also:
///
/// * [RenderSliverMultiBoxAdaptor], which uses this style to distinguish
/// offstage children from onstage children.
offstage,
/// Slightly more compact version of the [sparse] style.
///
/// See also:
///
/// * [Element], which uses this style.
dense,
/// Style that enables transitioning from nodes of one style to children of
/// another.
///
/// See also:
///
/// * [RenderParagraph], which uses this style to display a [TextSpan] child
/// in a way that is compatible with the [DiagnosticsTreeStyle.sparse]
/// style of the [RenderObject] tree.
transition,
/// Style for displaying content describing an error.
///
/// See also:
///
/// * [FlutterError], which uses this style for the root node in a tree
/// describing an error.
error,
/// Render the tree just using whitespace without connecting parents to
/// children using lines.
///
/// See also:
///
/// * [SliverGeometry], which uses this style.
whitespace,
/// Render the tree without indenting children at all.
///
/// See also:
///
/// * [DiagnosticsStackTrace], which uses this style.
flat,
/// Render the tree on a single line without showing children.
singleLine,
/// Render the tree using a style appropriate for properties that are part
/// of an error message.
///
/// The name is placed on one line with the value and properties placed on
/// the following line.
///
/// See also:
///
/// * [singleLine], which displays the same information but keeps the
/// property and value on the same line.
errorProperty,
/// Render only the immediate properties of a node instead of the full tree.
///
/// See also:
///
/// * [DebugOverflowIndicatorMixin], which uses this style to display just
/// the immediate children of a node.
shallow,
/// Render only the children of a node truncating before the tree becomes too
/// large.
truncateChildren,
}
/// Configuration specifying how a particular [DiagnosticsTreeStyle] should be
/// rendered as text art.
///
/// See also:
///
/// * [sparseTextConfiguration], which is a typical style.
/// * [transitionTextConfiguration], which is an example of a complex tree style.
/// * [DiagnosticsNode.toStringDeep], for code using [TextTreeConfiguration]
/// to render text art for arbitrary trees of [DiagnosticsNode] objects.
class TextTreeConfiguration {
/// Create a configuration object describing how to render a tree as text.
///
/// All of the arguments must not be null.
TextTreeConfiguration({
@required this.prefixLineOne,
@required this.prefixOtherLines,
@required this.prefixLastChildLineOne,
@required this.prefixOtherLinesRootNode,
@required this.linkCharacter,
@required this.propertyPrefixIfChildren,
@required this.propertyPrefixNoChildren,
this.lineBreak = '\n',
this.lineBreakProperties = true,
this.afterName = ':',
this.afterDescriptionIfBody = '',
this.afterDescription = '',
this.beforeProperties = '',
this.afterProperties = '',
this.mandatoryAfterProperties = '',
this.propertySeparator = '',
this.bodyIndent = '',
this.footer = '',
this.showChildren = true,
this.addBlankLineIfNoChildren = true,
this.isNameOnOwnLine = false,
this.isBlankLineBetweenPropertiesAndChildren = true,
this.beforeName = '',
this.suffixLineOne = '',
this.manditoryFooter = '',
}) : assert(prefixLineOne != null),
assert(prefixOtherLines != null),
assert(prefixLastChildLineOne != null),
assert(prefixOtherLinesRootNode != null),
assert(linkCharacter != null),
assert(propertyPrefixIfChildren != null),
assert(propertyPrefixNoChildren != null),
assert(lineBreak != null),
assert(lineBreakProperties != null),
assert(afterName != null),
assert(afterDescriptionIfBody != null),
assert(afterDescription != null),
assert(beforeProperties != null),
assert(afterProperties != null),
assert(propertySeparator != null),
assert(bodyIndent != null),
assert(footer != null),
assert(showChildren != null),
assert(addBlankLineIfNoChildren != null),
assert(isNameOnOwnLine != null),
assert(isBlankLineBetweenPropertiesAndChildren != null),
childLinkSpace = ' ' * linkCharacter.length;
/// Prefix to add to the first line to display a child with this style.
final String prefixLineOne;
/// Suffix to add to end of the first line to make its length match the footer.
final String suffixLineOne;
/// Prefix to add to other lines to display a child with this style.
///
/// [prefixOtherLines] should typically be one character shorter than
/// [prefixLineOne] as
final String prefixOtherLines;
/// Prefix to add to the first line to display the last child of a node with
/// this style.
final String prefixLastChildLineOne;
/// Additional prefix to add to other lines of a node if this is the root node
/// of the tree.
final String prefixOtherLinesRootNode;
/// Prefix to add before each property if the node as children.
///
/// Plays a similar role to [linkCharacter] except that some configurations
/// intentionally use a different line style than the [linkCharacter].
final String propertyPrefixIfChildren;
/// Prefix to add before each property if the node does not have children.
///
/// This string is typically a whitespace string the same length as
/// [propertyPrefixIfChildren] but can have a different length.
final String propertyPrefixNoChildren;
/// Character to use to draw line linking parent to child.
///
/// The first child does not require a line but all subsequent children do
/// with the line drawn immediately before the left edge of the previous
/// sibling.
final String linkCharacter;
/// Whitespace to draw instead of the childLink character if this node is the
/// last child of its parent so no link line is required.
final String childLinkSpace;
/// Character(s) to use to separate lines.
///
/// Typically leave set at the default value of '\n' unless this style needs
/// to treat lines differently as is the case for
/// [singleLineTextConfiguration].
final String lineBreak;
/// Whether to place line breaks between properties or to leave all
/// properties on one line.
final bool lineBreakProperties;
/// Text added immediately before the name of the node.
///
/// See [errorTextConfiguration] for an example of using this to achieve a
/// custom line art style.
final String beforeName;
/// Text added immediately after the name of the node.
///
/// See [transitionTextConfiguration] for an example of using a value other
/// than ':' to achieve a custom line art style.
final String afterName;
/// Text to add immediately after the description line of a node with
/// properties and/or children if the node has a body.
final String afterDescriptionIfBody;
/// Text to add immediately after the description line of a node with
/// properties and/or children.
final String afterDescription;
/// Optional string to add before the properties of a node.
///
/// Only displayed if the node has properties.
/// See [singleLineTextConfiguration] for an example of using this field
/// to enclose the property list with parenthesis.
final String beforeProperties;
/// Optional string to add after the properties of a node.
///
/// See documentation for [beforeProperties].
final String afterProperties;
/// Mandatory string to add after the properties of a node regardless of
/// whether the node has any properties.
final String mandatoryAfterProperties;
/// Property separator to add between properties.
///
/// See [singleLineTextConfiguration] for an example of using this field
/// to render properties as a comma separated list.
final String propertySeparator;
/// Prefix to add to all lines of the body of the tree node.
///
/// The body is all content in the node other than the name and description.
final String bodyIndent;
/// Whether the children of a node should be shown.
///
/// See [singleLineTextConfiguration] for an example of using this field to
/// hide all children of a node.
final bool showChildren;
/// Whether to add a blank line at the end of the output for a node if it has
/// no children.
///
/// See [denseTextConfiguration] for an example of setting this to false.
final bool addBlankLineIfNoChildren;
/// Whether the name should be displayed on the same line as the description.
final bool isNameOnOwnLine;
/// Footer to add as its own line at the end of a non-root node.
///
/// See [transitionTextConfiguration] for an example of using footer to draw a box
/// around the node. [footer] is indented the same amount as [prefixOtherLines].
final String footer;
/// Footer to add even for root nodes.
final String manditoryFooter;
/// Add a blank line between properties and children if both are present.
final bool isBlankLineBetweenPropertiesAndChildren;
}
/// Default text tree configuration.
///
/// Example:
/// ```
/// <root_name>: <root_description>
/// │ <property1>
/// │ <property2>
/// │ ...
/// │ <propertyN>
/// ├─<child_name>: <child_description>
/// │ │ <property1>
/// │ │ <property2>
/// │ │ ...
/// │ │ <propertyN>
/// │ │
/// │ └─<child_name>: <child_description>
/// │ <property1>
/// │ <property2>
/// │ ...
/// │ <propertyN>
/// │
/// └─<child_name>: <child_description>'
/// <property1>
/// <property2>
/// ...
/// <propertyN>
/// ```
///
/// See also:
///
/// * [DiagnosticsTreeStyle.sparse], uses this style for ASCII art display.
final TextTreeConfiguration sparseTextConfiguration = TextTreeConfiguration(
prefixLineOne: '├─',
prefixOtherLines: ' ',
prefixLastChildLineOne: '└─',
linkCharacter: '│',
propertyPrefixIfChildren: '│ ',
propertyPrefixNoChildren: ' ',
prefixOtherLinesRootNode: ' ',
);
/// Identical to [sparseTextConfiguration] except that the lines connecting
/// parent to children are dashed.
///
/// Example:
/// ```
/// <root_name>: <root_description>
/// │ <property1>
/// │ <property2>
/// │ ...
/// │ <propertyN>
/// ├─<normal_child_name>: <child_description>
/// ╎ │ <property1>
/// ╎ │ <property2>
/// ╎ │ ...
/// ╎ │ <propertyN>
/// ╎ │
/// ╎ └─<child_name>: <child_description>
/// ╎ <property1>
/// ╎ <property2>
/// ╎ ...
/// ╎ <propertyN>
/// ╎
/// ╎╌<dashed_child_name>: <child_description>
/// ╎ │ <property1>
/// ╎ │ <property2>
/// ╎ │ ...
/// ╎ │ <propertyN>
/// ╎ │
/// ╎ └─<child_name>: <child_description>
/// ╎ <property1>
/// ╎ <property2>
/// ╎ ...
/// ╎ <propertyN>
/// ╎
/// └╌<dashed_child_name>: <child_description>'
/// <property1>
/// <property2>
/// ...
/// <propertyN>
/// ```
///
/// See also:
///
/// * [DiagnosticsTreeStyle.offstage], uses this style for ASCII art display.
final TextTreeConfiguration dashedTextConfiguration = TextTreeConfiguration(
prefixLineOne: '╎╌',
prefixLastChildLineOne: '└╌',
prefixOtherLines: ' ',
linkCharacter: '╎',
// Intentionally not set as a dashed line as that would make the properties
// look like they were disabled.
propertyPrefixIfChildren: '│ ',
propertyPrefixNoChildren: ' ',
prefixOtherLinesRootNode: ' ',
);
/// Dense text tree configuration that minimizes horizontal whitespace.
///
/// Example:
/// ```
/// <root_name>: <root_description>(<property1>; <property2> <propertyN>)
/// ├<child_name>: <child_description>(<property1>, <property2>, <propertyN>)
/// └<child_name>: <child_description>(<property1>, <property2>, <propertyN>)
/// ```
///
/// See also:
///
/// * [DiagnosticsTreeStyle.dense], uses this style for ASCII art display.
final TextTreeConfiguration denseTextConfiguration = TextTreeConfiguration(
propertySeparator: ', ',
beforeProperties: '(',
afterProperties: ')',
lineBreakProperties: false,
prefixLineOne: '├',
prefixOtherLines: '',
prefixLastChildLineOne: '└',
linkCharacter: '│',
propertyPrefixIfChildren: '│',
propertyPrefixNoChildren: ' ',
prefixOtherLinesRootNode: '',
addBlankLineIfNoChildren: false,
isBlankLineBetweenPropertiesAndChildren: false,
);
/// Configuration that draws a box around a leaf node.
///
/// Used by leaf nodes such as [TextSpan] to draw a clear border around the
/// contents of a node.
///
/// Example:
/// ```
/// <parent_node>
/// ╞═╦══ <name> ═══
/// │ ║ <description>:
/// │ ║ <body>
/// │ ║ ...
/// │ ╚═══════════
/// ╘═╦══ <name> ═══
/// ║ <description>:
/// ║ <body>
/// ║ ...
/// ╚═══════════
/// ```
///
/// See also:
///
/// * [DiagnosticsTreeStyle.transition], uses this style for ASCII art display.
final TextTreeConfiguration transitionTextConfiguration = TextTreeConfiguration(
prefixLineOne: '╞═╦══ ',
prefixLastChildLineOne: '╘═╦══ ',
prefixOtherLines: ' ║ ',
footer: ' ╚═══════════',
linkCharacter: '│',
// Subtree boundaries are clear due to the border around the node so omit the
// property prefix.
propertyPrefixIfChildren: '',
propertyPrefixNoChildren: '',
prefixOtherLinesRootNode: '',
afterName: ' ═══',
// Add a colon after the description if the node has a body to make the
// connection between the description and the body clearer.
afterDescriptionIfBody: ':',
// Members are indented an extra two spaces to disambiguate as the description
// is placed within the box instead of along side the name as is the case for
// other styles.
bodyIndent: ' ',
isNameOnOwnLine: true,
// No need to add a blank line as the footer makes the boundary of this
// subtree unambiguous.
addBlankLineIfNoChildren: false,
isBlankLineBetweenPropertiesAndChildren: false,
);
/// Configuration that draws a box around a node ignoring the connection to the
/// parents.
///
/// If nested in a tree, this node is best displayed in the property box rather
/// than as a traditional child.
///
/// Used to draw a decorative box around detailed descriptions of an exception.
///
/// Example:
/// ```
/// ══╡ <name>: <description> ╞═════════════════════════════════════
/// <body>
/// ...
/// ├─<normal_child_name>: <child_description>
/// ╎ │ <property1>
/// ╎ │ <property2>
/// ╎ │ ...
/// ╎ │ <propertyN>
/// ╎ │
/// ╎ └─<child_name>: <child_description>
/// ╎ <property1>
/// ╎ <property2>
/// ╎ ...
/// ╎ <propertyN>
/// ╎
/// ╎╌<dashed_child_name>: <child_description>
/// ╎ │ <property1>
/// ╎ │ <property2>
/// ╎ │ ...
/// ╎ │ <propertyN>
/// ╎ │
/// ╎ └─<child_name>: <child_description>
/// ╎ <property1>
/// ╎ <property2>
/// ╎ ...
/// ╎ <propertyN>
/// ╎
/// └╌<dashed_child_name>: <child_description>'
/// ════════════════════════════════════════════════════════════════
/// ```
///
/// See also:
///
/// * [DiagnosticsTreeStyle.error], uses this style for ASCII art display.
final TextTreeConfiguration errorTextConfiguration = TextTreeConfiguration(
prefixLineOne: '╞═╦',
prefixLastChildLineOne: '╘═╦',
prefixOtherLines: ' ║ ',
footer: ' ╚═══════════',
linkCharacter: '│',
// Subtree boundaries are clear due to the border around the node so omit the
// property prefix.
propertyPrefixIfChildren: '',
propertyPrefixNoChildren: '',
prefixOtherLinesRootNode: '',
beforeName: '══╡ ',
suffixLineOne: ' ╞══',
manditoryFooter: '═════',
// No need to add a blank line as the footer makes the boundary of this
// subtree unambiguous.
addBlankLineIfNoChildren: false,
isBlankLineBetweenPropertiesAndChildren: false,
);
/// Whitespace only configuration where children are consistently indented
/// two spaces.
///
/// Use this style for displaying properties with structured values or for
/// displaying children within a [transitionTextConfiguration] as using a style that
/// draws line art would be visually distracting for those cases.
///
/// Example:
/// ```
/// <parent_node>
/// <name>: <description>:
/// <properties>
/// <children>
/// <name>: <description>:
/// <properties>
/// <children>
/// ```
///
/// See also:
///
/// * [DiagnosticsTreeStyle.whitespace], uses this style for ASCII art display.
final TextTreeConfiguration whitespaceTextConfiguration = TextTreeConfiguration(
prefixLineOne: '',
prefixLastChildLineOne: '',
prefixOtherLines: ' ',
prefixOtherLinesRootNode: ' ',
bodyIndent: '',
propertyPrefixIfChildren: '',
propertyPrefixNoChildren: '',
linkCharacter: ' ',
addBlankLineIfNoChildren: false,
// Add a colon after the description and before the properties to link the
// properties to the description line.
afterDescriptionIfBody: ':',
isBlankLineBetweenPropertiesAndChildren: false,
);
/// Whitespace only configuration where children are not indented.
///
/// Use this style when indentation is not needed to disambiguate parents from
/// children as in the case of a [DiagnosticsStackTrace].
///
/// Example:
/// ```
/// <parent_node>
/// <name>: <description>:
/// <properties>
/// <children>
/// <name>: <description>:
/// <properties>
/// <children>
/// ```
///
/// See also:
///
/// * [DiagnosticsTreeStyle.flat], uses this style for ASCII art display.
final TextTreeConfiguration flatTextConfiguration = TextTreeConfiguration(
prefixLineOne: '',
prefixLastChildLineOne: '',
prefixOtherLines: '',
prefixOtherLinesRootNode: '',
bodyIndent: '',
propertyPrefixIfChildren: '',
propertyPrefixNoChildren: '',
linkCharacter: '',
addBlankLineIfNoChildren: false,
// Add a colon after the description and before the properties to link the
// properties to the description line.
afterDescriptionIfBody: ':',
isBlankLineBetweenPropertiesAndChildren: false,
);
/// Render a node as a single line omitting children.
///
/// Example:
/// `<name>: <description>(<property1>, <property2>, ..., <propertyN>)`
///
/// See also:
///
/// * [DiagnosticsTreeStyle.singleLine], uses this style for ASCII art display.
final TextTreeConfiguration singleLineTextConfiguration = TextTreeConfiguration(
propertySeparator: ', ',
beforeProperties: '(',
afterProperties: ')',
prefixLineOne: '',
prefixOtherLines: '',
prefixLastChildLineOne: '',
lineBreak: '',
lineBreakProperties: false,
addBlankLineIfNoChildren: false,
showChildren: false,
propertyPrefixIfChildren: ' ',
propertyPrefixNoChildren: ' ',
linkCharacter: '',
prefixOtherLinesRootNode: '',
);
/// Render the name on a line followed by the body and properties on the next
/// line omitting the children.
///
/// Example:
/// ```
/// <name>:
/// <description>(<property1>, <property2>, ..., <propertyN>)
/// ```
///
/// See also:
///
/// * [DiagnosticsTreeStyle.errorProperty], uses this style for ASCII art
/// display.
final TextTreeConfiguration errorPropertyTextConfiguration = TextTreeConfiguration(
propertySeparator: ', ',
beforeProperties: '(',
afterProperties: ')',
prefixLineOne: '',
prefixOtherLines: '',
prefixLastChildLineOne: '',
lineBreak: '\n',
lineBreakProperties: false,
addBlankLineIfNoChildren: false,
showChildren: false,
propertyPrefixIfChildren: ' ',
propertyPrefixNoChildren: ' ',
linkCharacter: '',
prefixOtherLinesRootNode: '',
afterName: ':',
isNameOnOwnLine: true,
);
/// Render a node on multiple lines omitting children.
///
/// Example:
/// `<name>: <description>
/// <property1>
/// <property2>
/// <propertyN>`
///
/// See also:
///
/// * [DiagnosticsTreeStyle.shallow]
final TextTreeConfiguration shallowTextConfiguration = TextTreeConfiguration(
prefixLineOne: '',
prefixLastChildLineOne: '',
prefixOtherLines: ' ',
prefixOtherLinesRootNode: ' ',
bodyIndent: '',
propertyPrefixIfChildren: '',
propertyPrefixNoChildren: '',
linkCharacter: ' ',
addBlankLineIfNoChildren: false,
// Add a colon after the description and before the properties to link the
// properties to the description line.
afterDescriptionIfBody: ':',
isBlankLineBetweenPropertiesAndChildren: false,
showChildren: false,
);
enum _WordWrapParseMode { inSpace, inWord, atBreak }
/// Builder that builds a String with specified prefixes for the first and
/// subsequent lines.
///
/// Allows for the incremental building of strings using `write*()` methods.
/// The strings are concatenated into a single string with the first line
/// prefixed by [prefixLineOne] and subsequent lines prefixed by
/// [prefixOtherLines].
class _PrefixedStringBuilder {
_PrefixedStringBuilder({
@required this.prefixLineOne,
@required String prefixOtherLines,
this.wrapWidth}) :
_prefixOtherLines = prefixOtherLines;
/// Prefix to add to the first line.
final String prefixLineOne;
/// Prefix to add to subsequent lines.
///
/// The prefix can be modified while the string is being built in which case
/// subsequent lines will be added with the modified prefix.
String get prefixOtherLines => _nextPrefixOtherLines ?? _prefixOtherLines;
String _prefixOtherLines;
set prefixOtherLines(String prefix) {
_prefixOtherLines = prefix;
_nextPrefixOtherLines = null;
}
String _nextPrefixOtherLines;
void incrementPrefixOtherLines(String suffix, {@required bool updateCurrentLine}) {
if (_currentLine.isEmpty || updateCurrentLine) {
_prefixOtherLines = prefixOtherLines + suffix;
_nextPrefixOtherLines = null;
} else {
_nextPrefixOtherLines = prefixOtherLines + suffix;
}
}
final int wrapWidth;
/// Buffer containing lines that have already been completely laid out.
final StringBuffer _buffer = StringBuffer();
/// Buffer containing the current line that has not yet been wrapped.
final StringBuffer _currentLine = StringBuffer();
/// List of pairs of integers indicating the start and end of each block of
/// text within _currentLine that can be wrapped.
final List<int> _wrappableRanges = <int>[];
/// Whether the string being built already has more than 1 line.
bool get requiresMultipleLines => _numLines > 1 || (_numLines == 1 && _currentLine.isNotEmpty) ||
(_currentLine.length + _getCurrentPrefix(true).length > wrapWidth);
bool get isCurrentLineEmpty => _currentLine.isEmpty;
int _numLines = 0;
void _finalizeLine(bool addTrailingLineBreak) {
final bool firstLine = _buffer.isEmpty;
final String text = _currentLine.toString();
_currentLine.clear();
if (_wrappableRanges.isEmpty) {
// Fast path. There were no wrappable spans of text.
_writeLine(
text,
includeLineBreak: addTrailingLineBreak,
firstLine: firstLine,
);
return;
}
final Iterable<String> lines = _wordWrapLine(
text,
_wrappableRanges,
wrapWidth,
startOffset: firstLine ? prefixLineOne.length : _prefixOtherLines.length,
otherLineOffset: firstLine ? _prefixOtherLines.length : _prefixOtherLines.length,
);
int i = 0;
final int length = lines.length;
for (final String line in lines) {
i++;
_writeLine(
line,
includeLineBreak: addTrailingLineBreak || i < length,
firstLine: firstLine,
);
}
_wrappableRanges.clear();
}
/// Wraps the given string at the given width.
///
/// Wrapping occurs at space characters (U+0020).
///
/// This is not suitable for use with arbitrary Unicode text. For example, it
/// doesn't implement UAX #14, can't handle ideographic text, doesn't hyphenate,
/// and so forth. It is only intended for formatting error messages.
///
/// This method wraps a sequence of text where only some spans of text can be
/// used as wrap boundaries.
static Iterable<String> _wordWrapLine(String message, List<int> wrapRanges, int width, { int startOffset = 0, int otherLineOffset = 0}) sync* {
if (message.length + startOffset < width) {
// Nothing to do. The line doesn't wrap.
yield message;
return;
}
int startForLengthCalculations = -startOffset;
bool addPrefix = false;
int index = 0;
_WordWrapParseMode mode = _WordWrapParseMode.inSpace;
int lastWordStart;
int lastWordEnd;
int start = 0;
int currentChunk = 0;
// This helper is called with increasing indexes.
bool noWrap(int index) {
while (true) {
if (currentChunk >= wrapRanges.length)
return true;
if (index < wrapRanges[currentChunk + 1])
break; // Found nearest chunk.
currentChunk+= 2;
}
return index < wrapRanges[currentChunk];
}
while (true) {
switch (mode) {
case _WordWrapParseMode.inSpace: // at start of break point (or start of line); can't break until next break
while ((index < message.length) && (message[index] == ' '))
index += 1;
lastWordStart = index;
mode = _WordWrapParseMode.inWord;
break;
case _WordWrapParseMode.inWord: // looking for a good break point. Treat all text
while ((index < message.length) && (message[index] != ' ' || noWrap(index)))
index += 1;
mode = _WordWrapParseMode.atBreak;
break;
case _WordWrapParseMode.atBreak: // at start of break point
if ((index - startForLengthCalculations > width) || (index == message.length)) {
// we are over the width line, so break
if ((index - startForLengthCalculations <= width) || (lastWordEnd == null)) {
// we should use this point, because either it doesn't actually go over the
// end (last line), or it does, but there was no earlier break point
lastWordEnd = index;
}
final String line = message.substring(start, lastWordEnd);
yield line;
addPrefix = true;
if (lastWordEnd >= message.length)
return;
// just yielded a line
if (lastWordEnd == index) {
// we broke at current position
// eat all the wrappable spaces, then set our start point
// Even if some of the spaces are not wrappable that is ok.
while ((index < message.length) && (message[index] == ' '))
index += 1;
start = index;
mode = _WordWrapParseMode.inWord;
} else {
// we broke at the previous break point, and we're at the start of a new one
assert(lastWordStart > lastWordEnd);
start = lastWordStart;
mode = _WordWrapParseMode.atBreak;
}
startForLengthCalculations = start - otherLineOffset;
assert(addPrefix);
lastWordEnd = null;
} else {
// save this break point, we're not yet over the line width
lastWordEnd = index;
// skip to the end of this break point
mode = _WordWrapParseMode.inSpace;
}
break;
}
}
}
/// Write text ensuring the specified prefixes for the first and subsequent
/// lines.
///
/// If [allowWrap] is true, the text may be wrapped to stay within the
/// allow `wrapWidth`.
void write(String s, {bool allowWrap = false}) {
if (s.isEmpty)
return;
final List<String> lines = s.split('\n');
for (int i = 0; i < lines.length; i += 1) {
if (i > 0) {
_finalizeLine(true);
_updatePrefix();
}
final String line = lines[i];
if (line.isNotEmpty) {
if (allowWrap && wrapWidth != null) {
final int wrapStart = _currentLine.length;
final int wrapEnd = wrapStart + line.length;
if (_wrappableRanges.isNotEmpty && _wrappableRanges.last == wrapStart) {
// Extend last range.
_wrappableRanges.last = wrapEnd;
} else {
_wrappableRanges..add(wrapStart)..add(wrapEnd);
}
}
_currentLine.write(line);
}
}
}
void _updatePrefix() {
if (_nextPrefixOtherLines != null) {
_prefixOtherLines = _nextPrefixOtherLines;
_nextPrefixOtherLines = null;
}
}
void _writeLine(
String line, {
@required bool includeLineBreak,
@required bool firstLine,
}) {
line = '${_getCurrentPrefix(firstLine)}$line';
_buffer.write(line.trimRight());
if (includeLineBreak)
_buffer.write('\n');
_numLines++;
}