While working on the MVC Marathon, I decided to try out ReSharper and write some good templates to make my time unit testing in ASP.NET MVC a little quicker. I soon realized that I needed a macro that would turn the unit test class name (HomeControllerTests) into the name of the controller being tested (HomeController).
Since ReSharper only has a few macros that will make copies of other template variables, I started scouring the internet for clues and found this post. It’s a little outdate for ReSharper for 4.5, so here’s what I had to do to make my custom macro.
You can download the source or fork your own on GitHub.
Creating the Macro
- Create a new C# class library project.
- Add references to the following dlls:
JetBrains.Platform.ReSharper.UI.dll
JetBrains.ReSharper.Feature.Services.dll - Add a new class to your project and mark it with the Macro attribute and implement the IMacro interface:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using JetBrains.ReSharper.Feature.Services.LiveTemplates.Macros;
using JetBrains.ReSharper.Feature.Services.LiveTemplates.Hotspots;
namespace ChrisLaco.ReSharper.Macros
{
// {#0:pattern}, etc are arguments that are passed to EvaluateQuickResult arguments[]
[Macro(“applyRegex”, ShortDescription = “Replace string matching {#0:pattern} with {#1:string} in {#2:variable}”, LongDescription = “Applies regex to replace value in another template variable.”)]
class ApplyRegexMacro : IMacro
{
#region IMacro Members
public string EvaluateQuickResult(IHotspotContext context, IList<string> arguments)
{
if (arguments.Count != 3)
{
return null;
}
else
{
try
{
// Using the regex argument, replate the value of the specified template variable with the specified string
var result = Regex.Replace(arguments2, arguments0, arguments1, RegexOptions.IgnoreCase);
return result;
}
catch (Exception e)
{
return “<” + e.Message + “>”;
}
}
}
public HotspotItems GetLookupItems(IHotspotContext context, IList<string> arguments)
{
return null;
}
public string GetPlaceholder()
{
return “a”;
}
public bool HandleExpansion(IHotspotContext context, IList<string> arguments)
{
return false;
}
// Must return the same # of parameters declared in the Macro
public ParameterInfo[] Parameters
{
get
{
return new[] { new ParameterInfo(ParameterType.String), new ParameterInfo(ParameterType.String), new ParameterInfo(ParameterType.VariableReference) };
}
}
#endregion
}
} - In AssemblyInfo.cs, apply assembly attributes for the ReSharper plugin manager:
using System.Reflection;
using System.Runtime.InteropServices;
using JetBrains.UI.Application.PluginSupport;
[assembly: PluginTitle(“ChrisLaco.ReSharper.Macros”)]
[assembly: PluginDescription(“Provides additional template macros: ApplyRegexMacro.”)]
[assembly: PluginVendor(“Christopher H. Laco”)]
This information will be displayed in the ReSharper -> Plugins dialag:
- To install plugin, simply copy the compiled dll into one of the follow directories:
C:\Documents and Settings\username\Application Data\JetBrains\ReSharper\v4.5\vs9.0\Plugins
C:\Program Files\JetBrains\ReSharper\v4.5\Bin\Plugins
Using the Macro
See more posts about:
All Categories