使用Visual Studio SDK制作GLSL词法着色插件

创建插件项目


新建项目,在扩展性标签中,选择Editor Classifier模版,命名为ShaderEditor,点击确定。

Visual Studio为我们生成了如下几个文件。

ShaderEditorFormat.cs文件的默认代码如下:

复制代码
 1     [Export(typeof(EditorFormatDefinition))]
 2     [ClassificationType(ClassificationTypeNames = "ShaderEditor")]
 3     [Name("ShaderEditor")]
 4     [UserVisible(true)] //this should be visible to the end user
 5     [Order(Before = Priority.Default)] //set the priority to be after the default classifiers
 6     internal sealed class ShaderEditorFormat : ClassificationFormatDefinition {
 7         /// <summary>
 8         /// Defines the visual format for the "ShaderEditor" classification type
 9         /// </summary>
10         public ShaderEditorFormat() {
11             this.DisplayName = "ShaderEditor"; //human readable version of the name
12             this.BackgroundColor = Colors.BlueViolet;
13             this.TextDecorations = System.Windows.TextDecorations.Underline;
14         }
15     }

这段代码定义了一个名为"ShaderEditor"的着色类型,编译工程并运行,我们可以在Visual Studio实验实例的工具->选项->字体和颜色中找到一个名为"ShaderEditor"的条目。同时我们会发现所有文本文件的颜色都变成了Colors.BlueViolet并带上了下划线。修改this.DisplayName = "ShaderEditor"的内容,可以改变在字体和颜色中显示的名字。下面的格式设置可以任意修改成喜欢的样式,但要注意在这里的格式只是插件首次安装时的默认设置,这些条目和其它着色选项一样,都可以被用户任意更改。

复制代码
3. 创建GLSL的着色类型
复制代码

复制代码
我们已经了解了如何将着色类型添加到Visual Studio,现在修改ShaderEditorFormat.cs,添加我们的着色类型。
复制代码
 1     [Export(typeof(EditorFormatDefinition))]
 2     [ClassificationType(ClassificationTypeNames = "GLSLText")]
 3     [Name("GLSLText")]
 4     [UserVisible(true)]
 5     [Order(Before = Priority.Default)]
 6     internal sealed class GLSLTextFormatDefinition : ClassificationFormatDefinition {
 7         public GLSLTextFormatDefinition() {
 8             this.DisplayName = "GLSL文本";
 9             this.ForegroundColor = Colors.Brown;
10         }
11     }
12 
13     [Export(typeof(EditorFormatDefinition))]
14     [ClassificationType(ClassificationTypeNames = "GLSLIdentifier")]
15     [Name("GLSLIdentifier")]
16     [UserVisible(true)]
17     [Order(Before = Priority.Default)]
18     internal sealed class GLSLIdentifierFormatDefinition : ClassificationFormatDefinition {
19         public GLSLIdentifierFormatDefinition() {
20             this.DisplayName = "GLSL标识符";
21             this.ForegroundColor = Colors.Brown;
22         }
23     }
24 
25     [Export(typeof(EditorFormatDefinition))]
26     [ClassificationType(ClassificationTypeNames = "GLSLComment")]
27     [Name("GLSLComment")]
28     [UserVisible(true)]
29     [Order(Before = Priority.Default)]
30     internal sealed class GLSLCommentFormatDefinition : ClassificationFormatDefinition {
31         public GLSLCommentFormatDefinition() {
32             this.DisplayName = "GLSL注释";
33             this.ForegroundColor = Colors.DarkGray;
34         }
35     }
36 
37     [Export(typeof(EditorFormatDefinition))]
38     [ClassificationType(ClassificationTypeNames = "GLSLKeyword")]
39     [Name("GLSLKeyword")]
40     [UserVisible(true)]
41     [Order(Before = Priority.Default)]
42     internal sealed class GLSLKeywordFormatDefinition : ClassificationFormatDefinition {
43         public GLSLKeywordFormatDefinition() {
44             this.DisplayName = "GLSL关键字";
45             this.ForegroundColor = Colors.Blue;
46         }
47     }
48 
49     [Export(typeof(EditorFormatDefinition))]
50     [ClassificationType(ClassificationTypeNames = "GLSLClass")]
51     [Name("GLSLClass")]
52     [UserVisible(true)]
53     [Order(Before = Priority.Default)]
54     internal sealed class GLSLClassFormatDefinition : ClassificationFormatDefinition {
55         public GLSLClassFormatDefinition() {
56             this.DisplayName = "GLSL类型";
57             this.ForegroundColor = Colors.Green;
58         }
59     }
60 
61     [Export(typeof(EditorFormatDefinition))]
62     [ClassificationType(ClassificationTypeNames = "GLSLQualifier")]
63     [Name("GLSLQualifier")]
64     [UserVisible(true)]
65     [Order(Before = Priority.Default)]
66     internal sealed class GLSLQualifierFormatDefinition : ClassificationFormatDefinition {
67         public GLSLQualifierFormatDefinition() {
68             this.DisplayName = "GLSL限定符";
69             this.ForegroundColor = Colors.Pink;
70         }
71     }
72 
73     [Export(typeof(EditorFormatDefinition))]
74     [ClassificationType(ClassificationTypeNames = "GLSLVariable")]
75     [Name("GLSLVariable")]
76     [UserVisible(true)]
77     [Order(Before = Priority.Default)]
78     internal sealed class GLSLVariableFormatDefinition : ClassificationFormatDefinition {
79         public GLSLVariableFormatDefinition() {
80             this.DisplayName = "GLSL系统变量";
81             this.ForegroundColor = Colors.DarkOrange;
82         }
83     }
84 
85     [Export(typeof(EditorFormatDefinition))]
86     [ClassificationType(ClassificationTypeNames = "GLSLFunction")]
87     [Name("GLSLFunction")]
88     [UserVisible(true)]
89     [Order(Before = Priority.Default)]
90     internal sealed class GLSLFunctionFormatDefinition : ClassificationFormatDefinition {
91         public GLSLFunctionFormatDefinition() {
92             this.DisplayName = "GLSL系统函数";
93             this.ForegroundColor = Colors.DarkTurquoise;
94         }
95     }

4. 导出着色类型


Editor Classifier使用了MEF框架,关于MEF的具体细节,请参考MSDN的相关文档。

我们需要注意的是,在MEF中,光定义了着色类型还不够,我们需要导出一个ClassificationTypeDefinition,才能在系统中生效。

打开ShaderEditorType.cs,我们看到系统生成的代码如下:

复制代码
1     internal static class ShaderEditorClassificationDefinition {
2         [Export(typeof(ClassificationTypeDefinition))]
3         [Name("ShaderEditor")]
4         internal static ClassificationTypeDefinition ShaderEditorType = null;
5     }

这里的Name与之前默认生成的ShaderEditor相同,同理,我们将这里的代码修改成方才定义的类型

复制代码
 1     internal static class ShaderEditorClassificationDefinition {
 2         [Export(typeof(ClassificationTypeDefinition))]
 3         [Name("GLSLText")]
 4         internal static ClassificationTypeDefinition GLSLTextType = null;
 5 
 6         [Export(typeof(ClassificationTypeDefinition))]
 7         [Name("GLSLIdentifier")]
 8         internal static ClassificationTypeDefinition GLSLIdentifierType = null;
 9 
10         [Export(typeof(ClassificationTypeDefinition))]
11         [Name("GLSLComment")]
12         internal static ClassificationTypeDefinition GLSLCommentType = null;
13 
14         [Export(typeof(ClassificationTypeDefinition))]
15         [Name("GLSLKeyword")]
16         internal static ClassificationTypeDefinition GLSLKeywordType = null;
17 
18         [Export(typeof(ClassificationTypeDefinition))]
19         [Name("GLSLClass")]
20         internal static ClassificationTypeDefinition GLSLClassType = null;
21 
22         [Export(typeof(ClassificationTypeDefinition))]
23         [Name("GLSLQualifier")]
24         internal static ClassificationTypeDefinition GLSLQualifierType = null;
25 
26         [Export(typeof(ClassificationTypeDefinition))]
27         [Name("GLSLVariable")]
28         internal static ClassificationTypeDefinition GLSLVariableType = null;
29 
30         [Export(typeof(ClassificationTypeDefinition))]
31         [Name("GLSLFunction")]
32         internal static ClassificationTypeDefinition GLSLFunctionType = null;
33     }

5. 关联文件类型


打开ShaderEditor.cs

复制代码
 1     [Export(typeof(IClassifierProvider))]
 2     [ContentType("text")]
 3     internal class ShaderEditorProvider : IClassifierProvider {
 4         [Import]
 5         internal IClassificationTypeRegistryService ClassificationRegistry = null; // Set via MEF
 6 
 7         public IClassifier GetClassifier(ITextBuffer buffer) {
 8             return buffer.Properties.GetOrCreateSingletonProperty<ShaderEditor>(delegate { return new ShaderEditor(ClassificationRegistry); });
 9         }
10     }
相关推荐
love530love1 小时前
用自然语言让 AI Agent 卸载软件 —— 以卸载 Visual Studio 2026 为例
ide·人工智能·windows·agent·visual studio·ai agent·marvis
C++ 老炮儿的技术栈8 小时前
MFC 自定义纯色居中文字进度条控件
c语言·数据库·c++·windows·算法·c·visual studio
LL3436389 小时前
2026最新5款AI编程工具平替实测|终端与IDE vibe coding迭代优缺点深度对比
ide·ai编程
00后程序员张9 小时前
iOS 打包方式汇总 从 Xcode Archive 到轻量级工具链的 IPA 构建
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
Balabala噗10 小时前
VScode接入MiniMax流程
ide·vscode·编辑器
Tian_Hang2 天前
Eclipse Ditto 的权限策略
java·服务器·前端·网络·ide·ubuntu·eclipse
Tian_Hang2 天前
Eclipse Mosquitto 安装及介绍
java·运维·服务器·ide·sql·mysql·eclipse
星云_byto2 天前
五分钟从零配置好VSCode + Claude Code + DeepSeek V4 Pro
ide·vscode·编辑器·cursor·codex·claude code·deepseekv4
诚信定制8393 天前
Typora插件开发指南:打造专属IDE式写作环境
ide