次の方法で共有


Add #Region Macro

Another macro from Devhawk allows you to quickly add your own regions to code.

From his site: https://www.devhawk.net/art_addregion.aspx

In order to make it easier to create regions in my code, I wrote this macro. It asks the developer for a region name, then inserts #region and #endregion statements before and after the currently selected text. So if I want to create a region around four methods, I just select them, run the macro, choose a name and it's done.

 Sub AddRegion()

    Dim CloseUndoContext As Boolean = False



    If DTE.UndoContext.IsOpen = False Then

        CloseUndoContext = True

        DTE.UndoContext.Open("DevHawkAddRegionMacro", False)

    End If



    Try

        Dim name As String = InputBox("Enter the region name")



        If (name.Trim().Length = 0) Then Exit Sub



        Dim sel As TextSelection = DTE.ActiveDocument.Selection



        Dim lTop = sel.TopPoint.Line

        Dim lBot = IIf(sel.BottomPoint.AtStartOfLine, sel.BottomPoint.Line - 1, sel.BottomPoint.Line)



        sel.MoveToLineAndOffset(lBot, 1)

        sel.EndOfLine()

        sel.NewLine()



        If DTE.ActiveDocument.Language = "CSharp" Then

            sel.Text = "#endregion"

        ElseIf DTE.ActiveDocument.Language = "Basic" Then

            sel.Text = "#End Region"

        Else

            Throw New System.Exception("Invalid Language: " + DTE.ActiveDocument.Language)

        End If



        sel.MoveToLineAndOffset(lTop, 1)

        sel.StartOfLine()



        If DTE.ActiveDocument.Language = "CSharp" Then

            sel.Text = "#region " + name.Trim()

        ElseIf DTE.ActiveDocument.Language = "Basic" Then

            sel.Text = "#Region """ + name.Trim() + """"

        Else

            Throw New System.Exception("Invalid Language: " + DTE.ActiveDocument.Language)

        End If



        sel.NewLine()

    Finally

        If CloseUndoContext Then DTE.UndoContext.Close()

    End Try

End Sub
 Here is some more information on using macros in the IDE. 

Comments

  • Anonymous
    July 05, 2004
    Very useful macro!
  • Anonymous
    July 19, 2004
    This is a great macro!! Really useful!

    But the one thing it doesn't do is to mimic the indentation of the section (imagine '->' is a tab) this is what happens:

    #region test1
    ->->->my code ()
    ->->->{
    ->->->}
    ->->->#end region

    this is what would be perfect:

    ->->->#region test1
    ->->->my code ()
    ->->->{
    ->->->}
    ->->->#end region

    thanks,

    john.e.boy
  • Anonymous
    July 23, 2004
    Thanks so much.
  • Anonymous
    July 24, 2004
    Add #Region Macro