I would like to create a Syntax Highlighter in Visual Studio 2012 (and above) that supports different themes (Dark, Light, Blue).
V
There's another, cleaner way using the VsixColorCompiler that ships with the VS SDK.
First, create a ClassificationTypeDefinition and ClassificationFormatDefinition as usual. This will define the default colour in all themes:
public static class MyClassifications
{
public const string CustomThing = "MyClassifications/CustomThing";
[Export]
[Name(CustomThing)]
public static ClassificationTypeDefinition CustomThingType = null;
[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = CustomThing)]
[UserVisible(true)] // Note: must be user-visible to be themed!
[Name(CustomThing)]
public sealed class CustomThingFormatDefinition : ClassificationFormatDefinition
{
public CustomThingFormatDefinition()
{
ForegroundColor = Color.FromRgb(0xFF, 0x22, 0x22); // default colour in all themes
DisplayName = "Custom Thing"; // appears in Fonts and Colors options
}
}
}
Next, create a colours.xml file. This will allow us to override the colour for specific themes:
Now edit your .csproj to include a post-build command to compile the XML to a .pkgdef next to your normal package's .pkgdef (VS2015 SDK shown here):
Whenever you make a change, be sure to clear the MEF cache between builds to force it to update. Additionally, the following registry keys may need to be deleted as well:
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\14.0\FontAndColors\Cache\{75A05685-00A8-4DED-BAE5-E7A50BFA929A}
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\14.0Exp\FontAndColors\Cache\{75A05685-00A8-4DED-BAE5-E7A50BFA929A}