共用方式為


Completor Class

Definition

Important

This API is not CLS-compliant.

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

public ref class Completor : IDisposable
[System.CLSCompliant(false)]
public class Completor : IDisposable
public class Completor : IDisposable
[<System.CLSCompliant(false)>]
type Completor = class
    interface IDisposable
type Completor = class
    interface IDisposable
Public Class Completor
Implements IDisposable
Inheritance
Completor
Attributes
Implements

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

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 Inheritors

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(Int32) method as the first operation to delete the commit character from the line.

Constructors

Completor(LanguageService, IVsTextView, String)

Initializes a new instance of the Completor class.

Properties

AtEndOfLine

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

IsExpansionActive

Determines if a code snippet is being edited.

Methods

Apply()

Applies all changes made through the Completor object.

Dispose()

This method calls Apply() if you have not already done it.

RefreshLine()

Obtains the current line of text from the Source object.

TypeBackspace(Int32)

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

TypeChar(Char)

Inserts the specified character and updates the internal caret position.

TypeChars(String)

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

TypeDelete(Int32)

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

TypeLeft(Int32)

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

TypeRight(Int32)

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

Applies to