Compartilhar via


EditArray Class

Definition

Important

This API is not CLS-compliant.

This class encapsulates a batch edit operation. The reason this class exists is because performing thousands of tiny edits on a large document can be pretty slow, so the best thing to do is merge the edits into bigger chunks and that is exactly what this class will do for you. The trick is that when merging edits you need to be careful not to include any IVsTextLineMarkers in the merged chunks, because editing over the top of the marker will blow it away, which is not what the user wants. The user wants to keep all their breakpoints and bookmarks, and red and blue squigglies and so on. So this class also takes care of that.

public ref class EditArray : IDisposable, System::Collections::IEnumerable
[System.CLSCompliant(false)]
public class EditArray : IDisposable, System.Collections.IEnumerable
public class EditArray : IDisposable, System.Collections.IEnumerable
[<System.CLSCompliant(false)>]
type EditArray = class
    interface IEnumerable
    interface IDisposable
type EditArray = class
    interface IEnumerable
    interface IDisposable
Public Class EditArray
Implements IDisposable, IEnumerable
Inheritance
EditArray
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)

This constructor takes a view and will use CompoundViewAction to make the updates and it will update the current selection accordingly.

Properties

Count

Return the number of edits in the array.

Source

Gets the Source object associated with this EditArray object.

TextView

Gets the IVsTextView object associated with this EditArray object.

Methods

Add(EditSpan)

Add a new atomic edit to the array. The edits cannot intersect each other.
The spans in each edit must be based on the current state of the buffer, and not based on post-edit spans. This EditArray will calculate the post edit spans for you.

ApplyEdits()

Applies all edit operations that have been accumulated.

Dispose()

Disposes the EditArray object and its resources.

Finalize()

Tears down the EditArray object.

GetEnumerator()

Allows enumeration of EditSpan objects

ToString()

Converts the array of edit operations to a formatted string.

Applies to