|
Every time I install Visual Studio afresh I have to hunt down this macro. I use it so much I forget that it isn't part of Visual Studio.
Great work. 
|
|
|
|
|
If you're a C# developer, you owe it to yourself to buy Resharper.
You want a region? Highlight the code and hit ctrl-alt-j and select Region from the list.
Resharper is a definite productivity aid for me, although I'm not entirely pleased with the VS.NET 2k5 implementation of it (and I really dislike VS.NET 2k5, mostly because it's so dang buggy and crashes frequently).
|
|
|
|
|
This macro is faster (less keystrokes) allows you to tweak it to do regions as you wish, and is free.
Plenty of reasons to use this macro rather than resharper.
Phillip H. Blanton
www.ComponentScience.net
Email: Phillip at that domain that you see one line above the line you are reading now.
|
|
|
|
|
Right, but have you used Resharper? I mean, sure this script is super customizable, but I've got better things to be doing than customizing a script. I just want to get my work done and then get out of the office
It does *way* more than just regions... If you were a java developer and were used to Idea, then Resharper makes the .net transition that much easier.
From a macros perspective:
ctrl-n to open a class (from anywhere)
alt-insert to insert: Constructor, Read Property, Write Property, Read/Write Property, Implement Interface Member, Override Inherited Member, Generate Delegating members
ctrl-alt-j to Surround with: if, while, for, do..while, lock, #region, {}, try..catch, try..finally, cast.
ctrl-d: dupe the current line/highlighted text (no copy/paste)
ctrl-/: comment out a line/uncomment out a line
alt-insert: add namespace using lines as needed for unknown types (this is a lifesaver)
Not to mention all the code completion differences and the vb like compile behind telling you immediately that code won't compile. This is the #1 best feature of R# IMO (and it's somewhat broken in 2k5 which bugs me to no end).
I think it would take a lot of work to write custom scripts/macros for all this. Resharper (esp. for vs.net2k3) is well worth the money and productivity enhancements.
Another benefit to a COTS component is that your entire development team can be standardized so that you only have to give one training class on the tool and everyone then talk the same "language."
There are some built in macros for some of this stuff in vs.net 2k5, but they don't work nearly as nicely (like the one for adding properties) IMO.
I'm not knocking the macro you wrote, but was just wanting to make sure your readers knew that there was a COTS tool that would do similar things + a lot more (for a cost of course).
|
|
|
|
|
By the way, I use ReSharper now and still use my macro for regions.
Phillip H. Blanton
|
|
|
|
|
Public Module MakeTryCatch
Sub MakeTryCatch()
Regions.MakeTryCatch()
End Sub
Public Class Regions
' MakeTryCatch inserts Try and Catch tags
' around selected text in the VS editor.
Shared Sub MakeTryCatch()
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
rName = InputBox("MessageBox?: (S/N)", "Utiliza Mensaje de Exceptions", "N") ', _
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}try {1}", pad, vbCrLf))
startpoint.Insert(String.Format("{0} {1}", pad & Chr(123), vbCrLf))
If endpoint.LineLength = 0 Then
endpoint.Insert(String.Format("{0} {1}", pad & Chr(125), vbCrLf))
Else
endpoint.Insert(String.Format("{0} {1}", vbCrLf & pad & Chr(125), vbCrLf))
End If
endpoint.Insert(String.Format("{0}catch(Exception ex) {1}", pad, vbCrLf))
endpoint.Insert(String.Format("{0} {1}", pad & Chr(123), vbCrLf))
If (rName.ToUpper.Equals("S")) Then
endpoint.Insert(String.Format("{0}MessageBox.Show(ex.Message); {1}", pad & vbTab, vbCrLf))
Else
endpoint.Insert(String.Format("{0}throw ex; {1}", pad & vbTab, vbCrLf))
End If
endpoint.Insert(String.Format("{0} {1}", pad & Chr(125), vbCrLf))
Else
' VB Code
startpoint.Insert(String.Format("{0}Try {1}", pad, vbCrLf))
If endpoint.LineLength = 0 Then
endpoint.Insert(String.Format("{0}Catch ex As Exception {1}", pad, vbCrLf))
Else
endpoint.Insert(String.Format("{0}Catch ex As Exception {1}", vbCrLf & pad, vbCrLf))
End If
If (rName.ToUpper.Equals("S")) Then
endpoint.Insert(String.Format("{0}MsgBox(ex.toString) {1}", pad & vbTab, vbCrLf))
Else
endpoint.Insert(String.Format("{0}Throw ex {1}", pad & vbTab, vbCrLf))
End If
endpoint.Insert(String.Format("{0} {1}", pad & Chr(125), vbCrLf))
End If
Finally
DTE.UndoContext.Close()
End Try
dmateoc
|
|
|
|
|
|
Hi,
Thanks for the nice macro! A small request:
I prefer a space between #region and the first line of its content as well as between its last line and #endregion.
Like this:
#region blabla
[code]
#endregion
When I include an empty line before and after the code block in the
selection, the macro should write the #(end)region with the padding of
the code, not at the start of the line.
Can somebody help me to implement this behavior? I don't know how to do it in VB.
Thanks.
Nico
|
|
|
|
|
This is pretty easy to do. Firstly I wold suggest that since you already have to select the method before pressing the keystroke to apply a region, then you can just select an extra line above and below the method.
The other way to do it would be to modify the script such that it adds the extra lines for you. Here is the relevant section...
'ELR: ADDED Test for Languages
If DTE.ActiveDocument.Language = "CSharp" Then
' C Sharp Code
startpoint.Insert(String.Format("{0}#region {1}{2}{2}", pad, rName, vbCrLf))
If endpoint.LineLength = 0 Then
endpoint.Insert(String.Format("{2}{0}#endregion //{1}{2}", pad, rName, vbCrLf))
Else
endpoint.Insert(String.Format("{2}{0}#endregion //{1}{2}", vbCrLf & pad, rName, vbCrLf))
End If
Else
' VB Code
startpoint.Insert(String.Format("{0}#Region {1}{2}{2}", pad, rName, vbCrLf))
If endpoint.LineLength = 0 Then
endpoint.Insert(String.Format("{2}{0}#End Region '{1}{2}", pad, rName, vbCrLf))
Else
endpoint.Insert(String.Format("{2}{0}#End Region '{1}{2}", vbCrLf & pad, rName, vbCrLf))
End If
End If
Phillip H. Blanton
www.ComponentScience.net
Email: Phillip at that domain that you see one line above the line you are reading now.
|
|
|
|
|
one can continiously improve the script, so he's another small bit.
these days i find myself using p/invokes and attributes more often. to handle those you may want to add the following:
...<br />
ElseIf (line.StartsWith("[") Or line.EndsWith("]")) Then<br />
tmppoint = startpoint<br />
tmppoint.LineDown()<br />
line = GetDesc(tmppoint)<br />
...<br />
if you can be bothered you can check if it takes more than 1 line too :->
|
|
|
|
|
need a update
Regards,
Arun Manglick
|
|
|
|
|
need a update
Regards,
Arun Manglick
|
|
|
|
|
I really need to update the original article with the latest source. I am going to try to contact the Code Project people and see if there is an easy way for me to modify the article. For now, if you want to see the updated article, you can get it off of My blog.
Thanks for all the input guys. This is a heck of a lot of fun for me and it has resulted in a really cool macro that I am finding wonderfully useful.
I want to thank everyone who has tossed in their input.
Phillip H. Blanton
www.ComponentScience.net
Email: Phillip at that domain that you see one line above the line you are reading now.
|
|
|
|
|
Hey evbd, I'm just a little bit confused by reading these posts. I noticed tht the author said tht he wasn't able 2 edit this document n then i saw so many more suggestions by other users as well. So, does anybody have the latest file with ALL snippets in it??
Thnxz in advance,
Blue Demon.
|
|
|
|
|
 there you go. that's what i have (I added SvenRieke's mod just for you but)
;);)
Imports EnvDTE
Imports System.Diagnostics
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 ( ' 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
|
|
|
|
|
 There is a buglet in your posting. The following line...
rName = rName = InputBox("Region Name:", "Pick a ... snip
is returning either TRUE or FALSE depending on the contents of the input box. This results in every region being named TRUE or FALSE.
Here is the full source code for the macro. If you have followed the directions in the original article, then you will need to replace the stuff in between the
Public Module
and...
End Module
with this...
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 ( ' 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
Phillip H. Blanton
www.ComponentScience.net
Email: Phillip at that domain that you see one line above the line you are reading now.
|
|
|
|
|
Hi there, good example.
I've made a little variation for I didn't like typing extra stuff in the inputbox and prefer the macro to pick the 1st line of the region (i.e. name of the funcion).
It may be useful for some.
...
'IV 2004-12-13: rName = InputBox("Region Name:")
rName = GetDesc(DTE.ActiveDocument.Selection.TopPoint.CreateEditPoint())
...
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
' ignore comments block
If (line.StartsWith("///")) Then
tmppoint = startpoint
tmppoint.LineDown()
line = GetDesc(tmppoint)
' comments block, strip
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
One point of interest thou - there's no way of getting the newly created region collapsed, is there? 
|
|
|
|
|
Nice macro, and I like this variation.
But I added the code like this to be more flexible:
<br />
rName = InputBox("Region Name:", "Pick a name", GetDesc(DTE.ActiveDocument.Selection.TopPoint.CreateEditPoint()))<br />
So it still opens the input box, but fills the textbox with the first line of the selected code.
So you can change it or just press enter to use it.
|
|
|
|
|
if I select my code till the latest } the #endregion is written on the same line as the }, while to work correctly it should have been written after a vbCrLf.
2 solutions:
1 - always add a vbCrLf (so if someone select till the line after the code than it will add one more cr)
2 - verify if the last selected char is a cr: if it is not than add a vbCrLf, otherwise it's ok this way
Simone
|
|
|
|
|
I made solution number 2:
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
Simone
|
|
|
|
|
I know of that issue, but I prefer to select the code using the Shift key and the down arrow, or dragging my mouse from the beginning of the top line down to just below the last mine, that way I don't get the extra blank line before #endregion.
Your solution fixes it for everyone though, so that is great!
Thanks!
I just gotta say, I am loving how much better this little macro has gotten in the last week. This is definitely a fun way to work.
Phillip H. Blanton
www.ComponentScience.net
Email: Phillip at that domain that you see one line above the line you are reading now.
|
|
|
|
|
Use
ElseIf DTE.ActiveDocument.Language = "Basic" Then
to make sure the language is VB.
Further, include
' VB Code<br />
startpoint.Insert(String.Format("{0}#Region ""{1}""{2}", pad, rName.Replace("""", """"""), vbCrLf))
to escape double quotes.
Good work though, buddy!
|
|
|
|
|
Also, for support of "C" and "C++" source documents, the line
If DTE.ActiveDocument.Language = "CSharp" Then
can be changed to:
If DTE.ActiveDocument.Language = "CSharp" OR DTE.ActiveDocument.Language = "C/C++" Then
----------
Nice article. - Thanks for showing the power of macros in VS.
John
|
|
|
|
|
I can't edit this article now that it has been moved out of Unedited Reader Contributions, so I have posted the updated macro code to the first reply to this message. The macro was updated to support VB, tabs, and to pad properly.
I am going to try to enclose the code in <pre></pre> tags so that it will display properly. I hope it works.
Phillip H. Blanton
www.ComponentScience.net
Email: Phillip at that domain that you see one line above the line you are reading now.
|
|
|
|
|

' 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))
endpoint.Insert(String.Format("{0}#endregion // {1}{2}", pad, rName, vbCrLf))
Else
' VB Code
startpoint.Insert(String.Format("{0}#Region ""{1}""{2}", pad, rName, vbCrLf))
endpoint.Insert(String.Format("{0}#End Region '{1}{2}", pad, rName, vbCrLf))
End If
Finally
DTE.UndoContext.Close()
End Try
End Sub
Phillip H. Blanton
www.ComponentScience.net
Email: Phillip at that domain that you see one line above the line you are reading now.
|
|
|
|
|