|
Sub MakeRegion()
Regions.MakeRegion()
End Sub
Public Class Regions
' MakeRegion inserts #region and #endregion tags
' around selected text in the VS editor.
Shared Sub MakeRegion()
Dim rName As String = ""
Dim pad As String = ""
Dim junk As String
Dim count, i As Integer
Dim startpoint, endpoint, tmppoint 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
'IV 2004-12-13: rName = InputBox("Region Name:")
rName = InputBox("Region Name:", "Pick a name", _
GetDesc(DTE.ActiveDocument.Selection.TopPoint.CreateEditPoint()))
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
' IV: Get the description from the 1st line of code in the region
' i.e. ignore c# comment tags (///) or take 1st line of the comments (//)
' Requires adjustments for VB and other langs
Private Shared Function GetDesc(ByVal startpoint As EditPoint) As String
Dim line As String = ""
Dim tmppoint As EditPoint
line = startpoint.GetText(startpoint.LineLength)
If (line.Length > 0) Then
line = line.TrimStart(" ", vbTab)
If DTE.ActiveDocument.Language = "CSharp" Then
If (line.StartsWith("///")) Then
tmppoint = startpoint
tmppoint.LineDown()
line = GetDesc(tmppoint)
ElseIf (line.StartsWith("//")) Then
line = line.TrimStart("//", " ")
End If
line = line.Replace("{", String.Empty)
End If
line = line.TrimEnd(" ", vbTab)
End If
Return line
End Function
End Class
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.