![]() |
Languages »
C# »
Applications
Intermediate
License: The Code Project Open License (CPOL)
Automating the code writing process using macrosBy Dan HandevikThis article describes the documentator macros for making code writing with Visual Studio 2003 a lot faster and easier. |
C#, VB, Windows, .NET 1.1, .NET 2.0VS.NET2003, VS2005, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
See history for a full changelog. The documentation found in the zip contains an updated documentation in addition to the changelog of the macros.
When writing code and documentation for source files, there is much manual work to complete the task. Often you follow a specific code standard and some code you write quite often with small changes. I have developed a set of macros for easing my daily work and thus making me more efficient as a developer. These macros employ our company's code standard, that hopefully are not so far from yours, and they can be fine-tuned to match your standards.
The macros focus on inline XML documentation, region blocks, automation and refactoring. They work with Visual Studio 2003 and primarily with C#, but some macros also support VB.NET.
Download the supplied files and follow the instructions given below:
Meridium.Documentator.FixKeyMapping to install the shortcuts Meridium.Documentator.FixKeyMappingsAfterResharperInstallation macro Meridium.Documentator.FixKeyMapping to install the shortcuts The full explanation of the macros exists in the manual, but I will give some descriptions for the main features.
For instance, if you have written a method that looks like this:
public int MyMethod(string name, int maxNo, Font font)
{
//my code
}
and then invoke the Document and regionize macro (Ctrl+d), you'll end up as follows:
#region public int MyMethod(string name, int maxNo, Font font)
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="maxNo"></param>
/// <param name="font"></param>
/// <returns></returns>
public int MyMethod(string name, int maxNo, Font font)
{
//my code
}
#endregion
The cursor is then placed at the first XML doc tag (<summary>) and you are ready to start documenting. If you have also activated the EnableTabMapping, you can use Tab and Shift+Tab to move between the different tags in the XML doc section.
It also supports updates, so if you add a parameter or change the order of the parameters and then activate the macro again, the documentation and region will be updated.
Another major macro is the PasteTemplate macro that supports automation and refactoring by templates. For now, the templates are hard coded into the macros, but you can easily modify them to suit your own needs.
For example, if you want to create a try catch clause, enter the following text:
tryc
and activate the PasteTemplate macro (Ctrl+J), you'll end up as follows:
try
{
}
catch(Exception ex)
{
}
More automation functions exists. Try fori, trycm, trycf, tryf, gp, sp and gsp.
A more complex automation is supported when creating events. If you define the following event:
public event EventHandler MyEvent;
and activate the PasteTemplate macro, you'll end up with the following code:
#region public event EventHandler MyEvent
/// <summary>
/// This event is fired when
/// </summary>
public event EventHandler MyEvent;
#endregion
#region protected virtual void OnMyEvent(EventArgs e)
/// <summary>
/// Notifies the listeners of the MyEvent event
/// </summary>
/// <param name="e">The argument to send to the
listeners</param>
protected virtual void OnMyEvent(EventArgs e)
{
if(MyEvent != null)
{
MyEvent(this,e);
}
}
#endregion
This saves a lot of time.
The same macro also adds refactoring capabilities. For instance, if you want to convert a field to a property,
public string Name;
place the cursor at the end of the line and activate the PasteTemplate macro, you'll end up as follows:
public string Name;
get { return _name; }
set { _name = value; }
}
private string _name;
Then add the Document and regionize macro to that, and in a few seconds, you have refactored and documented your field.
The field refactoring method contains a few variants depending on the field initialization; look in the manual for a full description. I have added some more refactoring types, but you'll have to look in the manual for the complete specification.
Given below are some macros described in short, a full description exists in (yes, you are right) the manual.
| Macro | Shortcut | Summary |
| Ctrl + d | Adds updates to the XML documentation on the current code element and puts #region #endregion directives around the section. |
|
| Ctrl + Shift+ j | Checks for keywords and pastes an appropriate template. Compare to Borland's template functions and VS 2005 Code Snippets. | |
| Ctrl + m, Ctrl + m | Toggles the parent #region block. |
|
| Ctrl + Shift + + | Expands all #region blocks. |
|
| Ctrl + Shift + - | Collapses all #region blocks. |
|
| Ctrl + Shift + Alt + h | Creates a Documentation heading. | |
| Ctrl + Shift + d | Formats the current document, compare to Edit.FormatSection. |
|
| Ctrl + Alt + s | Sorts the selected lines in an ascending order. | |
| Sort lines descending | Ctrl + Alt + Shift + s | Sorts the selected lines in a descending order. |
| Installs user configured toolbar(s) and items in the toolbox. | ||
| Ctrl + < | Advances to the next XML doc tag if cursor is in an XML doc block. | |
| Ctrl + > | Goes to the previous XML doc tag if cursor is in an XML doc block. | |
| Connects all shortcuts to the macros, except the Act on tab and Act on shift tab macros. | ||
| Remove key mappings | Removes all keymappings for the Macros. (Doesn't restore any overridden macros though) | |
| Fix key mappings after resharper installation | Fixes some keymapping collisions between resharper, DocumentatorMacros and VisualStudio. | |
MakeCData |
Wraps the selected text in a <![CDATA[ ... ]]> structure. |
|
FixHTMLValidationErrors |
Fixes common validation errors in HTML documents. | |
KillLine |
Ctrl + Shift + k | Kills the rest of the line |
EnterCodeComment |
Ctrl + Shift + c | Creates a comment on the format <date>: <domain>\<user> |
EnterCodeRemark |
Ctrl + Shift + Alt + c | Appends a remark comment to the current class documentation |
RemoveWordPasteHtmlTags |
Removes Word specific HTML tags and attributes if you paste some formatted text from Word. | |
HTMLEncode |
HTML encodes the marked text |
For properties, indexers and some methods, a default documentation is provided when the Document(AndRegionize) macro is invoked.
The following elements are auto documentated:
| Method/Element | Example | ||||||||
| Constructors |
<summary> |
||||||||
|
Destructors |
<summary> |
||||||||
void Dispose(...) |
<summary> |
||||||||
string ToString(...) |
<summary> Returns a <see cref="String"/>that represents the current <see cref="MyType"/>.</summary> <returns>A <see cref="String"/> that represents the current <see cref="MyType"/>.</returns> |
||||||||
bool Equals(MyType o) |
<summary> Determines whether the specified <see cref="MyType"/> is equal to the current <b> MyType</b>. </summary> <param name="o">The <see cref="MyType"/> to compare with the current <see cref="MyType"/>.</param><returns> true if the specified <see cref="MyType"/> is equal to the current <b>MyType</b>.;otherwise, false.</returns> |
||||||||
bool Equals(MyType a, MyType b) |
<summary> Determines whether the specified <see cref="MyType"/> instances are considered equal.</summary> <param name="a">The first <see cref="MyType"/> to compare</param><param name="b">The second <see cref="MyType"/> to compare</param><returns> true if <i> a</i> is the same instance as <i>a</i> or if both are null references or if <c>a.Equals(b)</c> returns true; otherwise, false. </returns> |
||||||||
int GetHashCode(...) |
<summary> Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table. </summary> <returns>A hash code for the current <see cref="MyType"/>.</returns> |
||||||||
| Properties |
<summary> |
||||||||
| Indexers |
<summary> |
||||||||
void ControlName_EventName(object param1, [Any]Args param2) |
<summary> |
||||||||
|
|
<summary> Creates a new object that is a copy of the current instance. </summary> <param name="source"></param><returns>A new object that is a copy of this instance.</returns> |
||||||||
bool Contains(Object obj) |
<summary> Returns a value indicating whether the specified <see cref="Object"/>is contained in the <see cref="Class1"/>.</summary> <param name="a">The <see cref="Object"/> to locate in the <see cref="Class1"/></param><returns><b> true</b> if the <i>Object</i> parameter is a member of the <see cref="Class1"/>; otherwise, <b>false</b>.</returns> |
||||||||
bool IsWord(..) |
<returns>True if it is word, otherwise false.</returns> |
||||||||
bool IsWord1Word2WordN(...) |
<returns>True if word1 word2 is wordn, otherwise false.</returns> |
||||||||
[Any]Format(string format, param object[] args) |
<param name="format">A string containing zero or more format specifications.</param><param name="args">An array of objects to format.</param> |
||||||||
int Compare(MyType x, MyType y) |
<summary>Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.<summary> <param name="x">The first object to compare.</param><param name="y">The second object to compare.</param><returns>A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings:
|
||||||||
int CompareTo(MyType o) |
<summary>Compares the current instance with another object of the same type.</summary><param name="o">The <see cref="MyType"> to compare with this instance.</param><returns>A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings:
|
I have to thank Roland Weigelt for his inspiring GhostDoc and some other macros (region expand). The Key mapping binder is also found on the net, I can't remember where though.
The upcoming Visual Studio 2005 will be a good update for the automation and refactoring with the introduction of code snippets.
Contains support for
FixKeyMappingsAfterReSharper25Installation - renamed from FixKeyMappingsAfterReSharperInstallation. Used to setup keymappings after Resharper 2.5 has been installed FixKeyMappingsAfterResharper3Installation - Used to setup keymappings after Resharper 3 has been installed KillHtmlStyleAndClassFormatting - removes style and class formatting from selected HTML code PasteTemplate - added the disp keyword EnterCodeRemark - Better output of code remarks RemoveWordPasteHtmlTags - removes all mso-* styles, lang attributes from span tags. DocumentThis - Auto documents public void Dispose(bool disposing) Version 1.4.0.0 is a branch from v 1.3.0.0 that supports Visual Studio 2003. The 2003 branch misses all additional functionality after version 1.3.0.0 and has to be developed separately. I have no intention (personal need) to bring the 2003 branch up to the same functionality as the 2005 branch but if you feel like implementing in the 2003 branch, please send me your modifications and I can update this project.
FixKeyMappingsAfterResharper203Installation - updates the keyboard shortcuts KillLine - Kills the rest of the line EnterCodeComment - Enters a code comment on the format <date>: <domain>\<user> EnterCodeRemark - Appends a remark comment to the current class documentation RemoveWordPasteHtmlTags - Removes Word specific HTML tags and attributes if you paste some formatted text from Word. HTMLEncode - HTML encodes the marked text FixKeyMappingsAfterResharperInstallation - Supports Resharper 2.5 CollapseAllRegions - now supports nested regions (thanks to DragonWang_SFIS) and commented ones. PasteTemplate
PropertyChanged events now fully documented private _variables and converts them to public Properties MakeCData - now does not HTML encode the selected text before creating the CData section. FixHTMLValidationErrors
style attributes language and ms_positioning attributes from div tags topmargin attributes from body tags alt attributes to images if missing (defaults to the same as src) DocumentThis
[Any]Format(string format, param object[] args) methods Compare methods MakeCData HtmlValidationErrors FixKeyMappingsAfterResharperInstallation - Supports DocumentAndRegionizeThis - now works with VB.NET CommentSection - Macro removed (VS 2005 internal CommentSection now works with XML/HTML/Javascript code LoadColorableSettings SaveColorableSettings FixKeyMappingsAfterReSharperInstallation PasteSeeParameterXmlDocTag
DocumentAndRegionize
get/set properties Is" methods PasteSeeParameterXmlDocTag DocumentThis RemoveKeyMappings DocumentAndRegionize
Event methods Clone methods Contains methods PasteTemplate
ActOnTab
ActOnShiftTab
EnableTabMappings - Removed DisableTabMappings - Removed PasteTemplate
PasteTemplate static properties DocumentAndRegionize
<return> tags on properties anymore <value> tags on properties
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 5 Jul 2007 Editor: Deeksha Shenoy |
Copyright 2005 by Dan Handevik Everything else Copyright © CodeProject, 1999-2009 Web12 | Advertise on the Code Project |