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));
            }
        }
Example #2
0
        /// <summary>
        /// Translates this snapshot span to a different snapshot of the same <see cref="ITextBuffer"/>.
        /// </summary>
        /// <param name="targetSnapshot">The snapshot to which to translate.</param>
        /// <param name="spanTrackingMode">The <see cref="SpanTrackingMode"/> to use in the translation.</param>
        /// <returns>A new snapshot span.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="targetSnapshot"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="targetSnapshot"/> does not refer to the same <see cref="ITextBuffer"/> as this snapshot span.</exception>
        public SnapshotSpan TranslateTo(ITextSnapshot targetSnapshot, SpanTrackingMode spanTrackingMode)
        {
            if (targetSnapshot == this.Snapshot)
            {
                return(this);
            }
            else
            {
                if (targetSnapshot == null)
                {
                    throw new ArgumentNullException("targetSnapshot");
                }
                if (targetSnapshot.TextBuffer != this.Start.Snapshot.TextBuffer)
                {
                    throw new ArgumentException(Strings.InvalidSnapshot);
                }

                Span targetSpan = targetSnapshot.Version.VersionNumber > this.Snapshot.Version.VersionNumber
                                    ? Tracking.TrackSpanForwardInTime(spanTrackingMode, Span, this.Snapshot.Version, targetSnapshot.Version)
                                    : Tracking.TrackSpanBackwardInTime(spanTrackingMode, Span, this.Snapshot.Version, targetSnapshot.Version);

                return(new SnapshotSpan(targetSnapshot, targetSpan));
            }
        }