Posts Tagged ‘ReSharper 6.1’

Replacing Tags and Custom Code Analysis in HTML

Tuesday, April 24th, 2012

This is the second part of a previous post on Searching for Patterns in HTML using ReSharper 6. In the first part we covered the basics of searching for HTML tags, including the use of CSS selectors, using Structural Search and Replace. In this second and final post, we are going to put what we have learned to use, to not only allow us to replace our findings, but also as a means of extending ReSharper with custom patterns, which is the core idea behind Structural Search and Replace.

Replacing tags

The same dialog box that is used to search for patterns, can also be employed to replace patterns. On the top-right of the window, there is a Replace button which activates a section to enter a replacement pattern

Find and replace modes in SSR

providing us with an option to specify the pattern we want to replace with, as well as the scope for it (Look In), be it Solution, Current Project or Current File.

Replace pattern

Hitting Replace will now replace all entries based on scope definition.

Replacing with placeholders

The Replace Pattern also allows placeholders to be used. One useful benefit could be to search for certain incorrect usages of a tag and have it fixed up. For instance, let’s assume that we need to make sure all links have an icon associated with them.

What we would need to do is find all href that do not contain an img tag and add it. Our patterns would look like this:

Search and replace pattern areas

In order to only select those links that do not have an image, we would need to define a CSS selector for $content$

Setting a constraint on tags

Detecting code smells with patterns

We have seen so far how to search and replace HTML. We can now take it one step further and use patterns to detect code smells or just HTML code constructs that we’d like to modify for one reason or another. As mentioned at the start of the post, that is Structural Search and Replace’s core idea, to allow us to extend ReSharper with custom analysis (Search) and quick-fixes (Replace).

There are two ways to do this: use the Search with Pattern dialog as shown thus far, but instead of hitting the Find button, we hit Save or define the Custom Pattern via ReSharper | Options. The former ends up also as a custom pattern so we’ll just show how it is done using the latter approach.

1. Select ReSharper | Options | Custom Patterns and click Add Pattern .

ReSharper Options - Add Pattern

2. Define the pattern as before. In our case, it would be

Configuring a pattern to show as warning

We can establish the level of severity (red rectangle) we wish to give the pattern. In our case, we select Warning. It is also important to define the Description of the search. This serves not only as the description (yellow rectangle) in the pattern entry, but also the message that will be displayed when the pattern is encountered. Finally we provide description for the replacement pattern (green rectangle). This replace is what is then considered a quick-fix.

As soon as we save this pattern, it will become active. ReSharper will now highlight anywhere in our code where this pattern matches.

Search pattern is encountered

Since it has a replacement, ReSharper now offers us to press Alt+Enter to fix the issue

Replace pattern suggested as a quick-fix

It’s important to note that we can still do a global Search and Replace even when patterns have been saved. We do not lose that functionality. What we do gain is custom code analysis that works both as we glance through our code and as part of ReSharper’s Find Code Issues functionality to highlight any potential issues in our code.

More Options

Structural Search and Replace in ReSharper allows for more customization when it comes to defining patterns. For example, we can:

  • Use regular expressions (and negative regular expressions) for attribute names, values and for content in between tags.
  • Use CSS selectors for tag and content placeholders.
  • Constraint control types for ASP.NET tag placeholders.
  • Limit minimum and maximum number of attributes inside tags and elements in content placeholders.

We have just covered some of them in these two posts. Explore them, let us know what you like, what you don’t like and send us your feedback.

Searching for Patterns in HTML using ReSharper 6

Wednesday, April 11th, 2012

In ReSharper 5, we added a feature called Structural Search and Replace (SSR). You could think of it as a cross between Regular Expressions and Templating, with the added benefit that you don’t need to know RegEx to use it. The basic idea behind it is to allow you to extend ReSharper’s code analysis by defining custom patterns, and optionally providing quick-fixes (for replacing these with other patterns).

In ReSharper 6.1, we extended SSR with support for other languages, specifically HTML and ASPX markup.

Let’s take a look at how this can help us with web applications we work on.

Searching for specific tags

Often you want to search for tags in your markup, for instance a div that is using a specific class. With ReSharper, we could search for the usages of the CSS class and filter it out, or as in the case we’re covering, use SSR. To do so:

1. Click on ReSharper | Find | Search with Pattern.

2. Select HTML from the drop-down list and enter the following pattern below:

SNAGHTML24e1e3f9

and click Find. If any match is find, it will be displayed in the Find Results window:

SNAGHTML19ba5077

Looking at the results, we can see that it not only finds patterns that match the exact class=”corner” pattern, but also anything that uses the corner class. The reason for that is that we have the option Match similar constructs selected. Deactivating that would result in displaying only exact pattern matches:

SNAGHTML19d7db58

It is important to know that with the previous search, a div tag of the format <div class=”corner” id”=”submenu”> would appear in the results. That is because we have Ignore unmatched elements selected. If we’d like the pattern to include tags that have other attributes not explicitly defined, we need to check this option.

Using CSS selectors to search for patterns

If you’re doing any kind of web development, you’ve no doubt used or at least heard of CSS selectors, which probably many learned to master when they worked with jQuery (including myself). Well that same power of CSS selectors is also available in ReSharper for searching for tags.

1. Enter the following pattern in the Find dialog

image

The values enclosed in $ signs are placeholders required for Structural Search and Replace, and the next step is to define them.

2. Instead of manually setting placeholder properties, we can use the newer feature of ReSharper that allows us to extract them from the pattern:

image

resulting in

image

3. We now have to define the different meanings of each placeholder. This is where the CSS selector comes into play. For the tag attribute, we’re going to define it as a tag with a selector:

SNAGHTML1a0a7f45

The attribute placeholder can also have certain conditions such as matching a regular expression or indicating the number of attributes we want to have as a minimum and maximum:

SNAGHTML1a0bedc0

In our case, we’re going to leave the defaults. Once complete, we can click Find once again to get the results:

SNAGHTML1a0d03b0

Of course, now that we have CSS selectors, we can take it further. For instance, to search for all p inside a div that has a class corner we can do:

SNAGHTML1a128a6c

Any CSS selector expression is valid.

Apart from tags and attributes, we can also have attribute value and content placeholders, and in the case of the latter, we can also use CSS selectors to define the contents.

More to come

In this first part on HTML patterns, we’ve seen how to search for patterns in HTML using both markup patterns as well as CSS selectors. However, it doesn’t stop there. We can perform replacements as well as save patterns so as to offer custom code analysis and quick-fixes. However, as the post is already long enough, we’ll save that for the second part.

ReSharper 6.1.1: New Maintenance Release Available

Thursday, February 16th, 2012

Did you report issues with ReSharper 6.1? Did you take part in the latest early access program? If you did, chances are that your efforts are now being rewarded: we have just made available ReSharper 6.1.1: a new maintenance release of the awesome Visual Studio productivity tool.

Let’s put it this way: if you’re fine with version 6.1, you probably shouldn’t be upgrading to 6.1.1. However, if you’ve been experiencing any of these 40 bugs with 6.1 that are now fixed, go on and download ReSharper 6.1.1.

Most prominent issues resolved for 6.1.1 include code cleanup options not being saved under the new settings engine (RSRP-287390), removing necessary import directives on code cleanup (RSRP-287334), bogus ambiguous reference errors in .cshtml files (RSRP-286884), and file masks applied to a broader set of files than expected (RSRP-287762). Here’s the entire list of fixes.

Enjoy!

Importing / Exporting Templates and Patterns in ReSharper 6.1

Monday, January 23rd, 2012

 

One of the changes introduced with the new settings in ReSharper 6.1 is how importing and exporting of templates and custom patterns are done (Custom Patterns were previously known as Structural Search and Replace).  As part of the unification of settings, Templates and Patterns now fall under the same modus-operandi when it comes to importing and exporting as other settings do. This is very similar to how our IDEA based IDE’s such as IntelliJ, WebStorm et al. work. However, certain legacy functionality is also available depending on what needs to be done.

The de-facto choice

Be it Templates or Patterns, when you want to import or export them, the recommended approach is to use the Manage Options dialog

 

SNAGHTMLa44bc04

 

To export, we merely select Export to File. ReSharper prompts us with a dialog box of what we would like to export:

 

SNAGHTMLa471579

 

At this point we can select whether we want Live Templates, Patterns (StructuralSearch*) or both. It is important to note that if you do not have any user defined templates and/or patterns, then these options are not available, i.e., you would not see the LiveTemplates / StructuralSearch checkboxes.

 

The exported file format is only compatible with ReSharper 6.1 and higher (.DotSettings file).

 

Importing

When it comes to importing templates and patterns, if these are in .DotSettings format, then it is merely about adding another layer. We select the file to import, and are prompted on what settings we’d like imported:

 

SNAGHTMLa62f7ad

 

The benefit of this approach is not only that it keeps all settings centralized, but it also allows us to mount and un-mount related templates.

 

* StructuralSearch comes from the old name giving to Custom Patterns which was Structural Search and Replace

 

Exporting Individual Templates

One option that is currently not available using the Manage Options dialog when dealing with the export of templates is to selectively choose which ones we’d like exported. If we want to only export specific templates, we need to use the Template Explorer (previously named Live Templates)

SNAGHTMLa66c44e

 

We can then select individual templates and choose the Export button. This is basically the old behavior prior to ReSharper 6.1. The difference however is that the file exported is in the new .DotSettings format. In this regard, there is no difference between this approach and the previous Manage Options one.

 

Dealing with legacy templates and patterns formats

The Templates Explorer window also has an option to import Templates

 

image

 

Although this allows the newer DotSettings file format, we usually should only use this when trying to import legacy templates (from version 6.0 and lower).

 

When it comes to Patterns, when trying to import older ones, we can use the Import from R# v6.0- button on the Custom Patterns options page

 

image

 

 

Summary

With the new settings we’ve hopefully made sharing of Patterns and Templates a little easier. There are still improvements to make but any/all feedback or changes you’d like to see are welcome. Please log them in our issue tracker so that they can be managed better.

Per Project Settings or How to have different naming styles for my test project

Wednesday, January 18th, 2012

 

One nitpick that I myself and many have had with ReSharper is not being able to have different naming styles for types based on different projects. In my code I normally name classes using CamelCase

 

image

 

and in my test project I use all_lowercase

 

image

 

Unless you add all_lowercase as a valid naming style, you’ll always end up with those squiggly lines under all your tests. And let’s admit it, it’s not a big deal but it’s annoying. #FirstWorldProblem

 

Say hello to my little layer

With ReSharper 6.1 we introduced the concepts of layers, which open up many new possibilities, including sharing settings on DropBox and or setting company wide standards. What you might not know is that it allows Per Project Settings too! No big deal if you didn’t know, since we currently do not support it via the User Interface so it would be hard to figure it out. However it’s pretty simple to do and I’m going to show you how.

 

Our goal here is to allow all_lowercase settings for classes in our test project (Tests) and not permit the same in other projects (Project). This is the sample project layout we’re going to be working with

 

image

To make this happen, we need to create specific settings for the Tests project and allow the possibility of classes being all_lowercase. That is, the Tests project needs its own DotSettings file.

 

Since there’s no specific button or option to add Per Project settings in the UI, this is where we need to kind of use a workaround. We need to have a Settings file for the project and then edit the options and save it to this file. There are many ways to create this new settings files. We can copy the solution one and rename it, we can export the current settings, we can mount a new layer and have ReSharper create it for us, etc.

We’re going to use this last method. Why? Because since ReSharper mounts it when it creates the file, it will allow us to edit the settings too.

 

 

1. With the Solution open, click on Manage Options under ReSharper

 

2. Under This Computer, click on the Add Layer icon (on the right) and select Create Settings File

image

 

 

3. Save the file to the Tests project folder, in our case that would be under SolutionDir\Tests and name it (and here’s the important part) Tests.csproj.DotSettings. That is, it should be named with the ProjectName, followed by csproj or vbproj based on the type of project and with the extension DotSettings.

 

4. Now that we have the settings file mounted, we need to change the required settings, which in our case is to allow classes to have all_lowercase (this obviously works for other settings too). As such, double-click the recently mounted file or right click and click on Edit

 

image

 

5. The Options dialog comes up. We want to edit the naming styles

 

image

 

Modify any other settings you wish at this point and once done, hit Save on the Options dialog.

 

image

 

At this point, we can remove the mounted settings as its not required for it to actually work. We can also leave it in order to easily access the settings for the project in the future. If you do leave it, make sure you uncheck the checkbox so that settings are not applied to the entire solution.

 

[Recommendation: If you decide to leave the settings mounted, mount these under Solution Shared or Solution Personal as opposed to This Computer so that you don’t end up with a long list of different settings files]

 

 

As soon as we hit Save, the new settings automatically are picked up by ReSharper and take effect. We can now see that the naming style no longer appears incorrect under the Tests project

 

image

 

and if we try and use the same style in the actual Project we do get the warning

 

image

 

The UI will come

Once you do this once, you can pretty much copy the same file over to your different projects and rename it to match the project name. In one of the next versions of ReSharper we will provide a more intuitive UI (or should we say we’ll provide an actual UI) for this. For now however, I think this minor hack will make a few people happy!

Introducing ReSharper 6.1 SDK

Wednesday, January 18th, 2012

This post is about ReSharper 6.1. For other features in ReSharper 6 (both 6.0 and 6.1), please see previous posts introducing ReSharper 6. Those cover support for JavaScript (including unit testing with QUnit), CSS, ASP.NET MVC 3 Razor, and Async CTP; bundled decompiler; explanations on ReSharper inspections; fine-tuning file headers; ReSharper settings revised in 6.1 (and their company-wide usage); recent improvements in IntelliSense options, and optimizing assembly references


Though every new release of ReSharper comes with extra features and functionality, there are always things that our users want that don’t come out of the box. This includes things like, for example, support for various unit test frameworks as well as various bits of functionality that are domain-specific or related to particular challenges a user is facing.

Luckily, ReSharper has always shipped with an Open API which provided the opportunity to write plug-ins — extensions to the core ReSharper mechanisms that would be provided in separate files and integrated into ReSharper at start-up. While the opportunity was always there, and has resulted in a number of plug-ins being written, there’s never been any framework that would simplify the task for plug-in developers. Until now.

With the release of ReSharper 6.1, we have also introduced the ReSharper SDK — a separate framework that has been designed to make plug-in development easier. The SDK includes the following:

  • ReSharper assemblies, including PDBs and documentation XML files. These files include not just the core ReSharper assemblies, but also the unit test framework, which lets you write unit tests for your plug-in.
  • MSBuild target files, which make referencing the ReSharper assemblies a lot easier.
  • Project and item templates for VS2010, which make getting started with plug-in development a breeze.

Two packages are provided with the SDK: the MSI installer, which comes with all of the above, as well as a ZIP package, which contains only the assemblies and target files, and is provided to help build and test plug-ins in Continuous Integration systems such as TeamCity. Both packages contain introductory documentation to help you get started, with the bulk of documents related to plug-in development available online. These documents contain a variety of topics such as how to port your plug-in to use the SDK, develop support for a new language, or deploy the plug-in. The documents are being actively developed, and you’re welcome to comment on them should things start getting unclear.

The SDK packages are available on the ReSharper Downloads Page under Related Downloads. The following JetBrains TV video provides a comprehensive overview of the various SDK features including plug-in development, testing and configuration under TeamCity.

Optimizing Assembly References with ReSharper 6.1

Tuesday, January 3rd, 2012

This post is about ReSharper 6.1. For other features in ReSharper 6 (both 6.0 and 6.1), please see previous posts introducing ReSharper 6:


One of the prominent new features in ReSharper 6.1 is the ability to easily manage your existing assembly references, browse usages of referenced assemblies, and remove unused references.

In fact, this is not a single feature but three features. Each of them is outlined below:

  • Optimize References. This is the main feature for analyzing referenced assemblies. It can be invoked on any single project from the project’s own context menu or from the context menu of the References node in Solution Explorer:

    The Optimize References tool window displays assembly references that are both unused and used in the current project, and shows how exactly references are used. The feature does not require your project to be compiled, which means you can invoke it even if your project is currently broken.

    One of the best things about this feature is that it allows you to explore usages of a specific referenced assembly, namespace or type; filter them by kind, group them and navigate from them directly to your source code. The screenshot below illustrates exactly why this project references EasyHttp.dll, which types from that assembly are used and where:

    Different icons mean different kinds of usages like read, write, attribute, or invocation usages — the same usage filtering functionality that is used in all other ReSharper search features based on Find Results window. However, one kind of usages is unique for Optimize References: it’s called “related usages” and indicates implicit (non-direct) assembly reference usages required for the compiler.
    In the simplest case, related usages are caused by a type’s hierarchy. That is, if you use a certain type, you have to reference all assemblies containing that type’s supertypes. Unfortunately, this is just one example along with many others which are much more complicated!
    In addition to exploring assembly usages, you can delete unused assembly references directly from this tool window — the toolbar contains two buttons, Remove all unused references and Remove selected references:

    Note that this feature also deletes all redundant C#, VB.NET or XAML namespace import directives which reference namespaces from the assemblies being removed.
  • Remove Unused References. This refactoring quickly detects unused assembly references without displaying their usages. Since it works faster than Optimize References, it is very useful if you don’t want to a full-fledged usage analysis and you’re just looking for a quick cleanup of your project references.

    Clicking Analyze Used References… button in this dialog box will open the Optimize References tool window for a thorough analysis of reference usage.
  • Safe Delete Assembly Reference. This refactoring either silently removes selected assembly references or opens a confirmation window indicating that some of the selected references are used (and shows how they are used.) The refactoring is available on pressing Alt+Del on an assembly reference (or references) selected in the Solution Explorer, or via Refactor This shortcut menu.

These 3 features introduced in ReSharper 6.1 will definitely help you maintain your solution and get rid of redundant dependencies. By the way, keep in mind that if you want to find out which code is referenced in a given scope other than a project (for example, in a folder or in a method), you can use ReSharper’s Find Symbols External To Scope feature that has been recently made available as the Referenced Code option via Navigate To shortcut menu.


The original post is authored by Kirill Skrygan (@kskrygan on twitter), ReSharper developer and the the author of the Optimize Assembly References feature set.

ReSharper 6.1.1 Early Access

Friday, December 30th, 2011

Do you like ReSharper 6.1? Judging by the feedback that keeps arriving via multiple channels, most users are happy with performance improvements and extended functionality.

However, could there be a release of an application as complex as ReSharper without bugs? This is very unlikely, and we do know that 6.1 RTM does have certain issues that affect a part of its audience.

With that in mind, we’re planning to deliver another minor update early next year that would exclusively focus on fixing issues discovered with 6.1 that don’t imply any substantial architectural changes. For the most part, the issues have to do with the new settings engine not always persisting changes.

Meanwhile, we felt it would be fair to deliver a build that would incorporate fixes implemented this far before the team goes on holidays until Jan 10. That said, everyone who is affected by this set of issues with 6.1 is welcome to download the initial build from ReSharper 6.1.1 Early Access Program. Expect further early builds mid January.

OK we’re done for the year. See you around in 2012!

ReSharper 6.1: Improvements in IntelliSense Options

Tuesday, December 27th, 2011

This post is about ReSharper 6.1. For other features in ReSharper 6 (both 6.0 and 6.1), please see previous posts introducing ReSharper 6:



ReSharper 6.1 introduces more fine-grained control over Intellisense.

image

As shown in the screenshot, in addition to the two older options (enhancing Visual Studio’s Intellisense with ReSharper or leaving as is) we can now select which technologies to apply Intellisense to. It is important to note that by de-selecting a technology, we do not switch off Visual Studio Intellisense for that technology, but only eliminate ReSharper’s enhancements of it.

Defining Autopopup behavior

The way Intellisense works inside Visual Studio, including ReSharper’s, is that matching elements appear as you type, allowing you to select the option by hitting Tab or Enter

This default behavior can be very productive, but it can also prove somewhat annoying, specially when we’re doing things like Test Driven Development or any kind of Create from usage scenario. For instance, we are trying to write a class or method that does not exist. We write it out and hit Enter, Intellisense completes it with the nearest match

In order to avoid these situations, we have added a new feature to ReSharper 6.1 which allows us to control not only where we want Intellisense to appear, but also how. That is, in addition to selecting the technology such as HTML, C# or XAML, we can now fine-tune Intellisense in each of these. Under ReSharper | Options | Intellisense | Autopopup we have the following screen

SNAGHTML82e9576[4]

Much like the Code Inspections, it is divided into different tabs based on the technology and an All tab that displays all entries. If we look through the list, we can see that under C#, we have several options, one of them being
SNAGHTML8326fdc

By default, all entries have the value “Display and preselect” which results in the behavior shown in the previous video. We can however change this with two other options

  • Do not display which will completely disable Intellisense
  • Display but do not preselect which will display matching values but will not preselect it.

This latter option is what we need in our case. We’d like to know of existing elements, but not necessarily have them completed when hitting Enter or Tab, etc. The video below displays this new behavior

Same as we have done with the After ‘new’, we can do with many others.We have tried to think of the most common scenarios where one would like to control this behavior. Please let us know if you feel something is missing. Hopefully however, this makes working with Intellisense productive and often less intrusive.

ReSharper 6.1, dotCover 1.2 and dotTrace 4.5.2 Performance Released

Tuesday, December 20th, 2011

Christmas is only a few days away and we’ve decided to celebrate it a little earlier by releasing not one, but all three of our main .NET tools: ReSharper 6.1, dotCover 1.2 and dotTrace 4.5.2 Performance.

ReSharper 6.1

As covered in previous posts, this release of ReSharper is much more than a few bug fixes.

Settings

In ReSharper 6.0 we laid the foundations for what would be the new settings management engine. This was quite a substantial change, both from an architectural point of view as well as functional. Unfortunately due to that pesky little annoyance called time, we did not manage to finish it for the 6.0 release. We have now though and ReSharper 6.1 introduces a wealth of new possibilities for sharing settings across solutions, teams, machines and even company-wide policies. And we’re still not done with settings, so keep your feedback coming for further releases.

image

Performance improvements

For every single release of ReSharper, we invest a large amount of time in performance improvements. This often isn’t an easy task since new releases also include new functionality. In 6.0 we had some serious performance issues with certain types of web projects. This has now thankfully been resolved in 6.1 and we’ve made 6.1 faster in many areas. Numbers are impressive: we have fixed 140 performance problems for 6.1.

Async and Visual Studio 11 support

Async support in C# is still in CTP stage. Visual Studio 11 is in early developer preview. Trying to support technology that is not entirely finished proves not only challenging but also risky. Any major changes could deem most of the work we’ve performed as obsolete. However, we know that you, our users, play with the early bits and miss ReSharper.

image

That’s why in 6.1 we provide support for Async features in C# and VB.NET, as well as initial experimental support for Visual Studio 11. Support for Metro style applications is not yet included, and in order to install ReSharper into VS11 Developer Preview, you have to use a separate .vsix installer available at ReSharper downloads.

Optimizing assembly references

Many users rely on ReSharper in streamlining their project maintenance tasks, and this new feature is a nice addition to the existing project-level toolset. In any project in your solution, ReSharper 6.1 detects assembly references that are not effectively used, and helps remove some or all of them.

image

SDK

During 6.0 development we invested heavily in trying to provide a better API for extending ReSharper. 6.1 also sees the birth of this effort. Along with an improved API, we also now provide a testing framework, Visual Studio templates for creating plug-ins, samples, as well as online documentation. ReSharper 6.1 SDK is available for download as a .msi or .zip package.

image

More awesome features

Here are a few more great features also added:

  • Structural Search and Replace for HTML and ASP.NET. You can now use Custom Patterns to refactor both HTML and ASP.NET markup.
  • Call tracking for ASP.NET MVC. Same call tracking features, extended to ASP.NET MVC 3.
  • New code inspections. Access to disposed closures, covariant array conversions and more.

We have also fixed many outstanding issues. Look for more blog posts describing 6.1 features and improvements in future here on the JetBrains .NET tools blog.

dotCover 1.2 and dotTrace 4.5.2 Performance

These are mainly maintenance releases with a primary focus of supporting ReSharper 6.1. In addition, dotCover 1.2 works side-by-side with TypeMock 6.2.x, provides performance improvements in report generation, reduces HTML report size and memory consumption.

Note that we are already working on next major dotCover and dotTrace Performance releases. EAPs are expected soon, exposing a ton of new functionality and improvements, so stay tuned for that.

Download them all!

You can download ReSharper 6.1, dotCover 1.2 and dotTrace 4.5.2 Performance right now!

Last but not least, we’d like to wish everyone very Happy Holiday season. Stay safe and see you all in the New Year!