Question

I have a Visual Studio (2010) package that combines multiple text operations, e.g. it inserts custom properties into my csharp files.

These inserts can become quite numerous and I would like to be able to reverse the effect of the Command with a single click of the undo button. Is there a way of doing this?

Was it helpful?

Solution

Yes. Assuming you have a valid

ITextBuffer buffer;

You just need to get an ITextEdit, like this:

var textEdit = buffer.CreateEdit();

Then you can:

textEdit.Delete(...)
textEdit.Insert(....)

and when you are done, you must

textEdit.Apply();

or

textEdit.Cancel();

If you don't apply or cancel your changes, other changes will not be allowed on the buffer.... so you probably want to wrap your changes in a try...finally so an exception doesn't shut down your editor. All your changes will be grouped and a single undo will reverse them all.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top