visual-studio-2010 - 将自定义编辑器添加到 Visual Studio 编辑器列表

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

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

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

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

enter image description here

但是它不会出现在那里。所以问题是:如何将自定义编辑器添加到此列表中?

谢谢你的帮助!

最佳答案

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

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

第 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 的编辑器列表中看到您的编辑器。

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

希望这篇文章可以为其他人节省很多时间。

关于visual-studio-2010 - 将自定义编辑器添加到 Visual Studio 编辑器列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902972/

相关文章:

c# - vshost.exe 一直在访问我的 .dll,我在构建它时无法更新它

c++ - 内存泄漏检测

dynamic - C# 4.0 'dynamic' 和 foreach 语句

c# - 将对象从 Controller 传递到另一个 Controller

html - HTML文字编辑器

vb.net - 更改文本框字体大小,保持文本框大小(高度)

c# - 如果只关闭一个窗口,如何防止应用程序终止?

c#-4.0 - 我们如何在 C# 中使用回调函数

replace - Notepad++ 将打开文档中的两行替换为其他 (10) 行

linux - 在 Mac 文本编辑器上打开 linux 机器中的文件