views:

317

answers:

5

I'm new to using Visual Studio 2008 IDE. Switching from vim. Is there a way to setup Visual Studio to automatically indent C comments /* */ such as:

/*<ENTER>

I want this:

/*
 * <CURSOR>

Edit: Seems like Visual Studio 2008 has this behavior for C# comments but not for C/C++ under: Text Editor > C# > Advanced > Generate XML documentation comments for ///

+1  A: 

It's my experience that it already does this.

BC
doesn't seem to be doing it for me.. must be my settings
Yada
The behavior is actually controlled through a macro. I believe it is GetCommentLinePrefix but not sure how it gets executed.
BC
+1  A: 

My comments are already automatically formatted like that, I think it is the default behaviour, but I have Visual Studio 2010 and ReSharper Installed. Unfortunately I can't find a setting in either one to turn the comment formatting on or off. However looking at the sample macros in Visual Studio 2010 you should be able to come up with a macro that will format you comments the way you like.

To find the sample macros

  1. Open Visual Studio
  2. Press Alt + F8 to open the Macro Explorer
  3. Expand the samples tab
  4. double click "VSEditor"

The most help full methods will be

  • NewCommentLine
  • GetCommentLinePrefix
TheLukeMcCarthy
I just fail to understand why VS does not do this by default. It is just a regular block comment.
Christopher Oezbek
I agree it should be like that out of the box, and for me it is. What makes it really frustrating is that I can't find a setting anywhere :-/ However macros are a great tool to have in your toolbox.
TheLukeMcCarthy
+2  A: 

You can use the following macro in your EnvironmentEvents project item. It should work for all tab styles (None,Block or Smart).

    Public Sub aftekeypress(ByVal key As String, ByVal sel As TextSelection, ByVal completion As Boolean) _
            Handles TextDocumentKeyPressEvents.AfterKeyPress

        If (Not completion And key = vbCr) Then
            Dim textDocument As TextDocument = DTE.ActiveDocument.Object("TextDocument")
            Dim startPoint As EditPoint = TextDocument.StartPoint.CreateEditPoint()
            startPoint.MoveToLineAndOffset(sel.ActivePoint.Line - 1, 1)
            Dim lines = startPoint.GetLines(sel.ActivePoint.Line - 1, sel.ActivePoint.Line)
            If lines.LastIndexOf("*") = 1 And lines.LastIndexOf("/") <= 0 Then
                If lines.LastIndexOf("/") = 0 Or _
                   DTE.Properties("TextEditor", "C/C++").Item("IndentStyle").Value = 0 Then
                    sel.Insert(" ")
                End If
                sel.Insert("* ")
            End If
        End If
    End Sub
Rod
Just pasting this does not do anything. Do I need to register it in any way?
Christopher Oezbek
You don't need to register it. You need to make sure you are in your `EnvironmentEvents` module inside MyMacros. You can only access this module in the Macro Explorer (Tools->Macros->Macro Explorer)
Rod
Nice! But it only works if the comments are not indented (lastIndexOf("*")...).
Christopher Oezbek
+1  A: 

It's not quite the same, but you could try the Comment Reflower addin:

http://www.kynosarges.de/CommentReflower.html (VS2008+ version) http://commentreflower.sourceforge.net/ (original version for VS2005)

This works in the emacs style; you type your comments in any old how, then type the shortcut of your choosing (or select it from the menu, if you insist) whilst the cursor is in the comment, and the comment is wrapped then. This isn't especially slick -- I think vim does this for you as you type, doesn't it? -- but it's reasonable enough and you soon get used to it.

I'm not in Windows at the moment, so I can't check, but from memory, whilst this won't format your C comments exactly as requested, it will produce something quite reasonable, along these lines:

/* This is what Comment Reflower will do with your multi-line
 * C comments, as far as I can remember.
 */

Also has support for pre-formatted regions, various doxygen formatting items, and various types of bullet point. (The options dialog is well worth a visit.)

Personally, I really rate it, and have rued the lack of anything similar in Xcode. (The iPhone code files I write are full of œ chars, from when I press the shortcut key out of habit.) My vim days are behind me, but I certainly prefer it to the emacs equivalent. (And I first encountered it when I was using vim, though this was a while ago now, and I don't remember finding it too awful at the time -- so presumably it stands up pretty well to the vim commenting functionality, too.)

(As a parting note -- there's a sample macro supplied with the Visual Studio 2005 sample macros set that claims to wrap comments for you. Perhaps it's still around in 2008? Anyway, it does an OK job, and I used it myself for some time, but it has a tendency to make a mess of anything more complicated than a line containing nothing but a C++ //-style comment. Comment Reflower is much better.)

brone
+1  A: 

Visual Assist X does the trick. It's the ReShaper for C/C++ and does better than ReSharper for C/C++. You'll have to configure the snippet "/**" so that it works well.

PS: In that snippet config you can even auto-generate the documentation as you'd like it. Something like:

Wernight
How to enable this snipped?
Christopher Oezbek
Edit the existing `/**` one (which does a `/***************************/`) from the VAX configuration.
Wernight
Also see the "Auto-extend multi-line comments" option on the Advanced | Corrections page of the VA Options dialog.
sean e