Share via


EditArray Class

Merges multiple edit operations to create a single operation.

This API is not CLS-compliant. 

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

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
Public Class EditArray _
    Implements IEnumerable, IDisposable
'Usage
Dim instance As EditArray
[CLSCompliantAttribute(false)]
public class EditArray : IEnumerable, IDisposable
[CLSCompliantAttribute(false)]
public ref class EditArray : IEnumerable, 
    IDisposable
public class EditArray implements IEnumerable, IDisposable

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 Implementers:

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 method one or more times with the span being affected. Finally, call the ApplyEdits method to apply all the edits at once.

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();
            }
        }
    }
}

Inheritance Hierarchy

System.Object
  Microsoft.VisualStudio.Package.EditArray

Thread Safety

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

See Also

Reference

EditArray Members

Microsoft.VisualStudio.Package Namespace