基于Visual Studio AddIns(插件)的形式开发的C#及JS的代码注释工具

来源:互联网 发布:相册制作软件下载 编辑:程序博客网 时间:2024/06/05 20:03


今天把前一阵跟同事一起做的Visual Studio代码注释小工具分享一下,目前主要包含如下两个:C#类及方法注释、JS的方法注释,主要采用的Visual StudioAddIns机制实现的,大致过程表述如下:


文件新建项目其他项目类型扩展性Visual Studio 外接程序,修改名称、位置、解决方案名称等信息,点击确定




熟悉后也可以不用使用向导创建 项目。


  


  下一步,选择使用C#创建外接程序


  下一步,选择使用应用程序主机,有两个选项,MicrosoftVisual Studio 2010 Microsoft Visual Studio2010 Macros(),两个都勾选


  下一步,填写外界程序名称和说明


  下一步,在选择外接程序选项中,在是否为外接程序创建命令栏用户界面?中勾选是的,创建工具菜单项,向导会为你在工具菜单中创建一个菜单


  下一步,关于信息,根据需要勾选是否生成关于对话框


  下一步,完成


这样系统向导就为你创建了一个项目,并且生成了一个Connect类,实现IDTExtensibility2IDTCommandTarget接口,具体的接口方法说明如下: 


OnConnection实现 IDTExtensibility2接口的 OnConnection方法。接收正在加载外接程序的通知。
  OnDisconnection实现 IDTExtensibility2接口的OnDisconnection方法。接收正在卸载外接程序的通知。
  OnAddInsUpdate实现 IDTExtensibility2接口的OnAddInsUpdate方法。当外接程序集合已发生更改时接收通知。
  OnStartupComplete实现 IDTExtensibility2接口的OnStartupComplete方法。接收宿主应用程序已完成加载的通知。
  OnBeginShutdown实现 IDTExtensibility2接口的OnBeginShutdown方法。接收正在卸载宿主应用程序的通知。
  QueryStatus实现 IDTCommandTarget接口的 QueryStatus方法。此方法在更新该命令的用性时调用。
  Exec实现 IDTCommandTarget接口的 Exec方法。此方法在调用该命令时调用。


接下来我们写C#类及方法的注释工具里两个主要方法代码复制如下:

  public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)        {            _applicationObject = (DTE2)application;            _addInInstance = (AddIn)addInInst;            if (connectMode == ext_ConnectMode.ext_cm_Startup)            {                object[] contextGUIDS = new object[] { };                Commands2 commands = (Commands2)_applicationObject.Commands;                string toolsMenuName = "Tools";                //将此命令置于“工具”菜单上。                //查找 MenuBar 命令栏,该命令栏是容纳所有主菜单项的顶级命令栏:                Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar =                     ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];                try                {                    //在 MenuBar 命令栏上查找“工具”命令栏:                    CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];                    CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;                    //如果希望添加多个由您的外接程序处理的命令,可以重复此 try/catch 块,                    //  只需确保更新 QueryStatus/Exec 方法,使其包含新的命令名。                    //将一个命令添加到 Commands 集合:                    Command command = commands.AddNamedCommand2                        (_addInInstance, "AddinElitelCodeCommentsAdd", "A01类注释(&1)", "添加类、方法、属性等的注释",                        true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,                         (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);                    //将对应于该命令的控件添加到“工具”菜单:                    command.AddControl(toolsPopup.CommandBar, 1);                }                catch (System.ArgumentException ex)                {                    //如果出现此异常,原因很可能是由于具有该名称的命令                    //  已存在。如果确实如此,则无需重新创建此命令,并且                    //  可以放心忽略此异常。                    MessageBox.Show(string.Format("加载代码注释工具失败:{0}", ex.Message));                }            }        }public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)        {            handled = false;            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)            {                if (commandName == "AddinElitelCodeComments.Connect.AddinElitelCodeCommentsAdd")                {                    EnvDTE.TextSelection LTRead = null;                    LTRead = this._applicationObject.DTE.ActiveDocument.Selection as EnvDTE.TextSelection;                    if (LTRead == null)                    {                        handled = false;                        return;                    }                    handled = true;                    string writer = "Anders lu"; //创作者名字                    string nowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //当前时间                    LTRead.NewLine();                    LTRead.Text = string.Format("///作    者:{0}", writer);                    LTRead.NewLine();                    LTRead.Text = string.Format("创建时间:{0}", nowTime);                    LTRead.NewLine();                    LTRead.Text = string.Format("描   述:");                    LTRead.NewLine();                    LTRead.Text = string.Format("修改履历:");                    LTRead.NewLine();                    LTRead.Text = string.Format("版   本    修改时间    修改人    变更内容");                    LTRead.NewLine();                    LTRead.Text = string.Format("1.0    {0}    {1}    新增", nowTime, writer);                    LTRead.NewLine();                    return;                }            }        }


最后把写好的程序编译后的成果物(一个dll文件,一个AddIn配置文件)放到如下目录下即可把插件安装进VisualStudio



其中*.AddIn里一个有一个节点大家要注意一下,参见如下截图,要根据使用的Visual Studio版本进行实际配置,也可以配置多个,这样这个插件的实用性就广的多。



 


代码中实际使用效果如下:



 



1 0
原创粉丝点击