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
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.
In the Project Types list, open the Other Project Types category and select the Extensibility category, then select the Visual Studio Integration Package template.
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.
In the Visual Studio Integration Package Wizard, select Visual C# as the language and the option to generate a new key file. Click Next.
Enter the appropriate company and package information. Click Next.
Select Menu Command. Click Next.
If you do not intend to support code snippets, you can just click Finish and ignore the next step.
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
In the Solution Explorer, right-click on the MyLanguagePackage project and select Add Reference.
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.
In the Solution Explorer, right-click on the VSPackage project and select Add -> Class.
Make sure Class is selected in the templates list.
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.
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;
Modify the MyLanguageService class to derive from the LanguageService class:
[C#] class MyLanguageService : LanguageService { }
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.
Implement the abstract methods as described in Implementing a Language Service (Managed Package Framework).
Register the Language Service
Open the vspkg.cs file and add the following using statements:
using Microsoft.VisualStudio; using Microsoft.VisualStudio.Package; using Microsoft.VisualStudio.TextManager.Interop;
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
Implement a parser and scanner for your language as described in Language Service Parser and Scanner (Managed Package Framework).
How you implement your parser and scanner is entirely up to you and is beyond the scope of this topic.
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
In the Solution Explorer, right-click on the VSPackage project and select Add -> Class.
Make sure Class is selected in the templates list.
Enter a suitable name for the class file and Click Add.
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;
Modify the class to derive from the desired MPF class.
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) { } } }
From the Edit -> IntelliSense menu, select Implement Abstract Class if the base class has any abstract methods that must be implemented.
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. |