Completor Class

Wraps and applies multiple changes to a text view as a single operation.

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

Syntax

<CLSCompliantAttribute(False)> _
Public Class Completor _
    Implements IDisposable

Dim instance As Completor
[CLSCompliantAttribute(false)]
public class Completor : IDisposable
[CLSCompliantAttribute(false)]
public ref class Completor : IDisposable
public class Completor implements IDisposable

Remarks

This helper class is used to manage changes to the current line due to completion actions; that is, actions that result in the automatic insertion of text.

This class gathers characters and cursor actions and applies them to the current caret position as a single compound action. This allows all of the characters to be undone in a single operation. This class supports inserting any text character, moving the caret left and right as well as Delete and Backspace (each of these is applied by calling a different method on this class).

In addition, all inserted characters and cursor movements are added to a macro recorder if such a recorder is turned on.

This class maintains an internal buffer that contains all the characters to be inserted. This buffer is treated as a stream of characters; any character can be inserted into this buffer, even control characters such as newline. Only after the characters are inserted are any control characters handled. For example, if you use this class to insert the string "Hello\nThere" into an empty source file, the source file will contain two lines. However, the caret is positioned six characters after the word "Hello" on the first line (five letters in "There" and one newline character for a total of six characters). This class does not understand multiple lines in this situation; all it sees is a stream of characters to insert.

Notes to Implementers:

This class is self-contained and there is typically no reason to derive from this class.

Notes to Callers:

Instantiate and use this class when you need to wrap a set of character-oriented insertions at the current caret position.

Nota

Since this class is typically used in a completion operation, the commit or completion character may have already been added to the line before this class applies any changes. If you need to replace the commit character, you need to call the TypeBackspace method as the first operation to delete the commit character from the line.

Examples

This example shows a simple completion operation using the Completor class in a derived version of the Declarations class. This operation wraps in quotes whatever text was committed.

namespace MyLanguagePackage
{
    class MyDeclarations : Declarations
    {
        LanguageService m_languageService;

        MyDeclarations(LanguageService service) : base()
        {
            m_languageService = service;
        }

        public override char OnAutoComplete(IVsTextView view,
                                            string committedText,
                                            char commitChar,
                                            int index)
        {
            if (committedText != null && view != null && m_languageService != null)
            {
                bool fHasCommitChar = commitChar != '\0' && commitChar != '\r';
                // If user completes word with single quotes, use single quotes;
                // Otherwise, always use double quotes.
                char quoteChar = (commitChar == '\'') ? '\'' : '"';

                Completor completor = new Completor(m_languageService,
                                                    view,
                                                    "Add Quotes");
                // All edits are applied when the completor object is disposed off.
                using (completor)
                {
                    int delta = fHasCommitChar ? 1 : 0;
                    completor.TypeLeft(committedText.Length + delta);
                    completor.TypeChar(quoteChar);
                    completor.TypeRight(committedText.Length + delta);
                    if (commitChar != quoteChar)
                    {
                        completor.TypeChar(quoteChar);
                    }
                }
            }

            return '\0';    // No further operations needed
        }
    }
}

Inheritance Hierarchy

System.Object
  Microsoft.VisualStudio.Package.Completor

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

Completor Members

Microsoft.VisualStudio.Package Namespace