Completor Class

 
clsCompliantAlternativeNone

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)

System.Object
  Microsoft.VisualStudio.Package.Completor

[CLSCompliantAttribute(false)]
public class Completor : IDisposable

NameDescription
System_CAPS_pubmethodCompletor(LanguageService, IVsTextView, String)

Initializes a new instance of the Completor class.

NameDescription
System_CAPS_pubpropertyAtEndOfLine

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

System_CAPS_pubpropertyIsExpansionActive

Determines if a code snippet is being edited.

NameDescription
System_CAPS_pubmethodApply()

Applies all changes made through the Completor object.

System_CAPS_pubmethodDispose()

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

System_CAPS_pubmethodEquals(Object)

(Inherited from Object.)

System_CAPS_protmethodFinalize()

(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

(Inherited from Object.)

System_CAPS_pubmethodGetType()

(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

(Inherited from Object.)

System_CAPS_pubmethodRefreshLine()

Obtains the current line of text from the Source object.

System_CAPS_pubmethodToString()

(Inherited from Object.)

System_CAPS_pubmethodTypeBackspace(Int32)

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

System_CAPS_pubmethodTypeChar(Char)

Inserts the specified character and updates the internal caret position.

System_CAPS_pubmethodTypeChars(String)

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

System_CAPS_pubmethodTypeDelete(Int32)

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

System_CAPS_pubmethodTypeLeft(Int32)

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

System_CAPS_pubmethodTypeRight(Int32)

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.

System_CAPS_noteNote

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,
                                                    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
        }
    }
}

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