EditArray EditArray EditArray EditArray Class

Definition

Important

This API is not CLS-compliant.

Merges multiple edit operations to create a single operation.

public ref class EditArray : IDisposable, System::Collections::IEnumerable
[System.CLSCompliant(false)]
public class EditArray : IDisposable, System.Collections.IEnumerable
type EditArray = class
    interface IEnumerable
    interface IDisposable
Public Class EditArray
Implements IDisposable, IEnumerable
Inheritance
EditArrayEditArrayEditArrayEditArray
Attributes
Implements

Examples

Here is an example of how to replace or insert the first line of a document (which might, for example, contain document property information). Although this demonstrates a single edit operation, the example can be readily expanded to support multiple lines and multiple edit operations.

using Microsoft.VisualStudio.Package;  

namespace MyLanguagePackage  
{  
    class MyLanguageService : LanguageService  
    {  
        void SetPropertyValue(string propertyName, string propertyValue)  
        {  
            int lineNumberToReplace = 0;  
            string propertyLine = String.Format("//!{0} = {1}",  
                                                propertyName,  
                                                propertyValue);  
            string documentLine = src.GetLine(lineNumberToReplace);  
            bool fInsertLine = true;  
            if (documentLine.IndexOf("//!") == 0))  
            {  
                fInsertLine = false;  
            }  

            // Now insert or replace the line  
            EditArray editArray = new EditArray(this.LastActiveTextView,  
                                                true,  
                                                "Update property");  
            if (editArray != null)  
            {  
                TextSpan span = new TextSpan();  
                if (fInsertLine)  
                {  
                    span.iStartLine  = lineNumberToReplace;  
                    span.iStartIndex = 0;  
                    span.IEndLine    = lineNumberToReplace;  
                    span.iEndIndex   = 0;  
                    propertyLine += "\n";  
                }  
                else  
                {  
                    int lineLength = 0;  
                    src.GetLines().GetLengthOfLine(lineNumberToReplace,out lineLength);  
                    span.iStartLine  = lineNumberToReplace;  
                    span.iStartIndex = 0;  
                    span.iEndLine    = lineNumberToReplace;  
                    span.iEndIndex   = lineLength;  
                }  

                editArray.Add(new EditSpan(span, propertyLine));  
                // Multiple Adds can be done here before ApplyEdits  
                editArray.ApplyEdits();  
            }  
        }  
    }  
}  

Remarks

This class wraps multiple edit operations to create a single operation that can be undone when updating a source file. This efficiency is obtained by merging adjacent edit operations. In addition, this class ensures that existing markers such as breakpoints and bookmarks are not lost when edit operations are applied to the source file.

This class is typically used to wrap a formatting event, where multiple lines of source can be changed in what seems a single operation to the user.

To create a single edit event, this class uses the CompoundViewAction class if an IVsTextView object is available; otherwise, the CompoundAction class is used.

In the case of an EditArray object, an edit operation is either an insertion or a replacement. A deletion is a special case of a replacement where the replacement string is empty.

Notes to Inheritors

This class contains all the functionality needed to merge and apply multiple edits. There is no need to derive from this class.

Notes to Callers

To wrap multiple edit operations, instantiate a new EditArray object then call the Add(EditSpan) method one or more times with the span being affected. Finally, call the ApplyEdits() method to apply all the edits at once.

Constructors

EditArray(Source, IVsTextView, Boolean, String) EditArray(Source, IVsTextView, Boolean, String) EditArray(Source, IVsTextView, Boolean, String) EditArray(Source, IVsTextView, Boolean, String)

Initializes a new instance of the EditArray class using a Source object and an IVsTextView object.

Properties

Count Count Count Count

Gets the number of edit operations represented in the EditArray object.

Source Source Source Source

Gets the Source object associated with this EditArray object.

TextView TextView TextView TextView

Gets the IVsTextView object associated with this EditArray object.

Methods

Add(EditSpan) Add(EditSpan) Add(EditSpan) Add(EditSpan)

Adds the specified EditSpan object to the array of edit operations.

ApplyEdits() ApplyEdits() ApplyEdits() ApplyEdits()

Applies all edit operations that have been accumulated.

Dispose() Dispose() Dispose() Dispose()

Disposes the EditArray object and its resources.

Finalize() Finalize() Finalize() Finalize()

Tears down the EditArray object.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Gets a default enumerator for the edit operations.

ToString() ToString() ToString() ToString()

Converts the array of edit operations to a formatted string.

Extension Methods

FindAncestor<TElementType>(TElementType, Func<TElementType,TElementType>, Func<TElementType,Boolean>) FindAncestor<TElementType>(TElementType, Func<TElementType,TElementType>, Func<TElementType,Boolean>) FindAncestor<TElementType>(TElementType, Func<TElementType,TElementType>, Func<TElementType,Boolean>) FindAncestor<TElementType>(TElementType, Func<TElementType,TElementType>, Func<TElementType,Boolean>)
FindAncestor<TAncestorType,TElementType>(TElementType, Func<TElementType,TElementType>) FindAncestor<TAncestorType,TElementType>(TElementType, Func<TElementType,TElementType>) FindAncestor<TAncestorType,TElementType>(TElementType, Func<TElementType,TElementType>) FindAncestor<TAncestorType,TElementType>(TElementType, Func<TElementType,TElementType>)

Microsoft internal use only.

FindAncestorOrSelf<TElementType>(TElementType, Func<TElementType,TElementType>, Func<TElementType,Boolean>) FindAncestorOrSelf<TElementType>(TElementType, Func<TElementType,TElementType>, Func<TElementType,Boolean>) FindAncestorOrSelf<TElementType>(TElementType, Func<TElementType,TElementType>, Func<TElementType,Boolean>) FindAncestorOrSelf<TElementType>(TElementType, Func<TElementType,TElementType>, Func<TElementType,Boolean>)
FindAncestorOrSelf<TAncestorType,TElementType>(TElementType, Func<TElementType,TElementType>) FindAncestorOrSelf<TAncestorType,TElementType>(TElementType, Func<TElementType,TElementType>) FindAncestorOrSelf<TAncestorType,TElementType>(TElementType, Func<TElementType,TElementType>) FindAncestorOrSelf<TAncestorType,TElementType>(TElementType, Func<TElementType,TElementType>)
IsAncestorOf<TElementType>(TElementType, TElementType, Func<TElementType,TElementType>) IsAncestorOf<TElementType>(TElementType, TElementType, Func<TElementType,TElementType>) IsAncestorOf<TElementType>(TElementType, TElementType, Func<TElementType,TElementType>) IsAncestorOf<TElementType>(TElementType, TElementType, Func<TElementType,TElementType>)

Microsoft internal use only.

Applies to