private ITextView TestAutoFormat(int position, string textToType, string initialContent = "") { AstRoot ast; ITextView textView = TextViewTest.MakeTextView(initialContent, position, out ast); textView.TextBuffer.Changed += (object sender, TextContentChangedEventArgs e) => { List <TextChangeEventArgs> textChanges = TextUtility.ConvertToRelative(e); ast.ReflectTextChanges(textChanges); if (e.Changes[0].NewText.Length == 1) { char ch = e.Changes[0].NewText[0]; if (AutoFormat.IsAutoformatTriggerCharacter(ch)) { int offset = 0; if (e.Changes[0].NewText[0].IsLineBreak()) { position = e.Changes[0].OldPosition + 1; textView.Caret.MoveTo(new SnapshotPoint(e.After, position)); offset = -1; } FormatOperations.FormatLine(textView, textView.TextBuffer, ast, offset); } } else { ITextSnapshotLine line = e.After.GetLineFromPosition(position); textView.Caret.MoveTo(new SnapshotPoint(e.After, Math.Min(e.After.Length, line.Length + 1))); } }; Typing.Type(textView.TextBuffer, position, textToType); return(textView); }
private void OnTextBufferChanged(object sender, TextContentChangedEventArgs e) { if (REditorSettings.SyntaxCheck && e.Changes.Count > 0) { var changes = TextUtility.ConvertToRelative(e); foreach (var change in changes) { _errorTags.ReflectTextChange(change.Start, change.OldLength, change.NewLength, trivialChange: !_document.EditorTree.IsReady); } if ((_errorTags.RemovedTags.Count > 0) && (TagsChanged != null)) { int start = Int32.MaxValue; int end = Int32.MinValue; foreach (var errorTag in _errorTags.RemovedTags) { start = Math.Min(start, errorTag.Start); end = Math.Max(end, errorTag.End); } // RemovedTags haven't had their positions updated, verify their // values won't break the SnapshotSpan creation ITextSnapshot snapshot = _textBuffer.CurrentSnapshot; start = Math.Min(start, snapshot.Length); end = Math.Min(end, snapshot.Length); TagsChanged(this, new SnapshotSpanEventArgs( new SnapshotSpan(snapshot, start, end - start))); } TasksUpdated?.Invoke(this, EventArgs.Empty); } }
private void OnTextBufferChanged(object sender, TextContentChangedEventArgs e) { if (e.Changes.Count > 0) { // In case of tabbing multiple lines update comes as multiple changes // each is an insertion of whitespace in the beginning of the line. // We don't want to combine them since then change will technically // damage existing elements while actually it is just a whitespace change. // All changes are relative to the current snapshot hence we have to transform // them first and make them relative to each other so we can apply changes // sequentially as after every change element positions will shift and hence // next change must be relative to the new position and not to the current // text buffer snapshot. Changes are sorted by position. List <TextChangeEventArgs> textChanges = TextUtility.ConvertToRelative(e); TreeUpdateTask.OnTextChanges(textChanges); } }