using Microsoft.VisualStudio.Text; // ... // Initialize a new TrackingSpan ITrackingSpan span = buffer.CurrentSnapshot.CreateTrackingSpan( new Span(startIndex, length), SpanTrackingMode.EdgeExclusive ); // Update the span when the document changes void OnTextBufferChanged(object sender, TextContentChangedEventArgs e) { span = e.After.CreateTrackingSpan( span.GetSpan(e.Before), SpanTrackingMode.EdgeExclusive ); }In this example, we're creating a new ITrackingSpan from the current buffer snapshot using a specified start index and length. Then, we're hooking into an event handler for TextContentChangedEventArgs, which allows us to update the span whenever the document changes. This ensures that the span always stays up-to-date and accurately reflects the location of the tracked text.