Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indent content of region with C#?

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.

like image 326
Greg McGuffey Avatar asked Sep 25 '09 23:09

Greg McGuffey


People also ask

How do you indent an entire code?

Technically, it is fine to either indent using the tab key or with the space bar. Indenting once with the tab key means just pressing the tab key once. Indenting once with the space bar means pressing the space bar 4 times.


3 Answers

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
like image 86
Todd Schiller Avatar answered Sep 25 '22 09:09

Todd Schiller


Out of the box? You can't.

like image 23
Mitch Wheat Avatar answered Sep 21 '22 09:09

Mitch Wheat


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.

like image 25
RichC Avatar answered Sep 25 '22 09:09

RichC