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


今天把前一阵跟同事一起做的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版本进行实际配置,也可以配置多个,这样这个插件的实用性就广的多。



 


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



 



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值