CompoundAction Class

 
clsCompliantAlternativeNone

Manages a group of edit operations that are treated as a single operation.

Namespace:   Microsoft.VisualStudio.Package
Assembly:  Microsoft.VisualStudio.Package.LanguageService.14.0 (in Microsoft.VisualStudio.Package.LanguageService.14.0.dll)

System.Object
  Microsoft.VisualStudio.Package.CompoundActionBase
    Microsoft.VisualStudio.Package.CompoundAction

[CLSCompliantAttribute(false)]
public class CompoundAction : CompoundActionBase

NameDescription
System_CAPS_pubmethodCompoundAction(Source, String)

Initializes a new instance of the CompoundAction class.

NameDescription
System_CAPS_pubmethodAbort()

Terminates the current compound action, throwing away all edits.(Overrides CompoundActionBase.Abort().)

System_CAPS_pubmethodClose()

Closes the compound action and commits all edits to the source file.(Overrides CompoundActionBase.Close().)

System_CAPS_pubmethodDispose()

Deallocates any resources just before the CompoundAction object is destroyed.(Inherited from CompoundActionBase.)

System_CAPS_pubmethodEquals(Object)

(Inherited from Object.)

System_CAPS_protmethodFinalize()

(Inherited from Object.)

System_CAPS_pubmethodFlushEditActions()

Flushes any pending edit actions from the current compound action.(Overrides CompoundActionBase.FlushEditActions().)

System_CAPS_pubmethodGetHashCode()

(Inherited from Object.)

System_CAPS_pubmethodGetType()

(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

(Inherited from Object.)

System_CAPS_pubmethodToString()

(Inherited from Object.)

NameDescription
System_CAPS_protfieldaction

Interface for a CompoundAction action.(Inherited from CompoundActionBase.)

System_CAPS_protfieldopened

Specifies if a compound action has been opened.(Inherited from CompoundActionBase.)

This class is used to simplify wrapping a collection of edit operations into a single undoable event. This is achieved by calling the OpenCompoundAction method on the IVsCompoundAction interface that is obtained from the current IVsTextLines object stored in the Source object. When this class is disposed off, the IVsCompoundAction interface is closed and that commits all the edit events made to the IVsTextLines object as a single operation.

Notes to Implementers:

This class contains everything necessary to open a compound action given a Source object and to close that action when this class is disposed off.

Notes to Callers:

Instantiate a CompoundAction object with the Source object when you need to wrap one or more edit operations that can be undone in a single action. Then perform your edit operations as normal. When the new CompoundAction object is disposed of, the edit operations are stored as a single action.

If you have access to the IVsTextView object, use CompoundViewAction class instead as it allows the text view to optimize the edits so only the final result is seen by the user.

This example shows how to use the CompoundAction class. This example inserts a list of words at the current location in the source file. Without the CompoundAction object, each of these insertions is treated as a separate edit event and requires a separate undo operation. However, with the CompoundAction object, the entire list can be undone with a single undo operation.

using Microsoft.VisualStudio.Package

namespace MyLanguagePackage
{
    class CMyLanguageService : LanguageService
    {
        // Insert the list of words, one per line.
        void InsertWords(Source src,string[] wordList)
        {
            if (LastActiveTextView != null)
            {
                CompoundAction action = new CompoundAction(src,"Update source");
                using (action)
                {
                    int currentLine = 0;
                    int currentCol = 0;
                    LastActiveTextView.GetCaretPos(out currentLine, out currentCol);
                    // Insert list in reverse so the words appear in the proper
                    // order in the sourec file.
                    for (int i = wordList.Length - 1, i >= 0; i--)
                    {
                        string w = wordList[i] + "\n";
                        src.SetText(currentLine, currentCol, currentLine, currentCol, w);
                    }
                }
            }
        }
    }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: