Completor Class


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

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


public class Completor : IDisposable

System_CAPS_pubmethodCompletor(LanguageService, IVsTextView, String)

Initializes a new instance of the Completor class.


Determines if the internal caret position is at the end of the current line.


Determines if a code snippet is being edited.


Applies all changes made through the Completor object.


Cleans up any allocations made just before the object is destroyed.


(Inherited from Object.)


(Inherited from Object.)


(Inherited from Object.)


(Inherited from Object.)


(Inherited from Object.)


Obtains the current line of text from the Source object.


(Inherited from Object.)


Performs the specified number of backspaces on the line being edited and updates the internal caret position.


Inserts the specified character and updates the internal caret position.


Inserts the specified string of characters and updates internal caret position.


Performs the specified number of delete operations on the line being edited.


Moves the internal caret position the specified number of positions to the left.


Moves the internal caret position the specified number of positions to the right.

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.


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.

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,
                                                    "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.TypeRight(committedText.Length + delta);
                    if (commitChar != quoteChar)

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

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

Return to top