I'm trying to set the default colours of my Classifier to the same as an existing classification and I'm having problems. I'm mainly interested as it's obviously showing something important I should understand but don't, and I'm struggling to find more detail info about what you should do.
To recreate this (and I can provide my test solution if required), create a VSIX project, then add an Editor Classifier to it. Add the references and usings to make this function work.
public NewClassifierFormat()
{
this.DisplayName = "NewClassifier"; // Human readable version of the name
this.TextDecorations = System.Windows.TextDecorations.Underline;
IVsFontAndColorStorage fcStorage = ServiceProvider.GlobalProvider.GetService(typeof(SVsFontAndColorStorage)) as IVsFontAndColorStorage;
if (fcStorage != null)
{
Guid textEditorGuid = new Guid(FontsAndColorsCategory.TextEditor);
//TODO
//This function call seems to stop this definition appearing in the options page -
//sometimes only in a release build??
if (fcStorage.OpenCategory(
textEditorGuid,
(int)__FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | (int)__FCSTORAGEFLAGS.FCSF_READONLY) == VSConstants.S_OK)
{
ColorableItemInfo[] pInfo = new ColorableItemInfo[1];
if (fcStorage.GetItem("Keyword", pInfo) == VSConstants.S_OK)
{
IVsFontAndColorUtilities util = ServiceProvider.GlobalProvider.GetService(typeof(SVsFontAndColorStorage)) as IVsFontAndColorUtilities;
if (util != null)
{
//0x00bbggrr
uint colRGB;
if (util.GetRGBOfEncodedColor(pInfo[0].crForeground, pInfo[0].crForeground, textEditorGuid, out colRGB) == VSConstants.S_OK)
{
Color col = Color.FromRgb((byte)(colRGB & 0x000000ff), (byte)(colRGB >> 8 & 0x000000ff), (byte)(colRGB >> 16 & 0x000000ff));
this.ForegroundColor = col;
}
}
}
fcStorage.CloseCategory();
}
}
}
With this code, the default color is set correctly, and I can see those colours in the editor view. However, the options page doesn't display "NewClassifier" as an option, and so the user can change it. In my original project it seems to work in Debug builds, but not in release.
If I replace all the code below the TODO statement with just a simple ForegroundColor = Colors.Purple, then it all works as expected, and the user can specify their own colour for the "NewClassifier" setting in "Fonts and Colors", and the default is Purple.
How should this be done, and why does the call to OpenCategory stop it working correctly? Ideally, I'd like to set a default for light and dark themes myself, but couldn't work that out, and this method seemed the easiest way. I'd also like this method to actually look for "URL Hyperlink", but that didn't work either.