public void OnCharTyped(char @char) { // format on ':' if (@char == RobotsTxtSyntaxFacts.NameValueDelimiter) { ITextBuffer buffer = _textView.TextBuffer; SyntaxTree syntaxTree = buffer.GetSyntaxTree(); RobotsTxtDocumentSyntax root = syntaxTree.Root as RobotsTxtDocumentSyntax; // find in syntax tree var caret = _textView.Caret.Position.BufferPosition; RobotsTxtLineSyntax lineSyntax = root.Records .SelectMany(r => r.Lines) .FirstOrDefault(p => p.DelimiterToken.Span.Span.End == caret); if (lineSyntax != null) { using (ITextUndoTransaction transaction = _undoHistory.CreateTransaction("Automatic Formatting")) { using (ITextEdit edit = buffer.CreateEdit()) { // fix indent // find property before RobotsTxtLineSyntax before = lineSyntax.Record.Lines .TakeWhile(p => p != lineSyntax) .LastOrDefault(); // reference point if (before != null) { SnapshotPoint referencePoint = before.NameToken.Span.Span.Start; // compare ITextSnapshotLine referenceLine = referencePoint.GetContainingLine(); ITextSnapshotLine line = lineSyntax.DelimiterToken.Span.Span.End.GetContainingLine(); SnapshotSpan referenceIndent = new SnapshotSpan(referenceLine.Start, referencePoint); SnapshotSpan indent = new SnapshotSpan(line.Start, lineSyntax.NameToken.Span.Span.Start); if (indent.GetText() != referenceIndent.GetText()) { edit.Replace(indent, referenceIndent.GetText()); } } // remove white space before ':' if (lineSyntax.NameToken.Span.Span.End != lineSyntax.DelimiterToken.Span.Span.Start) { edit.Delete(new SnapshotSpan(lineSyntax.NameToken.Span.Span.End, lineSyntax.DelimiterToken.Span.Span.Start)); } edit.Apply(); } transaction.Complete(); } } } }
private void DeleteSpan(NormalizedSnapshotSpanCollection applicabilitySpans) { using (ITextUndoTransaction undoTransaction = _undoHistory.CreateTransaction("HTML Cut")) { _editorOperations.AddBeforeTextBufferChangePrimitive(); bool successfulEdit = true; using (ITextEdit edit = _textView.TextBuffer.CreateEdit()) { foreach (SnapshotSpan span in applicabilitySpans) { successfulEdit &= edit.Delete(span); } if (successfulEdit) { edit.Apply(); } } _editorOperations.AddAfterTextBufferChangePrimitive(); if (successfulEdit) { undoTransaction.Complete(); } } }
private bool InvokeZenCoding() { ThreadHelper.ThrowIfNotOnUIThread(); Span zenSpan = GetSyntaxSpan(out string syntax); if (zenSpan.IsEmpty || _view.Selection.SelectedSpans[0].Length > 0 || !IsValidTextBuffer()) { return(false); } var parser = new Parser(); string result = parser.Parse(syntax, ZenType.HTML); if (!string.IsNullOrEmpty(result)) { using (ITextUndoTransaction undo = _undoManager.TextBufferUndoHistory.CreateTransaction("ZenCoding")) { ITextSelection selection = UpdateTextBuffer(zenSpan, result); var newSpan = new Span(zenSpan.Start, selection.SelectedSpans[0].Length); _dte.ExecuteCommand("Edit.FormatSelection"); SetCaret(newSpan, false); selection.Clear(); undo.Complete(); } return(true); } return(false); }
/// <remarks> /// Edits are queued up by our TextBufferChanged handler and then we finally add them to the /// undo stack here in response to PostChanged. The reason and history behind why we do this /// is as follows: /// /// Originally this was done for VB commit, which uses undo events (i.e. TransactionCompleted) to /// trigger commit. Their commit logic relies on the buffer being in a state such that applying /// an edit synchronously raises a Changed event (which is always the case for PostChanged, but /// not for Changed if there are nested edits). /// /// JaredPar made a change (CS 1182244) that allowed VB to detect that UndoTransactionCompleted /// was being fired from a nested edit, and therefore delay the actual commit until the following /// PostChanged event. /// /// So this allowed us to move TextBufferUndoManager back to adding undo actions directly /// from the TextBufferChanged handler (CS 1285117). This is preferable, as otherwise there's a /// "delay" between when the edit happens and when we record the edit on the undo stack, /// allowing other people to stick something on the undo stack (i.e. from /// their ITextBuffer.Changed handler) in between. The result is actions being "out-of-order" /// on the undo stack. /// /// Unfortunately, it turns out VB snippets actually rely on this "out-of-order" behavior /// (see Dev10 834740) and so we are forced to revert CS 1285117) and return to the model /// where we queue up edits and delay adding them to the undo stack until PostChanged. /// /// It would be good to revisit this at again, but we would need to work with VB /// to fix their snippets / undo behavior, and verify that VB commit is also unaffected. /// </remarks> private void TextBufferPostChanged(object sender, EventArgs e) { // Only process a top level PostChanged event. Nested events will continue to process TextChange events // which are added to the queue and will be processed below if (_inPostChanged) { return; } _inPostChanged = true; try { // Do not do a foreach loop here. It's perfectly possible, and in fact expected, that the Complete // method below can trigger a series of events which leads to a nested edit and another // ITextBuffer::Changed. That event will add to the _editVersionList queue and hence break a // foreach loop while (_editVersionList.Count > 0) { var cur = _editVersionList.Dequeue(); using (ITextUndoTransaction undoTransaction = _undoHistory.CreateTransaction(Strings.TextBufferChanged)) { TextBufferChangeUndoPrimitive undoPrimitive = new TextBufferChangeUndoPrimitive(_undoHistory, cur); undoTransaction.AddUndo(undoPrimitive); undoTransaction.Complete(); } } } finally { _editVersionList.Clear(); // Ensure we cleanup state in the face of an exception _inPostChanged = false; } }
public DragDropPointerEffects HandleDataDropped(DragDropInfo dragDropInfo) { try { SnapshotPoint position = dragDropInfo.VirtualBufferPosition.Position; string header = string.Format(_template, _ext); ITextSnapshotLine line = _view.TextBuffer.CurrentSnapshot.GetLineFromPosition(position); if (!line.Extent.IsEmpty) { header = Environment.NewLine + header; } using (ITextUndoTransaction transaction = _undoManager.TextBufferUndoHistory.CreateTransaction($"Dragged {_ext}")) using (ITextEdit edit = _view.TextBuffer.CreateEdit()) { edit.Insert(position, header); edit.Apply(); transaction.Complete(); } Telemetry.TrackUserTask("FileDragged"); } catch (Exception ex) { Telemetry.TrackException("DragDrop", ex); } return(DragDropPointerEffects.Copy); }
public void Dispose() { _selectionTracker.EndTracking(); _transaction.AddUndo(new EndSelectionTrackingUndoUnit(_selectionTracker, _textBuffer)); _transaction.Complete(); _transaction.Dispose(); }
private async Task <bool> MakePrettierAsync() { string input = _view.TextBuffer.CurrentSnapshot.GetText(); string output = await _node.ExecuteProcessAsync(input, _encoding, _filePath); VirtualSnapshotPoint snapshotPoint = _view.Selection.ActivePoint; if (string.IsNullOrEmpty(output) || input == output) { return(false); } using (ITextEdit edit = _view.TextBuffer.CreateEdit()) using (ITextUndoTransaction undo = _undoManager.TextBufferUndoHistory.CreateTransaction("Make Prettier")) { edit.Replace(0, _view.TextBuffer.CurrentSnapshot.Length, output); edit.Apply(); undo.Complete(); } var newSnapshotPoint = new SnapshotPoint(_view.TextBuffer.CurrentSnapshot, snapshotPoint.Position.Position); _view.Caret.MoveTo(newSnapshotPoint); _view.ViewScroller.EnsureSpanVisible(new SnapshotSpan(newSnapshotPoint, 0)); return(true); }
public void Dispose() { if (_transaction != null) { _transaction.Complete(); } _service.EndUndoTransaction(this); }
public void PreOverType(out bool handledCommand) { handledCommand = false; if (ClosingPoint == null) { return; } // Brace completion is not cancellable. var cancellationToken = CancellationToken.None; var snapshot = this.SubjectBuffer.CurrentSnapshot; var document = snapshot.GetOpenDocumentInCurrentContextWithChanges(); SnapshotPoint closingSnapshotPoint = ClosingPoint.GetPoint(snapshot); if (!HasForwardTyping && _session.AllowOverType(this, cancellationToken)) { SnapshotPoint?caretPos = this.GetCaretPosition(); Debug.Assert(caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position); // ensure that we are within the session before clearing if (caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position && closingSnapshotPoint.Position > 0) { using (ITextUndoTransaction undo = CreateUndoTransaction()) { _editorOperations.AddBeforeTextBufferChangePrimitive(); SnapshotSpan span = new SnapshotSpan(caretPos.Value, closingSnapshotPoint.Subtract(1)); using (ITextEdit edit = SubjectBuffer.CreateEdit()) { edit.Delete(span); if (edit.HasFailedChanges) { Debug.Fail("Unable to clear closing brace"); edit.Cancel(); undo.Cancel(); } else { handledCommand = true; edit.Apply(); MoveCaretToClosingPoint(); _editorOperations.AddAfterTextBufferChangePrimitive(); undo.Complete(); } } } } } }
public void Commit() { if (!_inTransaction) { throw new InvalidOperationException("The transaction is already complete"); } _editorOperations?.AddAfterTextBufferChangePrimitive(); _transaction?.Complete(); EndTransaction(); }
public void Complete() { if (!_active) { throw new InvalidOperationException(EditorFeaturesResources.The_transaction_is_already_complete); } _editorOperations.AddAfterTextBufferChangePrimitive(); _transaction?.Complete(); EndTransaction(); }
public void Dispose() { if (!EditorShell.Current.IsUnitTestEnvironment) { _selectionTracker.EndTracking(); _transaction.AddUndo(new EndSelectionTrackingUndoUnit(_selectionTracker)); _transaction.Complete(); _transaction.Dispose(); } }
bool ExecuteCommandCore(EditorCommandArgs args, CommandExecutionContext context, Operation operation) { ITextView textView = args.TextView; ITextBuffer textBuffer = args.SubjectBuffer; if (!XmlBackgroundParser.TryGetParser(textBuffer, out var parser)) { return(false); } var xmlParseResult = parser.GetOrProcessAsync(textBuffer.CurrentSnapshot, default).Result; var xmlDocumentSyntax = xmlParseResult.XDocument; if (xmlDocumentSyntax == null) { return(false); } string description = operation.ToString(); var editorOperations = editorOperationsFactoryService.GetEditorOperations(textView); var multiSelectionBroker = textView.GetMultiSelectionBroker(); var selectedSpans = multiSelectionBroker.AllSelections.Select(selection => selection.Extent); using (context.OperationContext.AddScope(allowCancellation: false, description: description)) { ITextUndoHistory undoHistory = undoHistoryRegistry.RegisterHistory(textBuffer); using (ITextUndoTransaction undoTransaction = undoHistory.CreateTransaction(description)) { switch (operation) { case Operation.Comment: CommentSelection(textBuffer, selectedSpans, xmlDocumentSyntax, editorOperations, multiSelectionBroker); break; case Operation.Uncomment: UncommentSelection(textBuffer, selectedSpans, xmlDocumentSyntax); break; case Operation.Toggle: ToggleCommentSelection(textBuffer, selectedSpans, xmlDocumentSyntax, editorOperations, multiSelectionBroker); break; } undoTransaction.Complete(); } } return(true); }
/// <summary> /// Commits all modifications made with specified <see cref="ITextBufferEdit"/> and link the commit /// with <see cref="ITextUndoTransaction"/> in the Editor, if <see cref="_undoHistory"/> is available. /// </summary> /// <param name="edit">A set of editing operations on an <see cref="ITextBuffer"/>.</param> /// <param name="description">The description of the transaction.</param> private void ApplyEdit(ITextBufferEdit edit, string description) { if (_undoHistory != null) { using (ITextUndoTransaction transaction = _undoHistory.CreateTransaction(description)) { edit.Apply(); transaction.Complete(); } } else { edit.Apply(); } }
public void Complete() { if (!_active) { throw new InvalidOperationException("This transaction is already compelte"); } _editorOperations.AddAfterTextBufferChangePrimitive(); if (_transaction != null) { _transaction.Complete(); } EndTransaction(); }
public void Complete() { if (!_active) { throw new InvalidOperationException(EditorFeaturesResources.TheTransactionIsAlreadyComplete); } _editorOperations.AddAfterTextBufferChangePrimitive(); if (_transaction != null) { _transaction.Complete(); } EndTransaction(); }
public void PreOverType(out bool handledCommand) { handledCommand = false; // AllowOverType may make changes to the buffer such as for completing intellisense if (!HasForwardTyping && (_context == null || _context.AllowOverType(this))) { SnapshotPoint?caretPos = CaretPosition; SnapshotPoint closingSnapshotPoint = _closingPoint.GetPoint(SubjectBuffer.CurrentSnapshot); Debug.Assert(caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position); // ensure that we are within the session before clearing if (caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position && closingSnapshotPoint.Position > 0) { using (ITextUndoTransaction undo = CreateUndoTransaction()) { _editorOperations.AddBeforeTextBufferChangePrimitive(); SnapshotSpan span = new SnapshotSpan(caretPos.Value, closingSnapshotPoint.Subtract(1)); using (ITextEdit edit = _subjectBuffer.CreateEdit()) { edit.Delete(span); if (edit.HasFailedChanges) { Debug.Fail("Unable to clear closing brace"); edit.Cancel(); undo.Cancel(); } else { handledCommand = true; edit.Apply(); MoveCaretToClosingPoint(); _editorOperations.AddAfterTextBufferChangePrimitive(); undo.Complete(); } } } } } }
private void FormatDocument() { using (ITextUndoTransaction transaction = _undoManager.TextBufferUndoHistory.CreateTransaction(Resources.Text.FormatDocument)) { EditorConfigFormatter formatter = _view.Properties.GetOrCreateSingletonProperty(() => new EditorConfigFormatter(_view.TextBuffer)); bool changed = formatter.Format(); if (changed) { transaction.Complete(); } else { transaction.Cancel(); } } }
private void TextBufferPostChanged(object sender, EventArgs e) { if (_createdTransaction != null) { #if false if (_initiatingOperations != null) { _initiatingOperations.AddAfterTextBufferChangePrimitive(); } _initiatingOperations = null; #endif _createdTransaction.Complete(); _createdTransaction.Dispose(); _createdTransaction = null; } }
public void PreTab(out bool handledCommand) { handledCommand = false; if (!HasForwardTyping) { handledCommand = true; using (ITextUndoTransaction undo = CreateUndoTransaction()) { _editorOperations.AddBeforeTextBufferChangePrimitive(); MoveCaretToClosingPoint(); _editorOperations.AddAfterTextBufferChangePrimitive(); undo.Complete(); } } }
public void Dispose() { if (_undoTransaction != null) { if (_discardChanges) { _undoTransaction.Cancel(); } else { _editorOperations.AddAfterTextBufferChangePrimitive(); _undoTransaction.Complete(); if (_undoAfterClose) { _undoManager.TextBufferUndoHistory.Undo(1); } } } }
private static async Task <bool> ExecuteAsync(IWpfTextView view, ITextBufferUndoManager undoManager, NodeProcess node) { string input = view.TextBuffer.CurrentSnapshot.GetText(); string output = await node.ExecuteProcess(input); if (string.IsNullOrEmpty(output) || input == output) { return(false); } using (ITextEdit edit = view.TextBuffer.CreateEdit()) using (ITextUndoTransaction undo = undoManager.TextBufferUndoHistory.CreateTransaction("Sort properties")) { edit.Replace(0, view.TextBuffer.CurrentSnapshot.Length, output); edit.Apply(); undo.Complete(); } return(true); }
private async Task <bool> MakePrettier() { string input = _view.TextBuffer.CurrentSnapshot.GetText(); string output = await _node.ExecuteProcess(input, _encoding, _filePath); if (string.IsNullOrEmpty(output) || input == output) { return(false); } using (ITextEdit edit = _view.TextBuffer.CreateEdit()) using (ITextUndoTransaction undo = _undoManager.TextBufferUndoHistory.CreateTransaction("Make Prettier")) { edit.Replace(0, _view.TextBuffer.CurrentSnapshot.Length, output); edit.Apply(); undo.Complete(); } return(true); }
/// <summary> /// 调用ZenCoding /// </summary> /// <returns></returns> private bool InvokeZenCoding() { ThreadHelper.ThrowIfNotOnUIThread(); string syntax = string.Empty; //获取用户输入的标签 Span zenSpan = GetSyntaxSpan(out syntax); if (zenSpan.IsEmpty || _view.Selection.SelectedSpans[0].Length > 0 || !IsValidTextBuffer()) { return(false); } ////用户输入html语法标签 //string syntax1 = _view.TextBuffer.CurrentSnapshot.GetText(zenSpan); var parser = new Parser(); string result = parser.Parse(syntax, ZenType.HTML); if (!string.IsNullOrEmpty(result)) { //撤销事务使用 using (ITextUndoTransaction undo = _undoManager.TextBufferUndoHistory.CreateTransaction("ZenCoding")) { ITextSelection selection = UpdateTextBuffer(zenSpan, result); var newSpan = new Span(zenSpan.Start, selection.SelectedSpans[0].Length); //选区内容格式化命令 EditorExtensions.EditorExtensionsPackage.DTE.ExecuteCommand("Edit.FormatSelection"); SetCaret(newSpan, false); selection.Clear(); undo.Complete(); } return(true); } return(false); }
public void Dispose() { IREditorDocument document = REditorDocument.TryFromTextBuffer(_textBuffer); bool changed = true; if (document != null) { changed = document.EndMassiveChange(); } if (!changed) { _transaction.Cancel(); } else { _transaction.AddUndo(new EndMassiveChangeUndoUnit(_textBuffer)); _transaction.Complete(); } _transaction.Dispose(); }
public void Dispose() { var document = _textBuffer.GetEditorDocument <IREditorDocument>(); var changed = true; if (document != null) { changed = document.EndMassiveChange(); } if (!changed) { _transaction.Cancel(); } else { _transaction.AddUndo(new EndMassiveChangeUndoUnit(_textBuffer)); _transaction.Complete(); } _transaction.Dispose(); }
public void PreBackspace(out bool handledCommand) { handledCommand = false; SnapshotPoint?caretPos = CaretPosition; ITextSnapshot snapshot = SubjectBuffer.CurrentSnapshot; if (caretPos.HasValue && caretPos.Value.Position > 0 && (caretPos.Value.Position - 1) == _openingPoint.GetPoint(snapshot).Position && !HasForwardTyping) { using (ITextUndoTransaction undo = CreateUndoTransaction()) { using (ITextEdit edit = SubjectBuffer.CreateEdit()) { SnapshotSpan span = new SnapshotSpan(_openingPoint.GetPoint(snapshot), _closingPoint.GetPoint(snapshot)); edit.Delete(span); if (edit.HasFailedChanges) { edit.Cancel(); undo.Cancel(); Debug.Fail("Unable to clear braces"); // just let this backspace proceed normally } else { // handle the command so the backspace does // not go through since we've already cleared the braces handledCommand = true; edit.Apply(); undo.Complete(); EndSession(); } } } } }
private async Task <bool> MakePrettier() { string input = _view.TextBuffer.CurrentSnapshot.GetText(); string output = await _node.ExecuteProcess(input, _encoding); if (string.IsNullOrEmpty(output) || input == output) { return(false); } using (ITextEdit edit = _view.TextBuffer.CreateEdit()) using (ITextUndoTransaction undo = _undoManager.TextBufferUndoHistory.CreateTransaction("Make Prettier")) { edit.Replace(0, _view.TextBuffer.CurrentSnapshot.Length, output); edit.Apply(); var dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(DTE)); dte.ExecuteCommand("Edit.FormatDocument"); undo.Complete(); } return(true); }
public void Commit() { _transaction.Complete(); }
private void Start(CancellationToken cancellationToken) { // this is where the caret should go after the change SnapshotPoint pos = TextView.Caret.Position.BufferPosition; ITrackingPoint beforeTrackingPoint = pos.Snapshot.CreateTrackingPoint(pos.Position, PointTrackingMode.Negative); ITextSnapshot snapshot = SubjectBuffer.CurrentSnapshot; SnapshotPoint closingSnapshotPoint = ClosingPoint.GetPoint(snapshot); if (closingSnapshotPoint.Position < 1) { Debug.Fail("The closing point was not found at the expected position."); EndSession(); return; } SnapshotPoint openingSnapshotPoint = closingSnapshotPoint.Subtract(1); if (openingSnapshotPoint.GetChar() != OpeningBrace) { // there is a bug in editor brace completion engine on projection buffer that already fixed in vs_pro. until that is FIed to use // I will make this not to assert // Debug.Fail("The opening brace was not found at the expected position."); EndSession(); return; } OpeningPoint = snapshot.CreateTrackingPoint(openingSnapshotPoint, PointTrackingMode.Positive); var document = snapshot.GetOpenDocumentInCurrentContextWithChanges(); if (!_session.CheckOpeningPoint(this, cancellationToken)) { EndSession(); return; } using (ITextUndoTransaction undo = CreateUndoTransaction()) { // insert the closing brace using (ITextEdit edit = SubjectBuffer.CreateEdit()) { edit.Insert(closingSnapshotPoint, ClosingBrace.ToString()); if (edit.HasFailedChanges) { Debug.Fail("Unable to insert closing brace"); // exit without setting the closing point which will take us off the stack edit.Cancel(); undo.Cancel(); return; } else { snapshot = edit.Apply(); } } SnapshotPoint beforePoint = beforeTrackingPoint.GetPoint(TextView.TextSnapshot); // switch from positive to negative tracking so it stays against the closing brace ClosingPoint = SubjectBuffer.CurrentSnapshot.CreateTrackingPoint(ClosingPoint.GetPoint(snapshot), PointTrackingMode.Negative); Debug.Assert(ClosingPoint.GetPoint(snapshot).Position > 0 && (new SnapshotSpan(ClosingPoint.GetPoint(snapshot).Subtract(1), 1)) .GetText().Equals(ClosingBrace.ToString()), "The closing point does not match the closing brace character"); // move the caret back between the braces TextView.Caret.MoveTo(beforePoint); _session.AfterStart(this, cancellationToken); undo.Complete(); } }