Create #region macro for C#

Yesterday I was playing around with VS.NET and discovered that I didn't use any macro's at all. So I opened up the macro IDE and looked at the examples. It's pretty easy stuff. So I wrote my first macro. It is a set of macro's actually which create #region ... #endregion sections. Two pre-defined ones for members and properties and a generic one which pops up an input box for the region name. Bind them to a hotkey and you're set. I'm pretty sure the code can be more efficient, it's my first attempt to write a macro that manipulates an active document in the IDE, so if I'm doing things in an inefficient way, that's the reason ;)

The initial macro I wanted to create was a create property macro which also created the related member variable in the region I've defined for that. Unfortunately, the IDE's code elements collection doesn't contain region sections, so this would be a hard thing to do efficiently.

The code is pasted below. Copy it into a new macro project and bind the macro's to hotkeys. Have fun :). Oh, you can of course modify the macro to work with VB.NET, but be aware of the fact that VB.NET doesn't support regions in a lot of places in your code.

Usage: select some lines in your code, call one of the macro's, and it's a region, baby :) You can also apply the macro's on empty selections.

''' Makes the selected lines the class property declarations region
Sub MakeMemberRegion()
	MakeRegion("Class Member Declarations")
End Sub

''' Makes the selection the class property declaration region.
Sub MakePropertyRegion()
	MakeRegion("Class Property Declarations")
End Sub

''' Makes a generic C# region of the selected lines and asks for the name of the region.
Sub MakeRegion()
	Dim regionName As String = InputBox("Region name: ")
	MakeRegion(regionName)
End Sub

Sub MakeRegion(ByVal regionName As String)
	Dim selection As TextSelection = DTE.ActiveDocument.Selection()
	Dim start As EditPoint = selection.TopPoint.CreateEditPoint()
	Dim endpt As EditPoint = selection.BottomPoint.CreateEditPoint()

	DTE.UndoContext.Open("Make Region")
	Try
		' insert the start of the region
		start.Insert("#region " & regionName)
		start.SmartFormat(start)
		Dim cursorPoint As EditPoint = start.CreateEditPoint()
		start.Insert(Environment.NewLine)

		If selection.IsEmpty Then
			start.Insert("#endregion")
			start.SmartFormat(start)
			start.Insert(Environment.NewLine & Environment.NewLine)
			Dim ts As TextSelection = DTE.ActiveWindow.Selection
			ts.MoveToPoint(cursorPoint)
		Else
			' insert the end of the region
			If Not (endpt.AtStartOfLine) Then
				endpt.Insert(Environment.NewLine)
			End If
			endpt.Insert("#endregion")
			endpt.SmartFormat(endpt)
			endpt.Insert(Environment.NewLine & Environment.NewLine)
		End If
	Finally
		DTE.UndoContext.Close()
	End Try
End Sub

1 Comment

  • Great!



    Just was I was looking for! My scenario is the same as yours (playing around bla bla) but I stopped after two minutes thinking that someone migth have done this already and put it on the net. Voilá! Here it was!



    Thanks a lot!

Comments have been disabled for this content.