首先,有一個項目用到了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; } } }
插件下載地址:NvelocityEditor.vsix
感謝你的閱讀,希望對你有幫助.....