views:

310

answers:

3

I would really like to have VS2008 automatically indent the contents of a region. A sample would probably be best.

What is does now:

#region [ Fields ]
public int Count;
public int Total;
#endregion

What I'd like is:

#region [ Fields ]
    public int Count;
    public int Total;
#endregion

How can I get VS to do this?

EDIT: For what its worth, VS does do this in VB.NET.

+1  A: 

Out of the box? You can't.

Mitch Wheat
Any addin/tools that would do this?
Greg McGuffey
Not that I'm aware of. I don't use regions! I just checked ReSharper options, but couldn't see anything.
Mitch Wheat
A: 

Tools Options Text Editor C# Tabs Indent Smart Ident

Evaristo
This didn't actually indent the contents of a region (at least for me).
Greg McGuffey
+1  A: 

This macro should work, for the most part:

Public Sub IndentRegions()
    'Assume that the document has been smart formatted
    Dim TS As TextSelection = DTE.ActiveDocument.Selection
    TS.SelectAll()
    Dim lines As String() = TS.Text.Split(vbNewLine)

    Dim level As Integer = 0

    TS.StartOfDocument()

    While Not TS.ActivePoint.AtEndOfDocument

        If lines(TS.ActivePoint.Line - 1).Trim().StartsWith("#endregion") Then
            level = level - 1
        End If

        For i = 1 To level
            TS.Indent()
        Next

        If lines(TS.ActivePoint.Line - 1).Trim().StartsWith("#region") Then
            level = level + 1
        End If

        TS.LineDown()
        TS.StartOfLine()
    End While
End Sub
Todd Schiller
Thanks for the macro. This does do what is suggested (assuming you actually make a selection...DOH!): it indents the lines under a region. However, as soon as I use Ctl+K+D to reformat, the work is undone! So, for now, I'm just living with it.
Greg McGuffey
You could write a macro that performs the smart format and then runs indent regions. I don't know if you could re-map Ctrl+K D to the macro (I'm sure you could map another key sequence, though).
Todd Schiller
That's a good idea...I'm gonna try that.
Greg McGuffey