Walkthrough: Creating a Language Service (Managed Package Framework)

Using the managed package framework (MPF) language classes to implement a language service in Visual C# is straightforward. You need a VSPackage to host the language service, the language service itself, and a parser for your language.

Create a VSPackage

  1. Create a new VSPackage to host your language service by selecting File -> New Project.

    If you are adding a language service to an existing VSPackage, skip the following steps and go directly to the "Create the Language Service Class" procedure.

  2. In the Project Types list, open the Other Project Types category and select the Extensibility category, then select the Visual Studio Integration Package template.

  3. Enter MyLanguagePackage for the name of the project and click OK.

    You can use whatever name you want. These procedures detailed here assume MyLanguagePackage as the name.

  4. In the Visual Studio Integration Package Wizard, select Visual C# as the language and the option to generate a new key file. Click Next.

  5. Enter the appropriate company and package information. Click Next.

  6. Select Menu Command. Click Next.

    If you do not intend to support code snippets, you can just click Finish and ignore the next step.

  7. Enter Insert Snippet as the Command Name and cmdidInsertSnippet for the Command ID. Click Finish.

    The Command Name and Command ID can be whatever you want, these are just examples.

Create the Language Service Class

  1. In the Solution Explorer, right-click on the MyLanguagePackage project and select Add Reference.

  2. In the Add Reference dialog box, select Microsoft.VisualStudio.Package.LanguageService in the .NET tab and click OK.

    This needs to be done only once for the language package project.

  3. In the Solution Explorer, right-click on the VSPackage project and select Add -> Class.

  4. Make sure Class is selected in the templates list.

  5. Enter MyLanguageService.cs for the name of the class file and click Add.

    You can use whatever name you want. These procedures detailed here assume MyLanguageService as the name.

  6. In the MyLanguageService.cs file, add the following using statements.

    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Package;
    using Microsoft.VisualStudio.TextManager.Interop;
    using Microsoft.VisualStudio.OLE.Interop;
    
  7. Modify the MyLanguageService class to derive from the LanguageService class:

    [C#]
    class MyLanguageService : LanguageService
    {
    }
    
  8. Position the caret on "LanguageService" and from the Edit -> IntelliSense menu, select Implement Abstract Class. This adds the minimum necessary methods to implement a language service class.

  9. Implement the abstract methods as described in Implementing a Language Service (Managed Package Framework).

Register the Language Service

  1. Open the vspkg.cs file and add the following using statements:

    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Package;
    using Microsoft.VisualStudio.TextManager.Interop;
    
  2. Register your language service class as described in Registering a Language Service (Managed Package Framework). This includes the ProvideXX attributes and "Proffering the Language Service" sections.

The Parser and Scanner

Language Service Features

To implement each feature in the language service, you typically derive a class from the appropriate MPF language service class, implement any abstract methods as necessary, and override the appropriate methods. What classes you create and/or derive from is dependent on the features you intend to support. These features are discussed in detail in Language Service Features (Managed Package Framework). The following procedure is the general approach to deriving a class from the MPF classes.

Deriving From an MPF Class

  1. In the Solution Explorer, right-click on the VSPackage project and select Add -> Class.

  2. Make sure Class is selected in the templates list.

    Enter a suitable name for the class file and Click Add.

  3. In the new class file, add the following using statements.

    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Package;
    using Microsoft.VisualStudio.TextManager.Interop;
    using Microsoft.VisualStudio.OLE.Interop;
    
  4. Modify the class to derive from the desired MPF class.

  5. Add a class constructor that takes at least the same parameters as the base class's constructor and pass the constructor parameters on to the base class constructor.

    For example, the constructor for a class derived from the Source class might look like the following:

    namespace MyLanguagePackage
    {
        class MySource : Source
        {
            public MySource(LanguageService service,
                            IVsTextLines textLines,
                            Colorizer colorizer)
                : base(service, textLines, colorizer)
            {
            }
        }
    }
    
  6. From the Edit -> IntelliSense menu, select Implement Abstract Class if the base class has any abstract methods that must be implemented.

  7. Otherwise, position the caret inside the class and enter the method to be overridden.

    For example, type public override to see a list of all methods that can be overridden in that class.

See Also

Other Resources

Implementing a Language Service By Using the Managed Package Framework

Change History

Date

History

Reason

July 2008

Rewrote and refactored project.

Content bug fix.