开发者

VS 2010 Extensibility: Create a extension to automatically wrap selected text (code) in comments and adding a comment above it

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开发者_如何学C'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?


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.


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜