Word Completion in a Legacy Language Service

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Word completion fills in the missing characters on a partially typed word. If there is only one possible completion, the word is completed when the completion character is entered. If the partial word matches more than one possibility, a list of possible completions is displayed. A completion character can be any character that is not used for identifiers.

Legacy language services are implemented as part of a VSPackage, but the newer way to implement language service features is to use MEF extensions. To find out more, see Extending the Editor and Language Services.

Note

We recommend that you begin to use the new editor API as soon as possible. This will improve the performance of your language service and let you take advantage of new editor features.

Implementation Steps

  1. When the user selects Complete Word from the IntelliSense menu, the VSConstants.VSStd2KCmdID command is sent to the language service.

  2. The ViewFilter class catches the command and calls the Completion method with the parse reason of ParseReason.

  3. The Source class then calls the ParseSource method to get the list of possible word completions and then displays the words in a tool tip list using the CompletionSet class.

    If there is only one matching word, the Source class completes the word.

    Alternatively, if the scanner returns the trigger value TokenTriggers when the first character of an identifier is typed, the Source class detects this and calls the Completion method with the parse reason of ParseReason. In this case the parser must detect the presence of a member selection character and provide a list of members.

Enabling Support for the Complete Word

To enable support for word completion set the CodeSense named parameter passed to the ProvideLanguageServiceAttribute user attribute associated with the language package. This sets the EnableCodeSense property on the LanguagePreferences class.

Your parser must return a list of declarations in response to the parse reason value ParseReason, for word completion to operate.

Implementing Complete Word in the ParseSource Method

For word completion, the Source class calls the GetDeclarations method on the AuthoringScope class to obtain a list of possible word matches. You must implement the list in a class that is derived from the Declarations class. See the Declarations class for details on the methods you must implement.

If the list contains only a single word, then the Source class automatically inserts that word in place of the partial word. If the list contains more than one word, the Source class presents a tool tip list from which the user can select the appropriate choice.

Also look at the example of a Declarations class implementation in Member Completion in a Legacy Language Service.