Regionerate (pronounced ri-jeh-neh-rate) is a new open-source tool for developers and team leaders that allows you to automatically
apply layout rules on C# code. Watch how it works.
Regionerate runs on Visual Studio 2005, Visual Studio 2008, #develop 2.0, NAnt and on command line.
News
Spacing,
published Monday, September 29, 2008
You can easily tweak the way Regionerate spaces your code.
Padding the First and Last Child
You can set the amount of new lines that will be added before the first child and after the last child of every region by setting the PadFirstChild and PadLastChild attributes on the CreateRegion element.
For example, this Code Layout snippet will place 2 new lines between the region's declaration and its members:
The default value for PadFirstChild and PadLastChild is 1, meaning that if you don't specify any other value, the items will be padded with a single line.
Separating Consecutive Children
You can also control the amount of new lines between two consecutive children. This can be done at the ForEach level and at the Put level by using the SeparatingLines attribute.
For example, this Code Layout snippet will put 2 separating lines between every two consecutive regions inside an interface:
Here's a short screencast I created that demonstrates how I use free productivity enhancers such as GhostDoc (by Roland Weigelt), Regionerate and Visual Studio's built-in refactoring tools to quickly create and maintain skeleton code. It also demonstrates the usage of Regionerate's new Code Layout Browser.
I know there are some commercial tools that do more, but I'm just presenting a free (partial-)alternative. Make sure your speakers are turned on.
If you have a custom Code Layout and you want to make it accessible through the Code Layout Browser, all you need to do is place it in the "My Code Layouts" folder under your installation folder. (Typically "C:\Program Files\Regionerate\My Code Layouts")
The file must have an .xml extension. The label inside the Code Layout Browser will be the same as the file name just without the extension. ("My Custom Code Layout.xml" will be shown as "My Custom Code Layout")
If you want to have a custom image assigned to it, just place a 128x128 png file in the same directory with the same name. For example, if you want to have a custom image for "My Custom Code Layout.xml", just name your image "My Custom Code Layout.png". If you don't place a custom image, the default image will be used.
Remember: if you have a custom Code Layout and want to use it as your primary Code Layout (the primary Code Layout is the one that will be accessible when hitting the keyboard shortcut without holding it down), use the settings dialog to select it.
Configuration,
published Saturday, November 10, 2007
The Configuration element in your Code Layout document can be used to configure certain settings that define Regionerate's behavior. These settings are shared between all layouts inside the Code Layout document.
The Symbol element can be used to determine which symbol is embedded in your regions. (What Are Symbols?)
Use the Unpack element to open certain existing regions. (Read more about unpacking)
Rendering options:
ShowCount - Determines if Regionerated regions will automatically and recursively count their children.
Tabs - The amount of tab characters Regionerated regions declarations have.
The term "Unpacking" refers to a pre-processing stage that occurs before your code is analyzed and constructed. When Regionerate unpacks your code, it strips up all members from regions which are managed by it ("Regionerated regions"). After the code is shaped according to the Code Layout, these members are packed back into their regions.
While Regionerate automatically handles Regionerated regions, it allows you to specify which additional regions should be unpacked.
For example, you might have some regions that were generated by the IDE or a 3rd party tool for default interface implementations, such as:
1: publicclass Foo : IList<string>
2: {
3: #region IList<string> Members
4:
5: ...
6:
7: #endregion
8:
9: #region ICollection<string> Members
10:
11: ...
12:
13: #endregion
14: }
You can use the Unpack element under Configuration inside your Code Layout document to tell Regionerate to unpack any interface that matches a regular expression. For example:
<Configuration>
...
<Unpack>
<RegionsThatMatch="I.*\sMembers"/>
</Unpack>
</Configuration>
If you use the unpack command as above, every time you implement an interface, it will be unpacked and merged into the layout.
1: publicclass Foo : IList<string>
2: {
3:
4: #region Properties (3)
5:
6: ...
7:
8: #endregion Properties
9:
10: #region Methods (10)
11:
12: ...
13:
14: #endregion Methods
15:
16: }
Another possible use for unpacking is when migrating between symbols. If you used the Prefix symbol (the only option in versions prior to v0.6.6.5) long enough you have a code base filled with regions that look like:
#region [rgn] Fields (3)
If you change the symbol (lets say, from a Prefix to a Hidden Dragon), Regionerate will unpack and pack only regions the have the Hidden Dragon embedded in them, meaning that all "[rgn]" regions will stay untouched.
To migrate to the new symbol, you should tell Regionerate to unpack "[rgn]" regions:
1: <Configuration>
2: <Symbol>
3: <HiddenDragon/>
4: </Symbol>
5: <Unpack>
6: <RegionsThatMatch="\[rgn\]"/>
7: </Unpack>
8: </Configuration>
This way, the first time a code file is Regionerated with the new settings, Regionerate first unpacks all the old "[rgn]" regions and then it merges all the members into new Hidden Dragon symbolized regions.
By using symbols Regionerate knows which regions are self-managed ("Regionerated regions") and which regions should be kept untouched ("exisiting regions") .
Up until v0.6.6.5, symbols were visible as prefixes before the name of the region, for example:
#region [rgn] Fields (5)
Now you can choose between 3 different types of symbols:
1. Hidden Dragon - (default) The Hidden Dragon is a special character (#255) which is not considered whitespace, yet not visible. If you use the Hidden Dragon, the symbol will be transparently embedded in your region declarations and will look like this:
#region Fields (5)
I would recommend you keep using the Hidden Dragon unless you stumble upon any trouble with it or if you want to be able to visually identify Regionerated regions.
2. Prefix - Allows you to add a prefix to Regionerated region headers, for example:
#region [rgn] Fields (5)
3. Wrapper - Allows you to add a prefix and a suffix to Regionerated region headers, for example:
#region [Fields (5)]
The symbol can be declared in your Code Layout document under the Configuration element.
If you read about Regionerate in blog posts, you are most likely to read that it is "a tool that creates regions automatically". Most people don't understand that Regionerate is primarily about Code Layout and that #region is just one display style. Currently, Regionerate has 3 types of regions - Visible, Invisible and Comment.
If you take a look at the Code Layout schema, you can see that the CreateRegion statement has an attribute called RegionStyle which accepts any of the values mentioned above.
The Visible region style is the default style which will wrap your selection with #region-#endregion, for example:
Member leftovers are instances of a specific member type which were not already placed by a previous PutX statement. Member leftovers are placed by a PutX statement with no restricting Where statements.
For instance, A <PutFields/> statement will place all fields that weren't already placed by previous <PutFields> statements. A <PutMethods><Where><IsConstructor Equals="true"/></Where></PutMethods> will place all constructors which weren't placed by previous <PutMethods> statements. (see example at the bottom)
Take this Code Layout Snippet as an example:
<PutFields><!-- First --> <Where><Static Equals="true"/></Where> </PutFields> <PutFields/><!-- Second -->
public class WorkItem { private static string _b; private string _a; private readonly string _c; }
The first <PutFields> statement in the Code Layout snippet will place "_b" and the second <PutFields/> statement will place all the leftovers ("_a", and "_c").
How is this useful, you ask? Well, placing leftovers can help you put some order in your regions without having to specify each specific case. For example, you might want to create a region for constructors, having all the public constructors appear first followed by the rest of the constructors.
Without leftovers, the Code Layout should have looked something like:
Here's a quick guide to get you started on writing your own Code Layout. Code Layouts are simply XML files which follow the Code Layout Schema.
The Code Layout Schema (CodeLayoutSchema.xsd) can be found in your installation directory. If you are using Visual Studio, the Code Layout Schema is copied during installation into your Visual Studio Schemas directory.
Here are the steps neccesary to start writing a custom Code Layout in Visual Studio:
Create a new XML file in Visual Studio (File->New->File->XML File->Open)
Type in "<CodeLayout xmlns="http://regionerate.net/schemas/0.6/CodeLayout.xsd">"(substitue 0.6 to the current version you are working with).
If you are using Visual Studio, the file should be linked to the Code Layout Schema immediatly, and you should be able to receive intellisense assistance.
If you are not using Visual Studio, or for some reason you are not getting intellisense assitance:
Open the Properties window for the XML file.
Choose "Schemas" and click on the button.
Click on "Add" and browse for the CodeLayoutSchema.xsd file under your installation directory. (typically C:\Program Files\Regionerate)
Click OK. The XML file is now validated by the Code Layout Schema.
The Code Layout Schema structure is pretty simple, I'm sure you'll manage from here.
Once you have your Code Layout all ready, you can easily load it into the Visual Studio add-in by following these steps:
Click on Tools->Regionerate Settings.
Under "Code Layout" check "Use Custom".
Hit the "Browse" button and browse for your custom Code Layout.
Click "Save".
If you are having any difficulties with the structure of the Code Layout Schema, post a comment this post and I'll expand on the topic.
Regionerate can easily run as part of your NAnt build machine. Warning: boring details ahead.
The installation package comes with an assembly called Regionerate.Presentation.Addins.NAnt.Tasks.dll which contains the Regionerate NAnt Task.
You should provide this task with a few details about what it should do, such as which projects or solutions it should run on and which Code Layout it should apply on them.
Here are the steps which should be performed in order for Regionerate to run under NAnt:
1. Tell NAnt to load the Regionerate NAnt Task assembly by adding the following element to your build file:
Here is a detailed explanation of the different elements and attributes you can use in this task:
rgn element - Starts the Regionerate task. rgn/layoutattribute (optional, defaults to the default Code Layout) - Sets the Code Layout that will be used to apply layout within this task. rgn/noInfoattribute (optional, defaults to false) - Determines whether progress information should be displayed. rgn/noWarnattribute (optional, defaults to false) - Determines whether non-critical warnings should be displayed. Note that critical warnings will always be shown. rgn/solutions element (optional) - Regionerates solution (.sln) files. rgn/solutions/include element (mandatory) - Regionerates a solution file. rgn/solutions/include/name attribute (mandatory) - The path to the .sln file. rgn/ignoredProjects element (optional) - Ignore certain project (.csproj) files from all solutions. rgn/ignoredProjects/includeelement (mandatory) - Ignore a certain project (.csproj) file. rgn/ignoredProjects/include/name attribute (mandatory) - Name or partial name of a .csproj file. (e.g: "MyProj.csproj" for all projects which include "MyProj.csproj" in their file name or ".Tests" for all projects that include ".Tests" in their file name) rgn/projectselement (optional) - Regionerates project (.csproj) files. rgn/projects/include element (mandatory) - Regionerates a project file. rgn/projects/include/name attribute (mandatory) - The path to the .csproj file. rgn/ignoredFiles element (optional) - Ignore certain code (.cs) files from all solutions and projects. rgn/ignoredFiles/includeelement (mandatory) - Ignore a certain code (.cs) file. rgn/ignoredFiles/include/nameattribute (mandatory) - Name or partial name of a .cs file. (e.g: "WorkItem.cs" for all code files which include "WorkItem.cs" in their file name or "Attribute" for all code files that include "Attribute" in their file name)
3. Optionally, you may wish to check-in any modifications made with a message such as "Nightly Regionerate". Since this task is performed in a different way for every source control provider, it will not be discussed here for now. If you need help with a specific source control provider, comment this post and maybe I'll expand on it.
Regionerate is an automated tool which helps you to preserve your code's layout over time.
Regionerate lets you define regions in your code and determine the way members (fields, methods, properties etc.) should be placed inside them.
Regionerate is a zero-friction tool - when setting up, you can choose a Code Layout (the way you want your code to look) or just use the default Code Layout. From that moment on, Regionerate will make sure your code follows that Code Layout.
You can download Code Layouts from the Gallery or you can easily create a Code Layout yourself. Code Layouts are XML files which follow the Code Layout schema. These files determine which regions you want to create, which types of members you want to place in them, the restrictions you want on these members, their order and more.
You can Regionerate a class by a single click from within your Visual Studio or you can Regionerate your entire code by integrating it to your build machine.
Contribute
I'm currently looking for good people to help me with the remaining development tasks for Regionerate v1.0. If you like Regionerate and want to be a part of it, contact me at omer at rauchy dot net.