Question

I have created VSPackage that provides certain functionality in Visual Studio code window context menu. This action consists of several code edits plus some stuff around.

The problem is, each of these code edits get added to the undo stack separately. What I want is to handle this action as one atomic unit, i.e. pressing CTRL+Z rollbacks all code edits and everything else (plus of course, places the unit on top of redo stack).

Documentation regarding this topic is extremely poor, the only thing I found is something about IOleParentUndo unit - but I wasn't succesfull in implementing it.

I use

IVsTextLines.GetUndoManager()

to get the undo manager - that appears to be a good start.

Was it helpful?

Solution

If undo is managed at the document level then you may have to using the RunningDocumentTable to enumerate the tables, get their undo managers, and nuke from orbit:

class NavigateListener: IVsRunningDocTableEvents3
{
    private HashSet<IVsTextView> views = new HashSet<IVsTextView>();
    private IVsRunningDocumentTable table;
    private uint cookie;
...

Outside, call this register call -- add an unregister call that unadvises the events.

public void Register()
{
    table =(IVsRunningDocumentTable)    Package.GetGlobalService(typeof(SVsRunningDocumentTable));
    // Listen to show/hide events of docs to register activate/deactivate cursor  listeners.
    table.AdviseRunningDocTableEvents(this, out cookie);
}

The running table has lots of events, like save, etc, but you can listen to when a view is registered:

public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame)
{
    IVsTextView view = VsShellUtilities.GetTextView(pFrame);
    if (view != null)
    {
        views.Add(view);
    }
}

You may have trouble with documents that had changes saved but then were closed... remove them for now...

public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
{
    IVsTextView view = VsShellUtilities.GetTextView(pFrame);
    if (view != null)
    {
        views.Remove(view);
    }
    return VSConstants.S_OK;
}

Then you can do a super undo nuke. I haven't testing this part -- you'll have to play with it.

private void NukeFromOrbit()
{
    foreach( var view in views )
    {
        IVsTextLines buffer;
        view.GetBuffer(out buffer);
        IOleUndoManager manager;
        buffer.GetUndoManager(out manager);
        IEnumOleUndoUnits units;
        manager.EnumUndoable(out units);
        uint fetched=0;
        var unitArray = new IOleUndoUnit[1];
        while( units.Next(1, unitArray , out fetched ) == VSConstants.S_OK)
        {
            unitArray[0].Do(manager);
        }
    }
}

OTHER TIPS

Try to use something like this:

IDesignerHost host = ...;
DesignerTransaction transaction = host.CreateTransaction("Command Name");
try
{
  // Command Body
  TypeDescriptor.GetProperties(control)["Location"].SetValue(control, location);
  transaction.Commit();
}
catch
{
    transaction.Cancel();
    throw;
}

I encapsulated below code to undo couple of actions.

public class VSUndo : IDisposable
{
    public static UndoContext undoContext;

    public static VSUndo StartUndo()
    {
        undoContext = ((DTE2)Package.GetGlobalService(typeof(DTE))).UndoContext; 
        undoContext.Open(Guid.NewGuid().ToString());
        // return new instance for calling dispose to close current undocontext
        return new VSUndo(); 
    }

    public void Dispose()
    {
        undoContext.Close();
    }

}

then, you can just use:

using (VSUndo.StartUndo())
{
   // couple of actions that may need to undo together
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top