如何更改 ClassificationFormatDefinition

How to change a ClassificationFormatDefinition

我的 Visual Studio 扩展 (VSIX) 来自 Ook 语言示例(发现 here)。基本上,我有以下 ClassificationFormatDefinition 和一个函数 loadSavedColor 来加载用户配置的颜色。一切正常。

[Name("some_unique_name")]
internal sealed class OokE : ClassificationFormatDefinition
{
    public OokE()
    {
        DisplayName = "ook!"; //human readable version of the name
        ForegroundColor = loadSavedColor();
    }
}

问题:用户配置了新的颜色后,我喜欢让class已有的实例失效OokE或者改变已有的实例和设置 ForegroundColor。但是无论我做什么,语法颜色都不会更新。

我试过:

  1. 获取对 class OokE 的引用并更新 ForegroundColor
  2. 使对应的无效ClassificationTypeDefinition:

    [导出(类型(分类类型定义))] [姓名("ook!")] 内部静态 ClassificationTypeDefinition ookExclamation = null;

经过数小时的代码筛选后,我可以创建一些有用的东西。下面的方法 UpdateFont 调用 colorKeyName 等于 "some_unique_name" 就可以了。我希望它对某人有用。

private void UpdateFont(string colorKeyName, Color c)
{
    var guid2 = Guid.Parse("{A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0}");
    var flags = __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES;
    var store = GetService(typeof(SVsFontAndColorStorage)) as IVsFontAndColorStorage;

    if (store.OpenCategory(ref guid2, (uint)flags) != VSConstants.S_OK) return;
    store.SetItem(colorKeyName, new[]{ new ColorableItemInfo
        {
            bForegroundValid = 1,
            crForeground = (uint)ColorTranslator.ToWin32(c)
        }});
    store.CloseCategory();
}

设置新颜色后,您需要使用以下代码清除缓存:

IVsFontAndColorCacheManager cacheManager = this.GetService(typeof(SVsFontAndColorCacheManager)) as IVsFontAndColorCacheManager;
cacheManager.ClearAllCaches();
var guid = new Guid("00000000-0000-0000-0000-000000000000");
cacheManager.RefreshCache(ref guid);
guid = new Guid("{A27B4E24-A735-4d1d-B8E7-9716E1E3D8E0}"); // Text editor category