Undo / Redo Log Actions in Visual Studio Extension

I wrote a Visual Studio extension and I have a log of user actions related to my extension. I want to be able to include undo / redo information about the changes that my tool makes in the log, and I expect that I can do this if you have an event listener when the undo / redo occurs, and check if the undo object is / replay one that matches the action generated by my tool.

Currently, I have code to get ITextUndoHistory for the current IWpfTextView , which I get with ITextUndoHistoryRegistry , which I followed this answer to generate, Unfortunately, the ITextUndoHistory object that I get does not implement enough functionality to be useful. In particular, its UndoRedoHappened event is UndoRedoHappened , but always has a null transaction. In addition, the UndoStack / RedoStack throw a System.NotSupportedException . The only thing that works is CreateTransaction gives me the transaction object and allows me to set the name for the action displayed in the undo / delete list of Visual Studio, although I don't need to work this.

Is there any other way to access undo information in Visual Studio? Or maybe a more creative hack with what I have access to?

+4
source share
1 answer

I assume that you want to register undo / redo of extension commands of your editor. If this is not so ... this description below will not be useful .:-)

First you need to create a small class derived from ITextUndoPrimitive , which is a small β€œoperation” included in the undo transaction part. This has two important methods: Undo() , which is called when the user cancels the transaction, and Do() , which is called if the user disconnected the transaction and then clicked the Retry button. For the rest of the methods, just follow the trivial implementation: CanUndo / CanRedo should always return true and make the parent read / write property simple. CanMerge should always return false, and you can leave Merge() unimplemented. Of course, you can save any additional state in ITextUndoPrimitive.

How much you do in Do / Undo is up to you. Therefore, if your extension, say, modifies a text buffer and also writes to another file in a user project, you can cancel the recording of the file. I understand that you are simply trying to track what operations the user has disabled (perhaps for statistical purposes?), And therefore you can just update the user bit to "disable this" in your log and do with it.

When you complete your action, call CreateTransaction to start a new transaction, and then in this transaction call AddUndo () pass a new instance of your undo primitive. Then the editor will call Do / Undo, as described as appropriate.

Last note: the editor will automatically get rid of cancellation transactions when the transaction history is too long, or in some cases when the history is destroyed, and it should be reset. Therefore, expect that at some point your cancellation primitives will disappear and be GC'ed. Most importantly: do not hold on to them in any other place that could cause them to leak.

+4
source

Source: https://habr.com/ru/post/1397141/


All Articles