Udostępnij za pośrednictwem


Implementing the IBabelService Interface

If the default Babel implementation is not sufficient for your language, for example because you need to use a non-standard lexer or parser, then you can implement IBabelService Interface yourself.

The Babel Package

The Babel package implements a language service for Visual Studio. It looks for all the languages that use the Babel package GUID as the package and instantiates a language service that wraps each implementation of the IBabelService Interface. If the package fails to obtain IBabelService from your language service, your language service is ignored).

Note

You can support multiple languages based on IBabelService using only one installation of the Babel package.

The Babel package calls IBabelService to accomplish specific tasks. You must create a DLL that contains a class that implements IBabelService, implement a standard class factory to instantiate your class, and add code to register that DLL as a Visual Studio language service with the package GUID set to the Babel package. Your DLL must also implement a COM class factory and register itself as such with COM.

Examine the dllmain.cpp file in the Babel common library located in the Babel\Common folder (for example, <VSSDKInstallPath>\VisualStudioIntegration\Babel\Common\dllmain.cpp) to see what is required to create a DLL that registers as a language service.

Note

If you need to modify the Babel Package code itself and you intend to ship that modified version with your language service, then you must compile the Babel Package with a new package and service GUID. Then both the modified Babel Package and your language service must be installed and registered with Visual Studio.

Methods

The methods of the IBabelService Interface are called by the Babel Package, in the following order:

  1. IBabelService::Init Method

    Called once to provide IBabelService with the current language setting in Visual Studio (for example, French, German, or Japanese).

  2. IBabelService::ColorCount Method, IBabelService::GetColorInfo Method

    Called to obtain the number of custom colorable items and the style for each colorable item.

  3. IBabelService::GetMethodFormat Method, IBabelService::GetCommentFormat Method

    Called once to get custom method and comment information.

  4. IBabelService::GetImageList Method

    Called to get an image list of icons.

  5. IBabelService::ColorLine Method

    Called multiple times from the main (UI) thread to obtain colorization information for different spans of text. With the exception of the IBabelService::ParseSource Method method, all methods of IBabelService are called from this thread.

  6. IBabelService::ParseSource Method

    Called multiple times from the parse thread to obtain information used to display error markers, brace matching, statement completion, parameter info ToolTips, and so on.

    The ParseSource and ColorLine methods can be called simultaneously.

  7. IBabelService::Done Method

    Called once to indicate that the language service is no longer needed.

The architecture of the IBabelService implementation is shown in the following diagram.

Overview of the Babel service

Babel Service Overview

If you implement the IBabelService interface, you must supply a parser that can keep track of white space, parentheses, and comments, while being able to recover from errors and provide a complete syntax tree. See the topics How to: Enable Syntax Coloring and How to: Add Syntax Checking for details.

The IBabelService::ColorLine Method accesses the parser to get information about token types to support syntax highlighting. Your implementation of the IBabelService::ParseSource Method accesses your parser to get information about the functionality and scope of tokens.

The Babel Threading Model

The Babel package uses two threads, a background parsing thread and a main UI thread. In particular, the ParseSource method is called from the parse thread, while the ColorLine method is called from the main thread.

The ColorLine method should not share any state, that is, global variables or member variables, with the ParseSource method. If this proves to be impossible for your language, consider using separate lexers, one for the ColorLine method and one for the ParseSourcemethod. If you use only one lexer, then it should be locked when in use. The library used with the default IBabelService implementation uses this approach, because the scanners generated by lex or Flex use global variables.

See Also

Concepts

The Default Babel Implementation in the Language Service Package

Other Resources

Using the Babel Package

Change History

Date

History

Reason

July 2008

Rewrote and refactored project.

Content bug fix.