How-to: Add Member Completion

Most languages have some kind of modules, classes, or structures that have members. These members are typically selected using some kind of operator, such as the indirection operator (->) in C++. The grammar rules need to be extended so that statement completion works correctly when members are involved.

注意

You must provide your own lex/yacc-compatible tools; these tools are not included in the VS SDK and the language service project will not build without them. To build the language service using the sample grammar and parser for the sample My C Package, you must use version 1.24 or later of Bison and version 2.5.4a or later of Flex. Place the Bison and Flex executables in the Babel Tools folder, for example, <InstallPath>\VisualStudioIntegration\Babel\Tools. These version numbers are specific to the My C sample.

Methods of the Babel Service

The Babel service has two methods that specify how a member is selected, as shown in the following example:

void startName  ( in const Location& name) const;
void qualifyName( in const Location& selector, 
                  in const Location& name) const;

The grammar must call the startName method whenever an identifier is used in an expression. The qualifyName method is called for every identifier that is selected.

Example of using startName and qualifyName methods

The following example shows how both the startName and qualifyName methods are called in the Identifier production (in the parser.y file) for the My C language. Note how the qualifyName method is called with the '.' and the following identifier: the qualifyName method assumes the presence of the preceding identifier set by a call to the startName method.

Identifier
    : IDENTIFIER                 { g_service->startName($1); }
    | Identifier '.' IDENTIFIER  { g_service->qualifyName( $2, $3 ); }
    | Identifier '.' error       { g_service->qualifyName( $2, $2 ); }
    ;

Adding Member List Triggers

When typing a selector token, the member selection list automatically displays, unless it has been disabled in the option settings of Visual Studio. TriggerMemberSelect triggers a member completion list (see TriggerClass for more information on triggers). The following example shows a typical example in the getTokenInfo method:

 ...
 { '.',  ClassText,  "'.'", CharDelimiter, TriggerMemberSelect },   
 ...

See Also

Concepts

Language Service How-to Topics