Skip to content

Instantly share code, notes, and snippets.

@mkrueger
Created October 26, 2011 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkrueger/1315718 to your computer and use it in GitHub Desktop.
Save mkrueger/1315718 to your computer and use it in GitHub Desktop.
@@ -369,14 +369,14 @@
if (pos < 0)
return;
- editor.Document.BeginAtomicUndo ();
- var oldCaret = editor.Caret.Offset;
-
- var inserted = editor.Insert (pos, editor.EolMarker + directive.ToString ());
- if (preserveCaretPosition) {
- editor.Caret.Offset = (pos < oldCaret)? oldCaret + inserted : oldCaret;
+ using (var undo = editor.OpenUndoGroup ()) {
+ var oldCaret = editor.Caret.Offset;
+
+ var inserted = editor.Insert (pos, editor.EolMarker + directive.ToString ());
+ if (preserveCaretPosition) {
+ editor.Caret.Offset = (pos < oldCaret)? oldCaret + inserted : oldCaret;
+ }
}
- editor.Document.EndAtomicUndo ();
}
DirectiveNode GetRegisterInsertionPointNode ()
@@ -77,11 +77,11 @@
{
//insert the method name
MonoDevelop.Ide.Gui.Content.IEditableTextBuffer buf = window.CompletionWidget as MonoDevelop.Ide.Gui.Content.IEditableTextBuffer;
- if (buf != null) {
- buf.BeginAtomicUndo ();
- buf.DeleteText (window.CodeCompletionContext.TriggerOffset, buf.CursorPosition - window.CodeCompletionContext.TriggerOffset);
- buf.InsertText (buf.CursorPosition, methodInfo.Name);
- buf.EndAtomicUndo ();
+ if (buf != null) {
+ using (var undo = buf.OpenUndoGroup ()) {
+ buf.DeleteText (window.CodeCompletionContext.TriggerOffset, buf.CursorPosition - window.CodeCompletionContext.TriggerOffset);
+ buf.InsertText (buf.CursorPosition, methodInfo.Name);
+ }
}
//generate the codebehind method
@@ -72,21 +72,20 @@
{
MonoDevelop.Ide.Gui.Content.IEditableTextBuffer buf = window.CompletionWidget as MonoDevelop.Ide.Gui.Content.IEditableTextBuffer;
if (buf != null) {
- buf.BeginAtomicUndo ();
-
- int deleteStartOffset = window.CodeCompletionContext.TriggerOffset;
- if (text.StartsWith (docTypeStart)) {
- int start = window.CodeCompletionContext.TriggerOffset - docTypeStart.Length;
- if (start >= 0) {
- string readback = buf.GetText (start, window.CodeCompletionContext.TriggerOffset);
- if (string.Compare (readback, docTypeStart, StringComparison.OrdinalIgnoreCase) == 0)
- deleteStartOffset -= docTypeStart.Length;
+ using (var undo = buf.OpenUndoGroup ()) {
+ int deleteStartOffset = window.CodeCompletionContext.TriggerOffset;
+ if (text.StartsWith (docTypeStart)) {
+ int start = window.CodeCompletionContext.TriggerOffset - docTypeStart.Length;
+ if (start >= 0) {
+ string readback = buf.GetText (start, window.CodeCompletionContext.TriggerOffset);
+ if (string.Compare (readback, docTypeStart, StringComparison.OrdinalIgnoreCase) == 0)
+ deleteStartOffset -= docTypeStart.Length;
+ }
}
- }
-
- buf.DeleteText (deleteStartOffset, buf.CursorPosition - deleteStartOffset);
- buf.InsertText (buf.CursorPosition, text);
- buf.EndAtomicUndo ();
+
+ buf.DeleteText (deleteStartOffset, buf.CursorPosition - deleteStartOffset);
+ buf.InsertText (buf.CursorPosition, text);
+ }
}
}
}
@@ -416,8 +416,6 @@
string indent = textEditorData.Document.GetLineIndent (completionContext.TriggerLine);
AppendSummary (generatedComment, indent, out newCursorOffset);
}
- textEditorData.Document.EndAtomicUndo ();
- textEditorData.Document.BeginAtomicUndo ();
textEditorData.Insert (cursor, generatedComment.ToString ());
textEditorData.Caret.Offset = cursor + newCursorOffset;
return null;
@@ -148,11 +148,8 @@
var changes = new List<ICSharpCode.NRefactory.CSharp.Refactoring.Action> ();
changes.AddRange (formattingVisitor.Changes.
Where (c => (startOffset <= c.Offset && c.Offset < endOffset)));
- try {
- data.Document.BeginAtomicUndo ();
+ using (var undo = data.OpenUndoGroup ()) {
MDRefactoringContext.MdScript.RunActions (changes, null);
- } finally {
- data.Document.EndAtomicUndo ();
}
}
@@ -211,14 +211,12 @@
int guessedOffset = GuessSemicolonInsertionOffset (textEditorData, curLine);
if (guessedOffset != textEditorData.Caret.Offset) {
- if (textEditorData.Document.IsInAtomicUndo) {
- textEditorData.Document.EndAtomicUndo ();
- textEditorData.Document.BeginAtomicUndo ();
+ using (var undo = textEditorData.OpenUndoGroup ()) {
+ textEditorData.Remove (textEditorData.Caret.Offset - 1, 1);
+ textEditorData.Caret.Offset = guessedOffset;
+ lastInsertedSemicolon = textEditorData.Caret.Offset + 1;
+ retval = base.KeyPress (key, keyChar, modifier);
}
- textEditorData.Remove (textEditorData.Caret.Offset - 1, 1);
- textEditorData.Caret.Offset = guessedOffset;
- lastInsertedSemicolon = textEditorData.Caret.Offset + 1;
- retval = base.KeyPress (key, keyChar, modifier);
}
return retval;
}
@@ -127,14 +127,12 @@
var lastOffset = data.Editor.Caret.Offset;
changes.RemoveAll (c => ((TextReplaceAction)c).Offset > lastOffset);
}
- try {
- data.Editor.Document.BeginAtomicUndo ();
+
+ using (var undo = data.Editor.OpenUndoGroup ()) {
MDRefactoringContext.MdScript.RunActions (changes, null);
foreach (int line in lines)
data.Editor.Document.CommitLineUpdate (line);
- } finally {
- data.Editor.Document.EndAtomicUndo ();
}
stubData.Dispose ();
}
@@ -94,9 +94,9 @@
}
}
docs.Sort ((a, b) => b.Key.CompareTo (a.Key));
- data.Document.BeginAtomicUndo ();
- docs.ForEach (doc => data.Insert (doc.Key, doc.Value));
- data.Document.EndAtomicUndo ();
+ using (var undo = data.OpenUndoGroup ()) {
+ docs.ForEach (doc => data.Insert (doc.Key, doc.Value));
+ }
}
static bool NeedsDocumentation (TextEditorData data, IMember member)
@@ -80,16 +80,9 @@
int offset = textEditorData.Caret.Offset;
- bool wasInAtomicUndo = textEditorData.Document.IsInAtomicUndo;
- if (wasInAtomicUndo)
- textEditorData.Document.EndAtomicUndo ();
-
int insertedLength = textEditorData.Insert (offset, documentationEmpty);
// important to set the caret position here for the undo step
textEditorData.Caret.Offset = offset + insertedLength;
-
- if (wasInAtomicUndo)
- textEditorData.Document.BeginAtomicUndo ();
insertedLength = textEditorData.Replace (offset, insertedLength, documentation);
textEditorData.Caret.Offset = offset + insertedLength;
@@ -89,14 +89,9 @@
poEditorWidget.Redo ();
}
- void IUndoHandler.BeginAtomicUndo ()
+ IDisposable IUndoHandler.OpenUndoGroup ()
{
- poEditorWidget.BeginAtomicUndo ();
- }
-
- void IUndoHandler.EndAtomicUndo ()
- {
- poEditorWidget.EndAtomicUndo ();
+ return poEditorWidget.OpenUndoGroup ();
}
bool IUndoHandler.EnableUndo {
@@ -1267,12 +1267,16 @@
undoStack.Push (change);
}
- public void BeginAtomicUndo ()
+ class DisposeStub : IDisposable
{
+ public void Dispose ()
+ {
+ }
}
- public void EndAtomicUndo ()
+ public IDisposable OpenUndoGroup ()
{
+ return new DisposeStub ();
}
public bool EnableUndo {
@@ -94,18 +94,31 @@
hexEditor.HexEditorData.Redo ();
}
-
- void IUndoHandler.BeginAtomicUndo ()
+ class UndoGroup : IDisposable
{
- hexEditor.HexEditorData.BeginAtomicUndo ();
- }
-
+ HexEditorData data;
+
+ public UndoGroup (HexEditorData data)
+ {
+ if (data == null)
+ throw new ArgumentNullException ("data");
+ this.data = data;
+ data.BeginAtomicUndo ();
+ }
+
+ public void Dispose ()
+ {
+ if (data != null) {
+ data.EndAtomicUndo ();
+ data = null;
+ }
+ }
+ }
- void IUndoHandler.EndAtomicUndo ()
+ IDisposable IUndoHandler.OpenUndoGroup ()
{
- hexEditor.HexEditorData.EndAtomicUndo ();
+ return new UndoGroup (hexEditor.HexEditorData);
}
-
bool IUndoHandler.EnableUndo {
get {
@@ -86,13 +86,16 @@
}
static List<TextEditorData> textEditorDatas = new List<TextEditorData> ();
+ static List<IDisposable> undoGroups = new List<IDisposable> ();
+
public static void FinishRefactoringOperation ()
{
foreach (TextEditorData data in textEditorDatas) {
- data.Document.EndAtomicUndo ();
data.Document.CommitUpdateAll ();
}
textEditorDatas.Clear ();
+ undoGroups.ForEach (grp => grp.Dispose ());
+ undoGroups.Clear ();
}
internal static TextEditorData GetTextEditorData (string fileName)
@@ -104,7 +107,7 @@
TextEditorData result = doc.Editor;
if (result != null) {
textEditorDatas.Add (result);
- result.Document.BeginAtomicUndo ();
+ undoGroups.Add (result.OpenUndoGroup ());
return result;
}
}
@@ -380,6 +380,7 @@
skipChar = null;
}
char insertionChar = '\0';
+ IDisposable undoGroup = null;
if (skipChar == null && Options.AutoInsertMatchingBracket && braceIndex >= 0) {
if (!inStringOrComment) {
char closingBrace = closingBrackets [braceIndex];
@@ -396,7 +397,7 @@
if (count >= 0) {
startedAtomicOperation = true;
- Document.BeginAtomicUndo ();
+ undoGroup = Document.OpenUndoGroup ();
GetTextEditorData ().EnsureCaretIsNotVirtual ();
int offset = Caret.Offset;
@@ -409,7 +410,7 @@
char charBefore = Document.GetCharAt (Caret.Offset - 1);
if (!inString && !inComment && !inChar && ch == '"' && charBefore != '\\') {
startedAtomicOperation = true;
- Document.BeginAtomicUndo ();
+ undoGroup = Document.OpenUndoGroup ();
GetTextEditorData ().EnsureCaretIsNotVirtual ();
insertionChar = '"';
int offset = Caret.Offset;
@@ -436,8 +437,8 @@
HitReturn ();
}
}
- if (startedAtomicOperation)
- Document.EndAtomicUndo ();
+ if (undoGroup != null)
+ undoGroup.Dispose ();
return templateInserted || result;
}
@@ -667,39 +668,39 @@
internal void InsertTemplate (CodeTemplate template, MonoDevelop.Ide.Gui.Document document)
{
- Document.BeginAtomicUndo ();
- var result = template.InsertTemplateContents (document);
- var tle = new TextLinkEditMode (this, result.InsertPosition, result.TextLinks);
-
- if (PropertyService.Get ("OnTheFlyFormatting", false)) {
- var prettyPrinter = CodeFormatterService.GetFormatter (Document.MimeType);
- if (prettyPrinter != null) {
- int endOffset = result.InsertPosition + result.Code.Length;
- string oldText = Document.GetTextAt (result.InsertPosition, result.Code.Length);
- var policies = document.Project != null ? document.Project.Policies : null;
- string text = prettyPrinter.FormatText (policies, Document.Text, result.InsertPosition, endOffset);
-
- if (text != null)
- Replace (result.InsertPosition, result.Code.Length, text);
- else
- //if formatting failed, just use the unformatted text
- text = oldText;
-
- Caret.Offset = result.InsertPosition + TranslateOffset (oldText, text, Caret.Offset - result.InsertPosition);
- foreach (TextLink textLink in tle.Links) {
- foreach (ISegment segment in textLink.Links) {
- segment.Offset = TranslateOffset (oldText, text, segment.Offset);
+ using (var undo = Document.OpenUndoGroup ()) {
+ var result = template.InsertTemplateContents (document);
+ var tle = new TextLinkEditMode (this, result.InsertPosition, result.TextLinks);
+
+ if (PropertyService.Get ("OnTheFlyFormatting", false)) {
+ var prettyPrinter = CodeFormatterService.GetFormatter (Document.MimeType);
+ if (prettyPrinter != null) {
+ int endOffset = result.InsertPosition + result.Code.Length;
+ string oldText = Document.GetTextAt (result.InsertPosition, result.Code.Length);
+ var policies = document.Project != null ? document.Project.Policies : null;
+ string text = prettyPrinter.FormatText (policies, Document.Text, result.InsertPosition, endOffset);
+
+ if (text != null)
+ Replace (result.InsertPosition, result.Code.Length, text);
+ else
+ //if formatting failed, just use the unformatted text
+ text = oldText;
+
+ Caret.Offset = result.InsertPosition + TranslateOffset (oldText, text, Caret.Offset - result.InsertPosition);
+ foreach (TextLink textLink in tle.Links) {
+ foreach (ISegment segment in textLink.Links) {
+ segment.Offset = TranslateOffset (oldText, text, segment.Offset);
+ }
}
}
}
- }
-
- if (tle.ShouldStartTextLinkMode) {
- tle.OldMode = CurrentMode;
- tle.StartMode ();
- CurrentMode = tle;
+
+ if (tle.ShouldStartTextLinkMode) {
+ tle.OldMode = CurrentMode;
+ tle.StartMode ();
+ CurrentMode = tle;
+ }
}
- Document.EndAtomicUndo ();
}
static int TranslateOffset (string baseInput, string formattedInput, int offset)
@@ -1066,11 +1067,8 @@
[CommandHandler (MonoDevelop.Ide.Commands.EditCommands.JoinWithNextLine)]
internal void JoinLines ()
{
- try {
- Document.BeginAtomicUndo ();
+ using (var undo = Document.OpenUndoGroup ()) {
RunAction (Mono.TextEditor.Vi.ViActions.Join);
- } finally {
- Document.EndAtomicUndo ();
}
}
@@ -307,31 +307,31 @@
DisposeErrorMarkers (); // disposes messageBubbleCache as well.
if (IdeApp.Preferences.ShowMessageBubbles == ShowMessageBubbles.Never)
return;
- widget.Document.BeginAtomicUndo ();
- if (messageBubbleCache != null)
- messageBubbleCache.Dispose ();
- messageBubbleCache = new MessageBubbleCache (widget.TextEditor);
-
- foreach (Task task in tasks) {
- if (task.Severity == TaskSeverity.Error || task.Severity == TaskSeverity.Warning) {
- if (IdeApp.Preferences.ShowMessageBubbles == ShowMessageBubbles.ForErrors && task.Severity == TaskSeverity.Warning)
- continue;
- LineSegment lineSegment = widget.Document.GetLine (task.Line);
- if (lineSegment == null)
- continue;
- var marker = currentErrorMarkers.FirstOrDefault (m => m.LineSegment == lineSegment);
- if (marker != null) {
- marker.AddError (task.Severity == TaskSeverity.Error, task.Description);
- continue;
+ using (var undo = Document.OpenUndoGroup ()) {
+ if (messageBubbleCache != null)
+ messageBubbleCache.Dispose ();
+ messageBubbleCache = new MessageBubbleCache (widget.TextEditor);
+
+ foreach (Task task in tasks) {
+ if (task.Severity == TaskSeverity.Error || task.Severity == TaskSeverity.Warning) {
+ if (IdeApp.Preferences.ShowMessageBubbles == ShowMessageBubbles.ForErrors && task.Severity == TaskSeverity.Warning)
+ continue;
+ LineSegment lineSegment = widget.Document.GetLine (task.Line);
+ if (lineSegment == null)
+ continue;
+ var marker = currentErrorMarkers.FirstOrDefault (m => m.LineSegment == lineSegment);
+ if (marker != null) {
+ marker.AddError (task.Severity == TaskSeverity.Error, task.Description);
+ continue;
+ }
+ MessageBubbleTextMarker errorTextMarker = new MessageBubbleTextMarker (messageBubbleCache, task, lineSegment, task.Severity == TaskSeverity.Error, task.Description);
+ currentErrorMarkers.Add (errorTextMarker);
+
+ errorTextMarker.IsVisible =  !IdeApp.Preferences.DefaultHideMessageBubbles;
+ widget.Document.AddMarker (lineSegment, errorTextMarker, false);
}
- MessageBubbleTextMarker errorTextMarker = new MessageBubbleTextMarker (messageBubbleCache, task, lineSegment, task.Severity == TaskSeverity.Error, task.Description);
- currentErrorMarkers.Add (errorTextMarker);
-
- errorTextMarker.IsVisible =  !IdeApp.Preferences.DefaultHideMessageBubbles;
- widget.Document.AddMarker (lineSegment, errorTextMarker, false);
}
}
- widget.Document.EndAtomicUndo ();
widget.TextEditor.QueueDraw ();
}
@@ -386,10 +386,10 @@
if (PropertyService.Get ("AutoFormatDocumentOnSave", false)) {
var formatter = CodeFormatterService.GetFormatter (Document.MimeType);
if (formatter != null && formatter.SupportsOnTheFlyFormatting) {
- TextEditor.Document.BeginAtomicUndo ();
- var policies = Project != null ? Project.Policies : null;
- formatter.OnTheFlyFormat (policies, TextEditor.GetTextEditorData (), 0, Document.Length);
- TextEditor.Document.EndAtomicUndo ();
+ using (var undo = TextEditor.OpenUndoGroup ()) {
+ var policies = Project != null ? Project.Policies : null;
+ formatter.OnTheFlyFormat (policies, TextEditor.GetTextEditorData (), 0, Document.Length);
+ }
}
}
@@ -1131,15 +1131,12 @@
this.Document.Redo ();
}
- public void BeginAtomicUndo ()
- {
- this.Document.BeginAtomicUndo ();
- }
- public void EndAtomicUndo ()
+
+ public IDisposable OpenUndoGroup ()
{
- this.Document.EndAtomicUndo ();
+ return Document.OpenUndoGroup ();
}
-
+
public string SelectedText {
get {
return TextEditor.IsSomethingSelected ? Document.GetTextAt (TextEditor.SelectionRange) : "";
@@ -1490,26 +1487,26 @@
triggerOffset += data.EnsureCaretIsNotVirtual ();
if (blockMode) {
- data.Document.BeginAtomicUndo ();
+ using (var undo = data.OpenUndoGroup ()) {
- int minLine = data.MainSelection.MinLine;
- int maxLine = data.MainSelection.MaxLine;
- int column = triggerOffset - data.Document.GetLineByOffset (triggerOffset).Offset;
- for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++) {
- LineSegment lineSegment = data.Document.GetLine (lineNumber);
- if (lineSegment == null)
- continue;
- int offset = lineSegment.Offset + column;
- data.Replace (offset, length, complete_word);
+ int minLine = data.MainSelection.MinLine;
+ int maxLine = data.MainSelection.MaxLine;
+ int column = triggerOffset - data.Document.GetLineByOffset (triggerOffset).Offset;
+ for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++) {
+ LineSegment lineSegment = data.Document.GetLine (lineNumber);
+ if (lineSegment == null)
+ continue;
+ int offset = lineSegment.Offset + column;
+ data.Replace (offset, length, complete_word);
+ }
+ data.Caret.Offset = triggerOffset + idx;
+ int minColumn = System.Math.Min (data.MainSelection.Anchor.Column, data.MainSelection.Lead.Column);
+ data.MainSelection.Anchor = new DocumentLocation (data.Caret.Line == minLine ? maxLine : minLine, minColumn);
+ data.MainSelection.Lead = new DocumentLocation (data.Caret.Line, TextEditor.Caret.Column);
+
+ data.Document.CommitMultipleLineUpdate (data.MainSelection.MinLine, data.MainSelection.MaxLine);
+ data.Caret.PreserveSelection = false;
}
- data.Caret.Offset = triggerOffset + idx;
- int minColumn = System.Math.Min (data.MainSelection.Anchor.Column, data.MainSelection.Lead.Column);
- data.MainSelection.Anchor = new DocumentLocation (data.Caret.Line == minLine ? maxLine : minLine, minColumn);
- data.MainSelection.Lead = new DocumentLocation (data.Caret.Line, TextEditor.Caret.Column);
-
- data.Document.CommitMultipleLineUpdate (data.MainSelection.MinLine, data.MainSelection.MaxLine);
- data.Caret.PreserveSelection = false;
- data.Document.EndAtomicUndo ();
} else {
data.Replace (triggerOffset, length, complete_word);
data.Caret.Offset = triggerOffset + idx;
@@ -1909,12 +1906,12 @@
var policies = Project != null ? Project.Policies : null;
var editorData = TextEditor.GetTextEditorData ();
if (TextEditor.IsSomethingSelected) {
- TextEditor.Document.BeginAtomicUndo ();
- int max = TextEditor.MainSelection.MaxLine;
- for (int i = TextEditor.MainSelection.MinLine; i <= max; i++) {
- formatter.CorrectIndenting (policies, editorData, i);
+ using (var undo = TextEditor.OpenUndoGroup ()) {
+ int max = TextEditor.MainSelection.MaxLine;
+ for (int i = TextEditor.MainSelection.MinLine; i <= max; i++) {
+ formatter.CorrectIndenting (policies, editorData, i);
+ }
}
- TextEditor.Document.EndAtomicUndo ();
} else {
formatter.CorrectIndenting (policies, editorData, TextEditor.Caret.Line);
}
@@ -1380,34 +1380,33 @@
string blockStart = blockStarts[0];
string blockEnd = blockEnds[0];
- Document.BeginAtomicUndo ();
- LineSegment startLine;
- LineSegment endLine;
-
- if (TextEditor.IsSomethingSelected) {
- startLine = Document.GetLineByOffset (textEditor.SelectionRange.Offset);
- endLine = Document.GetLineByOffset (textEditor.SelectionRange.EndOffset);
- } else {
- startLine = endLine = Document.GetLine (textEditor.Caret.Line);
- }
- string startLineText = Document.GetTextAt (startLine.Offset, startLine.EditableLength);
- string endLineText = Document.GetTextAt (endLine.Offset, endLine.EditableLength);
- if (startLineText.StartsWith (blockStart) && endLineText.EndsWith (blockEnd)) {
- textEditor.Remove (endLine.Offset + endLine.EditableLength - blockEnd.Length, blockEnd.Length);
- textEditor.Remove (startLine.Offset, blockStart.Length);
+ using (var undo = Document.OpenUndoGroup ()) {
+ LineSegment startLine;
+ LineSegment endLine;
+
if (TextEditor.IsSomethingSelected) {
- TextEditor.SelectionAnchor -= blockEnd.Length;
+ startLine = Document.GetLineByOffset (textEditor.SelectionRange.Offset);
+ endLine = Document.GetLineByOffset (textEditor.SelectionRange.EndOffset);
+ } else {
+ startLine = endLine = Document.GetLine (textEditor.Caret.Line);
}
- } else {
- textEditor.Insert (endLine.Offset + endLine.EditableLength, blockEnd);
- textEditor.Insert (startLine.Offset, blockStart);
- if (TextEditor.IsSomethingSelected) {
- TextEditor.SelectionAnchor += blockEnd.Length;
+ string startLineText = Document.GetTextAt (startLine.Offset, startLine.EditableLength);
+ string endLineText = Document.GetTextAt (endLine.Offset, endLine.EditableLength);
+ if (startLineText.StartsWith (blockStart) && endLineText.EndsWith (blockEnd)) {
+ textEditor.Remove (endLine.Offset + endLine.EditableLength - blockEnd.Length, blockEnd.Length);
+ textEditor.Remove (startLine.Offset, blockStart.Length);
+ if (TextEditor.IsSomethingSelected) {
+ TextEditor.SelectionAnchor -= blockEnd.Length;
+ }
+ } else {
+ textEditor.Insert (endLine.Offset + endLine.EditableLength, blockEnd);
+ textEditor.Insert (startLine.Offset, blockStart);
+ if (TextEditor.IsSomethingSelected) {
+ TextEditor.SelectionAnchor += blockEnd.Length;
+ }
+
}
-
}
-
- Document.EndAtomicUndo ();
}
public void ToggleCodeComment ()
@@ -1470,32 +1469,32 @@
LineSegment anchorLine = TextEditor.IsSomethingSelected ? TextEditor.Document.GetLineByOffset (TextEditor.SelectionAnchor) : null;
int anchorColumn = TextEditor.IsSomethingSelected ? TextEditor.SelectionAnchor - anchorLine.Offset : -1;
- Document.BeginAtomicUndo ();
- foreach (LineSegment line in TextEditor.SelectedLines) {
- TextEditor.Insert (line.Offset, commentTag);
- }
- if (TextEditor.IsSomethingSelected) {
- if (TextEditor.SelectionAnchor < TextEditor.Caret.Offset) {
- if (anchorColumn != 0)
- TextEditor.SelectionAnchor = System.Math.Min (anchorLine.Offset + anchorLine.EditableLength, System.Math.Max (anchorLine.Offset, TextEditor.SelectionAnchor + commentTag.Length));
- } else {
- if (anchorColumn != 0) {
- TextEditor.SelectionAnchor = System.Math.Min (anchorLine.Offset + anchorLine.EditableLength, System.Math.Max (anchorLine.Offset, anchorLine.Offset + anchorColumn + commentTag.Length));
+ using (var undo = Document.OpenUndoGroup ()) {
+ foreach (LineSegment line in TextEditor.SelectedLines) {
+ TextEditor.Insert (line.Offset, commentTag);
+ }
+ if (TextEditor.IsSomethingSelected) {
+ if (TextEditor.SelectionAnchor < TextEditor.Caret.Offset) {
+ if (anchorColumn != 0)
+ TextEditor.SelectionAnchor = System.Math.Min (anchorLine.Offset + anchorLine.EditableLength, System.Math.Max (anchorLine.Offset, TextEditor.SelectionAnchor + commentTag.Length));
} else {
-// TextEditor.SelectionAnchor = anchorLine.Offset;
+ if (anchorColumn != 0) {
+ TextEditor.SelectionAnchor = System.Math.Min (anchorLine.Offset + anchorLine.EditableLength, System.Math.Max (anchorLine.Offset, anchorLine.Offset + anchorColumn + commentTag.Length));
+ } else {
+ // TextEditor.SelectionAnchor = anchorLine.Offset;
+ }
}
+ }
+
+ if (TextEditor.Caret.Column != 0) {
+ TextEditor.Caret.PreserveSelection = true;
+ TextEditor.Caret.Column += commentTag.Length;
+ TextEditor.Caret.PreserveSelection = false;
}
- }
-
- if (TextEditor.Caret.Column != 0) {
- TextEditor.Caret.PreserveSelection = true;
- TextEditor.Caret.Column += commentTag.Length;
- TextEditor.Caret.PreserveSelection = false;
+
+ if (TextEditor.IsSomethingSelected)
+ TextEditor.ExtendSelectionTo (TextEditor.Caret.Offset);
}
-
- if (TextEditor.IsSomethingSelected)
- TextEditor.ExtendSelectionTo (TextEditor.Caret.Offset);
- Document.EndAtomicUndo ();
Document.CommitMultipleLineUpdate (startLineNr, endLineNr);
}
@@ -1508,40 +1507,39 @@
LineSegment anchorLine = TextEditor.IsSomethingSelected ? TextEditor.Document.GetLineByOffset (TextEditor.SelectionAnchor) : null;
int anchorColumn = TextEditor.IsSomethingSelected ? TextEditor.SelectionAnchor - anchorLine.Offset : -1;
- Document.BeginAtomicUndo ();
- int first = -1;
- int last = 0;
- foreach (LineSegment line in TextEditor.SelectedLines) {
- string text = Document.GetTextAt (line);
- string trimmedText = text.TrimStart ();
- int length = 0;
- if (trimmedText.StartsWith (commentTag)) {
- TextEditor.Remove (line.Offset + (text.Length - trimmedText.Length), commentTag.Length);
- length = commentTag.Length;
+ using (var undo = Document.OpenUndoGroup ()) {
+ int first = -1;
+ int last = 0;
+ foreach (LineSegment line in TextEditor.SelectedLines) {
+ string text = Document.GetTextAt (line);
+ string trimmedText = text.TrimStart ();
+ int length = 0;
+ if (trimmedText.StartsWith (commentTag)) {
+ TextEditor.Remove (line.Offset + (text.Length - trimmedText.Length), commentTag.Length);
+ length = commentTag.Length;
+ }
+ last = length;
+ if (first < 0)
+ first = last;
+ }
+
+ if (TextEditor.IsSomethingSelected) {
+ if (TextEditor.SelectionAnchor < TextEditor.Caret.Offset) {
+ TextEditor.SelectionAnchor = System.Math.Min (anchorLine.Offset + anchorLine.EditableLength, System.Math.Max (anchorLine.Offset, TextEditor.SelectionAnchor - first));
+ } else {
+ TextEditor.SelectionAnchor = System.Math.Min (anchorLine.Offset + anchorLine.EditableLength, System.Math.Max (anchorLine.Offset, anchorLine.Offset + anchorColumn - last));
+ }
}
- last = length;
- if (first < 0)
- first = last;
- }
-
- if (TextEditor.IsSomethingSelected) {
- if (TextEditor.SelectionAnchor < TextEditor.Caret.Offset) {
- TextEditor.SelectionAnchor = System.Math.Min (anchorLine.Offset + anchorLine.EditableLength, System.Math.Max (anchorLine.Offset, TextEditor.SelectionAnchor - first));
- } else {
- TextEditor.SelectionAnchor = System.Math.Min (anchorLine.Offset + anchorLine.EditableLength, System.Math.Max (anchorLine.Offset, anchorLine.Offset + anchorColumn - last));
+
+ if (TextEditor.Caret.Column != DocumentLocation.MinColumn) {
+ TextEditor.Caret.PreserveSelection = true;
+ TextEditor.Caret.Column = System.Math.Max (DocumentLocation.MinColumn, TextEditor.Caret.Column - last);
+ TextEditor.Caret.PreserveSelection = false;
}
- }
-
- if (TextEditor.Caret.Column != DocumentLocation.MinColumn) {
- TextEditor.Caret.PreserveSelection = true;
- TextEditor.Caret.Column = System.Math.Max (DocumentLocation.MinColumn, TextEditor.Caret.Column - last);
- TextEditor.Caret.PreserveSelection = false;
+
+ if (TextEditor.IsSomethingSelected)
+ TextEditor.ExtendSelectionTo (TextEditor.Caret.Offset);
}
-
- if (TextEditor.IsSomethingSelected)
- TextEditor.ExtendSelectionTo (TextEditor.Caret.Offset);
-
- Document.EndAtomicUndo ();
Document.CommitMultipleLineUpdate (startLineNr, endLineNr);
}
@@ -70,13 +70,13 @@
{
IEditableTextBuffer buf = window.CompletionWidget as IEditableTextBuffer;
if (buf != null) {
- buf.BeginAtomicUndo ();
- buf.InsertText (buf.CursorPosition, element);
-
- // Move caret into the middle of the tags
- buf.CursorPosition = window.CodeCompletionContext.TriggerOffset + cursorOffset;
- buf.Select (buf.CursorPosition, buf.CursorPosition);
- buf.EndAtomicUndo ();
+ using (var undo = buf.OpenUndoGroup ()) {
+ buf.InsertText (buf.CursorPosition, element);
+
+ // Move caret into the middle of the tags
+ buf.CursorPosition = window.CodeCompletionContext.TriggerOffset + cursorOffset;
+ buf.Select (buf.CursorPosition, buf.CursorPosition);
+ }
}
}
}
@@ -186,10 +186,10 @@
if (newCaretOffset > seg.Offset) {
newCaretOffset += (indent.Length - oldIndent.Length);
}
- Editor.Document.BeginAtomicUndo ();
- Editor.Replace (seg.Offset, oldIndent.Length, indent);
- Editor.Caret.Offset = newCaretOffset;
- Editor.Document.EndAtomicUndo ();
+ using (var undo = Editor.OpenUndoGroup ()) {
+ Editor.Replace (seg.Offset, oldIndent.Length, indent);
+ Editor.Caret.Offset = newCaretOffset;
+ }
}
}
return ret;
@@ -245,19 +245,19 @@
string tag = String.Concat ("</", el.Name.FullName, ">");
if (XmlEditorOptions.AutoCompleteElements) {
- //make sure we have a clean atomic undo so the user can undo the tag insertion
- //independently of the >
- bool wasInAtomicUndo = this.Editor.Document.IsInAtomicUndo;
- if (wasInAtomicUndo)
- this.Editor.Document.EndAtomicUndo ();
+// //make sure we have a clean atomic undo so the user can undo the tag insertion
+// //independently of the >
+// bool wasInAtomicUndo = this.Editor.Document.IsInAtomicUndo;
+// if (wasInAtomicUndo)
+// this.Editor.Document.EndAtomicUndo ();
- buf.BeginAtomicUndo ();
- buf.InsertText (buf.CursorPosition, tag);
- buf.CursorPosition -= tag.Length;
- buf.EndAtomicUndo ();
+ using (var undo = buf.OpenUndoGroup ()) {
+ buf.InsertText (buf.CursorPosition, tag);
+ buf.CursorPosition -= tag.Length;
+ }
- if (wasInAtomicUndo)
- this.Editor.Document.BeginAtomicUndo ();
+// if (wasInAtomicUndo)
+// this.Editor.Document.BeginAtomicUndo ();
return null;
} else {
@@ -272,9 +272,7 @@
// Auto insert '>' when '/' is typed inside tag state (for quick tag closing)
//FIXME: avoid doing this when the next non-whitespace char is ">" or ignore the next ">" typed
if (XmlEditorOptions.AutoInsertFragments && tracker.Engine.CurrentState is XmlTagState && currentChar == '/') {
- buf.BeginAtomicUndo ();
buf.InsertText (buf.CursorPosition, ">");
- buf.EndAtomicUndo ();
return null;
}
@@ -700,14 +700,14 @@
internal void FormatDocument ()
{
var formatter = CodeFormatterService.GetFormatter (TextXmlMimeType);
- Editor.Document.BeginAtomicUndo ();
- var loc = Editor.Caret.Location;
- var text = formatter.FormatText (Document.Project != null ? Document.Project.Policies : null, Editor.Text);
- if (text != null) {
- Editor.Replace (0, Editor.Length, text);
- Editor.Caret.Location = loc;
+ using (var undo = Editor.OpenUndoGroup ()) {
+ var loc = Editor.Caret.Location;
+ var text = formatter.FormatText (Document.Project != null ? Document.Project.Policies : null, Editor.Text);
+ if (text != null) {
+ Editor.Replace (0, Editor.Length, text);
+ Editor.Caret.Location = loc;
+ }
}
- Editor.Document.EndAtomicUndo ();
}
string GetFileContent (string fileName)
@@ -93,15 +93,10 @@
{
this.widget.Editor.Document.Redo ();
}
-
- void IUndoHandler.BeginAtomicUndo ()
+
+ IDisposable IUndoHandler.OpenUndoGroup ()
{
- this.widget.Editor.Document.BeginAtomicUndo ();
- }
-
- void IUndoHandler.EndAtomicUndo ()
- {
- this.widget.Editor.Document.EndAtomicUndo ();
+ return this.widget.Editor.OpenUndoGroup ();
}
bool IUndoHandler.EnableUndo {
@@ -156,14 +156,9 @@
this.ComparisonWidget.OriginalEditor.Document.Redo ();
}
- void IUndoHandler.BeginAtomicUndo ()
+ IDisposable IUndoHandler.OpenUndoGroup ()
{
- this.ComparisonWidget.OriginalEditor.Document.BeginAtomicUndo ();
- }
-
- void IUndoHandler.EndAtomicUndo ()
- {
- this.ComparisonWidget.OriginalEditor.Document.EndAtomicUndo ();
+ return this.ComparisonWidget.OriginalEditor.OpenUndoGroup ();
}
bool IUndoHandler.EnableUndo {
@@ -658,23 +658,22 @@
protected virtual void UndoChange (TextEditor fromEditor, TextEditor toEditor, Hunk hunk)
{
- toEditor.Document.BeginAtomicUndo ();
- var start = toEditor.Document.GetLine (hunk.InsertStart);
- int toOffset = start != null ? start.Offset : toEditor.Document.Length;
- if (start != null && hunk.Inserted > 0) {
- int line = Math.Min (hunk.InsertStart + hunk.Inserted - 1, toEditor.Document.LineCount);
- var end = toEditor.Document.GetLine (line);
- toEditor.Remove (start.Offset, end.EndOffset - start.Offset);
- }
-
- if (hunk.Removed > 0) {
- start = fromEditor.Document.GetLine (Math.Min (hunk.RemoveStart, fromEditor.Document.LineCount));
- int line = Math.Min (hunk.RemoveStart + hunk.Removed - 1, fromEditor.Document.LineCount);
- var end = fromEditor.Document.GetLine (line);
- toEditor.Insert (toOffset, start.Offset == end.EndOffset ? toEditor.EolMarker : fromEditor.Document.GetTextBetween (start.Offset, end.EndOffset));
+ using (var undo = toEditor.OpenUndoGroup ()) {
+ var start = toEditor.Document.GetLine (hunk.InsertStart);
+ int toOffset = start != null ? start.Offset : toEditor.Document.Length;
+ if (start != null && hunk.Inserted > 0) {
+ int line = Math.Min (hunk.InsertStart + hunk.Inserted - 1, toEditor.Document.LineCount);
+ var end = toEditor.Document.GetLine (line);
+ toEditor.Remove (start.Offset, end.EndOffset - start.Offset);
+ }
+
+ if (hunk.Removed > 0) {
+ start = fromEditor.Document.GetLine (Math.Min (hunk.RemoveStart, fromEditor.Document.LineCount));
+ int line = Math.Min (hunk.RemoveStart + hunk.Removed - 1, fromEditor.Document.LineCount);
+ var end = fromEditor.Document.GetLine (line);
+ toEditor.Insert (toOffset, start.Offset == end.EndOffset ? toEditor.EolMarker : fromEditor.Document.GetTextBetween (start.Offset, end.EndOffset));
+ }
}
-
- toEditor.Document.EndAtomicUndo ();
}
class MiddleArea : DrawingArea
@@ -826,7 +826,33 @@
}
}
- public void BeginAtomicUndo ()
+ class UndoGroup : IDisposable
+ {
+ Document doc;
+
+ public UndoGroup (Document doc)
+ {
+ if (doc == null)
+ throw new ArgumentNullException ("doc");
+ this.doc = doc;
+ doc.BeginAtomicUndo ();
+ }
+
+ public void Dispose ()
+ {
+ if (doc != null) {
+ doc.EndAtomicUndo ();
+ doc = null;
+ }
+ }
+ }
+
+ public IDisposable OpenUndoGroup()
+ {
+ return new UndoGroup (this);
+ }
+
+ internal void BeginAtomicUndo ()
{
if (atomicUndoLevel == 0) {
if (this.syntaxMode != null && !SuppressHighlightUpdate)
@@ -840,7 +866,7 @@
atomicUndoLevel++;
}
- public void EndAtomicUndo ()
+ internal void EndAtomicUndo ()
{
if (atomicUndoLevel <= 0)
throw new InvalidOperationException ("There is no atomic undo operation running.");
@@ -1865,6 +1865,11 @@
{
return Document.OffsetToLineNumber (offset);
}
+
+ public IDisposable OpenUndoGroup()
+ {
+ return Document.OpenUndoGroup ();
+ }
#endregion
#region Search & Replace
@@ -1064,6 +1064,11 @@
{
return Document.OffsetToLineNumber (offset);
}
+
+ public IDisposable OpenUndoGroup()
+ {
+ return Document.OpenUndoGroup ();
+ }
#endregion
#region Parent functions
@@ -59,15 +59,15 @@
var formatter = CodeFormatterService.GetFormatter (mt);
if (formatter == null)
return;
- doc.Editor.Document.BeginAtomicUndo ();
- var loc = doc.Editor.Caret.Location;
- var text = formatter.FormatText (doc.Project != null ? doc.Project.Policies : null, doc.Editor.Text);
- if (text != null) {
- doc.Editor.Replace (0, doc.Editor.Length, text);
- doc.Editor.Caret.Location = loc;
- doc.Editor.Caret.CheckCaretPosition ();
+ using (var undo = doc.Editor.OpenUndoGroup ()) {
+ var loc = doc.Editor.Caret.Location;
+ var text = formatter.FormatText (doc.Project != null ? doc.Project.Policies : null, doc.Editor.Text);
+ if (text != null) {
+ doc.Editor.Replace (0, doc.Editor.Length, text);
+ doc.Editor.Caret.Location = loc;
+ doc.Editor.Caret.CheckCaretPosition ();
+ }
}
- doc.Editor.Document.EndAtomicUndo ();
}
}
@@ -97,18 +97,18 @@
return;
var selection = doc.Editor.SelectionRange;
- doc.Editor.Document.BeginAtomicUndo ();
- if (formatter.SupportsOnTheFlyFormatting) {
- formatter.OnTheFlyFormat (doc.Project != null ? doc.Project.Policies : null, doc.Editor, selection.Offset, selection.EndOffset);
- } else {
- var pol = doc.Project != null ? doc.Project.Policies : null;
- string text = formatter.FormatText (pol, doc.Editor.Text, selection.Offset, selection.EndOffset);
- if (text != null) {
- doc.Editor.Replace (selection.Offset, selection.Length, text);
- doc.Editor.SetSelection (selection.Offset, selection.Offset + text.Length - 1);
+ using (var undo = doc.Editor.OpenUndoGroup ()) {
+ if (formatter.SupportsOnTheFlyFormatting) {
+ formatter.OnTheFlyFormat (doc.Project != null ? doc.Project.Policies : null, doc.Editor, selection.Offset, selection.EndOffset);
+ } else {
+ var pol = doc.Project != null ? doc.Project.Policies : null;
+ string text = formatter.FormatText (pol, doc.Editor.Text, selection.Offset, selection.EndOffset);
+ if (text != null) {
+ doc.Editor.Replace (selection.Offset, selection.Length, text);
+ doc.Editor.SetSelection (selection.Offset, selection.Offset + text.Length - 1);
+ }
}
}
- doc.Editor.Document.EndAtomicUndo ();
}
}
}
@@ -110,7 +110,7 @@
StringBuilder buffer = null;
bool somethingReplaced;
bool utf8Failed;
-
+ IDisposable undoGroup;
public void BeginReplace ()
{
somethingReplaced = false;
@@ -121,7 +121,7 @@
document = SearchDocument ();
if (document != null) {
Gtk.Application.Invoke (delegate {
- document.Editor.Document.BeginAtomicUndo ();
+ undoGroup = document.Editor.OpenUndoGroup ();
});
return;
}
@@ -147,7 +147,12 @@
public void EndReplace ()
{
if (document != null) {
- Gtk.Application.Invoke (delegate { document.Editor.Document.EndAtomicUndo (); document.Editor.Document.CommitUpdateAll (); });
+ Gtk.Application.Invoke (delegate {
+ if (undoGroup != null) {
+ undoGroup.Dispose ();
+ undoGroup = null;
+ }
+ document.Editor.Document.CommitUpdateAll (); });
return;
}
if (buffer != null && somethingReplaced) {
@@ -41,7 +41,6 @@
void Undo ();
void Redo ();
- void BeginAtomicUndo ();
- void EndAtomicUndo ();
+ IDisposable OpenUndoGroup ();
}
}
@@ -175,11 +175,16 @@
{
}
- public void BeginAtomicUndo ()
+ class DisposeStub : IDisposable
{
+ public void Dispose ()
+ {
+ }
}
- public void EndAtomicUndo ()
+
+ public IDisposable OpenUndoGroup ()
{
+ return new DisposeStub ();
}
public Mono.TextEditor.TextEditorData GetTextEditorData ()
@@ -244,21 +244,21 @@
buffer.CursorPosition = pos + 1;
return;
}
- buffer.BeginAtomicUndo ();
- buffer.DeleteText (pos, 1);
- buffer.InsertText (pos, upper);
- buffer.CursorPosition = pos + 1;
- buffer.EndAtomicUndo ();
+ using (var undo = buffer.OpenUndoGroup ()) {
+ buffer.DeleteText (pos, 1);
+ buffer.InsertText (pos, upper);
+ buffer.CursorPosition = pos + 1;
+ }
} else {
string newText = selectedText.ToUpper ();
if (newText == selectedText)
return;
int startPos = buffer.SelectionStartPosition;
- buffer.BeginAtomicUndo ();
- buffer.DeleteText (startPos, selectedText.Length);
- buffer.InsertText (startPos, newText);
- buffer.Select (startPos, startPos + newText.Length);
- buffer.EndAtomicUndo ();
+ using (var undo = buffer.OpenUndoGroup ()) {
+ buffer.DeleteText (startPos, selectedText.Length);
+ buffer.InsertText (startPos, newText);
+ buffer.Select (startPos, startPos + newText.Length);
+ }
}
}
@@ -285,21 +285,21 @@
buffer.CursorPosition = pos + 1;
return;
};
- buffer.BeginAtomicUndo ();
- buffer.DeleteText (pos, 1);
- buffer.InsertText (pos, lower);
- buffer.CursorPosition = pos + 1;
- buffer.EndAtomicUndo ();
+ using (var undo = buffer.OpenUndoGroup ()) {
+ buffer.DeleteText (pos, 1);
+ buffer.InsertText (pos, lower);
+ buffer.CursorPosition = pos + 1;
+ }
} else {
string newText = selectedText.ToLower ();
if (newText == selectedText)
return;
int startPos = buffer.SelectionStartPosition;
- buffer.BeginAtomicUndo ();
- buffer.DeleteText (startPos, selectedText.Length);
- buffer.InsertText (startPos, newText);
- buffer.Select (startPos, startPos + newText.Length);
- buffer.EndAtomicUndo ();
+ using (var undo = buffer.OpenUndoGroup ()) {
+ buffer.DeleteText (startPos, selectedText.Length);
+ buffer.InsertText (startPos, newText);
+ buffer.Select (startPos, startPos + newText.Length);
+ }
}
}
@@ -471,14 +471,13 @@
}
--pos;
}
-
- data.Document.BeginAtomicUndo ();
- foreach (var info in removeList) {
- ((Mono.TextEditor.IBuffer)data.Document).Remove (info.Position, info.Length);
- data.Document.CommitLineUpdate (data.Document.OffsetToLineNumber (info.Position));
+ using (var undo = data.OpenUndoGroup ()) {
+ foreach (var info in removeList) {
+ ((Mono.TextEditor.IBuffer)data.Document).Remove (info.Position, info.Length);
+ data.Document.CommitLineUpdate (data.Document.OffsetToLineNumber (info.Position));
+ }
+ data.Caret.Offset = Math.Min (data.Caret.Offset, data.Document.Length - 1);
}
- data.Caret.Offset = Math.Min (data.Caret.Offset, data.Document.Length - 1);
- data.Document.EndAtomicUndo ();
}
[CommandUpdateHandler (EditCommands.RemoveTrailingWhiteSpaces)]
@@ -180,12 +180,17 @@
{
}
- public void BeginAtomicUndo ()
+ class DisposeStub : IDisposable
+ {
+ public void Dispose ()
+ {
+ }
+ }
+
+ public IDisposable OpenUndoGroup ()
{
- }
- public void EndAtomicUndo ()
- {
- }
+ return new DisposeStub ();
+ }
public TextEditorData GetTextEditorData ()
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment