Kijana Woodard

Software Minimalism


Creating a ReSharper Macro

Thursday, October 17, 2013

I wanted to get R# to do a little typing for me so that I can more easily add new blog posts.

A new post looks like this:

new Post
{
    Title = "Creating a ReSharper Macro",
    Slug = "creating-a-resharper-macro",
    FileName = "creating-a-resharper-macro.markdown",
    PublishedAtCst = DateTime.Parse("October 17, 2013"),
},

The slug and filename are independently adjustable for flexibility, but they usually start out as a derivative of whatever I'm going to name the post.

I created a R# Live Template that looks like this:

new Post
{
    Title = "$title$",
    Slug = "$slug$",
    FileName = "$slug$.markdown",
    PublishedAtCst = System.DateTime.Parse("$date$"),
},

This is great. When I activate the template, I get a chance to type for each $variable$. So I can type $title$, then $slug$, which gets used on two lines, and finally $date$.

For $date$, I hooked it to a R# Macro that formats today's date.

For $slug$, I want the title to be lowercased and the spaces to be replaced with hyphens. Unfortunately, there wasn't a built in macro that did this.

I found a post about extending macros that shows how to create your own macro. The post a bit confusing and out of date for R# 7+.

Fortunately, I found a couple examples which gave me some much needed hints to write and install the macro.

In addition to what's outlined in the blog post, here are some key steps:

  • When you create your project, add a reference to JetBrains.ReSharper.Feature.Services.dll from the R# program files bin directory. R# itself will then be able to pull in the rest of the dependencies from the bin folder as needed.
  • You specify that you will transform another variable in the template through the Parameters property and then manipulate that value in EvaluateQuickResult.
  • To install your plugin, put it in user app data, NOT the R# bin directory. It will be something like: C:\Users<USERNAME>\AppData\Local\JetBrains\ReSharper\v8.0\Plugins\<YOUR_PLUGIN>\
  • Each plugin gets it's own folder
  • If the plugins directory doesn't exist, create it.

It would be nice if everything needed to write a plugin/macro was available via nuget. It would also be nice if there was a "import plugin" feature in the R# options so you didn't have to find the right directory.

If JetBrains wanted to get really crazy, they could create a Publish Plugin feature that allowed quick and easy social code sharing.

I put the source code for the macro on GitHub.



If you see a mistake in the post *or* you want to make a comment, please submit an edit.

You can also contact me and I'll post the comment.

1 Comments...
Matt Ellis
Matt Ellis • 11 years ago

Something like https://resharper-plugins.jetbr..., you mean? :)

ReSharper plugins can be wrapped up as NuGet packages. You can find out more here: https://confluence.jetbrains.co...

and there's more documentation on macros here: https://confluence.jetbrains.co...

We're working on something for NuGet, but for now, you need to download the sdk from the website.
Nice post!
Matt