博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己开发Visual studio插件-一个nvelocity高亮插件
阅读量:7145 次
发布时间:2019-06-29

本文共 3264 字,大约阅读时间需要 10 分钟。

首先,有一个项目用到了nvelocity模板引擎,但是用vs开发模板的时候,没有高亮效果,所以非常不方便,鉴于这个,于是有了自己开发插件的念头,但是在vs sdk开发上面,这方面的资料真是少之又少,网上能参考的文章真是寥寥无几。

不过借鉴了几篇文章和参考MSDN后,总算开发出了一款VS插件,目前是支持nvelocity的语法高亮,比如nvelocity的关键字#set #parse等等 还有nvelocity的对象$xxx这样的格式,还有注释## #**#这样的,但是这里出现一个小曲

就是因为我们也知道nvelocity的模板其实大部分都是html的语法,所以如果写nvelocity更多的是写html,所以我们需要保留html的语法高亮还有html的智能提示,同时又要支持nvelocity的语法高亮,这里如果全部都自己来实现,估计是一个

非常大的工程,由于本人时间不是很多,也没这么多的精力和脑力去开发这么一款工具,所以智能另辟路径了,所以这款插件有个不好的地方,就是安装后,所有的文件,比如cs文件 aspx页面 html页面等等只要遇到nvelocity的语法 关键字 都回

被语法高亮了,但是不影响使用的,这点我亲自试过,估计其他的页面很少出现这些语法关键字,就算出现也不妨碍的

下面提示一下开发的思路

首先需要安装vs sdk

还有你的vs 需要是英文版的

我们是在 editor class的模板下面进行开发

关键的一步就算分词了,就算扫描你的代码,检查含有和nvelocity的语法匹配的就进行上色

我们用到lex分词工具

%option unicode, codepage:raw%{        // User code is all now in ScanHelper.cs%}%namespace Shane%option verbose, summary, noparser, nofiles, unicode%{    public int nextToken() { return yylex(); }    public int getPos() { return yypos; }    public int getLength() { return yyleng; }%}//=============================================================//=============================================================number ([0-9])+chars [A-Za-z]cstring [A-Za-z_]blank " "delim [ \t\n]word {chars}+singleLineComment "##"[^\n]*multiLineComment "#*"[^*]*\*(\*|([^*/]([^*])*\*))*\#velocity \$[!]?[{]?[a-zA-Z\d._]+[}]?comment {multiLineComment}|{singleLineComment}keyword #set|#foreach|#if|#elseif|#else|#include|#parse|#macro|#even|#odd|#each|#end|{velocity}%%{keyword}            return (int)NvelocityEditor.NvelocityTokenType.Keyword;{comment}            return (int)NvelocityEditor.NvelocityTokenType.Comment;%%

 

同时有一点要注意的就是

分词的时候,最好不要先所有词语都上色,这样会覆盖了原来html的语法的

using System.ComponentModel.Composition;using System.Windows.Media;using Microsoft.VisualStudio.Text.Classification;using Microsoft.VisualStudio.Utilities;namespace NvelocityEditor{    [Export(typeof(EditorFormatDefinition))]    [ClassificationType(ClassificationTypeNames = "NvelocityText")]    [Name("NvelocityText")]    [UserVisible(true)]    [Order(Before = Priority.High)]    internal sealed class NvelocityTextFormatDefinition : ClassificationFormatDefinition    {        public NvelocityTextFormatDefinition()        {            this.DisplayName = "Nvelocity文本";            this.ForegroundColor = Colors.Brown;        }    }    [Export(typeof(EditorFormatDefinition))]    [ClassificationType(ClassificationTypeNames = "NvelocityComment")]    [Name("NvelocityComment")]    [UserVisible(true)]    [Order(Before = Priority.High)]    internal sealed class NvelocityCommentFormatDefinition : ClassificationFormatDefinition    {        public NvelocityCommentFormatDefinition()        {            this.DisplayName = "Nvelocity注释";            this.ForegroundColor = Colors.Green;        }    }    [Export(typeof(EditorFormatDefinition))]    [ClassificationType(ClassificationTypeNames = "NvelocityKeyword")]    [Name("NvelocityKeyword")]    [UserVisible(true)]    [Order(Before = Priority.High)]    internal sealed class NvelocityKeywordFormatDefinition : ClassificationFormatDefinition    {        public NvelocityKeywordFormatDefinition()        {            this.DisplayName = "Nvelocity关键字";            this.ForegroundColor = Colors.Black;            this.BackgroundColor = Colors.Yellow;        }    }}

插件下载地址:

 

感谢你的阅读,希望对你有帮助.....

转载地址:http://ftgrl.baihongyu.com/

你可能感兴趣的文章
智能健康行业突破不大,却走向“歪路”
查看>>
机器人也有触感了!斯坦福大学开发人工感觉神经系统让蟑螂抽搐
查看>>
5 Reasons Why You Should Try Kibana
查看>>
阿里云网络漏洞扫描系统AVDS(商业化)发布
查看>>
Pam认证模块
查看>>
解决tomcat"Could not reserve enough space for object heap"
查看>>
sersync实现主机实时双向同步
查看>>
卸载并重新配置mysql服务
查看>>
同行的一个案例---删除表,需要恢复
查看>>
python splinter 小坑说明
查看>>
JAVA并行框架:Fork/Join
查看>>
控制input输入格式
查看>>
linux系统上安装java
查看>>
38.进程管理与计划任务---PS、Top、Crontab
查看>>
0301_互连模拟
查看>>
一次XEN启动中的错误捕获
查看>>
重大里程碑:顶级科学家达成23条人工智能发展原则!
查看>>
esxi嵌套华为Fusioncomputer安装VRM几个关键步骤。
查看>>
/etc/init.d/mysqld:line 260:my_print_defaults:command not found
查看>>
DNS设置引起的登录延迟
查看>>