• 自己开发Visual studio插件-一个nvelocity高亮插件


    首先,有一个项目用到了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 [ 	
    ]
    word {chars}+
    singleLineComment "##"[^
    ]*
    multiLineComment "#*"[^*]**(*|([^*/]([^*])**))*#
    velocity $[!]?[{]?[a-zA-Zd._]+[}]?
    
    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

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

  • 相关阅读:
    基于Python的数据分析(1):配置安装环境
    Learn flask in the hard way:配置环境的安装
    网络云盘的存储机制
    读书笔记:云计算概念、技术和架构
    小型开发团队中项目管理的方法及原则
    华为专家谈CMDB建设
    2018软件工程第二次作业——个人项目
    福大软工1816 · 第一次作业
    python学习摘要(3)--字符串处理函数
    python学习摘要(4)--列表简单处理
  • 原文地址:https://www.cnblogs.com/maijin/p/4605140.html
Copyright © 2020-2023  润新知