locked
Setting default colour of ClassificationFormatDefinition properly using an existing colour. RRS feed

  • Question

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

    Monday, January 11, 2016 4:25 PM

All replies

  • I've been trying to add hyperlink like functionality via an extension, and I wanted my classifier to have the same colour as the existing "URL Hyperlink" item. While I could get "Keyword" colour using IVsFontAndColorStorage's GetItem having opened the FontsAndColorsCategory.TextEditor category, using "URL Hyperlink" or any variation I tried never returned anything. What am I doing wrong? Should I be looking at a subcategory, or something else entirely?
    • Edited by tnt monty Thursday, January 7, 2016 5:50 PM grammar
    • Merged by DotNet Wang Tuesday, January 12, 2016 3:23 AM similar question
    Thursday, January 7, 2016 5:49 PM
  • Hi tnt monty,

    Have you debug your code, does there any exceptions throw?

    >>and I can provide my test solution if required

    I suggest you share your test project for us. So we could easily reproduce your problem.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 12, 2016 3:17 AM
  • No exceptions that I'm aware of, and I certainly didn't get any of the usual dialogs when an exception is fired. Is there somewhere I can submit my project on this site, as I don't have a way to easily distribute stuff from work. The project really is just the default classifier produced as described above, but with the addition of the code I've already added to this post.
    Tuesday, January 12, 2016 1:37 PM
  • I've been trying to add hyperlink like functionality via an extension, and I wanted my classifier to have the same colour as the existing "URL Hyperlink" item. While I could get "Keyword" colour using IVsFontAndColorStorage's GetItem having opened the FontsAndColorsCategory.TextEditor category, using "URL Hyperlink" or any variation I tried never returned anything. What am I doing wrong? Should I be looking at a subcategory, or something else entirely?

    This is a slightly different issue I think, so please don't forget to address this issue now that it's been merged.
    Tuesday, January 12, 2016 1:40 PM