' MakeRegion inserts #region and #endregion tags
' around selected text in the VS editor.
Sub MakeRegion()
Dim rName As String = ""
Dim pad As String = ""
Dim junk As String
Dim count, i As Integer
Dim startpoint, endpoint As EditPoint
With DTE.ActiveDocument.Selection
startpoint = .TopPoint.CreateEditPoint()
endpoint = .BottomPoint.CreateEditPoint
End With
If startpoint.EqualTo(endpoint) Then
Exit Sub
End If
'ELR: ADDED THIS, to move the startpoint to the start of the line
'so that the Pad function works correctly
If Not startpoint.AtStartOfLine Then
startpoint.StartOfLine()
End If
rName = InputBox("Region Name:")
DTE.UndoContext.Open("Insert A Region")
Try
junk = startpoint.GetText(startpoint.LineLength)
pad = String.Empty
For count = 0 To junk.Length - 1
If junk.Substring(count, 1).Equals(" ") Or _
junk.Substring(count, 1).Equals(vbTab) Then
pad += junk.Substring(count, 1)
Else
Exit For
End If
Next
'ELR: ADDED Test for Languages
If DTE.ActiveDocument.Language = "CSharp" Then
' C Sharp Code
startpoint.Insert(String.Format("{0}#region {1}{2}", pad, rName, vbCrLf))
if endpoint.LineLength=0 then
endpoint.Insert(String.Format("{0}#endregion // {1}{2}", pad, rName, vbCrLf))
Else
endpoint.Insert(String.Format("{0}#endregion // {1}{2}", vbCrLf & pad, rName, vbCrLf))
End If
Else
' VB Code
startpoint.Insert(String.Format("{0}#Region ""{1}""{2}", pad, rName, vbCrLf))
If endpoint.LineLength = 0 Then
endpoint.Insert(String.Format("{0}#End Region '{1}{2}", pad, rName, vbCrLf))
Else
endpoint.Insert(String.Format("{0}#End Region ' {1}{2}", vbCrLf & pad, rName, vbCrLf))
End If
End If
Finally
DTE.UndoContext.Close()
End Try
End Sub