17

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.

Greg McGuffey
  • 3,116
  • 3
  • 38
  • 58
  • 2
    might be worth reading: http://stackoverflow.com/questions/1476550/c-any-standard-way-to-divide-a-class-into-regions/1476582#1476582 – Mitch Wheat Sep 25 '09 at 23:58
  • 3
    Interesting comments and the Coding Horror blog was interesting. The comment that most interested me was the analogy with a surgeon. I.e. the patient is covered except where the surgeon will be working. That's how my mind works best, focus on the issue at hand. Thanks for the different point of view though! – Greg McGuffey Sep 26 '09 at 00:28
  • Typical C# programmer comments in this linked SO article! Even if you don't like regions (and I do, and miss the auto-indenting VB gave me), it's still a valid question! – Andy Brown Mar 06 '14 at 16:03

5 Answers5

6

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
  • 476
  • 7
  • 17
  • 1
    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 Sep 23 '10 at 05:02
  • 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 Sep 23 '10 at 14:47
4

Out of the box? You can't.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
4

Change the background color to another color other than the main page background (I used silver on top of white) and it will give it that indented feel that you are looking for.

Options > Environment > Fonts and Colors > Display Items: Preprocessor Keyword and then change the item background dropdown.

RichC
  • 7,829
  • 21
  • 85
  • 149
1

I would say the only way to simulate this is to use braces "{" and "}" inside regions, delimiting your code block. Unfortunatelly this only works inside Functions and Methods codes, hence this will not work for your example. In some cases, delimiting code blocks could be incompatible and require adjustments according with your code logic.

function Test(){

    #region Example
    {
        your code
    }
    #end region

}
-1

Tools Options Text Editor C# Tabs Indent Smart Ident