void SetNewDocument() { cachedTextColorsCollection = new CachedTextColorsCollection(); wpfTextView.TextBuffer.Replace(new Span(0, wpfTextView.TextBuffer.CurrentSnapshot.Length), string.Empty); ClearUndoRedoHistory(); cachedColorsList.Clear(); cachedColorsList.Add(0, cachedTextColorsCollection); }
public bool SetContent(DocumentViewerContent content, IContentType contentType) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (contentType == null) { contentType = defaultContentType; } HideCancelButton(); var newContent = new CurrentContent(content, contentType); if (currentContent.Equals(newContent)) { return(false); } currentContent = newContent; spanReferenceCollection = newContent.Content.GetCustomData <SpanDataCollection <ReferenceAndId> >(DocumentViewerContentDataIds.SpanReference) ?? SpanDataCollection <ReferenceAndId> .Empty; TextView.TextBuffer.ChangeContentType(contentType, null); cachedColorsList.Clear(); cachedColorsList.Add(0, content.ColorCollection); // If it's the same text, don't update text buffer and caret. It can be the same text if // it's not been cached, eg. it's the resources or some other content with UI content. // This simulates the cached code path above which also doesn't move the caret. And as // an added bonus, it will use less memory and CPU. bool sameText = IsSameTextAsCurrentSnapshot(TextView.TextSnapshot, content.Text); if (!sameText) { TextView.TextBuffer.Replace(new Span(0, TextView.TextBuffer.CurrentSnapshot.Length), content.Text); TextView.Caret.MoveTo(new SnapshotPoint(TextView.TextSnapshot, 0)); TextView.Caret.EnsureVisible(); TextView.Selection.Clear(); } return(true); }