Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'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?

like image 634
Martin Avatar asked Mar 22 '11 12:03

Martin


1 Answers

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.

like image 97
forsvarir Avatar answered Sep 28 '22 00:09

forsvarir