DocumentChangedEventArgs ComputeChanges(ISassStylesheet previous, ISassStylesheet current, ITextSnapshot snapshot, SingleTextChange change) { if (previous == null) return new DocumentChangedEventArgs { Stylesheet = current, ChangeStart = 0, ChangeEnd = snapshot.Length }; int start = 0; int end = Math.Min(change.Position + change.InsertedLength, snapshot.Length); // we need to scan both trees until we find where they start lining up again var offset = change.InsertedLength + (-1 * change.DeletedLength); var original = previous.Children.FindItemContainingPosition(change.Position - change.DeletedLength) ?? previous as Stylesheet; var updated = current.Children.FindItemContainingPosition(change.Position + change.InsertedLength) ?? current as Stylesheet; if (original != null && updated != null) start = updated.Start; while (true) { if (original == null || updated == null) break; // update our positions start = Math.Min(start, updated.Start); end = Math.Max(end, updated.End); if (original.GetType() == updated.GetType()) { // there are two types of changes (adding characters or removing characters) // if we added characters then we'll need to adjust end characters // if we deleted characters then we'll need to offset starting characters OR ending characters // checking for length being extended by adding characters if (original.Start == updated.Start && (original.End + change.InsertedLength) == updated.End) { end = updated.End; break; } // checking for length being shortened by deleting characters else if (original.Start == updated.Start && (original.End - change.DeletedLength) == updated.End) { break; } // checking for removal of nodes? else if (original.Start == (updated.Start - change.DeletedLength) && (original.End - change.DeletedLength) == updated.End) { break; } } original = original.InOrderSuccessor(); updated = updated.InOrderSuccessor(); } return new DocumentChangedEventArgs { Stylesheet = current, ChangeStart = Math.Min(start, change.Position), ChangeEnd = Math.Max(end, change.Position + change.InsertedLength) }; }
void Process(ITextSnapshot snapshot, SingleTextChange change) { var stylesheet = ParsingTask.Parse(new TextSnapshotParsingRequest(Buffer, Document)); if (stylesheet != null) { var previous = Document.Update(stylesheet); var args = ComputeChanges(previous, stylesheet, snapshot, change); OnDocumentChanged(args); // update intellisense cache if (Buffer.CurrentSnapshot == snapshot) { Task.Run(() => Cache.Update(stylesheet, new SnapshotTextProvider(snapshot))); } } }
void Process(ITextSnapshot snapshot, SingleTextChange change) { var stylesheet = ParsingTask.Parse(new TextSnapshotParsingRequest(Buffer, Document)); if (stylesheet != null) { var previous = Document.Update(stylesheet); var args = ComputeChanges(previous, stylesheet, snapshot, change); OnDocumentChanged(args); // update intellisense cache if (Buffer.CurrentSnapshot == snapshot) Task.Run(() => Cache.Update(stylesheet, new SnapshotTextProvider(snapshot))); } }
DocumentChangedEventArgs ComputeChanges(ISassStylesheet previous, ISassStylesheet current, ITextSnapshot snapshot, SingleTextChange change) { if (previous == null) { return new DocumentChangedEventArgs { Stylesheet = current, ChangeStart = 0, ChangeEnd = snapshot.Length } } ; int start = 0; int end = Math.Min(change.Position + change.InsertedLength, snapshot.Length); // we need to scan both trees until we find where they start lining up again var offset = change.InsertedLength + (-1 * change.DeletedLength); var original = previous.Children.FindItemContainingPosition(change.Position - change.DeletedLength) ?? previous as Stylesheet; var updated = current.Children.FindItemContainingPosition(change.Position + change.InsertedLength) ?? current as Stylesheet; if (original != null && updated != null) { start = updated.Start; } while (true) { if (original == null || updated == null) { break; } // update our positions start = Math.Min(start, updated.Start); end = Math.Max(end, updated.End); if (original.GetType() == updated.GetType()) { // there are two types of changes (adding characters or removing characters) // if we added characters then we'll need to adjust end characters // if we deleted characters then we'll need to offset starting characters OR ending characters // checking for length being extended by adding characters if (original.Start == updated.Start && (original.End + change.InsertedLength) == updated.End) { end = updated.End; break; } // checking for length being shortened by deleting characters else if (original.Start == updated.Start && (original.End - change.DeletedLength) == updated.End) { break; } // checking for removal of nodes? else if (original.Start == (updated.Start - change.DeletedLength) && (original.End - change.DeletedLength) == updated.End) { break; } } original = original.InOrderSuccessor(); updated = updated.InOrderSuccessor(); } return(new DocumentChangedEventArgs { Stylesheet = current, ChangeStart = Math.Min(start, change.Position), ChangeEnd = Math.Max(end, change.Position + change.InsertedLength) }); }