Freigeben über


EditArray Class

Merges multiple edit operations to create a single operation.

This API is not CLS-compliant. 

Inheritance Hierarchy

Object
  Microsoft.VisualStudio.Package.EditArray

Namespace:  Microsoft.VisualStudio.Package
Assemblies:   Microsoft.VisualStudio.Package.LanguageService.10.0 (in Microsoft.VisualStudio.Package.LanguageService.10.0.dll)
  Microsoft.VisualStudio.Package.LanguageService.12.0 (in Microsoft.VisualStudio.Package.LanguageService.12.0.dll)
  Microsoft.VisualStudio.Package.LanguageService (in Microsoft.VisualStudio.Package.LanguageService.dll)
  Microsoft.VisualStudio.Package.LanguageService.11.0 (in Microsoft.VisualStudio.Package.LanguageService.11.0.dll)
  Microsoft.VisualStudio.Package.LanguageService.9.0 (in Microsoft.VisualStudio.Package.LanguageService.9.0.dll)

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
Public Class EditArray _
    Implements IEnumerable, IDisposable
[CLSCompliantAttribute(false)]
public class EditArray : IEnumerable, IDisposable
[CLSCompliantAttribute(false)]
public ref class EditArray : IEnumerable, 
    IDisposable
[<CLSCompliantAttribute(false)>]
type EditArray =  
    class 
        interface IEnumerable 
        interface IDisposable 
    end
public class EditArray implements IEnumerable, IDisposable

The EditArray type exposes the following members.

Constructors

  Name Description
Public method EditArray Initializes a new instance of the EditArray class using a Source object and an IVsTextView object.

Top

Properties

  Name Description
Public property Count Gets the number of edit operations represented in the EditArray object.
Public property Source Gets the Source object associated with this EditArray object.
Public property TextView Gets the IVsTextView object associated with this EditArray object.

Top

Methods

  Name Description
Public method Add Adds the specified EditSpan object to the array of edit operations.
Public method ApplyEdits Applies all edit operations that have been accumulated.
Public method Dispose Disposes the EditArray object and its resources.
Public method Equals Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method Finalize Tears down the EditArray object. (Overrides Object.Finalize.)
Public method GetEnumerator Gets a default enumerator for the edit operations.
Public method GetHashCode Serves as the default hash function. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Converts the array of edit operations to a formatted string. (Overrides Object.ToString.)

Top

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

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

Microsoft.VisualStudio.Package Namespace