【问题标题】:Adding a custom editor to visual studio editor list将自定义编辑器添加到 Visual Studio 编辑器列表
【发布时间】:2012-10-15 19:51:47
【问题描述】:

我正在为 Visual Studio 编写自定义编辑器。我已经为新语言实现了一些基本功能,例如语法突出显示,我使用生成的 .vsix 文件成功安装了 tha 包。一切都很好,但是我的自定义编辑器需要能够与不同的文件扩展名相关联。

我错误地认为,因为我安装了编辑器,它会出现在下面

工具->选项..->文本编辑器->文件扩展名->编辑器列表:

但是它没有出现在那里。那么问题来了:如何将自定义编辑器添加到此列表中?

感谢您的帮助!

【问题讨论】:

    标签: visual-studio-2010 c#-4.0 text-editor vsix


    【解决方案1】:

    至少我得到了这个问题的风滚草徽章。

    经过大量逆向工程后,我找到了解决方案……没有记录……任何地方……

    第 1 步:

    首先,您需要创建一个编辑器工厂,其中包含它附带的所有花里胡哨 - MSVS 有一个扩展。

    第 2 步: 那你就得创建这样一个类

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
        class ProvideFileExtensionMapping : RegistrationAttribute
        {
            private readonly string _name, _id, _editorGuid, _package;
            private readonly int _sortPriority;
    
            public ProvideFileExtensionMapping(string id, string name, object editorGuid, string package, int sortPriority)
            {
                _id = id;
                _name = name;
                if (editorGuid is Type)
                {
                    _editorGuid = ((Type)editorGuid).GUID.ToString("B");
                }
                else
                {
                    _editorGuid = editorGuid.ToString();
                }
                _package = package;
                _sortPriority = sortPriority;
            }
    
            public override void Register(RegistrationContext context)
            {
                using (Key mappingKey = context.CreateKey("FileExtensionMapping\\" + _id))
                {
                    mappingKey.SetValue("", _name);
                    mappingKey.SetValue("DisplayName", _name);
                    mappingKey.SetValue("EditorGuid", _editorGuid);
                    mappingKey.SetValue("Package", _package);
                    mappingKey.SetValue("SortPriority", _sortPriority);
                }
            }
    
            public override void Unregister(RegistrationAttribute.RegistrationContext context)
            {
            }
        }
    

    第 3 步: 然后您需要将该类作为属性添加到您的编辑器工厂(您在步骤 1 中创建):

    [ProvideFileExtensionMapping("{E23E32ED-3467-4401-A364-1352666A3502}", "RText Editor", typeof(EditorFactory), GuidList.guidRTextEditorPluginEditorFactoryString, 100)]
    public sealed class EditorFactory : IVsEditorFactory, IDisposable{...}
    

    就是这样。您现在应该能够在 Visual Studio 的编辑器列表中看到您的编辑器。

    当文件映射正确时,您的编辑器将被调用。

    希望这篇文章可以为其他人节省大量时间..

    【讨论】:

    • 如果 Oracle 能让他们的 ODT 团队为他们的编辑器实现这个就好了
    猜你喜欢
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多