Syntax Highlight doesn't work for IVsInvisibleEditor

前端 未结 1 550
终归单人心
终归单人心 2021-01-29 05:12

I have created an instance of IVsInvisibleEditor. I\'ve used following code to achieve this:

    public IWpfTextViewHost CreateEditor(string targetFile)
    {
           


        
相关标签:
1条回答
  • 2021-01-29 06:10

    After spending days on this issue, I finally found what was the problem. So, in case that someone else also have trouble with it here is the explanation:

    When creating the InvisibleEditor, a lot of things are happening "underneath", one thing that should be set is also the ContentType for particular ITextBuffer. When sql file (which is passed as moniker parameter to RegisterInvisibleEditor method) is used, ContentType is set to plain text (but in case of csharp files, it is set to CSharp ContentType), which is clearly wrong, however, calling:

        Guid sqlGuid = LanguageServices.Guids.TSQL;
        docData.SetLanguageServiceID(ref sqlGuid);
    

    should solve this issue, setting language service will set the correct ContentType, but for some reason in my case this didn't work. After some debugging I found that ContentType, when it is opened by dbl click in the solution explorer, is different one than anticipated. It was "Sql Server Tools" ContentType not "T-SQL90", so, I tried to find guid of that type using IVsTextBuffer:

            Guid langId;
            vsTextBuffer.GetLanguageServiceID(out langId);
    

    this gave me correct guid, so when replaced with my tsql languange service, it worked as expected. In case that someone needs these guids, they can be found at HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\Languages (depending on VS installation).

    At the end, here is the helper class which I used to map these guids:

        public static class LanguageServices
        {
            public static class Guids
            {
                public static Guid VisualBasic = new Guid("E34ACDC0-BAAE-11D0-88BF-00A0C9110049");
                public static Guid CSharp = new Guid("694DD9B6-B865-4C5B-AD85-86356E9C88DC");
                public static Guid FSharp = new Guid("bc6dd5a5-d4d6-4dab-a00d-a51242dbaf1b");
                public static Guid CPlusPlus = new Guid("B2F072B0-ABC1-11D0-9D62-00C04FD9DFD9");
                public static Guid Css = new Guid("A764E898-518D-11d2-9A89-00C04F79EFC3");
                public static Guid Html = new Guid("58E975A0-F8FE-11D2-A6AE-00104BCC7269");
                public static Guid JavaScript = new Guid("59E2F421-410A-4fc9-9803-1F4E79216BE8");
                public static Guid TSQL = new Guid("43AF1158-FED5-432e-8E8F-23B6FD592857");
                public static Guid SQL = new Guid("ed1a9c1c-d95c-4dc1-8db8-e5a28707a864");
                public static Guid Xaml = new Guid("c9164055-039b-4669-832d-f257bd5554d4");
                public static Guid Xml = new Guid("f6819a78-a205-47b5-be1c-675b3c7f0b8e");
            }
        }
    
    0 讨论(0)
提交回复
热议问题