VS 2010 可扩展性:创建一个扩展以自动将选定的文本(代码)包装在注释中并在其上方添加注释

发布于 2024-10-25 08:54:56 字数 583 浏览 7 评论 0原文

我正在尝试开发一个扩展,其工作方式类似于 VS 2010 中的“注释”工具栏按钮,但我想将所有文本标记为“已注​​释”,并在其上方添加注释。

这是一个例子。我知道这很简单,但它更容易。我的想法是有一些额外的工具栏按钮来标记不再使用的代码,标记有错误的代码......以及类似的事情......这可以在任务窗口中找到,因为它以 TODO:

        // TODO MARTIN CODE NO LONGER USED
        /*if (myItem)
        {
            txtTest.Enabled = false;
            txtTest1.Value = 0;
            btnOk.Enabled = false;
        }*/

I 开头假设我需要使用:

       DTE.ActiveDocument

和:

       (((TextDocument)myDoc).Selection.Text).

然后再次写出代码,我需要做什么?

I am trying to develop an extension that will work similar to the Comment toolbar button in VS 2010, but I want to mark all text as Commented Out and put a comment above it.

Here's an example. I know it's simple, but it's just a lot easier. My idea is to have a number of additional toolbar buttons to mark code no longer used, mark code that has bugs ... and things like that... and this can be picked up in the Task window because it starts with TODO:

        // TODO MARTIN CODE NO LONGER USED
        /*if (myItem)
        {
            txtTest.Enabled = false;
            txtTest1.Value = 0;
            btnOk.Enabled = false;
        }*/

I presume I need to use:

       DTE.ActiveDocument

and:

       (((TextDocument)myDoc).Selection.Text).

Then to write out the code again, what do I need to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有心的人 2024-11-01 08:54:56

这并不完全是您问题的答案,但这是您可以考虑的另一种选择。您可以定义宏来执行您需要的每个操作,然后将它们分配给工具栏按钮。一个示例宏是:

Sub TODOComment()
    DTE.ExecuteCommand("Edit.CommentSelection")
    DTE.ActiveDocument.Selection.LineUp()
    DTE.ActiveDocument.Selection.EndOfLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "// TODO "
End Sub

这使用内置的注释部分功能,然后转到选择之前的行,在行末尾插入回车符并添加“TODO”注释。所以有明显的缺点(不要在文件顶部运行它),但它或多或少会做你所追求的事情。取消注释本质上是相同的(取消所有内容的注释,然后删除顶行)。

我不太了解自动化引擎,所以如果我想学习如何做这类事情,我倾向于做的是记录一个临时宏,执行我感兴趣的活动,然后调整输出以获得我所追求的结果。它往往可以节省大量时间来查找并不总是显而易见的文档。

This isn't exactly an answer to your question, but it is another option that you could consider. You can define macro's that do each of the operation you need, then assign them to toolbar buttons. An example macro would be:

Sub TODOComment()
    DTE.ExecuteCommand("Edit.CommentSelection")
    DTE.ActiveDocument.Selection.LineUp()
    DTE.ActiveDocument.Selection.EndOfLine()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "// TODO "
End Sub

This uses the built in comment out section functionality, then goes up to the line before the selection, inserts a return at the end of the line and adds a 'TODO' comment. So there are obvious shortcomings (don't run it at the top of the file), but it would do more or less what you're after. Uncomment would be essentially the same (uncomment everything, then delete the top line).

I don't know the automation engine that well, so what I tend to do if I want to learn how to do this sort of thing is record a temporary macro, perform the activities I'm interested in, then tweak the output to get the results I'm after. It tends to save quite a bit of time hunting through the not always obvious documentation.

唠甜嗑 2024-11-01 08:54:56

只需录制一个临时宏即可执行您想要的操作,然后将其粘贴到宏中,并根据需要进行调整。在这种情况下,我认为这就是您想要做的:

 DTE.ExecuteCommand("Edit.CommentSelection")
    DTE.ActiveDocument.Selection.LineUp()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "// TODO 无论如何"

Just record a temporary macro to do whatever you want, and paste that into your macro, adjusting as necessary. In this case, I think this is what you wanted to do:

    DTE.ExecuteCommand("Edit.CommentSelection")
    DTE.ActiveDocument.Selection.LineUp()
    DTE.ActiveDocument.Selection.NewLine()
    DTE.ActiveDocument.Selection.Text = "// TODO Whatever"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文