Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NormalizedSnapshotSpanCollection.cs « Model « TextData « Def « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 42b60ed1e19d359150ca2480196e6599b84f3623 (plain)
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
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
namespace Microsoft.VisualStudio.Text
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// A read-only collection of <see cref="SnapshotSpan"/> objects, all from the same snapshot. 
    /// </summary>
    /// <remarks>
    /// The snapshot spans are sorted by start position, 
    /// with adjacent and overlapping spans combined.
    /// </remarks>
    public sealed class NormalizedSnapshotSpanCollection : IList<SnapshotSpan>, ICollection<SnapshotSpan>, IEnumerable<SnapshotSpan>, IList, ICollection, IEnumerable
    {
        // imitate platform implementation of ReadOnlyCollection. Methods that write throw NotSupportException, and use explicit
        // interface implementation to keep them from being easily called or participating in intellisense.

        #region State and Construction

        // To save space and reduce allocations, we do not create an inner NormalizedSpanCollection if this collection is 
        // of size zero or one. If this.snapshot is null, the collection has size zero. If this.snapshot is nonnull and
        // this.spans is null, the collection is size one and this.span contains its single element (this accounts for
        // over 95% of the instances of this class). If this.spans is nonnull, the collection is size two or greater.
        // We can't do this by subclassing because of backward compatibility with the concrete constructor.

        private readonly ITextSnapshot snapshot;
        private readonly NormalizedSpanCollection spans;
        private readonly Span span;

        [SuppressMessage("Microsoft.Security", "CA2104", Justification = "Type is immutable")]
        public readonly static NormalizedSnapshotSpanCollection Empty = new NormalizedSnapshotSpanCollection();

        /// <summary>
        /// Initializes an empty <see cref="NormalizedSnapshotSpanCollection"/>.
        /// </summary>
        public NormalizedSnapshotSpanCollection()
        {
            // empty collection, signalled by this.snapshot == null
        }

        /// <summary>
        /// Initializes a new instance of a <see cref="NormalizedSnapshotSpanCollection"/> with a single element.
        /// </summary>
        /// <param name="span">The sole member of the collection.</param>
        /// <exception cref="ArgumentException"><paramref name="span"/> is not initialized.</exception>
        public NormalizedSnapshotSpanCollection(SnapshotSpan span)
        {
            if (span.Snapshot == null)
            {
                throw new ArgumentException(Strings.UninitializedSnapshotSpan);
            }
            this.snapshot = span.Snapshot;
            this.span = span;
        }

        /// <summary>
        /// Initializes a new instance of a <see cref="NormalizedSnapshotSpanCollection"/> from a <see cref="NormalizedSpanCollection"/> and a <see cref="ITextSnapshot"/>.
        /// </summary>
        /// <param name="snapshot">The <see cref="ITextSnapshot"/> to apply to <paramref name="spans"/>.</param>
        /// <param name="spans">The normalized spans.</param>
        /// <exception cref="ArgumentNullException"><paramref name="snapshot"/> or <paramref name="spans"/> is null.</exception>
        /// <exception cref="ArgumentException">The spans in <paramref name="spans"/> extend beyond the end of <paramref name="snapshot"/>.</exception>
        public NormalizedSnapshotSpanCollection(ITextSnapshot snapshot, NormalizedSpanCollection spans)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }
            if (spans == null)
            {
                throw new ArgumentNullException(nameof(spans));
            }
            if (spans.Count > 0 && spans[spans.Count - 1].End > snapshot.Length)
            {
                throw new ArgumentException(Strings.SpansBeyondEnd);
            }
            if (spans.Count == 1)
            {
                this.snapshot = snapshot;
                this.span = spans[0];
            }
            else if (spans.Count > 1)
            {
                this.snapshot = snapshot;
                this.spans = spans;
            }
        }

        /// <summary>
        /// Initializes a new instance of a <see cref="NormalizedSnapshotSpanCollection"/> from a list of <see cref="Span"/>s and a <see cref="ITextSnapshot"/>.
        /// </summary>
        /// <param name="snapshot">The <see cref="ITextSnapshot"/> to apply to <paramref name="spans"/>.</param>
        /// <param name="spans">An arbitrary set of <see cref="Span"/> objects.</param>
        /// <exception cref="ArgumentNullException"><paramref name="snapshot"/> or <paramref name="spans"/> is null.</exception>
        /// <exception cref="ArgumentException">The spans in <paramref name="spans"/> extend beyond the end of <paramref name="snapshot"/>.</exception>
        public NormalizedSnapshotSpanCollection(ITextSnapshot snapshot, IEnumerable<Span> spans)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }
            if (spans == null)
            {
                throw new ArgumentNullException(nameof(spans));
            }

            using (IEnumerator<Span> spanEnumerator = spans.GetEnumerator())
            {
                if (!spanEnumerator.MoveNext())
                {
                    // empty collection
                }
                else
                {
                    this.snapshot = snapshot;
                    Span span = spanEnumerator.Current;
                    if (!spanEnumerator.MoveNext())
                    {
                        // length one
                        this.span = span;
                        if (span.End > snapshot.Length)
                        {
                            throw new ArgumentException(Strings.SpansBeyondEnd);
                        }
                    }
                    else
                    {
                        // length at least two
                        this.spans = new NormalizedSpanCollection(spans);
                        if (this.spans[this.spans.Count - 1].End > snapshot.Length)
                        {
                            throw new ArgumentException(Strings.SpansBeyondEnd);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of a <see cref="NormalizedSnapshotSpanCollection"/> from a list of <see cref="Span"/>s and a <see cref="ITextSnapshot"/>.
        /// </summary>
        /// <param name="snapshot">The <see cref="ITextSnapshot"/> to apply to <paramref name="spans"/>.</param>
        /// <param name="spans">An arbitrary set of <see cref="Span"/> objects.</param>
        /// <exception cref="ArgumentNullException"><paramref name="snapshot"/> or <paramref name="spans"/> is null.</exception>
        /// <exception cref="ArgumentException">The spans in <paramref name="spans"/> extend beyond the end of <paramref name="snapshot"/>.</exception>
        public NormalizedSnapshotSpanCollection(ITextSnapshot snapshot, IList<Span> spans)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }
            if (spans == null)
            {
                throw new ArgumentNullException(nameof(spans));
            }

            if (spans.Count == 0)
            {
                // empty collection
            }
            else
            {
                this.snapshot = snapshot;
                if (spans.Count == 1)
                {
                    // length one
                    this.span = spans[0];
                    if (this.span.End > snapshot.Length)
                    {
                        throw new ArgumentException(Strings.SpansBeyondEnd);
                    }
                }
                else
                {
                    // length at least two
                    this.spans = new NormalizedSpanCollection(spans);
                    if (this.spans[this.spans.Count - 1].End > snapshot.Length)
                    {
                        throw new ArgumentException(Strings.SpansBeyondEnd);
                    }
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of a <see cref="NormalizedSnapshotSpanCollection"/> from a list of <see cref="SnapshotSpan"/> objects.
        /// </summary>
        /// <param name="snapshotSpans">An arbitrary set of <see cref="SnapshotSpan"/> objects.</param>
        /// <exception cref="ArgumentNullException"><paramref name="snapshotSpans"/> is null.</exception>
        /// <exception cref="ArgumentException">A <see cref="SnapshotSpan"/> is uninitialized, or it
        /// does not refer to the same <see cref="ITextSnapshot"/> as the other snapshot spans, or it refers to an uninitialized <see cref="ITextSnapshot"/>.</exception>
        public NormalizedSnapshotSpanCollection(IEnumerable<SnapshotSpan> snapshotSpans)
        {
            if (snapshotSpans == null)
            {
                throw new ArgumentNullException(nameof(snapshotSpans));
            }

            using (IEnumerator<SnapshotSpan> spanEnumerator = snapshotSpans.GetEnumerator())
            {
                if (!spanEnumerator.MoveNext())
                {
                    // empty
                }
                else
                {
                    SnapshotSpan firstSpan = spanEnumerator.Current;
                    this.snapshot = firstSpan.Snapshot;
                    if (!spanEnumerator.MoveNext())
                    {
                        // length one
                        this.span = firstSpan.Span;
                    }
                    else
                    {
                        // length at least two
                        bool alreadyNormalized = true;
                        List<Span> spans = new List<Span>();
                        Span currentSpan = firstSpan.Span;
                        spans.Add(currentSpan);
                        int lastEnd = currentSpan.End;
                        do
                        {
                            SnapshotSpan snapshotSpan = spanEnumerator.Current;
                            if (snapshotSpan.Snapshot != this.snapshot)
                            {
                                if (snapshotSpan.Snapshot == null)
                                {
                                    throw new ArgumentException(Strings.UninitializedSnapshotSpan);
                                }
                                else
                                {
                                    throw new ArgumentException(Strings.InvalidSnapshot);
                                }
                            }
                            currentSpan = snapshotSpan.Span;
                            spans.Add(currentSpan);
                            if (currentSpan.Start <= lastEnd)
                            {
                                alreadyNormalized = false;
                            }
                            lastEnd = currentSpan.End;
                        } while (spanEnumerator.MoveNext()) ;
                        this.spans = alreadyNormalized
                                        ? NormalizedSpanCollection.CreateFromNormalizedSpans(spans)
                                        : new NormalizedSpanCollection(spans);
                    }
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of a <see cref="NormalizedSnapshotSpanCollection"/> from a list of <see cref="SnapshotSpan"/> objects.
        /// </summary>
        /// <param name="snapshotSpans">An arbitrary set of <see cref="SnapshotSpan"/> objects.</param>
        /// <exception cref="ArgumentNullException"><paramref name="snapshotSpans"/> is null.</exception>
        /// <exception cref="ArgumentException">A <see cref="SnapshotSpan"/> is uninitialized, or it
        /// does not refer to the same <see cref="ITextSnapshot"/> as the other snapshot spans, or it refers to an uninitialized <see cref="ITextSnapshot"/>.</exception>
        public NormalizedSnapshotSpanCollection(IList<SnapshotSpan> snapshotSpans)
        {
            // TODO: possibly eliminate based on slight usage?
            if (snapshotSpans == null)
            {
                throw new ArgumentNullException(nameof(snapshotSpans));
            }

            if (snapshotSpans.Count == 0)
            {
                // empty collection
            }
            else
            {
                this.snapshot = snapshotSpans[0].Snapshot;
                if (this.snapshot == null)
                {
                    throw new ArgumentException(Strings.UninitializedSnapshotSpan);
                }
                if (snapshotSpans.Count == 1)
                {
                    // length one
                    this.span = snapshotSpans[0].Span;
                }
                else
                {
                    // length at least two
                    bool alreadyNormalized = true;
                    List<Span> spans = new List<Span>(snapshotSpans.Count);
                    Span currentSpan = snapshotSpans[0].Span;
                    spans.Add(currentSpan);
                    int lastEnd = currentSpan.End;
                    for (int s = 1; s < snapshotSpans.Count; ++s)
                    {
                        if (snapshotSpans[s].Snapshot != this.snapshot)
                        {
                            if (snapshotSpans[s].Snapshot == null)
                            {
                                throw new ArgumentException(Strings.UninitializedSnapshotSpan);
                            }
                            else
                            {
                                throw new ArgumentException(Strings.InvalidSnapshot);
                            }
                        }
                        currentSpan = snapshotSpans[s].Span;
                        spans.Add(currentSpan);
                        if (currentSpan.Start <= lastEnd)
                        {
                            alreadyNormalized = false;
                        }
                        lastEnd = currentSpan.End;
                    }
                    this.spans = alreadyNormalized
                                    ? NormalizedSpanCollection.CreateFromNormalizedSpans(spans)
                                    : new NormalizedSpanCollection(spans);
                }
            }
        }

        public NormalizedSnapshotSpanCollection(ITextSnapshot snapshot, Span span)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException(nameof(snapshot));
            }

            if (span.End > snapshot.Length)
            {
                throw new ArgumentException(Strings.SpansBeyondEnd);
            }

            this.snapshot = snapshot;
            this.span = span;
        }
        #endregion

        public NormalizedSnapshotSpanCollection CloneAndTrackTo(ITextSnapshot targetSnapshot, SpanTrackingMode mode)
        {
            if (targetSnapshot == null)
            {
                throw new ArgumentNullException(nameof(targetSnapshot));
            }
            if (mode < SpanTrackingMode.EdgeExclusive || mode > SpanTrackingMode.Custom)
            {
                throw new ArgumentOutOfRangeException(nameof(mode));
            }

            if (this.snapshot == null)
            {
                return NormalizedSnapshotSpanCollection.Empty;
            }
            else if (targetSnapshot.TextBuffer != this.snapshot.TextBuffer)
            {
                throw new ArgumentException("this.Snapshot and targetSnapshot must be from the same ITextBuffer");
            }
            else if (this.snapshot == targetSnapshot)
            {
                return this;
            }
            else if (this.spans == null)
            {
                Span targetSpan = targetSnapshot.Version.VersionNumber > this.snapshot.Version.VersionNumber
                                  ? Tracking.TrackSpanForwardInTime(mode, this.span, this.snapshot.Version, targetSnapshot.Version)
                                  : Tracking.TrackSpanBackwardInTime(mode, this.span, this.snapshot.Version, targetSnapshot.Version);

                return new NormalizedSnapshotSpanCollection(targetSnapshot, targetSpan);
            }
            else
            {
                var targetSpans = new Span[this.spans.Count];
                for (int i = 0; (i < this.spans.Count); ++i)
                {
                    targetSpans[i] = targetSnapshot.Version.VersionNumber > this.snapshot.Version.VersionNumber
                                     ? Tracking.TrackSpanForwardInTime(mode, this.spans[i], this.snapshot.Version, targetSnapshot.Version)
                                     : Tracking.TrackSpanBackwardInTime(mode, this.spans[i], this.snapshot.Version, targetSnapshot.Version);
                }

                return new NormalizedSnapshotSpanCollection(targetSnapshot, targetSpans);
            }
        }

        #region Implicit Conversion
        /// <summary>
        /// Converts the specified <see cref="NormalizedSnapshotSpanCollection"/> to a <see cref="NormalizedSpanCollection"/>.
        /// </summary>
        /// <param name="spans">The collection to convert.</param>
        /// <returns>A <see cref="NormalizedSpanCollection"/> containing the corresponding normalized collection of <see cref="Span"/> objects.</returns>
        public static implicit operator NormalizedSpanCollection(NormalizedSnapshotSpanCollection spans)
        {
            if (spans == null)
            {
                return null;
            }
            else if (spans.spans != null)
            {
                // length greater than one
                return spans.spans;
            }
            else if (spans.snapshot != null)
            {
                // length one
                return new NormalizedSpanCollection(spans.span);
            }
            else
            {
                // length zero;
                return NormalizedSpanCollection.Empty;
            }
        }
        #endregion

        #region Set Operations
        /// <summary>
        /// Computes the union of two snapshot span collections and normalizes the result.
        /// </summary>
        /// <param name="left">The first <see cref="NormalizedSnapshotSpanCollection"/>.</param>
        /// <param name="right">The second <see cref="NormalizedSnapshotSpanCollection"/>.</param>
        /// <returns>The normalized union of the input collections.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="left"/> or <paramref name="right"/> is null.</exception>
        /// <exception cref="ArgumentException">The collections refer to different snapshots.</exception>
        public static NormalizedSnapshotSpanCollection Union(NormalizedSnapshotSpanCollection left, NormalizedSnapshotSpanCollection right)
        {
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left));
            }
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }

            if (left.Count == 0)
            {
                return right;
            }
            if (right.Count == 0)
            {
                return left;
            }

            if (left.snapshot != right.snapshot)
            {
                throw new ArgumentException(Strings.MismatchedSnapshots);
            }

            NormalizedSpanCollection leftSpans = left.spans ?? new NormalizedSpanCollection(left.span);
            NormalizedSpanCollection rightSpans = right.spans ?? new NormalizedSpanCollection(right.span);

            return new NormalizedSnapshotSpanCollection(left[0].Snapshot, NormalizedSpanCollection.Union(leftSpans, rightSpans));
        }

        /// <summary>
        /// Computes the overlap of two normalized snapshot span collections and normalizes the result.
        /// </summary>
        /// <param name="left">The first <see cref="NormalizedSnapshotSpanCollection"/>.</param>
        /// <param name="right">The second <see cref="NormalizedSnapshotSpanCollection"/></param>
        /// <returns>The normalized set of overlapping snapshot spans.</returns>
        /// <remarks>Empty SnapshotSpans never overlap any other SnapshotSpan.</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="left"/> or <paramref name="right"/> is null.</exception>
        /// <exception cref="ArgumentException">The input collections refer to different snapshots.</exception>
        public static NormalizedSnapshotSpanCollection Overlap(NormalizedSnapshotSpanCollection left, NormalizedSnapshotSpanCollection right)
        {
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left));
            }
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }

            if (left.Count == 0)
            {
                return left;
            }
            if (right.Count == 0)
            {
                return right;
            }

            if (left.snapshot != right.snapshot)
            {
                throw new ArgumentException(Strings.MismatchedSnapshots);
            }

            NormalizedSpanCollection leftSpans = left.spans ?? new NormalizedSpanCollection(left.span);
            NormalizedSpanCollection rightSpans = right.spans ?? new NormalizedSpanCollection(right.span);

            return new NormalizedSnapshotSpanCollection(left[0].Snapshot, NormalizedSpanCollection.Overlap(leftSpans, rightSpans));
        }

        /// <summary>
        /// Computes the intersection of two normalized snapshot span collections and normalizes the result.
        /// </summary>
        /// <param name="left">The first <see cref="NormalizedSnapshotSpanCollection"/>.</param>
        /// <param name="right">The second<see cref="NormalizedSnapshotSpanCollection"/>.</param>
        /// <returns>The normalized set of intersecting spans.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="left"/> or <paramref name="right"/> is null.</exception>
        /// <exception cref="ArgumentException">The collections refer to different snapshots.</exception>
        public static NormalizedSnapshotSpanCollection Intersection(NormalizedSnapshotSpanCollection left, NormalizedSnapshotSpanCollection right)
        {
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left));
            }
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }

            if (left.Count == 0)
            {
                return left;
            }
            if (right.Count == 0)
            {
                return right;
            }

            if (left.snapshot != right.snapshot)
            {
                throw new ArgumentException(Strings.MismatchedSnapshots);
            }

            NormalizedSpanCollection leftSpans = left.spans ?? new NormalizedSpanCollection(left.span);
            NormalizedSpanCollection rightSpans = right.spans ?? new NormalizedSpanCollection(right.span);

            return new NormalizedSnapshotSpanCollection(left[0].Snapshot, NormalizedSpanCollection.Intersection(leftSpans, rightSpans));
        }

        /// <summary>
        /// Computes the difference between two normalized snapshot span collections and normalizes the result.
        /// </summary>
        /// <param name="left">The collection from which to subtract <paramref name="right"/>.</param>
        /// <param name="right">The collection to subtract from <paramref name="left"/>.</param>
        /// <returns>The normalized set difference.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="left"/> or <paramref name="right"/> is null.</exception>
        /// <exception cref="ArgumentException">The input collections refer to different snapshots.</exception>
        public static NormalizedSnapshotSpanCollection Difference(NormalizedSnapshotSpanCollection left, NormalizedSnapshotSpanCollection right)
        {
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left));
            }
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }

            if (left.Count == 0)
            {
                return left;
            }
            if (right.Count == 0)
            {
                return left;
            }

            if (left.snapshot != right.snapshot)
            {
                throw new ArgumentException(Strings.MismatchedSnapshots);
            }

            NormalizedSpanCollection leftSpans = left.spans ?? new NormalizedSpanCollection(left.span);
            NormalizedSpanCollection rightSpans = right.spans ?? new NormalizedSpanCollection(right.span);

            return new NormalizedSnapshotSpanCollection(left[0].Snapshot, NormalizedSpanCollection.Difference(leftSpans, rightSpans));
        }

        /// <summary>
        /// Determines whether this collection overlaps with another normalized snapshot span collection.
        /// </summary>
        /// <param name="set">The collection.</param>
        /// <returns><c>true</c> if the collections refer to the same snapshot and their spans overlap, otherwise <c>false</c>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="set"/> is null.</exception>
        /// <exception cref="ArgumentException">The collections refer to different snapshots.</exception>
        public bool OverlapsWith(NormalizedSnapshotSpanCollection set)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }
            else if (set.Count == 0 || this.Count == 0)
            {
                return false;
            }
            else if (set.snapshot != this.snapshot)
            {
                throw new ArgumentException(Strings.MismatchedSnapshots);
            }
            else
            {
                NormalizedSpanCollection thisSpans = this.spans ?? new NormalizedSpanCollection(this.span);
                return thisSpans.OverlapsWith(set);
            }
        }

        /// <summary>
        /// Determines whether this collection overlaps with a snapshot span.
        /// </summary>
        /// <param name="span">The snapshot span to test.</param>
        /// <returns><c>true</c> if the collection and the span refer to the same snapshot and their spans overlap, otherwise <c>false</c>.</returns>
        /// <exception cref="ArgumentException">The collection and the span refer to different snapshots.</exception>
        public bool OverlapsWith(SnapshotSpan span)
        {
            if (this.snapshot == null)
            {
                // we are size zero
                return false;
            }
            else if (span.Snapshot != this.snapshot)
            {
                throw new ArgumentException(Strings.MismatchedSnapshots);
            }
            else if (this.spans != null)
            {
                return this.spans.OverlapsWith(span.Span);
            }
            else
            {
                return this.span.OverlapsWith(span.Span);
            }
        }

        /// <summary>
        /// Determines whether this collection intersects with another normalized snapshot span collection.
        /// </summary>
        /// <param name="set">The colllection.</param>
        /// <returns><c>true</c> if the collections intersect, otherwise <c>false</c>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="set"/> is null.</exception>
        /// <exception cref="ArgumentException">The input collections refer to different snapshots.</exception>
        public bool IntersectsWith(NormalizedSnapshotSpanCollection set)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }
            else if (set.Count == 0 || this.Count == 0)
            {
                return false;
            }
            else if (set.snapshot != this.snapshot)
            {
                throw new ArgumentException(Strings.MismatchedSnapshots);
            }
            else
            {
                NormalizedSpanCollection thisSpans = this.spans ?? new NormalizedSpanCollection(this.span);
                return thisSpans.IntersectsWith(set);
            }
        }

        /// <summary>
        /// Determines whether this collection overlaps with a snapshot span.
        /// </summary>
        /// <param name="span">The snapshot span to test.</param>
        /// <returns><c>true</c> if the collection and the span refer to the same snapshot and their spans overlap, otherwise <c>false</c>.</returns>
        /// <exception cref="ArgumentException">The collection and the span refer to different snapshots.</exception>
        public bool IntersectsWith(SnapshotSpan span)
        {
            if (this.snapshot == null)
            {
                // we are size zero
                return false;
            }
            else if (span.Snapshot != this.snapshot)
            {
                throw new ArgumentException(Strings.MismatchedSnapshots);
            }
            else if (this.spans != null)
            {
                return this.spans.IntersectsWith(span);
            }
            else
            {
                return this.span.IntersectsWith(span.Span);
            }
        }
        #endregion
        
        #region IList<SnapshotSpan> Members

        /// <summary>
        /// Gets the index of the specified <see cref="SnapshotSpan"/>.
        /// </summary>
        /// <param name="item">The <see cref="SnapshotSpan"/>.</param>
        /// <returns>The index of the snapshot span.</returns>
        public int IndexOf(SnapshotSpan item)
        {
            if (this.snapshot == item.Snapshot)
            {
                if (this.spans != null)
                {
                    return this.spans.IndexOf(item.Span);
                }
                else if (this.snapshot != null && this.span == item.Span)
                {
                    return 0;
                }
            }
            return -1;
        }

        /// <summary>
        /// Inserts a snapshot span into the list. This method throws a <see cref="NotSupportedException"/>.
        /// </summary>
        /// <param name="index">The location at which to insert the snapshot span.</param>
        /// <param name="item">The snapshot span to insert.</param>
        void IList<SnapshotSpan>.Insert(int index, SnapshotSpan item)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Removes a snapshot span at the specified location. This method throws a <see cref="NotSupportedException"/>.
        /// </summary>
        /// <param name="index">The location at which to remove the snapshot span.</param>
        void IList<SnapshotSpan>.RemoveAt(int index)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Gets the snapshot span at the specified location. The setter throws a <see cref="NotSupportedException"/>.
        /// </summary>
        /// <param name="index">The location at which to get the snapshot span.</param>
        /// <returns>The snapshot span.</returns>
        public SnapshotSpan this[int index]
        {
            get
            {
                if (this.spans != null)
                {
                    return new SnapshotSpan(this.snapshot, this.spans[index]);
                }
                else if (this.snapshot != null && index == 0)
                {
                    return new SnapshotSpan(this.snapshot, this.span);
                }
                else
                {
                    // Analyzer has a bug where it is giving a false positive in this location.
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
                    throw new ArgumentOutOfRangeException(nameof(index));
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
                }
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        #endregion

        #region ICollection<SnapshotSpan> Members

        /// <summary>
        /// Adds a snapshot span to the collection. This method throws a <see cref="NotSupportedException"/>.
        /// </summary>
        /// <param name="item">The snapshot span.</param>
        void ICollection<SnapshotSpan>.Add(SnapshotSpan item)
        {
            throw new NotSupportedException();
        }


        /// <summary>
        /// Clears the collection. This method throws a <see cref="NotSupportedException"/>.
        /// </summary>
        void ICollection<SnapshotSpan>.Clear()
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Determines whether the collection contains the specified snapshot span.
        /// </summary>
        /// <param name="item">The snapshot span.</param>
        /// <returns><c>true</c> if the collection contains the snapshot span, otherwise <c>false</c>.</returns>
        public bool Contains(SnapshotSpan item)
        {
            if (this.spans != null)
            {
                return item.Snapshot == this.snapshot && this.spans.Contains(item);
            }
            else if (this.snapshot == null)
            {
                return false;
            }
            else
            {
                return item.Snapshot == this.snapshot && item.Span == this.span;
            }
        }

        /// <summary>
        /// Copies the collection to an array of snapshot spans at the specified location.
        /// </summary>
        /// <param name="array">The array of snapshot spans.</param>
        /// <param name="arrayIndex">The location to which to copy the snapshot spans.</param>
        /// <exception cref="ArgumentNullException"><paramref name="array"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is negative or greater than the array length, 
        /// or the number of spans in the collection is greater than the length of the array minus the array index.</exception>
        public void CopyTo(SnapshotSpan[] array, int arrayIndex)
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }
            if (arrayIndex < 0 || arrayIndex > array.Length || this.Count > array.Length - arrayIndex)
            {
                throw new ArgumentOutOfRangeException(nameof(arrayIndex));
            }
            if (this.spans != null)
            {
                for (int s = 0; s < this.spans.Count; ++s)
                {
                    array[arrayIndex++] = new SnapshotSpan(this.snapshot, this.spans[s]);
                }
            }
            else if (this.snapshot != null)
            {
                array[arrayIndex] = new SnapshotSpan(this.snapshot, this.span);
            }
        }

        /// <summary>
        /// Gets the number of spans in the collection.
        /// </summary>
        public int Count
        {
            get 
            {
                if (this.spans != null)
                {
                    return this.spans.Count;
                }
                else
                {
                    return this.snapshot == null ? 0 : 1;
                }
            }
        }

        /// <summary>
        /// Determines whether the collection is read-only. Always returns <c>true</c>.
       /// </summary>
        bool ICollection<SnapshotSpan>.IsReadOnly
        {
            get { return true; }
        }

        /// <summary>
        /// Removes the specified span from the collection. This method throws a <see cref="NotSupportedException"/>.
        /// </summary>
        /// <param name="item">The snapshot span.</param>
        /// <returns><c>true</c> if it was possible to remove the span.</returns>
        bool ICollection<SnapshotSpan>.Remove(SnapshotSpan item)
        {
            throw new NotSupportedException();
        }

        #endregion

        #region IEnumerable<SnapshotSpan> Members

        /// <summary>
        /// Gets an enumerator for the collection.
        /// </summary>
        /// <returns>The enumerator.</returns>
        public IEnumerator<SnapshotSpan> GetEnumerator()
        {
            if (this.spans != null)
            {
                foreach (Span span in this.spans)
                {
                    yield return new SnapshotSpan(this.snapshot, span);
                }
            }
            else if (this.snapshot != null)
            {
                yield return new SnapshotSpan(this.snapshot, this.span);
            }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            if (this.spans != null)
            {
                foreach (Span span in this.spans)
                {
                    yield return new SnapshotSpan(this.snapshot, span);
                }
            }
            else if (this.snapshot != null)
            {
                yield return new SnapshotSpan(this.snapshot, this.span);
            }
        }

        #endregion

        #region IList Members

        /// <summary>
        /// Adds an object to the list. This method throws a <see cref="NotSupportedException"/>.
        /// </summary>
        /// <param name="value">The object to add.</param>
        /// <returns>The location at which the object was added.</returns>
        int IList.Add(object value)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Clears the list. This method throws a <see cref="NotSupportedException"/>.
        /// </summary>
        void IList.Clear()
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Determines whether the collection contains the specified snapshot span.
        /// </summary>
        /// <param name="value">The snapshot span.</param>
        /// <returns><c>true</c> if the snapshot span is contained in the collection, otherwise <c>false</c>.</returns>
        public bool Contains(object value)
        {
            if (value is SnapshotSpan)
            {
                SnapshotSpan val = (SnapshotSpan)value;
                if (this.spans != null)
                {
                    return this.snapshot == val.Snapshot && this.spans.Contains(val.Span);
                }
                else if (this.snapshot == null)
                {
                    return false;
                }
                else
                {
                    return this.snapshot == val.Snapshot && this.span == val.Span;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Gets the index of the specified snapshot span.
        /// </summary>
        /// <param name="value">The snapshot span.</param>
        /// <returns>The location of the snapshot span.</returns>
        public int IndexOf(object value)
        {
            if (value is SnapshotSpan)
            {
                SnapshotSpan val = (SnapshotSpan)value;
                if (this.snapshot == val.Snapshot)
                {
                    if (this.spans != null)
                    {
                        return this.spans.IndexOf(val.Span);
                    }
                    else if (this.snapshot != null && this.span == val.Span)
                    {
                        return 0;
                    }
                }
            }
            return -1;
        }

        /// <summary>
        /// Inserts a snapshot span into the list at the specified location. This method throws a <see cref="NotSupportedException"/>.
        /// </summary>
        /// <param name="index">The location.</param>
        /// <param name="value">The snapshot span.</param>
        void IList.Insert(int index, object value)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Determines whether the collection is of fixed size. Always returns <c>true</c>.
        /// </summary>
        bool IList.IsFixedSize
        {
            get { return true; }
        }

        /// <summary>
        /// Removes the specified snapshot span. This method throws a <see cref="NotSupportedException"/>.
        /// </summary> 
        /// <param name="value">The snapshot span.</param>
        void IList.Remove(object value)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Removes a snapshot span at the specified location. This method throws a <see cref="NotSupportedException"/>.
        /// </summary>
        /// <param name="index">The location.</param>
        void IList.RemoveAt(int index)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Gets the snapshot span at the specified location. The setter throws a <see cref="NotSupportedException"/>.
        /// </summary>
        /// <param name="index">The location.</param>
        /// <returns>The snapshot span.</returns>
        object IList.this[int index]
        {
            get
            {
                if (this.spans != null)
                {
                    return new SnapshotSpan(this.snapshot, this.spans[index]);
                }
                else if (this.snapshot != null && index == 0)
                {
                    return new SnapshotSpan(this.snapshot, this.span);
                }
                else
                {
                    throw new ArgumentOutOfRangeException(nameof(index));
                }
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        #endregion

        #region ICollection Members

        /// <summary>
        /// Copies the snapshot spans in this collection to the specified array, starting at the specified index.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <param name="index">The location at which to start copying.</param>
        /// <exception cref="ArgumentNullException"><paramref name="array"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is negative, or greater than
        /// the length of the array, or the number of spans is greater than the length of the array less the index.</exception>
        /// <exception cref="ArgumentException"><paramref name="array"/> is not one-dimensional.</exception>
        public void CopyTo(Array array, int index)
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }
            if (index < 0 || index > array.Length || this.Count > array.Length - index)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if (array.Rank != 1)
            {
                throw new ArgumentException(Strings.ArrayRankNotOne);
            }
            if (this.spans != null)
            {
                for (int s = 0; s < this.spans.Count; ++s)
                {
                    array.SetValue(new SnapshotSpan(this.snapshot, this.spans[s]), index++);
                }
            }
            else if (this.snapshot != null)
            {
                array.SetValue(new SnapshotSpan(this.snapshot, this.span), index);
            }
        }

        /// <summary>
        /// Determines whether this collection is read-only. This property always returns <c>true</c>.
        /// </summary>
        bool IList.IsReadOnly
        {
            get { return true; }
        }

        /// <summary>
        /// Determines whether this collection is synchronized.
        /// </summary>
        bool ICollection.IsSynchronized
        {
            get { return true; }
        }

        /// <summary>
        /// Gets an object that can be used to synchronize access to this collection.
        /// </summary>
        object ICollection.SyncRoot
        {
            get 
            {
                return this.spans != null ? (this.spans as IList).SyncRoot : this;
            }
        }

        #endregion

        #region Operators and Overrides
        /// <summary>
        /// Determines whether two <see cref="NormalizedSnapshotSpanCollection"/> objects are the same.
        /// </summary>
        /// <param name="left">The first collection.</param>
        /// <param name="right">The second collection.</param>
        /// <returns><c>true</c> if the two sets are the same, otherwise <c>false</c>.</returns>
        public static bool operator ==(NormalizedSnapshotSpanCollection left, NormalizedSnapshotSpanCollection right)
        {
            if (object.ReferenceEquals(left, right))
            {
                return true;
            }
            if (object.ReferenceEquals(left, null) || object.ReferenceEquals(right, null))
            {
                return false;
            }

            if (left.Count != right.Count)
            {
                return false;
            }

            for (int i = 0; i < left.Count; ++i)
            {
                if (left[i] != right[i])
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Determines whether two <see cref="NormalizedSnapshotSpanCollection"/> are different..
        /// </summary>
        /// <param name="left">The first collection.</param>
        /// <param name="right">The second collection.</param>
        /// <returns><c>true</c> if the two collections are different.</returns>
        public static bool operator !=(NormalizedSnapshotSpanCollection left, NormalizedSnapshotSpanCollection right)
        {
            return !(left == right);
        }

        /// <summary>
        /// Gets a hash code for the collection.
        /// </summary>
        /// <returns>A 32-bit hash code associated with the collection.</returns>
        public override int GetHashCode()
        {
            return this.spans != null ? this.spans.GetHashCode() : this.span.GetHashCode();
        }

        /// <summary>
        /// Determines whether two snapshot span collections are equal
        /// </summary>
        /// <param name="obj">The second collection.</param>
        /// <returns><c>true</c> if the two collections are equal, otherwise <c>false</c>.</returns>
        public override bool Equals(object obj)
        {
            NormalizedSnapshotSpanCollection set = obj as NormalizedSnapshotSpanCollection;

            return this == set;
        }

        /// <summary>
        /// Converts the spans to a string..
        /// </summary>
        /// <returns>The string representation.</returns>
        public override string ToString()
        {
            return this.spans != null ? this.spans.ToString() : this.span.ToString();
        }
        #endregion

    }
}