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

2

6

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?

Buddybuderus answered 22/3, 2011 at 12:26 Comment(0)
H
4

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.

Hangeron answered 29/3, 2011 at 8:9 Comment(0)
L
0

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"
Luminary answered 31/3, 2011 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.