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.

Prerequisites

To complete this walkthrough, you must install the Visual Studio 2010 SDK.

Note

For more information about the Visual Studio SDK, see Visual Studio Integration SDK. To find out how to download the Visual Studio SDK, see Visual Studio Extensibility Developer Center on the MSDN Web site.

Locations for the Visual Studio Package Project Template

The Visual Studio Package Project Template can be found in three different template locations in the New Project dialog:

  1. Under Visual Basic Extensibility. The default language of the project is Visual Basic.

  2. Under C# Extensibility. The default language of the project is C#.

  3. Under Other Project Types Extensibility. The default language of the project is C++.

Create a VSPackage

  1. Create a new VSPackage with the Visual Studio Package project template.

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

  3. Select Visual C# as the language and the option to generate a new key file. Click Next.

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

  5. Select Menu Command. Click Next.

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

  6. 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.

    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.Package
    Imports Microsoft.VisualStudio.TextManager.Interop
    Imports Microsoft.VisualStudio.OLE.Interop
    
    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:

        Friend Class MyLanguageService
            Inherits LanguageService
    
    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 MyLanguagePackagePackage.cs file and add the following using statements:

    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.Package
    Imports Microsoft.VisualStudio.TextManager.Interop
    
    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. Use MyLanguageService where this topic uses TestLanguageService.

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.

    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.Package
    Imports Microsoft.VisualStudio.TextManager.Interop
    Imports Microsoft.VisualStudio.OLE.Interop
    
    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
        Friend Class MySource
            Inherits Source
            Public Sub New(ByVal service As LanguageService, ByVal textLines As IVsTextLines,
                           ByVal colorizer As Colorizer)
                MyBase.New(service, textLines, colorizer)
            End Sub
        End Class
    End Namespace
    
    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