Infosys Microsoft Alliance and Solutions blog

« Caching Controversy!!! | Main | Windows 7 »

Custom Classification Types and Formats for your Textual DSL

Classification types and their formats are defined to perform text formatting in Intellipad for your textual DSL. For example, you would want to define text formatting for your language tokens like keywords, comments, values etc. so as to make it more readable while viewing/editing inside the Intellipad. The out of the box classification types as well as their formats are defined in ClassificationTypes.xcml and ClassificationFormats.xcml respectively under the folder: C:\Program Files\Microsoft Oslo SDK 1.0\Bin\Intellipad\Settings folder.

The out of the box classification types are fine and do cover most of the tokens that you will usually find in any textual DSL, but what if we want to have our DSL specific classification types and formats? To my knowledge there are couple of ways in which you could do this today.

  • Add a new classification type to the ClassificationTypes.xcml file in the Settings folder, as shown below:

<act:Export Type='mvstc:ClassificationTypeDefinition'>
      <ls:ClassificationType Name='CustomKeyword' DerivesFrom='text' />
</act:Export>

To define the format for the classifiaction type that we just added, we need to define the format in the ClassificationFormats.xcml file in the Settings folder, as shown below:

<act:Export Name='{}{Microsoft.Intellipad}ClassificationFormat'>
      <ls:ClassificationFormat Name='CustomKeyword'
                             FontFamily='Consolas'
                             FontWeight='Bold'
                             Foreground='#FF2B91AF' />
</act:Export>

Make sure that “Name” attribute while defining the format should be same as the Name of the classification type that we had defined in the previous step.

In your DSL MGrammar code you can use the classification type created above in the following manner:

@{Classification["CustomKeyword"]}
token MyToken = “SampleToken”;

Now if you save the .xcml files, close (if already open) and open up Intellipad, you will be able to see the new classification type in action.

  • Now the second option is to have a separate .xcml file for our DSL, where we could specify the classification types specific to our DSL and can be deployed when we install our own mode into Intellipad. In order to define the same classification type specified above in a separate .xcml file, you need to first create a file with the extension .xcml (Name doesn’t matter, but something related to your DSL would be better), and then specify the type and the format as shown below:

<act:Exports xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
             xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
             xmlns:act='clr-namespace:System.ComponentModel.Activation;assembly=Activation'
             xmlns:mvstc='clr-namespace:Microsoft.VisualStudio.Text.Classification;assembly=Microsoft.VisualStudio.Platform.Editor'
             xmlns:ls='clr-namespace:Microsoft.Intellipad.LanguageServices;assembly=Microsoft.Intellipad.Core'>
 

   <act:Export Type='mvstc:ClassificationTypeDefinition'>
      <ls:ClassificationType Name='CustomKeyword' DerivesFrom='text' />
   </act:Export>
   <act:Export Name='{}{Microsoft.Intellipad}ClassificationFormat'>
      <ls:ClassificationFormat Name='CustomKeyword'
                             FontFamily='Consolas'
                             FontWeight='Bold'
                             Foreground='#FF2B91AF' />
   </act:Export>
</act:Exports>

Once you have saved the file, you can either put this file under the Settings folder or you can create a new directory for your DSL under the C:\Program Files\Microsoft Oslo SDK 1.0\Bin\Intellipad\Components folder, and put it in that directory to enable Intellipad to pick up the types and their formats.

As per my understanding, Intellipad by default picks up the classification types by scanning the files with .xcml extension both in the Settings as well as the sub folders under the Components folder and picking up all the Export elements with the type as ClassificationTypeDefinition. Considering this fact, to me the second option would be a better one to go for, when it comes to ease of deployment and maintenance. But when it comes to performance, I feel having all the types in one file would be a better option. Hence if you have a lot of custom classification types that you would be creating for your DSL then I would suggest you go for the second option, but if the number of classification types you are going to create are less in number, then it would be better to define those in the ClassificationTypes.xcml and ClassificationFormats.xcml files under the Settings folder.

 

TrackBack

TrackBack URL for this entry:
http://www.infosysblogs.com/microsoft-mt/mt-tb.fcgi/374

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Infosys on Twitter