Creating plugins for dotPeek

May 15th, 2013 by Maarten.Balliauw

In a previous post, we’ve seen that dotPeek supports creating and loading plugins. The NuGet plugin for dotPeek is a good example to demonstrate how to get started creating plugins for dotPeek. Sources for this plugin are available on GitHub. But let’s create a plugin of our own!

Before we start: dotPeek does not have a formal SDK yet. However it shares a lot of commonalities with ReSharper. In fact: dotPeek is built on the same platform as ReSharper reusing the project model, PSI, navigation and so on. Which is good news: we can use the ReSharper SDK (see the downloads page) to create plugins for dotPeek. Another option is to start from a class library project and work with a small NuGet package I’ve created to help you get started.

Creating a new plugin

To create a plugin for dotPeek, we can make use of the instructions provided with the ReSharper SDK. This involves a lot of project modifications so we’ll take the easy way out: we can create a dotPeek plugin by installing a NuGet package.

Install-Package JetBrains.DotPeek.PluginDevelopment

In Visual Studio, we can start out with a new Class Library project, making use of .NET 3.5 or up. Next, we can install the JetBrains.DotPeek.PluginDevelopment NuGet package into the project. This NuGet package will convert the class library we have into a dotPeek plugin by adding assembly references and several attributes. Note that the NuGet package will target the latest version of dotPeek that is installed on your system (which also means we have to have a copy of dotPeek installed).

That’s everything we have to do: by running our project, dotPeek will launch with our fresh plugin loaded:

New plugin for dotPeek

Now let’s see where those menu items are coming from…

Menus, toolbars and handlers

Menu items, like the About menu we saw when loading our plugin in dotPeek, are triggered using handlers. The AboutActionHandler.cs file in our project is a sample handler implementation.

About action handler

There are some things to note here:

  • The ActionHandler attribute denotes the fact that this class is a handler with a specific name, “MyFirstDotPeekPlugin.AboutAction” in this case.
  • Handlers are added to menus and toolbars in the Actions.xml file, referencing the action name. More information on actions.xml can be found in the ReSharper plugin documentation. To make working with Actions.xml easier, the NuGet package also added an Actions.xsd containing the full XML schema we can use.
  • Handlers implement two methods of the IActionHandler interface:

    • Update – specifying whether the action is enabled or disabled.
    • Execute – the code which is executed once an action is being invoked.

    The following handler will be always available and can open a new command dialog when invoked:

    Command line tool window in dotPeek

    We can add it to a menu or toolbar by updating the actions.xml file:

    Actions.xml for plugins

    Distributing a plugin

    There comes a time when we want to make our plugin available to the public. Distributing a plugin is a simple process:

    • Distribute the assembly compiled from your project
    • Users can copy this assembly into %LOCALAPPDATA%\JetBrains\dotPeek\v<your target dotPeek version>\plugins

    Another option is to generate a simple installation batch file, check my NuPeek plugin’s GitHub repository for more information.

    Further reading

    In this blog post, we’ve only scratched the top of the surface. With plugins, a lot of things are possible: we can add menu items, toolbar buttons, tool windows, options panes and so on. In fact, many of the features shipped with dotPeek are in fact built as plugins. It is advised to consult the ReSharper Plugin Development Guide for additional information on developing dotPeek plugins.

    Meet dotTrace 5.3.2 Performance with CLR 4.5 support

    May 14th, 2013 by Daria.Dovzhikova

    Today we are happy to announce availability of a new maintenance release of our .NET performance profiler. Please feel free to download and try the latest dotTrace 5.3.2 Performance.

    Starting from this update dotTrace Performance supports the latest version of CLR. In case you have already updated CLR to version 4.5 or just plan to do it, we would recommend to use this version of the profiler.

    ReSharper 8 EAP: NuGet based Extension Manager

    May 10th, 2013 by Matt Ellis

    ReSharper has always had a powerful extensibility story – its Open API allows plugins to use the same APIs that the core product uses. But distribution – discovery, installing and uninstalling – has previously been left to the plugin author, meaning the user is left with an experience ranging from msi installers to zip and batch files. Recently, .NET has seen the success of the NuGet package manager, which makes it very easy to install, uninstall and update libraries in a project. We’d like to bring that ease of use and discovery to ReSharper, which is why we’re very happy to be taking the wraps off our NuGet based Extension Manager.

    ReSharper Extension Manager window

    The latest EAP build of ReSharper 8 adds support for extensions to be distributed as NuGet packages. You get to use a very familiar interface to browse and install extension packages from our new extensions gallery. Of course, you can easily uninstall and update, too, and ReSharper will check for updates for you, and notify you when a new build is available – perfect for keeping up to date with EAP changes.

    ReSharper update notification balloon tooltip

    Speaking of EAP changes – since ReSharper 8 is currently still under development, it is obviously changing and evolving with each EAP release. Which means our packages are marked as “prerelease”. Once 8.0 is released, these will of course become Stable. Until then, please make sure to select “Include Prerelease” when browsing with the Extension Manager, or in the Gallery.

    Extensions, Not Plugins

    A very important change, though, is that an extension is not just for plugins. An extension can also be used to distribute ReSharper’s settings files and External Annotations, meaning you can now write useful extensions without even having to write any code.

    The benefit of distributing .dotSettings files is that these are the files used by ReSharper to store Live Templates, Structural Search and Replace patterns, and other settings. For example, the xUnit.net test runner extension includes a plugin to provide the test runner capabilities, but also includes a .dotSettings file, adding lots of Live Templates to help generate code to write asserts faster.

    Live Templates imported by the xUnit.net extension

    And the sample patterns catalog extension adds a sample set of Structural Search and Replace patterns. These patterns are a very powerful, declarative way of analysing code and applying the standard warning squiggly underline highlight. What’s more, they can also provide a declarative means for creating a quick-fix for this code – adding an action to the alt+enter quick-fix menu to replace the code. See here for an introduction to SSR, and check out these posts for more information.

    Structural Search and Replace dialog with sample patterns imported

    Of course, settings files aren’t limited to Live Templates and SSR patterns. While these were the primary motivation for adding support for .dotSettings files, it opens the door for more interesting extensions – defining code formatting standards, code cleanup profiles, extra To-do items, or even a different set of opinionated code inspection defaults (don’t like our default “var” recommendations? Change them!).

    Extension packages take advantage of ReSharper’s settings layers. If the extension contains a .dotSettings file, it is added as a read-only layer. The settings in the file are merged with the other layers in your installation (This Computer, Solution and project files – see this post for more information on layers), and naturally appear in the Options dialog or Live Templates Explorer. This means you can edit the templates, patterns or other settings, and any changes are then saved in the This Computer layer.

    If you don’t want these extra settings (for example, you want the test runner, but not the included Live Templates) you can easily remove them from the system by disabling the layer in the Manage Options dialog. The settings, templates or patterns are immediately removed.

    ReSharper's Manage Options dialog showing xunitcontrib settings layer deselected

    External Annotations are xml files that contain a map between ReSharper’s annotation attributes, and pre-compiled assemblies. ReSharper’s code analysis uses these annotations to make better decisions about the warnings to show. For example, without annotations, this test code warns about potential null usage, even though the preceding Assert will throw an exception if the value is null – in other words, the value is guaranteed to be not-null when accessed.

    ReSharper warns of potential null reference exception, even though the line will never be hit due to preceding Not Null assert

    The xUnit.net extension adds annotations for the xunit.dll assembly, which tells ReSharper that Assert.Null will throw if the passed parameter is null. Now, ReSharper knows that the second assert will never get hit, and marks it as unreachable.

    ReSharper highlights unreachable code due to preceding assert

    ReSharper ships with annotations for the BCL, and some annotations for third party assemblies, notably to enable INotifyPropertyChanged support. Extensions can now distribute annotations for other third party assemblies.

    Dependencies

    Since extensions are NuGet packages, they can have dependencies. Someone could write an extension that exports a Live Template for creating test methods. That template could use a macro from another extension to make sure the method name you type gets converted to a valid name – changing spaces to underscores, for example.

    Bundled Extensions

    Another exciting benefit we get from the extension manager is the ability to have bundled, or pre-installed, extensions. These are simply extensions that are installed for you as part of the normal installation process. Once installed, they behave just like normal extensions, and we can push updates to the Gallery at any time, allowing us to update these plugins on a separate schedule to ReSharper itself.

    In the latest EAP build, we’re bundling our NuGet plugin which uses NuGet to add package references, rather than file references when importing types. We now get the best of both worlds – this really useful functionality is included by default for every ReSharper user, and we have the flexibility to update it at any time, decoupled from ReSharper’s release schedule.

    Custom sources

    Of course, since this is based on NuGet, we get to take advantage of the existing infrastructure and tooling available in the NuGet community. Our Gallery is based on the NuGet Gallery sources, and is the default, primary location for getting extensions (remember to select “Include Prerelease”!). But you can also use the Extension Manager page in the Options dialog to specify extra sources – local or remote file system or a custom NuGet feed, such as TeamCity or MyGet. This makes it easy to install private, company wide extensions that target your specific needs, or nightly builds of an extension you build, or perhaps create a curated feed of your favourites.

    custom_sources

    How to Build Your Own Extensions

    Building extensions is very straightforward. While we’re based on NuGet, we have a very different purpose to NuGet – extensibility information as opposed to assembly references. So we don’t use the standard libs, tools or content folders. Instead, we have our own folder structure that makes more sense for the data we need to distribute in the package. But that’s really the only difference. We still use the .nuspec file for metadata, and we pack and push the .nupkg file to the ReSharper Gallery in the same way – sign up for an account, get an API key and push away! You can get more information on creating an extension package at the ReSharper 8 devguide site, and if you have more questions, please email us at resharper-plugins@jetbrains.com, or send a message to the new resharper-plugins Google Group.

    Please note that the Extension Manager relies on NuGet’s core library to implement a lot of its functionality. Since this is a .NET 4 library, the Extension Manager is limited to Visual Studio 2010 and 2012. We’re looking at ways to improve this as we progress through the EAP.

    We hope you enjoy the changes we’ve introduced around extensions. Please try it out and let us know your feedback. We’re still quite early on in the EAP cycle, so expect changes – but if there’s something you want to see changed or updated, let us know! You can report issues at our YouTrack instance.

    And remember, you don’t need to write a plugin to release an extension – let’s see some Live Templates and SSR patterns!

    Peeking into NuGet packages with dotPeek

    May 7th, 2013 by Maarten.Balliauw

    Using dotPeek, we can peek into assemblies by downloading symbols or by decompiling them. Out of the box, we can also open NuGet packages stored locally. But what about NuGet packages that we haven’t downloaded yet? Well… there’s a plugin for that!

    The plugin is available for download. Extract the ZIP file contents and run the appropriate batch file for your version of dotPeek, e.g. Install-NuPeek.1.0.bat for dotPeek 1.0, or Install-NuPeek.1.1.bat for dotPeek 1.1. The plugin is compatible with dotPeek 1.0 and the dotPeek 1.1 EAP. Source code for this plugin is available on GitHub.

    After downloading and installing the plugin, dotPeek features a new toolbar icon as well as two new menu items under the File menu. The File | Open from NuGet… menu entry allows us to download and open a NuGet package from any NuGet repository out there. The File | Open NuGet packages.config… menu entry allows us to open all NuGet packages defined in a packages.config file.

    dotPeek open from NuGet

    Using the toolbar button, we can search for a NuGet package we want to load and decompile using dotPeek. We can download packages from the official NuGet gallery as well as any other NuGet repository out there such as your TeamCity server.

    Select package from NuGet gallery

    We can search for packages and open them. Using the Load dependencies checkbox, we can automatically download dependencies for the selected package as well. Once downloaded, dotPeek will display the NuGet package and download symbols or decompile contained assemblies.

    dotPeek open NuGet package

    In a future blog post, we’ll dive into how we can create plugins for dotPeek. If you can’t wait, the sources for this plugin are available on GitHub. In the meanwhile, download the dotPeek EAP and give it a go!

    New Features in the Latest ReSharper 8 EAP

    April 30th, 2013 by Dmitri Nesteruk

    Good news everyone — we’ve got another EAP build ready and it should come as no surprise that we’ve got a few new features to talk about. So without further ado, here’s what we’ve got in store for you today.

    Extension Manager or how to provide xUnit.net support

    We sometimes get requests to provide certain features out of the box, functionality that is currently provided via plugins. xUnit.net support is one of these.

    While doing this certainly has its benefits, it also has disadvantages. As such, we decided instead to take the opportunity to address the core problem, which is to simplify the discovery, installation and updating of ReSharper plugins.

    We’re very pleased to introduce our NuGet based Extension Manager. It’s now very easy to add extensions and plugins to ReSharper, all from a user interface you’re very familiar with from NuGet. Simply open the Extension Manager from the ReSharper menu, and you’ll be able to browse and search for extensions from the “Online” category - make sure you select “Include Prerelease” in the drop down first - these are still EAP packages!

    So to get xUnit.net test support, simply find the xunitcontrib package and hit Install.

    That’s all there is to it! ReSharper now has support for running xUnit.net tests, and includes Live Templates and External Annotations to make your test code writing experience better.

    We’ll be taking a more in-depth look at this feature very soon, but one important point to make right now is that because NuGet is a .NET4 library, the Extension Manager is only available in Visual Studio 2012 and 2010.

    ToString() Format Completion

    ReSharper already has a few features related to String.Format() usage, such as for example inspections and fixes related to format arguments being used in the format string. However, in this EAP we’re taking it a bit further by adding code completion for types which have well-known format specifiers (such as DateTime, TimeSpan, Guid, enumerations and many others):

    Multi-File Templates

    By popular demand, ReSharper’s support for file templates has been expanded to incorporate the possibility of creating several files from a single template. You start, as always, by creating a single file template. But then, ReSharper gives you controls to add additional files: you can either add a new file or a file from an existing template:

    Choosing one of these options adds another editor into the template editor window. In this editor, you can specify the placement of the file (either a relative location or an ASP.NET code-behind file), the name of the folder to place the file into, and of course the name of the file. Then, just edit this template as you would any other — you can reuse the parameters you declared for the first template.

    You can add as many files as you want and once you come to fire the template, all the necessary files (and folders, if they do not exist) will be automatically created in the right places.

    Go To Generic Subsitutions

    When you have the usage of a generic type such as List<Foo>, it’s easy enough to find all usages of List<T>, but what if you want to know the exact types that List<T> is being specialized with. To help with this, one of the new navigation options is to look for Generic Substitutions:

    Once you fire up this navigation item, ReSharper will search for all the types that are used to substitute the generic parameter T and will show you a summary tree grouped by the type used to substitute the generic parameter. You can drill down into the tree to find out the exact locations where a particular type is used:

    Blue Theme Support

    Visual Studio 2012 Update 2 has been released with support for a Blue Theme, and we’ve made adjustments to ensure that ReSharper plays nice with it too:

    We’ve got a lot more in store, and we’ll be blogging in the coming weeks about new features as well as giving in-depth overviews of the ones we’ve already mentioned. Meanwhile, please feel free to check out the EAP and let us know what you think!

    Refactoring Legacy Code Bases, Free Online Event May 8

    April 17th, 2013 by rdemmer

    In cooperation with Microsoft, join JetBrains’ Hadi Hariri for the free online event, Refactoring Legacy Code Bases using Microsoft Visual Studio and ReSharper. The session is intended for architects, designers and professional developers.

    Refactoring Legacy Code Bases
    Wednesday, May 8, 9:00 AM - 10:00 AM Pacific Time (GMT-08:00)

    Not everyone has the possibility of working on Greenfield projects or doing TDD or BDD. Many of us often end up having to work with legacy code bases that have little notion of what SOLID design principles are and unit tests are non-existent. It can be daunting to have to maintain these types of systems. However, with a little bit of effort, we can try and make the best of the situation. We can combine some well-known refactoring techniques, along with code analysis and design concepts to improve the quality of the code.

    Registration is required to attend. We hope to see you there!

    ReSharper’s Web Path Mapping Explained

    April 15th, 2013 by Dmitri Nesteruk

    One of the many scenarios that ReSharper seeks to address in web projects is a situation where the locations of files during the design of the app are different from the locations where the app gets deployed. This makes life difficult for ReSharper, since it doesn’t know where to go looking for the relevant files. Luckily, the Path Mapping feature lets you configure the locations of those paths.

    There are two mechanisms that can both be used to configure path mappings – context actions and explicitly opening the Path Mapping dialog.

    When ReSharper sees a path that it doesn’t recognize, it will underline the path with a wavy line, complaining that it doesn’t know where the path is. At this point, pressing Alt+Enter will show you several options for how the situation can be handled:

    The first option, Ignore path, simply tells ReSharper to not bother inspecting this particular path wherever it may appear. This is useful in situations where, for example, you are referencing a file that doesn’t yet exist, and might be created later.

    The second option is to Set path mapping, i.e. define explicitly how the path is handled.

    Once again, there are two options: Substitute replaces a path with a different path, whereas Ignore chooses to ignore the path and is functionally equivalent to the Ignore path context action.

    The Path mapping dialog box doesn’t only show up when you use a context action: you can also open it up by selecting the Path mapping element of the properties of the web project you’re working with. Note that we’re talking about the property grid (F4) rather than the tabbed property pages. To bring up the dialog, just press the button next to Path mapping.

    Incidentally, the text value next to the button can be one of two possible options – either Custom indicating that some mappings have already been created, or Not set when none have yet been defined. At any rate, just as with the context action, pressing the button brings up the Path mapping window where you can create, delete and change mappings:

    We hope you find this feature of ReSharper useful and your experience of developing web applications becomes a little more enjoyable. Develop with pleasure!

    ReSharper 7.1.3 is Available for Visual Studio 2012 Update 2

    April 12th, 2013 by Jura Gorohovsky

    Have you upgraded Visual Studio 2012 to Update 2 RTM? If you have, please download ReSharper 7.1.3, which is mostly about compatibility with this new Visual Studio update.

    Specifically, ReSharper can now successfully run MSTest fixtures in VS2012 Update 2 (this was the most important problem that has prevented usage of ReSharper 7.1.2 with VS2012 Update 2); adds support for Windows Phone 8 unit test projects; and becomes compatible with the final release of LightSwitch HTML Client as part of VS2012 Update 2.

    To make things clearer, here are some easy rules specifying whether you need ReSharper 7.1.3 depending on the version of Visual Studio that you’re using:

    • Visual Studio 2005, 2008, or 2010: you don’t need ReSharper 7.1.3, skipping this update is a safe bet.
    • Visual Studio 2012 prior to Update 2: you should NOT install ReSharper 7.1.3 if you’re using MSTest.
    • Visual Studio 2012 Update 2: ReSharper 7.1.3 is primarily for you, so go ahead and download it.

    Webinar Recording: Jon Skeet Inspects ReSharper

    April 11th, 2013 by rdemmer

    On Tuesday, April 9th, we held a free webinar with Hadi Hariri and C# legend Jon Skeet. The concept was simple enough: to play with some of the most sophisticated ReSharper code inspections and have Jon try to break ReSharper with tricky code samples.

    The recording of this webinar is embedded below and can be found directly on YouTube and JetBrains.tv. The examples used are also available for download.

    We would like to thank all of the webinar attendees for your participation, questions and tremendous turnout and of course to Jon, thank you for taking your time to join us and share your wealth of knowledge!

    About This Webinar:

    Jon Skeet, man, father, legend. Some call him the Chuck Norris of C#. The guy that the compiler bends to obey joins us for this webinar to walk us through some of the inspections that ReSharper offers. He explains the theory behind it, tells us whether he agrees with it or not and does his best to try and break ReSharper. You can follow Jon on Twitter @jonskeet.

     

     

    ReSharper 8 EAP, Build 3: What’s New!

    April 8th, 2013 by Dmitri Nesteruk

    You have probably already guessed that this post is about a new set of features that has appeared in the new build of the ReSharper EAP late last week, so, without further ado, here’s what we’ve got in store for you:

    Bulb Menus in Margin

    In an attempt to streamline the programmer’s interaction with gutter marks and bulb pop-ups, we have added an option to merge both of these constructs into a single construct that we call an action indicator. Here’s what it looks like:

    As you can see, both the gutter mark options from unit tests and the context action on that element have been merged into a single menu. The use of action indicators is an optional feature that can be found in ReSharper’s Options under Environment|Editor|Editor Appearance:

    Option #2 in the above list revers to the R#7.1-style mechanism of showing a bulb pop-up. The last option prevents any visual queues that a bulb item is available (save for the indicator itself), and only shows the menu when you explicitly open it with Alt+Enter.

    Please note that this feature is available only for Visual Studio 2010 and later.

    Decompiler Improvements

    We’ve already talked a bit about decompilation (both in ReSharper and dotPeek), but this deserves a mention: we now know how to correctly decompile

    • async methods and await expressions, including async lambdas.

    • Expression trees — the decompiler will now help you when exporting Linq to SQL or Entity Framework code in query expressions. This means in ReSharper 7 you could only decompile an expression into this:

      whereas with ReSharper 8 you can decompile it into this:

    • Field-like events, taking into account the lock-free nature of C# 4’s add/remove accessors.

    Fix in Scope

    In-place fixes are great, but what’s even better is when you can apply that fix in a given scope. Take those pesky unused references, for example – now you can get rid of them not only in a file, but also on a project/solution level. All with a single quick-fix:

    Now quite a few fixes can be applied to a larger scope than just the code under the cursor — including making a field read-only or removing a redundant cast.

    Default Alignment Setting Adjustments

    One common complaint about ReSharper’s default settings was that certain constructs we indented, shall we say, aggressively. The two particular cases of note are anonymous lambdas

    And another case is indentation when a function call including parameters gets too big and is indented similar to

    These and similar cases will now be treated more gently by ReSharper’s default settings.

    Code Inspection Performance Improvements

    Last, but certainly not least, after another round of performance improvements we’ve managed to once again speed up ReSharper’s code analysis. Specifically, the Find Code Issues command enjoys an over 2× performance improvement.

    Now, you know what to do – grab the EAP and check out those features today!